UVALive 7361 Immortal Porpoises (矩阵快速幂)
来源:互联网 发布:七天网络查询分数 编辑:程序博客网 时间:2024/06/10 12:56
大体题意:
让你求n个斐波那契数列,对1e9取模!
思路:
很明显矩阵快速幂 求矩阵1 1
1 0的n 次方即可!
但是好久没写了,写了很久很久!!= = !
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<cstdlib>#include<vector>#include<set>#include<map>#include<stack>#include<cctype>#include<string>#include<iostream>#include<queue>#define fout freopen("out.txt","w","stdout");#define mr make_pair#define fi first#define se secondusing namespace std;typedef long long ll;typedef unsigned long long LLU;const int maxn = 1000 + 5;const int mod = 1000000000;const double eps = 1e-8;const int inf = 0x3f3f3f3f;struct Mar{ ll a[2][2]; void clear(){ memset(a,0,sizeof a); } bool operator = (Mar rhs) { a[0][0] = rhs.a[0][0]; a[0][1] = rhs.a[0][1]; a[1][0] = rhs.a[1][0]; a[1][1] = rhs.a[1][1]; return 1; }}unit;Mar mul(Mar m1,Mar m2){ Mar tmp; tmp.clear(); tmp.a[0][0] = (m1.a[0][0]%mod*m2.a[0][0]%mod + m1.a[0][1]%mod * m2.a[1][0]%mod)%mod; tmp.a[0][1] = (m1.a[0][0]%mod*m2.a[0][1]%mod + m1.a[0][1]%mod * m2.a[1][1]%mod)%mod; tmp.a[1][0] = (m1.a[1][0]%mod*m2.a[0][0]%mod + m1.a[1][1]%mod * m2.a[1][0]%mod)%mod; tmp.a[1][1] = (m1.a[1][0]%mod*m2.a[0][1]%mod + m1.a[1][1]%mod * m2.a[1][1]%mod)%mod; return tmp;}Mar my_pow(Mar a,ll n,ll id){ Mar ans = unit; while(n){ if (n & 1) ans = mul(ans,a); n/=2; a = mul(a,a); } return ans;}ll gcd(ll a,ll b){ return !b ? a : gcd(b,a%b);}ll lcm(ll a,ll b){ return a*b/gcd(a,b);}int main(){ unit.a[0][0] = 1; unit.a[0][1] = 0; unit.a[1][0] = 0; unit.a[1][1] = 1; int T; scanf("%d",&T); while(T--){ ll k,n; Mar tmp; tmp.a[0][0] = 1; tmp.a[0][1] = 1; tmp.a[1][0] = 1; tmp.a[1][1] = 0; Mar tt = mul(tmp,tmp); scanf("%lld %lld",&k,&n); Mar ans = my_pow(tmp,n,k); printf("%lld %lld\n",k,ans.a[1][0]); } return 0;}
0 0
- UVALive 7361 Immortal Porpoises (矩阵快速幂)
- UVAlive 7361 Immortal Porpoises(矩阵快速幂)
- 【纽约】-C- Immortal Porpoises(矩阵)
- UVALive 7361(矩阵快速幂)
- uvaLive7361-Immortal Porpoises-斐波那契
- UvaLive 3704 Cellular Automaton (矩阵快速幂)
- UVALive 6184_One-Dimensional Cellular Automaton(矩阵快速幂)
- poj3070(矩阵快速幂,矩阵乘法)
- UVA10655矩阵快速幂(构造矩阵)
- 快速幂,矩阵快速幂(模板)
- 快速幂(矩阵快速幂)
- 快速矩阵快速幂
- 矩阵相关(研究总结,矩阵,矩阵快速幂)
- 矩阵快速幂(模板)
- hdu2604(矩阵快速幂)
- hdu4968(矩阵快速幂)
- hdu5015(矩阵快速幂)
- Evolution(矩阵快速幂)
- String a=new String("a") 创建了几个对象
- ubuntu查看端口命令
- dlq
- Java的TabbedPane的简单使用
- 自定义listview 带下拉刷新动画特效
- UVALive 7361 Immortal Porpoises (矩阵快速幂)
- F28027第十课---SCI操作解读
- 自定义PopWindow模仿微信拍照上传头像
- unbuntu_16.04+Mongodb 启动报错:"/sys/kernel/mm/transparent_hugepage/enabled is 'always'"
- LayoutInflater和View.inflate布局时需要知道的原理
- 开源项目android-process-button使用和源码分析
- Threejs性能监视和可视化控制
- 树形dp____Magic boy Bi Luo with his excited tree( hdu 5834 2016ccpc网络赛)
- [自製Javascript遊戲引擎][第1節][JavaScript基本介紹與工具安裝]