[bzoj 1409] Password 矩阵快速幂+欧拉函数
考试的时候想到了矩阵快速幂+快速幂,但是忘(bu)了(hui)欧拉定理。
然后gg了35分。
题目显而易见,让求一个数的幂,幂是斐波那契数列里的一项,考虑到斐波那契也很大,所以我们就需要欧拉定理了
p是素数,所以可以搞
然后我们用矩阵快速幂求出幂,然后快速幂即可解决问题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define pos(i,a,b) for(int i=(a);i<=(b);i++)
#define Ma 50000
#define LL long long
LL prime[Ma],num_prime;
LL isnotprime[Ma]={1,1};
LL c,ou;
LL m,p,n,q;
struct matrix{
LL a[10][10];
matrix(){
memset(a,0,sizeof(a));
}
};
matrix mul(matrix aa,matrix b){
matrix c;
pos(i,0,1){
pos(j,0,1){
c.a[i][j]=0;
pos(k,0,1){
c.a[i][j]=(c.a[i][j]+aa.a[i][k]*b.a[k][j])%ou;
}
}
}
return c;
}
void getprime(){
pos(i,2,Ma-1){
if(!isnotprime[i]){
prime[num_prime++]=i;
}
for(int j=0;j<num_prime&&i*prime[j]<Ma;j++){
isnotprime[i*prime[j]]=1;
if(!(i%prime[j])){
c++;
break;
}
}
}
}
matrix init(){
matrix res;
pos(i,0,1){
pos(j,0,1)
res.a[i][j]=(i==j);
}
return res;
}
matrix ks(matrix aa,int k){
matrix res=init();
while(k){
if(k&1)
res=mul(res,aa);
k>>=1;
aa=mul(aa,aa);
}
return res;
}
LL oula(LL nn){
LL mm=(int)sqrt(nn+0.5);
LL an=nn;
for(int i=0;prime[i]<=mm;i++){
if(nn%prime[i]==0){
an=an/prime[i]*(prime[i]-1);
while(nn%prime[i]==0)
nn/=prime[i];
}
}
if(nn>1)
an=an/nn*(nn-1);
return an;
}
LL qpow(LL a,LL k,LL c){
LL an=1;
a=a%c;
while(k){
if(k&1){
an=(an*a)%c;
}
k>>=1;
a=(a*a)%c;
}
return an;
}
LL ans;
int main(){
getprime();
scanf("%lld%lld",&m,&p);
while(m--){
ans=0;
scanf("%lld%lld",&n,&q);
ou=oula(q);
matrix A;
A.a[0][0]=1;
A.a[0][1]=1;
A.a[1][0]=1;
A.a[1][1]=0;
matrix res=ks(A,n-1);
LL tmp=res.a[0][0];
//cout<<"ou="<<ou<<" tmp="<<tmp<<endl;
ans=qpow(p,tmp,q)%q;
printf("%lld\n",ans);
}
return 0;
}
[bzoj 1409] Password 矩阵快速幂+欧拉函数的更多相关文章
- HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂
装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...
- hdu 5895(矩阵快速幂+欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5895 f(n)=f(n-2)+2*f(n-1) f(n)*f(n-1)=f(n-2)*f(n-1)+2 ...
- HDU 4549 矩阵快速幂+快速幂+欧拉函数
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...
- hdu4549 矩阵快速幂 + 欧拉降幂
R - M斐波那契数列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- Super A^B mod C (快速幂+欧拉函数+欧拉定理)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 题目:Problem Description Given A,B,C, You should quick ...
- hdu 2814 快速求欧拉函数
/** 大意: 求[a,b] 之间 phi(a) + phi(a+1)...+ phi(b): 思路: 快速求欧拉函数 **/ #include <iostream> #include & ...
- BZOJ 1297 迷路(矩阵快速幂)
很容易想到记忆化搜索的算法. 令dp[n][T]为到达n点时时间为T的路径条数.则dp[n][T]=sigma(dp[i][T-G[i][n]]); 但是空间复杂度为O(n*T),时间复杂度O(n*n ...
随机推荐
- 【LeetCode】233. Number of Digit One
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers les ...
- 2.vue 安装教程
安装node.js 从node.js官网下载并安装node,安装过程很简单,一路"下一步"就可以了(傻瓜式安装). 安装完成之后,打开命令行工具,输入 node -v,如下图,如果 ...
- 4.如何实现用MTQQ通过服务器实现订阅者和发布者的通讯
1.本例子意在用moquette服务器来作为消息转发,通过订阅者订阅消息,发布者发布消息,然后发布者的消息可以通过服务器转发给订阅者 服务器例子: https://github.com/andsel/ ...
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
fastJson对于json格式字符串的解析主要用到了一下三个类: JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. JSONObject:fas ...
- 使用python操作mysql数据库
这是我之前使用mysql时用到的一些库及开发的工具,这里记录下,也方便我查阅. python版本: 2.7.13 mysql版本: 5.5.36 几个python库 1.mysql-connector ...
- JQuery实战——页面进度条效果
今早逛阮一峰大神的博客 ECMAScript 6 入门 时候看到页面顶部有个进度条显示当前浏览的进度,如图: 顶部进度条会根据当前页面高度进行宽度调整,实战一番,视觉使用animated方法实现.下面 ...
- CSS3学习系列之动画
Transitions功能使用方法 在css3中,transitions功能通过将元素的某个属性从一个属性值在指定的时间内平滑过渡到另一个属性值来实现动画功能,可通过transitions属性来使用t ...
- 基于vue2.0的网易云音乐 (实时更新)
本人在自学vue,之后想在学习过程中加以实践.由于之前有做过jquery的播放器效果,ui仿照网易云,地址 www.daiwei.org/music 于是就想做vue 的网易云播放器,网上也有类似的项 ...
- rownumber和rowid伪劣用法
select rownum ,deptno,dname loc from dept; select deptno,dname,loc from dept where rownum=1; select ...
- Chrome浏览器扩展开发系列之二:Google Chrome浏览器扩展的调试
1) 查看扩展程序的详细信息和ID 通过Chrome 浏览器的“ 工具->更多工具->扩展程序”,打开chrome://extensions页面,选中右上角的“开发者模式”,可以 ...