problem DescriptionA number sequence is defined as follows:
F (1) = 1, f (2) = 1, f (n) = (A * F (n-1) + B * F (n-2)) MoD 7.
Given A, B, and N, you is to calculate the value of f (n).
InputThe input consists of multiple test cases. Each test case contains 3 integers a, b and N in a single line (1 <= A, b <=, 1 <= n <= 100,000,000). Three zeros signal the end of the input and this test case are not a is processed.
Outputfor each test case, print the value of f (n) in a single line.
Sample Input1 1 31 20 0 0
Sample Output25
1. There is bound to be a cycle
This is based on the following facts (* Not quite understand *):
1. R (n+2) =f (n+2) mod p= (f (n+1) +f (n)) mod p= (f (n+1) mod p +f (n) modp) mod p
2. Greatest common divisor theorem for Fibonacci sequences: GCD (f (M), F (n)) =f (gcd (m,n) )The greatest common divisor theorem shows that if f (k) can be divisible by n, then f (IK) can be divisible by n, which indicates the periodicity of the factors contained in the Fibonacci sequence, as listed below:factor: 2,3,4,5, 6,7,8, 9,10,11,12Cycle: 3,4,6,5,12,8,6,12,15,10,12we say that the resulting sequence is the remaining sequence, so that once an F (k) can be divisible by n (which proves one of my speculations: for any prime p,f (P), F (P-1) and F (p+1) Three, one can be divisible by P), and then F (IK) can be divisible by n, That is, the remainder of the sequence occurs 0, the next remaining sequence value is N-1, will always repeat, there are two adjacent repeats the sequence must be repeated, that is, periodic. This cycle is called the Pisano cycle .
2. Correct thinkingbecause of the mod7 relationship, and F (1) =f (2) = 1, the value of f (n) is circular distributed and must be returned to F (n-1) =f (n) = 1. It is also possible to conclude that the loop is not greater than 49, since there are only 7 values for the adjacent F, so that there are 49 combinations of F (n-1) and f (n). so, as long as the loop factor is found, the search method is calculated based on where F (n-1) =f (n) reappears. You can first write a test program for this topic, set a B N (n compare hours) value, look at the output rule. just find K to make f (k-1) = f (n-1), F (k-2) =f (n-2), especially when K equals 2 o'clock, since f (1), F (2) is the beginning of the loop. and because F (n-1), F (n-2) can only take 0 to 6 a total of 7, so there are 49 combinations, that is, 50 must be found to meet the conditions of K, that is, the cycle period of less than 50.
3. Problem-Solving ideas:
The value of 1.N is very large, such a large number of problems are generally regular, to find out the cycle (cycle) is the key;
2. Find the law, the problem is from f (1) = 1 and f (2) = 1 start, and then modulo 7, that f (n) only 7 cases, so two numbers adjacent to only 7*7=49 species;
3. So from F (1) to F (49) there must be adjacent two F (m-1) = 1, f (m) = 1, so f (n) is a periodic function and 49 is a period.
3. Implementation code:
1#include <iostream>2 using namespacestd;3 4 intb;5 6 intFintN)7 {8 if(n==1|| n==2)9 return 1;Ten Else{ One return((A * f (N-1) + B * F (N-2)) %7); A - } - } the - intMain () - { - Long LongN; + while(1) - { +Cin>>A; ACin>>B; atCin>>N; - if(a==0&& b==0&& n==0) - Break; - Else - { -N%= the; inCout<<f (n) <<Endl; - } to } + return 0; -}
1005: Take surplus, cycle, find the law