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

HDU 1005 Number Sequence

2015-01-31 20:55 323 查看


Number Sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 115124 Accepted Submission(s): 27959



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 3
1 2 10
0 0 0


Sample Output

2
5


Author

CHEN, Shunbao

Source

ZJCPC2004

Recommend

JGShining | We have carefully selected several similar problems for you: 1008 1021 1019 1012 1108

这道题真的让我觉得有些头大,题意简单明了,就是按照它的公式推出后来的项目,然后提交发现是超时的,这个时候才发现后面的那个n非常大,但是通项,通项又求不出来,只好但是看的mod 7 觉得应该可以找到规律了吧,嗯,然后就找,那如果两个 1 再次出现不久刚好是个循环吗,但到这里还是把问题想的太简单了,提交了一次RE掉了,后来差到题解,才发现还这是不够的如果出现 7 7 n 这种例子,后面将全是0,然后数组不管开多大都会爆炸,所以不应该用题目已经给你了的值,应该根据输入的情况定出检查的两个值。所以修改后应该是推后两位去取值

#include<cstdio>
#define maxn 60
using namespace std;
int f[maxn] = {0,1,1};
int main(){
int a,b,c;
while(~scanf("%d %d %d",&a,&b,&c) && a && b && c){
int cnt;
for(int i = 3;i < 54;i++){
f[i] = (f[i-1]*a + f[i-2]*b)%7;
if(i > 5){                          //取值定在5之上是为了防止前面的重复区段
if(f[i-1] == f[3] && f[i] == f[4]){
cnt = i - 4;
break;
}
}
}
if(c > 2){
printf("%d\n",f[(c-3)%cnt + 3]);    //这段代码是为了防止取值取到零
}else{
printf("1\n");
}
}
return  0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: