HDU 1005 Number Sequence

简介: Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 115124    Accepted Su...

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
 

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;
}


相关文章
|
存储 缓存 关系型数据库
Redo日志 (4)—log sequence number(六十二)
Redo日志 (4)—log sequence number(六十二)
|
Java C++
HDU-1005,Number Sequence(有规律的数学题)
HDU-1005,Number Sequence(有规律的数学题)
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
132 0
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
HDOJ(HDU) 2113 Secret Number(遍历数字位数的每个数字)
117 0
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
118 0
HDOJ 1005 Number Sequence
HDOJ 1005 Number Sequence
99 0
|
人工智能 Java Windows
枚举 + 进制转换 --- hdu 4937 Lucky Number
Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 294    Accepted Submission(s):...
833 0
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
99 1

热门文章

最新文章