Find a few simple warm warm-up, feel that their algorithm or can't remember, can describe, will not write, should be the reason for less practice.
Today, a topic handed over several times to Ah, obviously is a very simple question ah ah ah ah, forget, first recall the mental process.
The title is like this.
Problem Description
The building is playing a number game with the students.
The rules of the game are this: the building and the students are all sitting around in a circle of n people. The building first says the number 1. Next, the classmate sitting on the left side of the building will say the next number 2. Then one of the students from the last classmate said the number down two she used out, that is to say 4. The next classmate is going to count three numbers, say 7. In turn.
in order to make the numbers not too large, the building and the students agreed that when the heart count to K-1, the next number from 0 to start. For example, when K=13, the first few numbers that the building and the students quoted were:
1, 2, 4, 7, 11, 3, 9, 3, 11, 7.
The game was going on for a while, and the building wondered what the sum of all the numbers he had been saying so far.
Input Format
the first line of the input contains three integer n,k,t, where n and K are the meanings described above, T represents the number of numbers that have been uttered so far in the building.
output Format
The output line contains an integer that represents the and of all the numbers in the building.
Sample Input
3 of 3
Sample Output
-
Sample Description
the number of buildings to be uttered is 1, 7, 9, and 17.
data size and conventions
1 < n,k,t < 1,000,000;
This is the problem. Because t what is still very big, to keep these data are not realistic, of course, a good acmer will not be used to save the rotten method (although I even acmer is not). If you do not save the data, it is easy to think of the need to rely on the law, (how I feel I will do only to find a regular, not happy)
The law is easy to think of, according to the title description, the sequence (1,2,4,7,11) is fixed in all cases, except that each case is a different number of models, and the number of the building turn to different. It is easy to have a recursive formula: F (n) = f (n-1) +n-1; (n here is subscript, not the number of people on the topic) so which is the building? Easy to know I% n = = 1 of that location is building. Thus I write the following code.
1#include <stdio.h>2 3 intMain ()4 {5 intn,k,t;6 inti =1, x =1, x0 =1;7 intsum =1;8CIN >> N>>k>>T;9 for(i=2; I <= (T-1) *n+1; i++)Ten { Onex = x +i-1; A if(x > K) × = x percentK; - if(i% n = =1) Sum + =x; - } thecout<<sum<<Endl; - - return 0; -}
Code One
The result is obvious and timed out. T and N are very large (even when not too large) because the for loop is t*n and the timeout is inevitable.
At that time I took the trouble to study on the Internet. Someone had the same problem as me, and one of them answered him, focusing only on the data of the building, without requiring every data. (Pan outside: answer this question is my classmate Ah, I ask the question when others have already started to answer the question!! Don't ask me how I know, who told him all the social networking sites are using the same picture of their own. )
Since only the data of the building is concerned, the recursive formula will be re-inferred. Based on the data of 3 13 3 in the topic. Easy to know f (n) = f (n-3) + 3n-6; here n is the meaning of subscript, in order to avoid misunderstanding into f (a) = f (a-3) + 3a-6; The topic is three people a cycle, if it is n personal one cycle? Recursive formulas are more general. But, at this time, good to die, I changed the recursive formula to f (a) = f (a-n) + 3a-6; This is obviously wrong, 3a-6 is also to change AH girl. At that time, I did not think, excitedly changed the hand. What is 3a-6, is A-1 + a-2 + a-3; expansion to n is A-1 + a-2 + a-3 +...+a-n; that is A*n + N (n + 1)/2; The recursive formula is f (a) = f (a-n) + A*n + N ( n + 1)/2; T is the number of counts, with I to cycle (1,2,3...t) A and I relationship is a = i * n + 1; slightly change the program.
However, my program is still wrong, nothing but the two sentences program x0 = x0 + (i * n + 1) * n-n* (n + 1)/2; x = x0%k; sum + = x; This seems correct, in fact, after the x0 to take the model to be assigned to x0, so that the next use. Because sum%d = (a%d +b%d)%d! = (a+b)%d, be sure to follow the rules in the topic. Changed after or not, I was decadent, feeling tired, however, at this time I have a light, the data are changed to Longlong type, and then the correct!!! The code below, a lot of comments in the code to record my thinking process, not willing to delete.
1#include <stdio.h>2 3 intMain ()4 {5 Long Longn,k,t;6 Long Longi =1, x =1, x0 =1;7 Long Longsum =1;8scanf"%i64d%i64d%i64d",&n,&k,&T);9 /*For (I=2;i <= (T-1) *n+1;i++)Ten { One x = x +i-1; A if (x > k) x = x%k; - if (i% n = = 1) sum + = x; - } the cout<<sum<<endl;*/ - for(i =1; I < t;i++) - { -x0 = x0 + (i * n +1) * n-n* (n +1)/2; + //cout<<x0<<endl; -x = x0%K; +x0 =x; ASum + =x; at } -printf"%i64d\n", sum); - return 0; -}
Code two
I also see a code on the Internet, the recursive formula and I are not the same, but the submission is also right. There are only 6 test data in the topic, which may not be really detected. The online code is as follows:
1#include <stdio.h>2 intMain ()3 {4 Long LongN,k,t,i,total=1, op=1, cishu=1;5scanf"%i64d%i64d%i64d",&n,&k,&t);6 for(i=1; i<t;i++)7{Long LongOp1= ((n*i+1+n* (I-1)) *n/2)%K;8op+=OP1;9op%=K;Tentotal+=op; One } Aprintf"%i64d\n", total); - return 0; -}
Code Three
I made a couple of blue bridge cups today.