Problem K
Time limit:1000 MS Memory limit:32 MB 64bit IO Format:%i64d
submitted:225 accepted:51
[Submit] [Status] [Web Board]
Description
A 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).
Input
The 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.
Output
For each test case, print the value of f (n) in a single line.
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5
Since the value of MOD7,F (n) can only be taken in 0-6, and A and B are not changed, it is bound to loop, and the loop section will not exceed 7*7=49;
#include <stdio.h>
int main ()
{
long a,b,n,i,s,q,p;
while (scanf ("%i64d%i64d%i64d", &a,&b,&n)!=eof)
{
if (a==0&&b==0&&n==0)
break;
P=1;
Q=1;
if (n==1| | n==2)
printf ("1\n");
else
{
n=n%49;
for (i=3; i<=n; i++)
{
s= (a*q+b*p)%7;
p=q;
q=s;
}
printf ("%i64d\n", s);
}
}
return 0;
}