Ordered search-pang Guo net (C language version, although implemented, but did not pass the test, the time exceeds 3 s, the use of the full sorting method, May great Gods guide)

Source: Internet
Author: User
Tags linux mint

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.