BZOJ5118:Fib数列2(O1快速模)
2024-10-21 06:35:26
题意:输入N,输出fib(2^N)%1125899839733759。(P=1125899839733759是素数)
思路:欧拉降幂,因为可以表示为矩阵乘法,2^N在幂的位置,矩阵乘法也可以降幂,所以有ans=a*base^num; num=2^N%(P-1)。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll Mod=;
inline ll mul(ll x,ll y,ll p){
return ((x*y-(ll)(((long double)x*y+0.5)/p)*p)%p+p)%p;
}
struct mat
{
ll M[][];
mat() { memset(M,,sizeof(M)); }
mat friend operator *(mat a,mat b)
{
mat res;
for(int k=;k<=;k++)
for(int i=;i<=;i++)
for(int j=;j<=;j++)
res.M[i][j]=(res.M[i][j]+mul(a.M[i][k],b.M[k][j],Mod))%Mod;
return res;
}
mat friend operator ^(mat a,ll x)
{
mat res; res.M[][]=res.M[][]=1LL;
while(x){
if(x&1LL) res=res*a; a=a*a; x/=;
} return res;
}
};
ll qpow(ll a,ll x,ll p){
ll res=; while(x){
if(x&1LL) res=mul(res,a,p);
a=mul(a,a,p); x/=;
} return res;
}
int main()
{
int T; ll N,num;
scanf("%d",&T);
while(T--){
scanf("%lld",&N);
num=qpow(2LL,N,Mod-);
num--; if(num<) num+=Mod-;
mat base,a;
base.M[][]=base.M[][]=base.M[][]=1LL; a.M[][]=1LL;
base=base^num;
a=base*a;
printf("%lld\n",a.M[][]);
}
return ;
}
最新文章
- IntelliJ IDEA注册码
- Atom
- wpa_supplicant测试
- hdu 3804 树链剖分
- MongoDB(二)
- Python练习28
- 抛弃JQ,回归原生js……
- js构建函数优秀案例
- Ubuntu14.04安装pycharm用于Python开发环境部署,并且支持pycharm使用中文输入
- DLC 基本定律与规则
- java 打印乘法口诀表
- Delphi操作Ini文件
- 【python】xsspider零碎知识点
- zookeeper简单实战
- 使用 Azure CLI 创建 Linux 虚拟机
- android端的ormlite框架
- 认识单点登录cas
- PHP7.1中使用openssl替换mcrypt
- emailautocomplete
- IIS发布常见错误-HTTP 错误 404.0 - Not-Found
热门文章
- Groovy系列-groovy比起Java--有哪些地方写起来更舒服?
- JQuery 操作 checkbox 二次赋值无效 attr ---->; prop
- This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in问题
- (from) Javascript 生成指定范围数值随机数
- Linux:Ubuntu16.04下创建Wifi热点
- matlab fread
- hadoop06---多线程
- @MarkFan 口语练习录音 20140401
- classpath是什么
- B-树 C++模板类封装(有图有真相)