HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5
题解:把系数矩阵的数换下即可
代码:
#include<stdio.h>
#include<string.h>
#define MAX 10
typedef long long ll;
ll p,q,MOD=7;
struct mat{
ll a[MAX][MAX];
};
mat operator *(mat x,mat y) //重载*运算
{
mat ans;
memset(ans.a,0,sizeof(ans.a));
for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
for(int k=1;k<=2;k++){
ans.a[i][j]+=x.a[i][k]*y.a[k][j];
ans.a[i][j]%=MOD;
}
}
}
return ans;
}
mat qsortMod(mat a,ll n) //矩阵快速幂
{
mat t;
t.a[1][1]=p;t.a[1][2]=q; //变式的系数
t.a[2][1]=1;t.a[2][2]=0;
while(n){
if(n&1) a=t*a; //矩阵乘法不满足交换律,t在前
n>>=1;
t=t*t;
}
return a;
}
int main()
{
ll n;
while(scanf("%lld%lld%lld",&p,&q,&n)!=EOF)
{
if(p==0&&q==0&&n==0)
break;
if(n==1) printf("1\n");
else if(n==2) printf("1\n");
else{
mat a;
a.a[1][1]=1;a.a[1][2]=0;
a.a[2][1]=1;a.a[2][2]=0;
a=qsortMod(a,n-2);
printf("%lld\n",a.a[1][1]);
}
}
return 0;
}
HDU - 1005 -Number Sequence(矩阵快速幂系数变式)的更多相关文章
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- Yet another Number Sequence 矩阵快速幂
Let’s define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n ...
- SDUT1607:Number Sequence(矩阵快速幂)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1607 题目描述 A number seq ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- CodeForces 392C Yet Another Number Sequence 矩阵快速幂
题意: \(F_n\)为斐波那契数列,\(F_1=1,F_2=2\). 给定一个\(k\),定义数列\(A_i=F_i \cdot i^k\). 求\(A_1+A_2+ \cdots + A_n\). ...
随机推荐
- @RequestMapping 参数详解
引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为applicatio ...
- Xor 思维题
Xor 思维题 题目描述 小\(Q\)与小\(T\)正在玩一棵树.这棵树有\(n\)个节点,编号为 \(1\),\(2\) \(3...n\),由\(n-1\)条边连接,每个节点有一个权值\(w_i\ ...
- 简单快速导出word文档
最近,我写公司项目word导出功能,应该只有2小时的工作量,却被硬生生的拉长2天,项目上线到业务正常运行也被拉长到2个星期. 为什么如此浪费时间呢? 1)公司的项目比较老,采用硬编码模式,意味着wor ...
- topic的相关操作
1.建立topic cd 进入kafka的安装根目录的bin目录下 执行:./kafka-topics.sh --zookeeper ip:port,ip:port,ip:port/kafka-tes ...
- async + await 异步
先执行A在执行B再执行.then里面的AAA() { XXXXX一堆代码 this.BBB().then(()=>{ 其他代码 })}, async BBB(){ let res = await ...
- JavaScript学习系列博客_12_JavaScript中的break、continue关键字
break关键字 -break关键字可以用来退出switch或循环语句 -不能在if语句中使用break和continue,但不是说if语句里面不能写break关键字,break关键字一定要包含在sw ...
- DML语言(数据操纵语言)
#DML语言/*数据操作语言:插入:insert修改:update删除:delete */ #一.插入语句#方式一:经典的插入/*语法:insert into 表名(列名,...) values(值1 ...
- 算法-图(2)Bellman-Ford算法求最短路径
template <class T,class E> void Bellman-Ford(Graph<T,E>&G, int v, E dist[], int path ...
- ARM开发板挂载Ubuntu18.04主机的NFS共享文件夹
环境 ubuntu主机环境:Window10 下装VMWare下装的 ubuntu18.04LTS x64 IP 192.168.10.119 Window10下配置192.168.10该网段 开发板 ...
- 兄弟,别再爬妹子图了整点JS逆向吧--陆金所密码加密破解
好久没有写爬虫文章了,今晚上得空看了一下陆金所登录密码加密,这个网站js加密代码不难,适合练手,篇幅有限,完整js代码我放在了这里从今天开始种树,不废话,直接开整. 前戏热身 打开陆金所网站,点击到登 ...