## Fox Socks

View as PDF

Points: 40
Time limit: 60.0s
Memory limit: 1G

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
##### Facebook Hacker Cup 2015 Round 2

Mr. Fox sure loves his socks! He stores his many indistinguishable socks in a set of bins, which are arranged in a circle and numbered in clockwise order. For every , the next bin clockwise of bin is bin , and the next bin clockwise of bin is bin . Initially, the bin contains socks.

Never being quite satisfied with his sock collection, Mr. Fox would like to perform operations on it, one after another. Each operation may be of one of the following 4 types, determined by the value of :

• Given integers , , , and , add socks to bin , add socks to the next bin clockwise of bin , add to the next bin clockwise of that one, and so on until this has been done for bins. Determine the total number of socks added in this process.
• Given integers , , and , remove all of the socks from bin and then add socks to it. Do the same for the next bin clockwise of , and so on until this has been done for bins. Determine the sum of two values: the total number of socks removed in this process, and the total number of socks added in this process.
• Given integers and , count the number of socks in bin (without removing them), the number of socks in the next bin clockwise of , and so on until the socks in bins have been counted. Determine the total number of socks counted in this process.
• Given integers and , check if bin contains an odd number of socks. Do the same for the next bin clockwise of , and so on until this has been done for bins. Determine the total number of these bins that contain an odd number of socks.

Can you help Mr. Fox keep track of his socks?
Note the value calculated during each of the operations as they're performed, and then output the sum of all of the values modulo .

#### Input

Input begins with an integer , the number of sock collections Mr. Fox has. For each sock collection, there are 7 lines containing the following space-separated integers:

The first two elements of each sequence of integers (, , , , , and ) are given, and the rest are computed with the following pseudorandom generators:

• , for
• , for
• , for
• , for
• , for
• , for

#### Output

For the th sock collection, print a line containing Case #i:  followed by the sum of all values calculated during each operation, modulo .

#### Explanation of Sample

The first collection has 5 bins that all have 0 socks. None of the operations involve any socks at all, so the answer is 0.

The second collection has 5 bins with 1, 2, 3, 4, and 5 socks. Mr. Fox performs the operations 1, 2, 3, and 4 in order. For each operation, , , , . He first adds 0 socks to the bins, then removes all 15 socks, then counts the 0 remaining socks, and then counts 0 odd bins, for a total of 15.

The third collection also has 5 bins with 1, 2, 3, 4, and 5 socks. Mr. Fox performs the same operations, but this time and take on the values 1, 2, 3, and 4 in that order. He adds 15 socks to the bins, then removes all 30 socks and adds 2 socks to each bin, then counts those 10 socks, and then counts 0 odd bins. The total is then .

#### Sample Input

5
5 4
0 0 0 0 0
1 2 0 1 0
1 1 0 0 0
1 1 0 0 0
0 0 0 0 0
0 0 0 0 0
5 4
1 2 0 1 1
1 2 0 1 0
1 1 0 0 0
5 5 0 1 4
0 0 0 0 0
0 0 0 0 0
5 4
1 2 0 1 1
1 2 0 1 0
1 1 0 0 0
5 5 0 1 4
1 2 0 1 1
1 2 0 1 1
5 2
1 2 0 1 1
4 4 0 0 0
1 1 0 0 0
5 5 0 0 0
0 0 0 0 0
0 0 0 0 0
100 100
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

#### Sample Output

Case #1: 0
Case #2: 15
Case #3: 65
Case #4: 6
Case #5: 505274484