Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 41692 Accepted Submission(s): 8995
Problem 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 are to calculate the value of f(n).
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are 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 on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3 1 2 10 0 0 0
Sample Output
2 5
Author
CHEN, Shunbao
Source
Recommend
JGShining
Statistic | Submit | Discuss | Note
Home | Top | Hangzhou Dianzi University Online Judge 3.0 Copyright © 2005-2011 HDU ACM Team. All Rights Reserved. Designer & Developer : Wang Rongtao LinLe GaoJie GanLu Total 0.000354(s) query 1, Server time : 2011-08-04 13:01:12, Gzip enabled | Administration |
自己写了一个找 周期 但是就是ac不了,下面的是百度上找的
#include<stdio.h>
int main()
{ int a,b,n;
while(scanf("%d %d %d",&a,&b,&n)!=EOF&&(a+b+n)!=0)
{
int f1=1,f2=1,f3=1,t,r[8][8]={0},i,flag=0,x;
for(i=3;i<=n;i++)
{ f3=(a*f2+b*f1)%7;
r[f1][f2]=i;
f1=f2;
f2=f3;
if(r[f1][f2]&&flag==0)
{
t=i-r[f1][f2]+1;
i=r[f1][f2]-3;
n=(n-i)%t+i ;
if(n==i)
n=t+i;
if(n==(i+1)) {f3=f1;break;}
if(n==(i+2)) {f3=f2;break;}
i+=2;
flag=1;
}
}
printf("%d\n",f3);
}
return 0;
}