华中农业大学第五届程序设计大赛 DGCD [fibonacci+矩阵乘法]【数学】
来源:互联网 发布:怎样给朋友发淘宝链接 编辑:程序博客网 时间:2024/05/07 00:05
题目链接:http://acm.hzau.edu.cn/problem.php?id=1202
————————————————————————————————————————
1202: GCD
Time Limit: 1 Sec Memory Limit: 1280 MB
Submit: 241 Solved: 44
[Submit][Status][Web Board]
Description
Input
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
Sample Input
1
1 2 3
Sample Output
1
————————————————————————————————————————
题目大意:
就是让你求
解题方法:
问什么叫方法,因为暴力啊。。。
首先暴力的打了一个
然后就变成了
然后根据fibonacci数列的性质
所以同一个矩阵乘法就可以了
附本题代码
——————————————————————————————————————————
#include <bits/stdc++.h>typedef long long int LL;using namespace std;const int N = 2e5+7;//const int INF = (~(1<<31));int read(){ int x=0,f=1;char ch = getchar(); while(ch<'0'||ch>'9') ch = getchar(); while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch = getchar();} return x;}/*******************************************/int n,m,MOD;const int M = 2;struct Matrix{ LL m[M][M]; void clear0(){ for(int i=0;i<M;i++) for(int j=0;j<M;j++) m[i][j]=0; } void clearE(){ for(int i=0;i<M;i++) for(int j=0;j<M;j++) m[i][j]=(i==j); }};Matrix operator *(Matrix &a,Matrix &b){ Matrix c;c.clear0(); for(int k=0;k<M;k++) for(int i=0;i<M;i++) for(int j=0;j<M;j++) c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j]+MOD)%MOD; return c;}Matrix operator ^(Matrix &a,LL b){ Matrix c;c.clearE(); while(b){ if(b&1) c=c*a; b>>=1,a=a*a; } return c;}LL solve(int x){ Matrix a,b; a.m[0][0]=0,a.m[0][1]=1; b.m[0][0]=0,b.m[0][1]=1; b.m[1][0]=1,b.m[1][1]=1; b=b^x;a=a*b; return a.m[0][0];}int main(){ int _=read(); while(_--){ n=read(),m=read(),MOD=read(); printf("%lld\n",solve(__gcd(n+2,m+2))); } return 0;}
阅读全文
0 0
- 华中农业大学第五届程序设计大赛 DGCD [fibonacci+矩阵乘法]【数学】
- 【待补】华中农业大学第五届程序设计大赛 BChoosy in Food [高斯消元]【数学】
- 【待补】华中农业大学第五届程序设计大赛 FEscape from the Darkness [解析几何]【数学】
- 华中农业大学第五届程序设计大赛网络同步赛
- 华中农业大学第五届程序设计大赛 EOne Stroke [枚举]【思维】
- 华中农业大学第五届程序设计大赛 ICandies [线段树]【数据结构】
- 华中农业大学第五届程序设计大赛 JColor Circle
- 华中农业大学第五届程序设计大赛 KDeadline []【思维】
- 华中农业大学第五届程序设计大赛网络同步赛 H MathematicalGame
- 华中农业大学第五届程序设计大赛网络同步赛 C Friends
- 华中农业大学第五届程序设计大赛 A Little Red Riding Hood [BIT优化dp]【动态规划】
- 华中农业大学第五届程序设计大赛 CFriends [树形dp]【动态规划】
- 华中农业大学第五届程序设计大赛 GSequence Number [树上二分]【数据结构】
- 华中农业大学第五届程序设计大赛 HMathematicalGame [01字典树]【字典树】
- 华中农业大学第五届程序设计大赛 【10/12】个人训练总结
- 华中农业大学第四届程序设计大赛网络同步赛题目
- 华中农业大学第四届程序设计大赛网络同步赛 J
- 华中农业大学第四届程序设计大赛F题LCS
- 动态语言
- [php学习笔记]与类有关的魔术方法、魔术常量、系统函数
- WebService中CXF的简单的配置和应用(ssm框架)
- SpringMVC 重定向
- SpringMVC-文件上传
- 华中农业大学第五届程序设计大赛 DGCD [fibonacci+矩阵乘法]【数学】
- 如何编译Docker源码
- php和html5的混合编写
- HTML、CSS中雪碧图的使用
- thinkphp3.2如何自动生成后台模块并且怎么访问
- [php学习笔记]MVC相关内容
- codeforces 810B
- mysql--事务
- 新闻客户端android