BZOJ1409 : Password
$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的更多相关文章
- 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?
p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...
- your password has expired.to log in you must change it
今天应用挂了,log提示密码过期.客户端连接不上. 打开mysql,执行sql语句提示密码过期 执行set password=new password('123456'); 提示成功,但客户端仍然连接 ...
- MySql Access denied for user 'root'@'localhost' (using password:YES) 解决方案
关于昨天下午说的MySQL服务无法启动的问题,解决之后没有进入数据库,就直接关闭了电脑. 今早打开电脑,开始-运行 输入"mysql -uroot -pmyadmin"后出现以下错 ...
- [上架] iOS "app-specific password" 上架问题
当你的 Apple ID 改用双重认证密码时,上架 iOS App 需要去建立一个专用密码来登入 Apple ID 才能上架. 如果使用 Application Loader 上传时,得到这个讯息: ...
- [LeetCode] Strong Password Checker 密码强度检查器
A password is considered strong if below conditions are all met: It has at least 6 characters and at ...
- 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: 问题原因: ...
- phpmyadmin #1045 - Access denied for user 'root'@'localhost' (using password: NO)
phpmyadmin访问遇到1045问题 #1045 - Access denied for user 'root'@'localhost' (using password: NO) 解决办法 找到p ...
- 保留password模式文本框textbox内的数据不丢失。
在asp.net 2.0环境下,使用textbox,提交到服务器再传回,如果textbox是password模式的,那么textbox内的密码(星号),就没有了! protected override ...
- 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 ...
随机推荐
- [codeforces 260]B. Ancient Prophesy
[codeforces 260]B. Ancient Prophesy 试题描述 A recently found Ancient Prophesy is believed to contain th ...
- [POJ1936]All in All
[POJ1936]All in All 试题描述 You have devised a new encryption technique which encodes a message by inse ...
- [BZOJ2820]YY的GCD
[BZOJ2820]YY的GCD 试题描述 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少 ...
- DICOM:DICOM3.0网络通信协议(续)
转载:http://blog.csdn.net/zssureqh/article/details/44278693 题记: 近一年来一直坚持周末写博客,整理工作和闲暇之余的点点滴滴.对于新知识点.新技 ...
- 3Sum Closest & 3Sum Smaller
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- iOS图形处理和性能(转)
在之前的文章里,我们探讨了基于多种不同技术来实现自定义的UIButton,当然不同的技术所涉及到的代码复杂度和难度也不一样.但是我也有意提到了基于不同方法的实现所体现出的性能表现也不一一相同. [ ...
- 工作空间项目不存在,eclipse中项目删不掉
解决:E:\androidworkspaceall\.metadata\.plugins\org.eclipse.core.resources\.projects ->删除对应项目
- android 输入法出现挤压屏幕、android输入键盘覆盖了屏幕控件的解决办法
如果希望输入法键盘弹出时不自动向上挤压activity,在Manifest清单文件中对应的activty中设置属性android:windowSoftInputMode="adjustPan ...
- int *p()与int (*p)()的区别
int *p()是返回指针的函数 int (*p)()是指向函数的指针 返回指针的函数: int *a(int x,int y); 有若干个学生的成绩(每个学生有4门课程),要求在用户输入学生序号 ...
- Android之Fragment(二)
本文主要内容 如何管理Fragment回退栈 Fragment如何与Activity交互 Fragment与Activity交互的最佳实践 没有视图的Fragment的用处 使用Fragment创建对 ...