BZOJ5296 [CQOI2018] 破解D-H协议 【数学】【BSGS】

题目分析:

  裸题。

代码:

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long ll;
 5 
 6 const int BASE = 10045601;
 7 
 8 #define mp make_pair
 9 
10 ll g,p;
11 ll srt = 50000;
12 vector<pair<int,ll> > hash[10200000];
13 
14 ll fast_pow(ll now,ll pw){
15     if(pw == 1) return now;
16     ll z = fast_pow(now,pw/2);
17     z *= z ; z %= p;
18     if(pw & 1)z *= now,z %= p;
19     return z;
20 }
21 
22 void CreatHash(){
23     ll xm = fast_pow(g,srt);
24     ll hh = xm;
25     for(ll i=1;(i-1)*srt<=INT_MAX;i++,hh = (hh*xm)%p){
26     hash[hh % BASE].push_back(mp(i,hh));
27     }
28 }
29 
30 void read(){
31     scanf("%lld%lld",&g,&p);
32     CreatHash();
33 }
34 
35 ll solve(ll now){
36     ll hh = g;
37     for(int i=1;i<=50000;i++,hh=(hh*g)%p){
38     ll nowp = (hh*now)%p;
39     for(int j=0;j<hash[nowp%BASE].size();j++){
40         ll out = hash[nowp%BASE][j].second;
41         if(out == nowp){
42         out = hash[nowp%BASE][j].first;
43         return out*srt-i;
44         }
45     }
46     }
47 }
48 
49 void work(){
50     int n;scanf("%d",&n);
51     for(int i=1;i<=n;i++){
52     ll a,b; scanf("%lld%lld",&a,&b);
53     ll hh = solve(a);
54     ll key = fast_pow(b,hh);
55     printf("%lld\n",key);
56     }
57 }
58 
59 int main(){
60     read();
61     work();
62     return 0;
63 }

 

转载于:https://www.cnblogs.com/Menhera/p/8934061.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值