http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126
http://acm.hdu.edu.cn/showproblem.php?pid=1005
Notice the difference between the above and the next one, a, B may take negative numbers, but the MoD and C + + is not the same, MoD will only get non-negative, two times jumped into the pit.
Then look for the cycle, as long as the f[i-1]==1&&f[i]==1 can jump out, and then I-2 is the cycle, output n% (i-2), to note that if equal to 0, in fact, N is equal to i-2, so to output F[I-2].
and is it that the cycle festival must have started from 1 1??
1#include <cstdio>2 intf[ -];3 intMain ()4 {5 //freopen ("A.txt", "R", stdin);6 inta,b,n,i;7f[1]=f[2]=1;8 while(~SCANF (" %d%d%d",&a,&b,&N))9 {Ten if(a==0&&b==0&&n==0) Break; One for(i=3;i< -; i++) A { -F[i]= ((a*f[i-1]+b*f[i-2])%7+7)%7; - //printf ("%i64d\n", F[i]); the if(f[i]==1&&f[i-1]==1) Break; - } -f[0]=f[i-2]; -printf"%d\n", f[n% (i-2)]); + } - return 0; +}
51nod 1126 the nth item of the recursive sequence && hdu-1005 number Sequence (period)