$f[n]\bmod q=p^{Fib[n]}\bmod q=p^{Fib[n]\bmod\varphi(q)}\bmod q$

首先线性筛预处理出所有素数,然后对于每次询问,求出$\varphi(q)$,再用矩阵快速幂求出Fib[n],最后用快速幂求答案即可。

#include<cstdio>
typedef long long ll;
const int N=46341;
int T,i,j,p[N],tot,vis[N],n,q;ll a,P;
struct mat{
ll a[2][2];
inline mat(){a[0][0]=a[0][1]=a[1][0]=a[1][1]=0;}
inline mat operator*(mat b){
mat c;
for(int i=0,j,k;i<2;i++)for(j=0;j<2;j++)for(k=0;k<2;k++)(c.a[i][j]+=a[i][k]*b.a[k][j]%P)%=P;
return c;
}
}A,B,C;
inline int phi(int n){
int t=1,i;
for(i=1;p[i]*p[i]<=n&&i<=tot;i++)if(n%p[i]==0){n/=p[i],t*=p[i]-1;while(n%p[i]==0)n/=p[i],t*=p[i];}
if(n>1)t*=n-1;
return t;
}
inline int fib(int x){
P=phi(q);
for(A=B=C=mat(),A.a[0][1]=A.a[1][0]=A.a[1][1]=B.a[1][0]=C.a[0][0]=C.a[1][1]=1;x;x>>=1,A=A*A)if(x&1)C=C*A;
C=C*B;
return C.a[0][0];
}
inline int pow(ll a,int b){ll t=1;for(;b;b>>=1,a=a*a%q)if(b&1)t=t*a%q;return t;}
int main(){
for(i=2;i<N;i++){
if(!vis[i])p[++tot]=i;
for(j=1;j<=tot;j++){
if(i*p[j]>=N)break;
vis[i*p[j]]=1;
if(i%p[j]==0)break;
}
}
scanf("%d%lld",&T,&a);
while(T--){
scanf("%d%d",&n,&q);
if(q==1){puts("0");continue;}
printf("%d\n",pow(a,fib(n)));
}
return 0;
}

  

BZOJ1409 : Password的更多相关文章

  1. 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?

    p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...

  2. your password has expired.to log in you must change it

    今天应用挂了,log提示密码过期.客户端连接不上. 打开mysql,执行sql语句提示密码过期 执行set password=new password('123456'); 提示成功,但客户端仍然连接 ...

  3. MySql Access denied for user 'root'@'localhost' (using password:YES) 解决方案

    关于昨天下午说的MySQL服务无法启动的问题,解决之后没有进入数据库,就直接关闭了电脑. 今早打开电脑,开始-运行 输入"mysql -uroot -pmyadmin"后出现以下错 ...

  4. [上架] iOS "app-specific password" 上架问题

    当你的 Apple ID 改用双重认证密码时,上架 iOS App 需要去建立一个专用密码来登入 Apple ID 才能上架. 如果使用 Application Loader 上传时,得到这个讯息: ...

  5. [LeetCode] Strong Password Checker 密码强度检查器

    A password is considered strong if below conditions are all met: It has at least 6 characters and at ...

  6. mysql 错误 ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number 解决办法

    MySQL创建用户(包括密码)时,会提示ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number: 问题原因: ...

  7. phpmyadmin #1045 - Access denied for user 'root'@'localhost' (using password: NO)

    phpmyadmin访问遇到1045问题 #1045 - Access denied for user 'root'@'localhost' (using password: NO) 解决办法 找到p ...

  8. 保留password模式文本框textbox内的数据不丢失。

    在asp.net 2.0环境下,使用textbox,提交到服务器再传回,如果textbox是password模式的,那么textbox内的密码(星号),就没有了! protected override ...

  9. Windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes

    Windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes http://blog.csdn.n ...

随机推荐

  1. Unity 3D 关于给APK包加广告的流程

    http://hunterwang.diandian.com/post/2012-11-23/40042217286 最近一直纠结着用Unity给安卓的应用添加广告,用Unity3d做安卓我还是第一次 ...

  2. Mysql函数集合

    Mysql提供了很多函数 提供的常用函数集合 一.数学函数 ABS(x) 返回x的绝对值 BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制) CEILING(x) 返回大于x的最小整 ...

  3. Javascript样例之文档章节滚动全版(DOM)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBMAAAC5CAIAAAD1bOCRAAAMEUlEQVR4nO3d4XEbyREGUGRCZQDGcG

  4. javascript quine

    javascript有一些奇怪的性质,恩,比如说,非常容易写一个quine,即自己输出自己代码的东西. function a(){console.log(a.toString()+";a() ...

  5. 2.saltstack笔记之目标,模块,返回写入数据库

    作者:刘耀 QQ:22102107 一.目标(targeting Minions) 1.匹配Minions Id 匹配所有 (*) [root@node1 salt]# salt '*' test.p ...

  6. 安装tar.bz2文件

    (1) 解包 – tar jxvf softname-10.0.1.tar.gz -C /usr/src/(-C指的是把文件解压到后面的路径下,此处可以不选) – cd /usr/src/softna ...

  7. 通过百度echarts实现数据图表展示功能

    现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...

  8. [Android Pro] 关于Android的HTTP客户端的小秘密

    原文:http://android-developers.blogspot.com/2011/09/androids-http-clients.html 译文:http://yunfeng.sinaa ...

  9. css3学习总结3--CSS3图像边框

    border-image属性 .className{ border-image:url(/course/54d1cae088dba03f2cd1fec1/img/border.png) 20 20 2 ...

  10. yum_rpm(利用dvd建立本地yum库)

    #wget "http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os" 建立起了index.html ...