P1306 斐波那契公约数:https://www.luogu.org/problemnew/show/P1306

这道题目就是求第n项和第m项的斐波那契数字,然后让这两个数求GCD,输出答案的后8位;

思路:

  1/gcd(F[n],F[m])=F[gcd(n,m)]。所以先求出gcd(n,m),然后构造斐波那契数列的矩阵快速幂。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e8;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/ struct mat{
int r,c;
ll a[][];
}; int gcd(int a,int b){
if(b == )return a;
return gcd(b, a%b);
}
mat mul(mat a,mat b){
mat tmp = a;
tmp.a[][] = tmp.a[][] = tmp.a[][] = tmp.a[][] = ;
for(int i=; i< a.r; i++){
for(int j=; j<b.c;j++){
for(int k=; k<a.c; k++){
tmp.a[i][j] += a.a[i][k] * b.a[k][j];
tmp.a[i][j] %= mod;
}
}
}
return tmp;
} int ksm(int n){
mat ans,ic;
ic.r = ic.c = ;
ic.a[][] = ic.a[][] = ic.a[][] = ;
ic.a[][] = ; ans.r = ,ans.c = ;
ans.a[][] = ans.a[][] = ;
while(n > ){
if(n&) ans = mul(ans, ic);
ic = mul(ic,ic);
n>>=;
}
return ans.a[][];
}
int main(){
int a,b;
scanf("%d%d", &a, &b);
int n = gcd(a, b);
if(n <=)cout<<<<endl;
else cout<<ksm(n-)<<endl;
return ;
}

P1306

  

最新文章

  1. 【分享】标准springMVC+mybatis项目maven搭建最精简教程
  2. windows下装多个node版本的方法(gnvm)
  3. window安装jekyll
  4. C# 换行符
  5. 用 flow.ci 让 Hexo 持续部署
  6. css-关于absolute和relative的一些笔记
  7. Bzoj3663/4660 CrazyRabbit
  8. BZOJ1001 [BeiJing2006]狼抓兔子(平面图最小割转最短路)
  9. ARM状态和THUMB状态
  10. hdu4924 Football Manager
  11. django中时区设置
  12. 【leetcode】15. 3Sum
  13. sql语句中获取datetime任何部分
  14. 复杂对象创建终结者(Builder Pattern)
  15. 概率dp-九度-1546-迷宫问题
  16. 导入Excel表格到SharePoint站点
  17. Log4net 参数详解
  18. X-008 FriendlyARM tiny4412 uboot移植之copy u-boot到DDR内存
  19. sqoop: mysql to hive
  20. 学习TensorFlow,TensorBoard可视化网络结构和参数

热门文章

  1. 思路重要or技术重要?
  2. 并发编程之Java内存模型
  3. NLP(十三)中文分词工具的使用尝试
  4. Extjs的文件上传问题
  5. go interface衍生的插件化处理
  6. Java性能权威指南读书笔记--之二
  7. git和githup
  8. Docker 的另外两个话题: DockerHub 和 容器网络
  9. 论文阅读 | Falcon: Balancing Interactive Latency and Resolution Sensitivity for Scalable Linked Visualizations
  10. myeclipse源码相关操作