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