$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. FOJ 1075

    #include<stdio.h> #include<iostream> using namespace std; ]={,}; ]={}; void func() { ,fl ...

  2. [转]Ubuntu 系统 Update-rc.d 命令

    本文转自:http://wangyan.org/blog/ubuntu-update-rc-d.html 以防止原博客挂掉,特此做了备份. Ubuntu或者Debian系统中update-rc.d命令 ...

  3. Java--多线程读取网络图片并保存在本地

    本例用到了多线程.时间函数.网络流.文件读写.正则表达式(在读取html内容response时,最好不要用正则表达式来抓捕html文本内容里的特征,因为服务器返回的多个页面的文本内容不一定使用相同的模 ...

  4. MVC3和MVC4相关问题

    从TFS获取的MVC项目生成一直提示这个错误,我的VS中MVC3和MVC4都有,TFS项目中的MVC版本应该是MVC3的,现在我要用这个项目,但是一直是这个错误,请高手指点,积分不多了,见谅 程序集“ ...

  5. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  6. iOS xib中TableView创建的2种模式

    在xcode 5.0中 用xib编辑tableview有2种模式,见下图 其中,dynamic prototype 动态原型 表示tableview会询问它指定的 data source获取数据,如果 ...

  7. mysql5.6 timestamp

    1.timestamp 默认值 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 在创建新记录和修改现有记录的时候都对这个数据列刷新 CURRENT_TIME ...

  8. sqlserver的执行计划

    一:执行计划生成过程 说到执行计划,首先要知道的是执行计划大概生成的过程,这样就可以做到就心中有数了,下面我画下简图: 1. 分析过程 这三个比较容易理解,首先我们要保证sql的语法不能错误,sele ...

  9. 【python】lamda表达式,map

    一个很好的博客:http://blog.csdn.net/mathboylinlin/article/details/9413551 博客不让转载,我只摘抄了里面几个例子,更多内容到博客里去看 lam ...

  10. 51nod 1264 线段相交

    题目:传送门. 题意:给两条线段,有一个公共点或有部分重合认为相交,问他们是否相交. 题解:这属于非规范相交的情况,模板题. #include <iostream> #include &l ...