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 = ; 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 = ; i < k; i ++)
{
t1 *= f1;
t1 %= m;
} t2 = f2;
for(int i = ; i < k; i ++)
{
t2 *= f2;
t2 %= m;
} sum = (( t1 + t2 ) % m);
sum %= m; for(int i = ; i < n; i++)
{
f3 = ( a * f2 + b * f1 ) % m;
t3 = f3;
for(int i = ; i < k; i ++)
{
t3 *= f3;
t3 %= m;
} sum += t3;
sum %= m; f1 = f2;
f2 = f3;
//cout<<sum<<endl;
} cout<<sum<<endl;
sum = ;
}
return ;
}

Buge's Fibonacci Number Problem的更多相关文章

  1. 【HDOJ】3509 Buge's Fibonacci Number Problem

    快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. /* 3509 */ #inc ...

  2. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  3. [UCSD白板题 ]Small Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  4. 【leetcode】509. Fibonacci Number

    problem 509. Fibonacci Number solution1: 递归调用 class Solution { public: int fib(int N) { ) return N; ...

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

    根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...

  6. 求四百万以内Fibonacci(number)数列偶数结果的总和

    又对啦...开心~~~~ 只是代码可能不符合PEP标准什么的... Each new term in the Fibonacci sequence is generated by adding the ...

  7. Fibonacci number

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  8. Algorithms - Fibonacci Number

    斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...

  9. 【LEETCODE】44、509. Fibonacci Number

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

随机推荐

  1. S​Q​L​获​取​当​前​时​间​(​日​期​)

    --获取当前日期(如:yyyymmdd) select CONVERT (nvarchar(12),GETDATE(),112) --获取当前日期(如:yyyymmdd hh:MM:ss) selec ...

  2. 【JAVA】FOR UPDATE 和 FOR UPDATE NOWAIT 区别 (转)

    1.for update 和 for update nowait 的区别:首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何限 ...

  3. Web服务器基础学习

    1)Socket通信相当于两个人通过电话联系,Http协议相当于电话联系时所使用的中文2)Http1.1前均为短连接,1.1版本为长连接,即服务器接收一次请求并发送响应后会等待一段时间看浏览器是否在这 ...

  4. wifi 破解

      基础的知识: ESSID :无线网络的名字 BSSID  :是AP的mac地址 CH: 工作信道 AP   :无线访问接入点 WEP WPA/WPA2 STATION   :客户机mac DHCP ...

  5. Oracle EBS - AOL

    AOL: (Path: /u43/dev6/interface/aol) 1. Goto system administrator response 2. View -> Request (Sa ...

  6. 完成对数据库的CRUD操作

    PS:查询相对复杂,要处理结果集,增删改则不用. package it.cast.jdbc; import java.sql.Connection; import java.sql.ResultSet ...

  7. php操作数据库的简单示例

    放假期间自己又写了几个简单的网页,但在服务器中打开时和在网站上打开时不一样,在服务器中打开的出现了错误,字体比一般的腰大好多,页面也相应地变大了,一些块即使用了浮动和clear浮动还是被遮住了,我只好 ...

  8. PHP处理一个5G文件,使用内存512M的,数据为整形,从大到小排序,优化排序算法

    $file='./new.txt'; $fp = fopen($file, "r"); $chunk = 4096;//一次处理1M的字节 1M=1024*1024 $fs = f ...

  9. ueditor编辑器使用

    下载ueditor1_4_3_3-gbk-asp 解压后重命名为ueditor上传至网站 在需要编辑器的位置增加asp代码: <td style="PADDING-LEFT: 10px ...

  10. 总结-Hibernate

    JPA 全称 Java Persistence API @Entity @Table(name = "user") public class User { @Id @Generat ...