HZAU 1202 GCD (矩阵快速幂 + GCD)
来源:互联网 发布:获取所有股票信息 js 编辑:程序博客网 时间:2024/04/29 19:54
Problem Description
Xiao Ming found the compute time of
gcd(fibn,fibn+1) is the most when he learnt the gcd, and the result of it is alwaysfib1 but he is not satisfied with the simple compute result.He wants to know what
gcd(1+Sn,1+Sm) equals.And gcd is greatest common divisor,
fib1=1,fib2=1,fibn=fibn−1+fibn−2(n≥3) Sn=∑i=1nfibi
Input Description
The first line is an positive integer T. (1 ≤ T ≤ 10^3) indicates the number of test cases. In the next T lines, there are three positive integer n, m, p(1 ≤ n, m, p ≤ 10^9) at each line.
Output Description
In each test case, output the compute result of
gcd(1+Sn,1+Sm)%p at one line.
Sample Input:
11 2 3
Sample Output:
1
题意
给出
思路
斐波那契数列有这样两条性质:
于是,题目的答案便是
最大公约数可以用辗转相除法求得,然后再利用矩阵快速幂得到斐波那契数第
AC 代码
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>using namespace std;typedef long long LL;LL n,m,mod;LL gcd(LL a,LL b){ if(b==0)return a; return gcd(b,a%b);}struct node{ LL mp[2][2]; void init(LL a,LL b,LL c,LL d) { mp[0][0]=a; mp[0][1]=b; mp[1][0]=c; mp[1][1]=d; } void mult(node x,node y) //两矩阵乘法 { memset(mp,0,sizeof(mp)); for(LL i=0; i<2; i++) for(LL j=0; j<2; j++) for(LL k=0; k<2; k++) mp[i][j]=(mp[i][j]+x.mp[i][k]*y.mp[k][j])%mod; };} init;struct node expo(struct node x, LL k) //进行k次幂运算{ struct node tmp; tmp.init(1,0,0,1); //单位矩阵 while(k) //快速幂部分 { if(k&1)tmp.mult(tmp,x); x.mult(x,x); k>>=1; } return tmp;}int main(){ int T; ios::sync_with_stdio(false); cin>>T; while(T--) { cin>>n>>m>>mod; int k=gcd(n+2,m+2); init.init(1,1,1,0); cout<<expo(init,k).mp[0][1]%mod<<endl; } return 0;}
- HZAU 1202 GCD (矩阵快速幂 + GCD)
- HZAU 1202 GCD( 斐波那契数列+矩阵快速幂)
- HZAU 1202 GCD(斐波那契数列性质+矩阵快速幂)
- NZAU 1202: GCD 矩阵快速幂 gcd和fib数列一些结论
- AOJ 401 gcd定理 + 矩阵快速幂
- GCD,快速GCD,扩展GCD
- gcd+lcm+快速幂
- Qbxt 模拟赛 Day4 T2 gcd(矩阵乘法快速幂)
- hdu1108 1061 gcd 快速幂
- 素数 快速幂 gcd.lcm
- 快速gcd vs 普通gcd
- 四一。 数论 。4.19(gcd 扩展gcd,素数 ,快速幂)
- hzau1202——GCD(斐波那契&矩阵快速幂)
- 矩阵快速幂-GCD-斐波那契-HZAU2017现场赛-D
- hdu 5780 gcd(线性筛+快速幂+数论)
- gcd
- GCD
- GCD
- C/C++与Python混合编程
- Java 成员变量和局部变量
- 1.SpringMVC入门
- C/C++常用标准库函数
- POJ 2376 Cleaning Shifts(贪心)
- HZAU 1202 GCD (矩阵快速幂 + GCD)
- ArrayList自动扩容解析
- 关于Unix网络编程卷一创建守护进程
- 关于Hybrid App 配置
- 445. Add Two Numbers II
- 扩展kmp(HDU4333)
- nat123+OpenVPN连接校园网
- 4.23
- 1001. A+B Format (20)