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. 数论 - Vanya and Computer Game

    Vanya and his friend Vova play a computer game where they need to destroy n monsters to pass a level ...

  2. Updating My Notepad_1.1

    The old version Notepad 1.0 you can get it from below link : My Notepad I am very happy to announce ...

  3. jQuery中的事件和动画效果

    刚刚学习了jqyery的一些事件和动画,下面我来总结一下: 1.基础事件 1.window事件,它的对应方法是ready(),$(document).ready()方法是事件模块中最重要的一个函数,可 ...

  4. PHP 创建缩略图

    一.成比例缩小图像 <?php /* 创建缩略图 */ $file = __DIR__.'/button.png'; $scale = 0.5; // 比例 $image = ImageCrea ...

  5. TID大会学习心得之敏捷软件架构-微服务

    敏捷微服务构建 王威: TW咨询师.架构转型教练.敏捷技术教练 敏捷的目标 敏捷的目标是提升效率?降低成本?减员增效? 敏捷:关注价值.快速反馈.快速响应.其的目标是提升响应力,响应力的提升不一定会提 ...

  6. session过期时间

    在一般系统登录后,都会设置一个当前session失效的时间,以确保在用户没有使用系统一定时间后,自动退出登录,销毁session. 具体设置很简单: 在主页面或者公共页面中加入:session.set ...

  7. EditText添加了ImageSpan后,在两者中间不能输入纯文本

    严格来说是连续插入两个ImageSpan之后,在其中间不能够输入纯文本内容. 最后发现问题出现在了SpannableString在设置ImageSpan的时候第四个参数flag的问题. spannab ...

  8. mongodb权限管理

    说到mongodb就得先谈谈mongodb的用户组,和传统的关系型数据库不一样,mongodb并没有在创建应用时就要求创建权限管理组,所以类似于Robomongo这样的数据库可视化工具在创建conne ...

  9. 从单幅图像高质量去除运动模糊——读JiaYaJia同名英文论文总结

    原始论文在这里 http://www.cse.cuhk.edu.hk/leojia/projects/motion_deblurring/ 一.概述 论文根据以下的基本模糊图像模型建立 其中I是我们观 ...

  10. http://www.cnblogs.com/holly8/p/6178828.html

    http://www.cnblogs.com/holly8/p/6178828.html