$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. 将Web应用性能提高十倍的10条建议

    导读 提高 web 应用的性能从来没有比现在更重要过.网络经济的比重一直在增长:全球经济超过 5% 的价值是在因特网上产生的(数据参见下面的资料).这个时刻在线的超连接世界意味着用户对其的期望值也处于 ...

  2. 修改 ~/.bashrc显示 git 当前分支

    vim ~/.bashrc # git branch show configuration PS1="\\w:\$(git branch 2>/dev/null | grep '^*' ...

  3. Online Object Tracking: A Benchmark 论文笔记(转)

    转自:http://blog.csdn.net/lanbing510/article/details/40411877 有博主翻译了这篇论文:http://blog.csdn.net/roamer_n ...

  4. 友盟消息推送和更新XML配置

    <receiver android:name="com.umeng.message.NotificationProxyBroadcastReceiver" android:e ...

  5. 浅析django的abstract,proxy, managed

    django.db.models.Model 的 Meta参数 参数 类型 说明 继承 abstract boolean 是否建表 不继承,子类自动充值为默认值(False) managed bool ...

  6. python如何获取某模块的版本信息

    1)module.__version__ 2)用dir(module)查看有没有版本信息 3)help(module)

  7. 利用jQuery.validate异步验证用户名是否存在

    转:http://www.cnblogs.com/linzheng/archive/2010/10/14/1851781.html HTML头部引用: <script type="te ...

  8. Delphi经验总结(3)

    ------------------------------------------------------- ◇删掉程序自己的exe文件 procedure TForm1.FormClose(Sen ...

  9. Java for LeetCode 144 Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  10. HDU 4059 容斥原理+快速幂+逆元

    E - The Boss on Mars Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...