您的位置:首页 > 产品设计 > UI/UE

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
);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: