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;
    }
  • 相关阅读:
    CC.NET+SVN+Msbuild
    react服务端/客户端,同构代码心得
    为什么国人很难出高质量开源
    FKP,一套全栈框架,基于react、webpack、koa1、babel
    嵌入式工程师的发展路线
    浅谈学习单片机的一些职业规划
    关于嵌入式新手面试的一些小技巧
    几点心得送给学习嵌入式的新手
    新手学习嵌入式需要掌握的几点知识点
    从迷茫到转机,一个嵌入式工程师的经历
  • 原文地址:https://www.cnblogs.com/yqbeyond/p/4435926.html
Copyright © 2011-2022 走看看