杭电 acm 1005

来源:百度知道 编辑:UC知道 时间:2024/06/08 06:44:50
刚接触ACM,在杭电 acm中提交之后,提示错误。有高手可以帮我指出吗?
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
我的代码是这样的:
#include <stdio.h>
void main()
{
long n;
int a,b;
long f1,f2,fn,fa[50000],j;
scanf("%d%d%ld",&a,&b,&n);
while(a||b||n)
{
fa[0]=fa[1]=f1=f2=1;
j=2;
while(1)
{
fa[j++]=fn=(a*f2+b*f1)%7;
f1=f2;
f2=fn;

#include <iostream>
using namespace std;

int main()
{
int f[100];
long A,B,n;
int r;
f[0]=f[1]=1;
cin >> A >> B >> n;
while(A&&B&&n)
{
int i=2;
r = 7;
while(true)
{
if(i==n) break;
f[i] = (A*f[i-1]+B*f[i-2])%7;
int j;
for(j=i-1;j>0;j--)
{
if(f[j]==f[i]&&f[j-1]==f[i-1]) break;
}
if(j==0) i++;
else
{
r = f[(n-1-i)%(i-j)+j];
break;
}
}
if(r==7) cout << f[n-1] << endl;
else cout << r << endl;
cin >> A >> B >> n;
}
return 0;
}
Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
1474226 2009-07-13 00:20:16 Accepted 1005 0MS 344K 532 B C++ Parker

#include <stdio.h>

int main()
{
int f[100];
long