The meaning of problems: Known f (1) = 1, f (2) = 1, f (n) = (A * f (n - 1) + B * f (n - 2)) mod 7, given A, B, n, seeking f (n)
Interpretations: n huge cycle certainly timeout at die 7, 0 <= f (n) <= 6, a total of seven choices, then f (n-1) and f (n-2) each have seven kinds Alternatively, a total of 49 kinds of combinations, bound to at least 50 repeat the foregoing compositions and find the loop section.
Pit: I do not know why the say online to find two consecutive loop section 1, it is pointed out that this great God is wrong, when a and b are two of a number, the sequence is 1,4,6 cycle, but I forget which two number, and I could not find blog.
In my opinion, the same double loop to find more secure, rather than looking for two 1.
#include<stdio.h> #include<iostream> #include<algorithm> #include<cstring> #include<math.h> #include<string> #include<map> #include<queue> #include<stack> #include<set> #define ll long long #define inf 0x3f3f3f3f using namespace std; int a,b,n; int f[10086]; int main()///hdu1005 { while(scanf(" % D% D% D " , & A, & B, & n-) = the EOF && (A + B +! N-)) { Memset (F, 0 , the sizeof (F)); F [ . 1 ] = F [ 2 ] = . 1 ; int I, J; int Minn = min (n-, 100 ); for (I = . 3 ; I <= Minn; I ++ ) F [I] = (A * F [I- . 1 ] + B * F [I- 2 ])% 7 ; /// Get the first loop section and the second occurrence of int Cha = - . 1 ; for (I = . 3; I <= Minn; I ++ ) { for (J = I + . 1 ; J <= Minn; J ++) /// not mean cycle time to identify this section IF (F [I] == F [J] && F [ + i . 1 ] == F [J + . 1 ]) { Cha = JI; /// difference circulation section BREAK ; } IF (Cha = -! . 1 ) BREAK ; } /// case i is the cycle starting section int IDX ; IF (Cha == - . 1 ) /// little data circulating section is not found, determined directly { IDX =n; } else { n=n+cha; idx=(n-2)%cha; if(idx==0) idx=2+cha; else idx=2+idx; } printf("%d\n",f[idx]); } return 0; }