HDU 1005 Number Sequence(坑 T_T !!!)
2015-11-28 19:40
381 查看
Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 136851 Accepted Submission(s): 33174
[align=left]Problem Description[/align]
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).
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, print the value of f(n) on a single line.
[align=left]Sample Input[/align]
1 1 3
1 2 10
0 0 0
[align=left]Sample Output[/align]
2
5
[align=left]Author[/align]
CHEN, Shunbao
[align=left]Source[/align]
ZJCPC2004
一直很纠结,不愿意写这道题,之前遇到好几次了
其实就是循环节,因为MOD 7,所以最多只要7*7=49次循环就能找到循环节出现坑的地方就是我用了一个len=i-2来作为循环节,一直WA,后来直接用i-2就过了,真是醉
可能是因为多组数据所以可能出现len没有更新,使用了前一次len的情况
醉了!!!!!!!!!!!!
#include<stdio.h> #include<iostream> #include<string> #include<string.h> #include<cstdlib> #include<algorithm> #include<map> #include<cmath> #include<stack> #include<queue> #include<set> #include<vector> #define F first #define S second #define PI acos(-1.0) #define E exp(1.0) #define INF 0xFFFFFFF #define MAX -INF #define len(a) (__int64)strlen(a) #define mem0(a) (memset(a,0,sizeof(a))) #define mem1(a) (memset(a,-1,sizeof(a))) using namespace std; __int64 gcd(__int64 a, __int64 b) { return b ? gcd(b, a % b) : a; } __int64 lcm(__int64 a, __int64 b) { return a / gcd(a, b) * b; } __int64 max(__int64 a, __int64 b) { return a > b ? a : b; } __int64 min(__int64 a, __int64 b) { return a < b ? a : b; } int f[110]; int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); int n,a,b; int len; f[1]=1; f[2]=1; while (scanf("%d%d%d", &a, &b, &n)!=EOF&&(a+b+n)) { int i; for(i=3;i<100;i++) { f[i]=(a*f[i-1]+b*f[i-2])%7; if(f[i-1]==f[1]&&f[i]==f[2]){break;} } n=n%(i-2); f[0]=f[i-2]; printf("%d\n",f ); } }
相关文章推荐
- iOS 8新特性—— UIAlertController
- Building Simulation Packet-Loss System in Channel
- 我的iOS学习历程 - UITabBarViewController
- 为什么Java中1000==1000为false而100==100为true?
- UESTC 65 CD Making 贪心法
- UI-之plist文件解析
- 【Leetcode】N-Queens II
- UINavigationController设置导航栏属性
- 【Leetcode】N-Queens
- UITabBarController
- Codeforces 288E Polo the Penguin and Lucky Numbers
- UITableView 的结构
- 使用druid连接池的超时回收机制排查连接泄露问题
- 在Legacy BIOS与UEFI 两种模式安装Windows 8操作系统的方法
- iOS 9人机界面指南:UI设计基础--模态情景(Modal Contexts)
- HDFS Short-Circuit Local Reads
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备四)
- easyui datagrid 表格组件列属性formatter和styler使用方法
- 导航栏控制器和标签栏控制器(UINavigationController和UITabBarController)混用