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
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的更多相关文章
- 【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 ...
- [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_ ...
- [UCSD白板题 ]Small Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- 【leetcode】509. Fibonacci Number
problem 509. Fibonacci Number solution1: 递归调用 class Solution { public: int fib(int N) { ) return N; ...
- (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)
根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...
- 求四百万以内Fibonacci(number)数列偶数结果的总和
又对啦...开心~~~~ 只是代码可能不符合PEP标准什么的... Each new term in the Fibonacci sequence is generated by adding the ...
- Fibonacci number
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- Algorithms - Fibonacci Number
斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...
- 【LEETCODE】44、509. Fibonacci Number
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- EF MySql 配置文件
<?xml version="1.0" encoding="utf-8"?><!--有关如何配置 ASP.NET 应用程序的详细信息,请访问 ...
- android studio 问题1
在使用androidStudio中,经常导入其他的项目,有时候会出现以下错误: rror:FAILURE: Build failed with an exception. * What went wr ...
- UML图示说明
UML图示说明 继承:实线空三角形箭头指向父类 接口:虚线空三角形箭头指向接口 关联:实线无箭头方指向拥有者 聚合:实线空心菱形箭头指向整体,部分可以单独存在 组合:实线菱形指向整体,部分不能单独存在 ...
- ubuntu14.04下安装ngnix,mediawiki,nodebb,everything,gitlab
本周折腾了以下几个东西,mediawiki(维基),nodebb(论坛),gitlab(私有git服务器). 本来的目的是搭建一个wiki,选用了mediawiki后,使用apache搭建好了. 搭论 ...
- php ajax请求和返回
define('NOW_TIME', $_SERVER['REQUEST_TIME']); define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']); d ...
- JavaScript 入门教程四 语言基础【2】
一.数据类型介绍: undefined null NaN 1.判断当前变量是否为 undefined: if (i === undefined) 或者 if (typeof (i) === " ...
- AFNetwork源码解析
1.关于AFRequestSerializer: 这里分好几个部分,我们首先从NSMutableRequest的相关方法来出发: 比如我们要上传一个文件,那么需要些很麻烦的请求体: HTTP请求头我们 ...
- django 有model生成SQL以及现有反向表生成model
已有models生成SQL语句 语法 python manage.py sqlall app_name # app_name, 在settings已经导入, 如: INSTALLED_APPS = ...
- 阿里云服务器Linux CentOS安装配置(三)yum安装mysql
阿里云服务器Linux CentOS安装配置(三)yum安装mysql 1.执行yum安装mysql命令:yum -y install mysql-server mysql-devel 2.启动mys ...
- ajax跨域通过 Cors跨域资源共享 进行GetPost请求
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...