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;
    }
  • 相关阅读:
    一文说透 Spring 循环依赖问题
    git修改已经push的commit message
    Connection Timeout 和CommandTimeout
    mvc 当中 [ValidateAntiForgeryToken] 的作用及用法
    mvc 当中 [ValidateAntiForgeryToken] 的作用及用法
    asp.net mvc与asp.net core Ajax删除操作delete中带ValidateAntiForgeryToken实例
    VS2017秘钥
    Sql server 2008 R2 配置管理工具服务显示远程过程调用失败:0x800706be
    SQL Server 2008找不到SQL Server配置管理器的问题
    如何为SQL Server2008添加登录账户并配置权限
  • 原文地址:https://www.cnblogs.com/yqbeyond/p/4435926.html
Copyright © 2011-2022 走看看