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).
 

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
原创粉丝点击