简体   繁体   中英

Test cases for algorithm puzzle

The following is the problem from Interviewstreet Can someone please give me a few test cases along with the output. My solution is within the time limit for all test cases but is giving Wrong Answer.

Circle Summation (30 Points)

There are N children sitting along a circle, numbered 1,2,...,N clockwise. The ith child has a piece of paper with number ai written on it. They play the following game:

In the first round, the child numbered x adds to his number the sum of the numbers of his neighbors.

In the second round, the child next in clockwise order adds to his number the sum of the numbers of his neighbors, and so on.

The game ends after M rounds have been played.

Input: The first line contains T , the number of test cases. T cases follow. The first line for a test case contains two space seperated integers N and M . The next line contains N integers, the ith number being ai .

Output: For each test case, output N lines each having N integers. The jth integer on the ith line contains the number that the jth child ends up with if the game starts with child i playing the first round. Output a blank line after each test case except the last one. Since the numbers can be really huge, output them modulo 1000000007 .

Constraints:

1 <= T <= 15
3 <= N <= 50
1 <= M <= 10^9
1 <= ai <= 10^9

Sample Input:

2
5 1
10 20 30 40 50
3 4
1 2 1

Sample Output:

80 20 30 40 50
10 60 30 40 50
10 20 90 40 50
10 20 30 120 50
10 20 30 40 100



23 7 12
11 21 6
7 13 24 

If it seems to do ok for small test-cases, but not all, I would guess you have an overflow problem.

Make sure you either...

  • Do the modulus after each addition, not just after adding all three numbers.
  • Use 64-bit numbers. This would still require modulus, but not as often.

1000000007 is pretty close to the limit of signed 32-bit numbers (214748367). You can add to modulated numbers without overflow, but not three.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM