[P1306] 斐波那契公约数 (矩阵快速幂+斐波那契数列)
一开始数据没加强,一个简单的程序可以拿过
gcd(f[n],f[m])=f[gcd(n,m)]
下面这个是加强数据之后的80分代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
int main()
{
ll n,m,a=,b=,c=;cin>>n>>m;
for(ll i=;i<gcd(n,m);i++)
{
c=(a+b)%;
a=b;
b=c;//cout<<c%100000000<<endl;
}
cout<<c%;
return ;
}//1 1 2 3 5
最后一个点TLE,吸氧了之后还是过不了
因此是算法的问题
我这个蒟蒻不会优化,于是看了题解
题解给的是矩阵的优化
蒟蒻并不会这个算法
下面是神仙程序
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ymw 100000000
using namespace std;long long n,m;
struct node{long long a[][],r,c;};
inline node mul(node x,node y)//x*y的结果返回给z
{
node z;
memset(&z,,sizeof(z));
for(register int i=;i<x.r;i++)
for(register int j=;j<y.c;j++)
for(register int k=;k<x.c;k++)
z.a[i][j]=(z.a[i][j]+x.a[i][k]*y.a[k][j])%ymw;
z.r=x.r;z.c=y.c;
return z;
}
inline long long ksm(long long y)//快速幂加速递推
{
node x,ans;
memset(&x,,sizeof(x));
memset(&ans,,sizeof(ans));
x.r=x.c=ans.c=;
ans.r=;
x.a[][]=x.a[][]=x.a[][]=;
ans.a[][]=ans.a[][]=;
while(y)
{
if(y&) ans=mul(ans,x);
x=mul(x,x);
y>>=;
}
return ans.a[][];
}
signed main()
{
scanf("%lld%lld",&n,&m);
n=__gcd(n,m);//计算
if(n<) return putchar()&;//特判
printf("%lld",ksm(n-));//输出
}
[P1306] 斐波那契公约数 (矩阵快速幂+斐波那契数列)的更多相关文章
- 洛谷- P1306 斐波那契公约数 - 矩阵快速幂 斐波那契性质
P1306 斐波那契公约数:https://www.luogu.org/problemnew/show/P1306 这道题目就是求第n项和第m项的斐波那契数字,然后让这两个数求GCD,输出答案的后8位 ...
- POJ 3070(求斐波那契数 矩阵快速幂)
题意就是求第 n 个斐波那契数. 由于时间和内存限制,显然不能直接暴力解或者打表,想到用矩阵快速幂的做法. 代码如下: #include <cstdio> using namespace ...
- hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)
Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- 数论+矩阵快速幂|斐波那契|2014年蓝桥杯A组9-fishers
标题:斐波那契 斐波那契数列大家都非常熟悉.它的定义是: f(x) = 1 .... (x=1,2) f(x) = f(x-1) + f(x-2) .... (x>2) 对于给定的整数 n 和 ...
- poj3070 (斐波那契,矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9630 Accepted: 6839 Descrip ...
- UVA10689 Yet another Number Sequence —— 斐波那契、矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10689 题解: 代码如下: #include <iostream> #include <cstdio> ...
- codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质
E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...
- HDU6395(分段+矩阵快速幂)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6395 给你一个式子,给出你A,B,C,D,P,n,让你求出第n项的式子Fn.(其中ABCDPn均在1e9的 ...
随机推荐
- windows上编译boost库
要用xx库,编译boost时就指定--with-xx.例如: # 下载并解压boost_1.58 # 进入boost_1.58目录 bjam.exe toolset=msvc-14.0 --build ...
- Android Studio启动时出现unable to access android sdk add-on list
目录 Android Studio First Run 检测 Android SDK 及更新,由于众所周知的原因,我们会「Unable to access Android SDK add-on lis ...
- 旋转矩阵 The Rotation Matrix
参考: http://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/how-does ...
- Linux(CentOS7)安装Tomcat
概述 Tomcat是运行Jsp文件的容器服务,能够处理URL请求,类似于IIS.相对于IIS,Tomcat可以部署到Linux.Windows.IOS等操作系统.这里主要整理将Tomcat部署到Lin ...
- 网页前端 html js 相关
1.注释 1.1HTML 注释 http://www.w3school.com.cn/html/html_comments.asp 注释标签 <!-- 与 --> 用于在 HTML 插入注 ...
- Ng Alain使用 - cli和克隆两种方式
感觉没啥要写的,但是在查看相关资料的过程中发现不少浮夸的人,可以说是完全不阅读官方文档,操作完全无厘头,,创建了删,配置,再删除,,,扯蛋....., 方式一:CLI(推荐) # 确保使用的是最新版本 ...
- Tychonov Theorem
(Remark: The proof presented in this post is a reorganization and interpretation of that given by Ja ...
- [转]git 删除远程仓库文件
来源:https://www.jianshu.com/p/de75a9e3d1e1 git删除远程文件夹或文件的方法 项目开发初期由于.gitignore 文件配置不正确很有可能导致某些不需要的目录上 ...
- [转]PO BO VO DTO POJO DAO概念及其作用(附转换图)
来源:http://www.blogjava.net/vip01/archive/2013/05/25/92430.html J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候 ...
- 支付宝 app支付 沙盘使用
文档说明 沙箱测试: App支付沙箱接入注意点 1.app支付支持沙箱接入:在沙箱调通接口后,必须在线上进行测试与验收,所有返回码及业务逻辑以线上为准:2.app支付只支持余额支付,不支持银行卡.余额 ...