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;
    }
  • 相关阅读:
    财务自由之路
    权力的48条法则
    将进酒
    DELL服务器报价,有公司需要可以联系,谢谢。北京经纬恒通商贸有限公司秦嘉俊
    实战HTML5表单
    《HTML5+CSS3精通》
    行路难
    事件入门
    DOM
    剑指offer---包含min函数的栈
  • 原文地址:https://www.cnblogs.com/aerer/p/9931032.html
Copyright © 2011-2022 走看看