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;
    }
  • 相关阅读:
    Hibernate+mysql 中文问题解决方案.
    FpSpread表格控件,FpSpread事件介绍(一)
    如何实现打开有宏的EXCEL时不提示
    使用VB.Net写一个简单的数据访问层(不能称ORM):CRUD操作
    Asp.NET 时间Since转换
    64位操作系统上。NET操作MSMQ的问题
    IIS7配置管理Windows2008 64位系统IIS7的问题
    数据库开发批量附加数据库
    IIS7中对静态文件的处理
    techsailor三步曲
  • 原文地址:https://www.cnblogs.com/yqbeyond/p/4435926.html
Copyright © 2011-2022 走看看