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. [NOIP2011]Mayan游戏 题解

    题目大意: 有一个5*7的方格,上面有几种颜色的方块,如果在一横行或者竖列上有连续三个或者三个以上相同颜色的方块,则它们将立即被消除,方块消除之后,消除位置之上的方块将掉落.每步移动可以且仅可以沿横向 ...

  2. tableview 位置发生偏移

    状况描述:1.首次进入该界面时正常 2.push了新的界面后,再返回该界面 tableview和导航栏直接出现了间隔区域 tableview为代码创建 _tableView =  [[UITableV ...

  3. 李洪强iOS经典面试题141-报错警告调试

    李洪强iOS经典面试题141-报错警告调试   报错警告调试 你在实际开发中,有哪些手机架构与性能调试经验 刚接手公司的旧项目时,模块特别多,而且几乎所有的代码都写在控制器里面,比如UI控件代码.网络 ...

  4. Python爬虫:Xpath语法笔记

    一.选取节点 常用的路劲表达式: 表达式 描述 实例   nodename 选取nodename节点的所有子节点 xpath(‘//div’) 选取了div节点的所有子节点 / 从根节点选取 xpat ...

  5. sqlserver 性能优化常用方法

    查看被锁表: select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys. ...

  6. DS实验题 融合软泥怪-2 Heap实现

    题目和STL实现:DS实验题 融合软泥怪-1 用堆实现优先队列 引言和堆的介绍摘自:Priority Queue(Heaps)--优先队列(堆) 引言: 优先队列是一个至少能够提供插入(Insert) ...

  7. 张小龙在2017微信公开课PRO版讲了什么(附演讲实录和2016微信数据报告)

    今天2017微信公开课PRO版在广州亚运城综合体育馆举行,这次2017微信公开课大会以“下一站”为主题,而此次的微信公开课的看点大家可能就集中在腾讯公司高级副总裁.微信之父——张小龙的演讲上了!今天中 ...

  8. JMeter 将上一个请求的结果作为下一个请求的参数——使用正则提取器(转载)

    在接口测试和压力测试过程中,经常会将几个流程串联起来才能测试.如:我要进行获取用户信息接口测试,我就要先登录成功后,才能获取用户信息.所以,我就要首先要登录,获得我的登录凭证(tokenId或tick ...

  9. SQL Server 2014 Backup Encryption

    转载自: Microsoft MVP Award Program Blog 来源:Microsoft MVP Award Program Blog 的博客:https://blogs.msdn.mic ...

  10. 从一个NOI题目再学习二分查找。

    二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...