Number Sequence--hdu1005

时间:2023-03-09 17:00:00
Number Sequence--hdu1005

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 128332    Accepted Submission(s): 31212

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).

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

这个题其实应该可以考虑到有循环的,看了好多博客说直接能得出来循环节为49,但是我太笨,不明白!

我还是自己找吧!一切数据都是由f[1]=1和f[2]=1演化出来的,所以当再次出现时,一定是在循环了!

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[];
int main()
{
int i,j,a,b,n;
f[]=;
f[]=;
while(scanf("%d%d%d",&a,&b,&n),a||b||n)
{
int t;
for(i=;i<;i++)
{
f[i]=(a*f[i-]+b*f[i-])%;
if(f[i-]==&&f[i]==)
break; }
i-=;
n=n%i;
if(n==)//当n=0时,说明正好是循环节的倍数,把它转化为一个循环的最后一个
n=i;
printf("%d\n",f[n]);
}
return ;
}