BZOJ 5296: [Cqoi2018]破解D-H协议 BSGS

模板题呀. 

code: 

#include <cstdio> 
#include <cstring>   
#include <cmath>    
#include <map>        
#include <algorithm>     
#define ll long long 
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std;
int g,mod;    
map<int,int>bu;   
int qpow(int x,ll y) 
{
    int tmp=1; 
    for(;y;y>>=1,x=(ll)x*x%mod) 
        if(y&1) tmp=(ll)tmp*x%mod; 
    return tmp;   
}   
int bsgs(int y,int z) 
{         
    // y^x = z ( % mod)    
    int i,j,m=sqrt(mod)+1,tmp,f;  
    z%=mod;                   
    if(z==1) return 0;   
    bu.clear(),bu[z]=0;   
    for(tmp=z,i=1;i<m;++i) tmp=(ll)tmp*y%mod,bu[tmp]=i;                 
    for(tmp=1,f=qpow(y,m),i=1;i<=m+1;++i) 
    {       
        tmp=(ll)tmp*f%mod;    
        if(bu.count(tmp))  return i*m-bu[tmp];        
    }
}   
void solve() 
{
    int i,j,A,B;  
    scanf("%d%d",&A,&B);      
    int a=bsgs(g,A),b=bsgs(g,B);     
    printf("%d\n",qpow(g,(ll)a*b));   
}
int main() 
{ 
    // setIO("input"); 
    int i,j,T; 
    scanf("%d%d%d",&g,&mod,&T);            
    while(T--) solve();  
    return 0;
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值