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 to 2 0 0 sample Output25in addition to F1 and F2, each number is determined by the first two numbers, which is determined by the formula , that is, if for the sequence F1 F2. FA FB. FC FD exists FA=FC and FD=FB so follow a certain cycle , for any FN, because it is mod 7 so its value can only be 0 1 2 3 4 5 6 This 7 kind of possible for any successive two digits, FA FB, possible combinations Is the 7*7=49 species, and in fact, the 0,0 sequence is a special case, unless a B is a multiple of 7, then all the sequences are 0, otherwise there is no possibility of 00. Therefore, if you extract an arbitrary subsequence length of 50, you can extract 49 consecutive pairs, 49 of which will definitely have at least one repetition, that is, the cycle time. Didn't do it, look at the key point is to find the follow -up link
1#include <stdio.h>2#include <math.h>3 intFintAintBintN)4 {5 if(n==2)6 return 1;7 if(n==1)8 return 1;9 return(A*f (a,b,n-1) +b*f (a,b,n-2))%7;Ten } One intMain () A { - intb; - intN; the while(SCANF ("%d%d%d", &a,&b,&n) = =3) - { - if(a==b&&b==n&&n==0) - Break; + Else -printf"%d\n", F (a,b,n% the)); + } A}
Number Sequence (HDoj1005)