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;
}

Buge's Fibonacci Number Problem

时间: 2024-10-02 06:36:18

Buge's Fibonacci Number Problem的相关文章

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

hdu 3509 Buge&#39;s Fibonacci Number Problem

点击此处即可传送 hdu 3509 题目大意:F1 = f1, F2 = f2;; F(n) = a*F(n-1) + b*F(n-2); S(n) = F1^k + F2^k +-.+Fn^k; 求S(n) mod m; 解题思路: 1:首先一个难题就是怎么判断矩阵的维数(矩阵的维数是个变量) 解决方法:开一个比较大的数组,然后再用一个公有变量记一下就行了,具体详见代码: 2:k次方,找规律: 具体上代码吧: /* 2015 - 8 - 16 晚上 Author: ITAK 今日的我要超越昨日

(斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)

根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输入n(其中用number代替,以免和方法中的n混淆)的值,可以得出斐波那契数. 代码如下: /* CC150 8.1 Write a method to generate the nth Fibonacci number Author : Mengyang Rao note : Use two me

Fibonacci number

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Large%20Search%20Engine%20Company%20/Search%20Engine%20Company%20-%20Interview%20Problems%20-%20SOLUTIONS/Phone%20Screen.ipynb Phone Sc

projecteuler----&gt;problem=25----1000-digit Fibonacci number

问题描述: The Fibonacci sequence is defined by the recurrence relation: Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55 F11 = 89 F12 = 144 The 12th term

Project Euler:Problem 25 1000-digit Fibonacci number

The Fibonacci sequence is defined by the recurrence relation: Fn = Fn?1 + Fn?2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55 F11 = 89 F12 = 144 The 12th term, F1

[LeetCode] 509. Fibonacci Number

Description The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N

[LeetCode] Fibonacci Number 斐波那契数字

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0,   F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given

Fibonacci Number LT509

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0,   F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given