洛谷- P1306 斐波那契公约数 - 矩阵快速幂 斐波那契性质
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
洛谷- P1306 斐波那契公约数 - 矩阵快速幂 斐波那契性质的更多相关文章
- [P1306] 斐波那契公约数 (矩阵快速幂+斐波那契数列)
一开始数据没加强,一个简单的程序可以拿过 gcd(f[n],f[m])=f[gcd(n,m)] 下面这个是加强数据之后的80分代码 #include<bits/stdc++.h> usin ...
- 【洛谷】P1357 花园(状压+矩阵快速幂)
题目 传送门:QWQ 分析 因为m很小,考虑把所有状态压成m位二进制数. 那么总状态数小于$ 2^5 $. 如果状态$ i $能转移到$ j $,那么扔进一个矩阵,n次方快速幂一下. 答案是对角线之和 ...
- 洛谷P1962 斐波那契数列(矩阵快速幂)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
- HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- POJ 3070(求斐波那契数 矩阵快速幂)
题意就是求第 n 个斐波那契数. 由于时间和内存限制,显然不能直接暴力解或者打表,想到用矩阵快速幂的做法. 代码如下: #include <cstdio> using namespace ...
- POJ3070 斐波那契数列 矩阵快速幂
题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...
- hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)
Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...
- hdu4549 M斐波那契数列 矩阵快速幂+快速幂
M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的 ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
随机推荐
- 贪心算法---The best time to buy and sell store-ii
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
- 【iOS】no identity found Command /usr/bin/codesign failed with exit code 1
今天遇到了这个问题,详情如下图: 后来发现是自己脑子短路了……只添加了 Provisioning Profiles, 而忘记添加 Certificates, 就是下面的两个:
- C# 二维码的生成
nuget 搜索qrcodenet,然后选择下载gma.qrcodenet public partial class Form1 : Form { public Form1() { Initializ ...
- poj 2524 Ubiquitous Religions(简单并查集)
对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...
- 使用webstorm调试node.js
折腾半天,还是webstorm顺手,但也遇到一些小问题. 1.代码补全问题 nodeJS自身的补全 File->Project Setting->JavaScript->Librar ...
- 99% 的人都不知道的 Kubernetes 网络疑难杂症排查方法
原文链接:Kubernetes 网络疑难杂症排查分享 大家好,我是 roc,来自腾讯云容器服务 (TKE) 团队,经常帮助用户解决各种 K8S 的疑难杂症,积累了比较丰富的经验,本文分享几个比较复杂的 ...
- SAP-批量删除生产订单
1.SE38运行:PPARCHP1 2.先用COOIS导出订单,已经CLSD,没有删除的
- 学习Vuex 个人的一些拙见。
首先说下什么是vuex?这个是对vue的状态的管理,这样说可能有点大,其实就是vue 里面 data 的管理,或者说是多个vue 组件共有的data 的一种管理, 在任何一个组件里面,都可以修改,访 ...
- 爱奇艺JAVA后台面经
链接:https://www.nowcoder.com/discuss/217425 1.volatile关键字的含义 2.Java NIO 讲一下 2.1 NIO selector,epoll的区别 ...
- adb 最常用最简单的命令-install/push/pull 使用
以vivo测试机为例1.网上下载adb工具包,安装---网上有教程2.手机连接电脑后,进入手机设置--更多设置---开发者选项,打开开发者选项和USB调试: (不同手机开发者选项进入方式不同)3.打开 ...