Note: although the test was not passed, I learned how to implement full sorting using recursion (in other words, the passing rate of this question is really low. Which of the following experts knows the correct answer ?) But I tested it with gcc just now. When N = 11, the running time is 2.3 s (my host is linux mint 16 64bit, core dual core ), I used to run 7 or 8 s on windows.
Question details:
Let array a contain n elements which are exactly an arrangement of 0 .. n-1. Given B [0], B [1], B [2], B [3],
Q: How many zeros are there .. n-1, satisfied (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?
The input contains five parameters: N, K, B0, B1, B2, B3, 4 <= N <12, 0 <= K, B0, B1, B2, b3 <N.
Solution code:
# Include
# Define MAX_N 11int a [MAX_N] = {0}; int g_count = 0; int g_N; int g_K; int g_B0; int g_B1; int g_B2; int g_B3; // swap a [I] and a [offset] void swap (int I, int offset) {int temp = a [I]; a [I] = a [offset]; a [offset] = temp;} // find permutation (fully arranged) void perm (int offset) {if (g_N-1 = offset) {g_count + = (a [a [g_B0] * g_B0 + a [a [g_B1] * g_B1 + a [a [g_B2] * g_B2 + a [a [g_B3] * g_B3) % g_N = g_K; return;} else {int I; for (I = offset; I <g_N; ++ I) {swap (I, offset ); perm (offset + 1); swap (I, offset) ;}} int howmany (int N, int K, int B0, int B1, int B2, int B3) {g_N = N; g_K = K; g_B0 = B0; g_B1 = B1; g_B2 = B2; g_B3 = B3; // init array aint I; for (I = 0; I <N; ++ I) {a [I] = I;} perm (0); return g_count ;}
// This part of Code may not be compiled on Pang Guo (You are naughty again). Change the local variable that reported the error to the global variable.
Original question address: http://hero.pongo.cn/Question/Details? ID = 292 & ExamID = 287