zoukankan      html  css  js  c++  java
  • Buge's Fibonacci Number Problem

    Buge's Fibonacci Number Problem

    Description

    snowingsea is having Buge’s discrete mathematics lesson, Buge is now talking about the Fibonacci Number. As a bright student, snowingsea, of course, takes it as a piece of cake. He feels boring and soon comes over drowsy.
    Buge,feels unhappy about him, he knocked at snowingsea’s head, says:”Go to solve the problem on the blackboard!”, snowingsea suddenly wakes up, sees the blackboard written :

    snowingsea thinks a moment,and writes down:



    snowingsea has a glance at Buge,Buge smiles without talking, he just makes a little modification on the original problem, then it becomes :

      

    The modified problem makes snowingsea nervous, and he doesn't know how to solve it. By the way,Buge is famous for failing students, if snowingsea cannot solve it properly, Buge is very likely to fail snowingsea. But snowingsea has many ACM friends. So,snowingsea is asking the brilliant ACMers for help. Can you help him?

    Input

    The input consists of several test cases. The first line contains an integer T representing the number of test cases. Each test case contains 7 integers, they are f1, f2, a, b, k, n, m which were just mentioned above, where 0 < f1, f2, a, b, n, m < 1000 000 000, and 0 ≤ k < 50.

    Output

    For each case, you should print just one line, which contains S(n,k) %m.

    Sample Input

    3
    1 1 1 1 1 2 100000
    1 1 1 1 1 3 100000
    1 1 1 1 1 4 100000
    

    Sample Output

    2
    4
    7
    

    HINT

    解题思路:就是一个简单的摸用算性质的应用,网上好多人用的是矩阵的一些性质,原谅我现代没好好学。

    #include<iostream>
    #define ll long long
    using namespace std;
     
    ll f1, f2, a, b, k, n, m;
    ll f3, t1, t2, t3;
    ll sum = 0;
     
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            cin>>f1>>f2>>a>>b>>k>>n>>m;
     
            f1 %= m;
            f2 %= m;
            t1 = f1;
            for(int i = 1; i < k; i ++)
            {
                t1 *= f1;
                t1 %= m;
            }
     
            t2 = f2;
            for(int i = 1; i < k; i ++)
            {
                t2 *= f2;
                t2 %= m;
            }
     
            sum = (( t1 + t2 ) % m);
            sum %= m;
     
            for(int i = 2; i < n; i++)
            {
                f3 = ( a * f2 +  b * f1 ) % m;
                t3 = f3;
                for(int i = 1; i < k; i ++)
                {
                    t3 *= f3;
                    t3 %= m;
                }
     
                sum += t3;
                sum %= m;
     
                f1 = f2;
                f2 = f3;
                //cout<<sum<<endl;
            }
     
            cout<<sum<<endl;
            sum = 0;
        }
        return 0;
    }
  • 相关阅读:
    PAT 1035. 插入与归并(25)
    PAT 1034. 有理数四则运算(20)
    PAT 1033. 旧键盘打字(20)
    PAT 1032. 挖掘机技术哪家强(20)
    PAT 1031. 查验身份证(15)
    PAT 1030. 完美数列(25)
    PAT 1029. 旧键盘(20)
    PAT 1028. 人口普查(20)
    PAT 1027. 打印沙漏(20)
    PAT 1026. 程序运行时间(15)
  • 原文地址:https://www.cnblogs.com/yqbeyond/p/4435926.html
Copyright © 2011-2022 走看看