zoukankan      html  css  js  c++  java
  • HDU1005

    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

    思路:常见的解法是矩阵快速幂,我还不会-_-||,而用递归来写的话则会超时,需要进行优化

    由同余定理:(a+b)%c=(a%c+b%c)%c     ,而题中要求%7,则a和b取值在0到6之间,所以共有7*7=49种情况,往后会成循环了,所以要将题中m改为m%49,即可AC;

    #include <iostream>
    
    using namespace std;
    int fb(int x,int y,int n)
    {
        if(n==1||n==2)
            return 1;
        return (x*fb(x,y,n-1)+y*fb(x,y,n-2))%7;
    }
    int main()
    {
        int x,y,m;
        while(cin>>x>>y>>m && x+y+m)
        {
            int a=fb(x,y,m%49);
            cout<<a<<endl;
        }
        return 0;
    }
  • 相关阅读:
    angular4浏览器兼容问题
    angular4组件生命周期
    angular4路由
    CDH 安装配置指南(Tarball方式)
    nginx-1.14.0安装
    redis-3.0.6安装
    CDH安装kafka
    CDH配置JAVA_HOME
    ntp集群时间同步
    VMware联网
  • 原文地址:https://www.cnblogs.com/aerer/p/9931032.html
Copyright © 2011-2022 走看看