zoukankan      html  css  js  c++  java
  • 杭电oj~1005 简单的找周期

    Number Sequence

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


    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
     
    #include<stdio.h>
    int fun(int,int,int );
    int main()
    {
        int a,b;
        long long int n;
    while(scanf("%d %d %lld",&a,&b,&n)!=EOF&&(a||b||n))
    {
        int arr[50]={1,1};
        for(int i=2;i<=48;i++)
        arr[i]=(a*arr[i-1]+b*arr[i-2])%7;
        if(n%49==0) printf("%d",arr[48]);
        else
            printf("%d
    ",arr[n%49-1]);
    }
    return 0;
    
    }
    

      

    周期是7*7==49~yeah!

    周期递归做会超时~毕竟n的规模是10^8,所以时间复杂度必须是O(n)!!!!!

  • 相关阅读:
    纸牌游戏----小猫钓鱼
    数据结构-----栈
    浅谈队列
    排序算法实例
    排序算法之------快速排序
    排序算法之----冒泡排序
    Visual Studio-基本使用
    C++-GUID from string
    OS-Windows CMD生成文件夹目录结构
    OS-Windows10 DownLoad
  • 原文地址:https://www.cnblogs.com/hellohacker/p/5727478.html
Copyright © 2011-2022 走看看