杭电ACM1005 Number Sequence
答案:2 悬赏:70
解决时间 2021-04-21 16:01
- 提问者网友:娇妻失忆
- 2021-04-21 02:28
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
int main(void)
{
__int64 a[100000];
int A,B,n,i;
while(scanf("%d%d%d",&A,&B,&n)!=EOF&&!(A==0&&B==0&&n==0))
{
a[1]=1;a[2]=1;
for(i=3;i<=n;i++)
a[i] = (A * a[i - 1] + B * a[i - 2])%7;
printf("%I64d\n",a[n]);
}
}
最佳答案
- 二级知识专家网友:为你轻狂半世殇
- 2021-04-21 03:24
#include
int main(void) {
int a1 = 1,a2 = 1,an;
int A,B,n,i;
while(1) {
scanf("%d%d%d",&A,&B,&n);
if(A == 0 && B == 0 && n == 0) break;
a1 = 1;
a2 = 1;
for(i = 3; i <= n; ++i) {
an = (A * a2 + B * a1) % 7;
a1 = a2;
a2 = an;
}
printf("%d\n",an);
}
return 0;
}
全部回答
- 1楼网友:桃花别处起长歌
- 2021-04-21 04:47
// 每个数据都走那么多肯定是tle啦,这题是有周期的。
// 下面是我的ac代码
#include
int main()
{
int a,b,n;
int f[201];
int i;
scanf("%d %d %d",&a,&b,&n);
while (a!=0||b!=0||n!=0)
{
f[1]=1;
f[2]=1;
if (n>=3)
{
for(i=3;i<200;i++)
{
f[i]=(a*f[i-1]+b*f[i-2])%7;
if ((f[i-1]==1)&&(f[i]==1))
{
break;
}
}
i=i-2;
n=n%i;
if (n==0)
{
n=i;
}
printf("%d\n",f[n]);
}
else
{
printf("1\n");
}
scanf("%d %d %d",&a,&b,&n);
}
return 0;
}
我要举报
大家都在看
推荐资讯