hdu 1005 number sequence
2016-01-24 15:11
495 查看
#include<cstring> #include<iostream> #include<cstdio> using namespace std; int main() { int i,j; int beg,end; int n; int ret[60]; int flag; int a,b; while(cin>>a>>b>>n) { flag=0; if(a==0&&b==0&&n==0) break; ret[1]=1; ret[2]=1; for(i=3;i<=n;i++) { ret[i]=(a*ret[i-1]+b*ret[i-2])%7; for(j=2;j<=i-1;j++)//计算出循环节 { if(ret[i]==ret[j]&&ret[i-1]==ret[j-1]) { beg=j; end=i; flag=1; break; } } if(flag) break; } if(flag) { printf("%d\n",ret[beg+(n-end)%(end-beg)]); } else printf("%d\n",ret ); } return 0; }这题目是找规律的,什么时候出现循环的时候就是得出结果的时候了,我刚开始想到这个思路的时候就开始猜想了,不是说一个数是与前面两个数相关联吗;
那么我们只需找出什么时候关联就行了,可以写个数据进行一下模拟,就出来了
相关文章推荐
- UVA 11995 I Can Guess The Data Structure!
- ZOJ 2812 Quicksum
- UIAlertController详解
- android.util.AndroidRuntimeException: requestFeature() must be called before adding content
- UISearchBar控件
- 虚拟机下安装UEFI+GPT+win7x64
- ubuntu下cmake3.4的安装(有GUI界面)
- QuickLook(简介)
- duilib 界面库 实现timer定时器
- 修改UITextField的占位文字颜色的三种层次
- 298. Binary Tree Longest Consecutive Sequence
- UE32 删除空行正则表达式 .
- APUE_文件管理
- UIImageView的两种创建方式的比较
- DP------Unique Paths II
- How to building OpenSSL 0.9.8c
- QtCharts笔记之QValueAxis
- bluestacks启动问题
- Which statement is true about loading data using the conventional path of SQL*Loader? A.Redo is not
- User SCOTT executes the following command on the EMP table but has not issued COMMIT, ROLLBACK, or a