HDU 1005 Number Sequence(坑 T_T !!!)

来源:互联网 发布:四大网络小说 编辑:程序博客网 时间:2024/05/20 17:39

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 136851    Accepted Submission(s): 33174

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 31 2 100 0 0
 
Sample Output
25
 
Author
CHEN, Shunbao
 
Source
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[n]);    }}


0 0
原创粉丝点击