hdu 1005 Number Sequence
来源:互联网 发布:淘宝引流宝怎么设置 编辑:程序博客网 时间:2024/06/06 02:28
Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 155586 Accepted Submission(s): 38044
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).
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 31 2 100 0 0
Sample Output
25
题目大意就是跟斐波那契数列差不多,a[i]=(A*a[i-1]+B*a[i-2])这道题的只能打下表,没有什么其他的方法暂时 要不然就会T掉(时间超限);坑点有两个:
坑点1:如果是A和B是7的倍数的话n大于2所有的结果都为0;
坑点2;在写那个for循环的时候只能是i<=一个不会爆掉的常值(200都过了),取n的时候会T掉或者re掉(数组越界)不知道是怎么回事;
坑点3:输出的时候要判断是否是7的整数倍,如果是的话输出的是最后一位;
ac代码
#include<stdio.h>#include<string.h>#include<stdlib.h>#define N 100005int main(){ int a[N]; int k, n, i; int A, B, ans, flag; while(scanf("%d%d%d",&A,&B,&n)) { if(A==0 && B==0 && n==0) break; a[0]=1;a[1]=1;flag=0; for(i=2;i<N;i++)///就是这里的N取不对很容易爆掉,不能用n { a[i]=(A*a[i-1]+B*a[i-2])%7; if(a[i]==1 && a[i-1]==1){ ans=i-1;break;} if(a[i]==0 && a[i-1]==0){flag=1; break;} } if(flag==1) {printf("0\n");continue;} if(i==n) {printf("%d\n",a[n-1]);continue;} if(n%ans==0){printf("%d\n",a[ans-1]);continue;} printf("%d\n",a[n%ans-1]); } return 0;}
0 0
- HDU 1005 Number Sequence
- hdu 1005 Number Sequence
- HDU 1005 Number Sequence
- hdu 1005 Number Sequence
- HDU 1005 Number Sequence
- HDU 1005 Number Sequence
- HDU 1005 Number Sequence
- HDU--1005--Number Sequence
- hdu 1005 Number Sequence
- HDU 1005 Number Sequence
- HDU 1005 Number Sequence
- HDU 1005 Number Sequence
- hdu - 1005 Number Sequence
- HDU 1005 Number Sequence
- hdu-1005-Number Sequence
- HDU 1005 Number Sequence
- HDU 1005 Number Sequence
- HDU 1005Number Sequence
- 表达式求值
- 子对象构造函数和析构函数的调用顺序
- java反射
- 让Magento 新上传的产品在分类显示最前面?
- 51nod-1434 区间LCM
- hdu 1005 Number Sequence
- 流水行船问题
- java类加载机制
- Android MVP
- 【50.26%】【hdu 5907】Find Q
- 使用HttpClient4.3.1模拟Http请求与无信任证书访问Https
- 二叉树的三种遍历
- 对TCP端口和连接数的一点思考
- linux下一次cp多个文件