• 主页
• 问答

# 杭电1005

jdtxse 2010-07-16 06:04:56

http://acm.hdu.edu.cn/showproblem.php?pid=1005杭电1005的地址在这，帮我看看吧。
``````#include<iostream>
using namespace std;
int main()
{
long long a,b,n,f[1000],i;
f[1]=1,f[2]=1;
while((cin>>a>>b>>n)&&a&&b&&n)
{
int k=3;
for(i=3;i<100;i++)
{
f[i]=(a*f[i-1]+b*f[i-2])%7;
if(k!=3&&f[i]==f[3]&&f[i-1]==f[2]&&f[i-2]==f[1])
break;
k++;
}
cout<<f[(n-1)%(k-3)+1]<<endl;
}
return 0;
}``````

Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29352 Accepted Submission(s): 5976

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
...全文
296 点赞 收藏 12

12 条回复

fanster28_ 2010-10-19

sxyclyp 2010-10-18

int fun(int A, int B)
{
a[0] = a[1] = 1;
int i;
for( i=2; i<51; i++)
{
a[i] = (A*(a[i-1]%7)+B*(a[i-2]%7))%7;
if(a[i] == 1 && a[i-1] == 1)
break;
}
return i-1;
}

fanster28_ 2010-10-18

sxyclyp 2010-10-18
(*^__^*) 嘻嘻……，楼主应该已经AC了吧。我根据楼主的思路走的，却WA了，不知道哪出了问题……

#include <iostream>
using namespace std;

int main()
{
int f[100],k;
int n,a,b;
f[1]=1;
f[2]=1;
while (cin>>a>>b>>n,a||b||n)
{
f[3]=(a*f[2]+b*f[1])%7;
f[4]=(a*f[3]+b*f[2])%7;
f[5]=(a*f[4]+b*f[3])%7;
for (k =6;k<100;k++)
{
f[k]= (a*f[k-1]+b*f[k-2])%7;
if ((k!=3)&&(f[k]==f[5])&&(f[k-1]==f[4])&&(f[k-2]==f[3]))
break;
}
if (n<= 2)cout<<f[n];
else
{
f[2]=f[k];
cout<<f[(n-2)%(k-2)+2]<<endl;
f[2]=1;
}
}
return 0;
}

jdtxse 2010-07-17

[Quote=引用 2 楼 fanster28_ 的回复:]

C/C++ code
#include<stdio.h>

int main()
{
int n, A, B, T;
int i, ans,ans_old;
int a[64];
while(1)
{
scanf("%d %d %d", &A, &B, &n);……
[/Quote]不懂啊。。；。

xiaowai0219 2010-07-17
[Quote=引用 7 楼 xiaowai0219 的回复:]
if(k!=3&&f[i]==f[3]&&f[i-1]==f[2]&&f[i-2]==f[1])
break;

A=B=1试下：
1 1 2 3 5 1 6 0 6 0……

[/Quote]
⊙﹏⊙b汗。。。

xiaowai0219 2010-07-17
if(k!=3&&f[i]==f[3]&&f[i-1]==f[2]&&f[i-2]==f[1])
break;

A=B=1试下：
1 1 2 3 5 1 6 0 6 0……

fanster28_ 2010-07-17

[Quote=引用 4 楼 jdtxse 的回复:]

C/C++ code
#include<stdio.h>

int main()
{
int n, A, B, T;
int i, ans,ans_old;
int a[64];
while(1)
{
scanf("%d %d %……
[/Quote]

``````
#include<iostream>
using namespace std;
int main()
{
long long a,b,n,f[1000],i;
f[1]=1,f[2]=1;
while((cin>>a>>b>>n)&&a&&b&&n)
{
int k=3;
for(i=3;i<100;i++)
{
f[i]=(a*f[i-1]+b*f[i-2])%7;
if(k!=3&&f[i]==f[3]&&f[i-1]==f[2]&&f[i-2]==f[1])
break;
k++;
}
cout<<f[(n-1)%(k-3)+1]<< " "; //不会是输出格式不对吧？endl 给为" ",输出在同一行
}
return 0;
}``````

hua_zhixing_ 2010-07-16

fanster28_ 2010-07-16

``````#include<stdio.h>

int main()
{
int n, A, B, T;
int i, ans,ans_old;
int a[64];
while(1)
{
scanf("%d %d %d", &A, &B, &n);
if((n == 0) && (A == 0) && (B == 0))
break;
ans = 1;
ans_old = 1;
A %= 7;
B %= 7;
if((n == 1) || (n == 2))
ans = 1;
else
{
if((A == 0) && (B == 0))
{
ans = 0;
}
else
{
for(i = 2;i < n ; ++i)
{
if(i > 4 && ans_old == a[2] && ans == a[3])
{
T = i - 4;
a[1] = a[T+1];
break;
}
ans = ans_old + ans;
ans_old = ans - ans_old;
ans = ans - ans_old;
ans = (B*ans + A*ans_old)%7;
a[i] = ans;
}
ans = a[(n-2)%T + 1];
}
}
printf("%d\n", ans);
}
return 0;
}
``````

ERcompose 2010-07-16

3.2w+