杭电1005

jdtxse 2010-07-16 06:04:56
感觉自己是对的。
自己给自己的测试数据都对。就是不能ac。
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
...全文
365 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanster28_ 2010-10-19
  • 打赏
  • 举报
回复
模7的约数只有7种,连续两个的组合有49种,所以51以内必定出现连续两个组合相同,递推公式推得周期
sxyclyp 2010-10-18
  • 打赏
  • 举报
回复
谢谢楼上,我从网上找的代码寻找周期从f(1)就开始的,函数如下
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;
}

不理解为什么周期一定在51以内……好像是用了什么鸽巢原理,百度了一下,还是不太懂……
fanster28_ 2010-10-18
  • 打赏
  • 举报
回复
我上面的那个代码是错的,但是hdu 的数据能过。。。

当b=0 和b!=0两种情况可以推出周期,
或者转化为矩阵,快速幂.
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_ 的回复:]

思路就是推导出周期,给你看个能ac的,

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;
这个周期是从f[1]开始的吧,
A=B=1试下:
1 1 2 3 5 1 6 0 6 0……
很明显不是f[1]开始的嘛
[/Quote]
⊙﹏⊙b汗。。。
算错了,当我没发。。。
xiaowai0219 2010-07-17
  • 打赏
  • 举报
回复
if(k!=3&&f[i]==f[3]&&f[i-1]==f[2]&&f[i-2]==f[1])
break;
这个周期是从f[1]开始的吧,
A=B=1试下:
1 1 2 3 5 1 6 0 6 0……
很明显不是f[1]开始的嘛
fanster28_ 2010-07-17
  • 打赏
  • 举报
回复
是这样的,周期不见得从f[1]开始,你明白这点就不会写错了
[Quote=引用 4 楼 jdtxse 的回复:]

看来看去,还是没感觉自己的代码哪里有漏。

引用 2 楼 fanster28_ 的回复:

思路就是推导出周期,给你看个能ac的,

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]
小楫轻舟 2010-07-17
  • 打赏
  • 举报
回复

#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
  • 打赏
  • 举报
回复
做OJ题,我也经常有“感觉自己是对的”的感觉,呵呵……
fanster28_ 2010-07-16
  • 打赏
  • 举报
回复
思路就是推导出周期,给你看个能ac的,

#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
  • 打赏
  • 举报
回复
顶。。。

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧