• 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 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
    ",f[(c-3)%cnt + 3]);    //这段代码是为了防止取值取到零
            }else{
                printf("1
    ");
            }
        }
        return  0;
    }
    


  • 相关阅读:
    Flume 读取实时更新的日志文件
    一些关于Flume收集日志的资料
    Java Pattern Matcher 正则表达式需要转义的字符
    多播 & multicast
    branch prediction
    一时紧张简单题都没做好,哈
    海量数据求中位数
    继续过Hard题目.0207
    压力工具代码及epoll使用
    C++里面mutable的作用
  • 原文地址:https://www.cnblogs.com/lccurious/p/5079881.html
Copyright © 2020-2023  润新知