Search For Fun
Time Limit: 1 Second Memory Limit: 32768 KB
There are n! permutations for 0, 1, 2, ..., n-1. For instance n=3 we have 6 sequences 012, 021, 102, 120, 201, 210. We put the sequence in an array a. Now it's your task to tell how many of the n! sequences satisfy
(a[a[b[0]]]*b[0]+a[a[b[1]]]*b[1]+a[a[b[2]]]*b[2]+a[a[b[3]]]*b[3])%n==k
Input
There are multiple tests, terminated with end of file.
Each line contains six integers, in the order n, k, b[0], b[1], b[2], b[3].
4 <= n < 12, 0 <= k < n, 0 <= b[i] < n.
Output
One number for each test - the number of sequences that satisfies the equation.
Sample Input
4 0 3 2 1 0 5 2 1 2 3 4
Sample Output
4 40Submit
Source: ZOJ Monthly, March 2003