1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int a,b,n; 6 int i; 7 int f[52]={0}; 8 f[0]=1; 9 f[1]=1;10 while(scanf("%d %d %d",&a,&b,&n)!=EOF){11 if(a==0){12 break;13 }14 for(i=2;i<52;i++){15 f[i]=(a*f[i-1]+b*f[i-2])%7;16 }17 if(n<=52){18 printf("%d\n",f[n-1]);19 }20 else{21 printf("%d\n",f[(n-52)%48+3]);22 }23 }24 return 0;25 }
Because N may be large, it determines the number of recursive layers, so it cannot be used for recursion, because the stack may not be so large.
Otherwise, recursion is easy.
So you have to find the rule:
The results of each F (n) are related to F (n-1) and F (n-2)
F (n-1) can only be 0 ~ One of the six, so there are 7 types (except F (1) and F (2 ))
And f (n-2) is also 0 ~ One of the six types is also 7 (except F (1) and F (2 ))
Then the result of F (n) is only 0 ~ A number between six
According to the size of a and B, the result of each F (n) is [(A times 0 ~ 6) + (B times 0 ~ 6)] % 7
If X = F (n-1) and Y = f (n-2) are set, the possible combinations are:
)
)
)
(3,0) (3,1) (3,2) (3,3) (3,4) (3,5) (3,6)
(4, 0) (4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6)
(5, 0) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5) (5, 6)
(6, 0) (6, 1) (6, 2) (6, 3) (6, 4) (6, 5) (6, 6)
Test with array a [100], a [0] = 1, a [1] = 1, the following is the last 48*2 of the 100 number, that is, a [4] ~ A [99], where a [4] ~ A [51] is a cycle, a [52] ~ A [99] shows that it is another cycle. (Test the first 100 items by yourself)
Summary:
For the array f [100] in the code, F [0] ~ F [3] is irregular. If you give these numbers, output them directly.
F [4] ~ F [51] indicates all data in the first cycle, with a total of 48 data records. F [52] ~ [99], F [100] ~ F [147]... and so on
If n is <= 52, the output will be done directly. If n is greater than 52, it will correspond to f [4] ~. F [51] is a number.
HDU 1005 (AC code)