随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 

求解 a^x≡b (mod c)

 

 ( x<c )

siz = sqrt( c )

 

a^( i*siz + j) ≡b (mod c)

a^j ≡  a^( - i* siz) *b  (mod c)

枚举 j  , 将 (j, a^j %c ) 存入map ; 

枚举 i, 查询map的值 ( a^(-i *siz) *b %c

 

 

复制代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <cmath>
using namespace std;
#define int long long
#define INF 1e18
 void gcd(int a,int b,int &d,int &x,int &y){
    if(b==0){
         d=a; x=1,y=0; return ;
     }
    gcd(b,a%b,d,y,x); y-=x*(a/b);
 }

  
int BSGS(int A,int B,int C){
     
    if(C==1){
        if(!B)
            return A!=1;
        return -1;
    }
    if(B==1){
        if(A)
            return 0;
        return -1;
    }
    if(A%C==0){
        if(!B)
            return 1;
        return -1;
    }
      
    unordered_map<int,int> mp ;
    int siz=ceil(sqrt(C));
    int D=1,s=1;
     
     
    // a^j %c
    for(int i=0;i<=siz-1;i++){
        mp[s]=min(mp[s],i);
        s=(s*A)%C;
    }
     
     // a^(-i*siz)*b %c
    for(int i=0;i<=siz-1;i++){
        int x,y;
        int g; gcd(D,C,g,x,y);
        x=(x*B%C+C)%C;
        if(mp.count(x))
            return i*siz+mp[x];
        D=(D*s)%C;
    }
    return -1;
}
  
 signed main(){
     int A,B,C;
    while(cin>>C>>A>>B){
        int res=BSGS(A,B,C);
        if(res==-1)
            printf("no solution\n");
        else
            printf("%lld\n",res);
    }
 }
复制代码

 

 

 手写hashmap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define int long long
#define INF 1e18
 void gcd(int a,int b,int &d,int &x,int &y){
    if(b==0){
         d=a; x=1,y=0; return ;
     }
    gcd(b,a%b,d,y,x); y-=x*(a/b);
 }
 struct HashMap{//哈希表
    static const int Hash=999917,maxn=46340;
    int num,link[Hash],son[maxn+5],next[maxn+5],w[maxn+5];
    int top,Stack[maxn+5];
    void clear(){//清空表
        num=0;
        while(top)
            link[Stack[top--]]=0;
    }
    void add(int x,int y){//添加键值元素
        son[++num]=y;
        next[num]=link[x];
        w[num]=INF;
        link[x]=num;
    }
    bool count(int y){//判断表中是否有对应值
        int x=y%Hash;
        for(int j=link[x];j;j=next[j])
            if(y==son[j])
                return true;
        return false;
    }
    int &operator [](int y){//获取键的对应值
        int x=y%Hash;
        for(int j=link[x];j;j=next[j])
            if(y==son[j])
                return w[j];
        add(x,y);
        Stack[++top]=x;
        return w[num];
    }
}mp ;
 
  
int BSGS(int A,int B,int C){
     
    if(C==1){
        if(!B)
            return A!=1;
        return -1;
    }
    if(B==1){
        if(A)
            return 0;
        return -1;
    }
    if(A%C==0){
        if(!B)
            return 1;
        return -1;
    }
  
    mp.clear();
    int siz=ceil(sqrt(C));
    int D=1,s=1;
     
     
    // a^j %c
    for(int i=0;i<=siz-1;i++){
        mp[s]=min(mp[s],i);
        s=(s*A)%C;
    }
     
     // a^(-i*siz)*b %c
    for(int i=0;i<=siz-1;i++){
        int x,y;
        int g; gcd(D,C,g,x,y);
        x=(x*B%C+C)%C;
        if(mp.count(x))
            return i*siz+mp[x];
        D=(D*s)%C;
    }
    return -1;
}
  
 signed main(){
     int A,B,C;
    while(cin>>C>>A>>B){
        int res=BSGS(A,B,C);
        if(res==-1)
            printf("no solution\n");
        else
            printf("%lld\n",res);
    }
 }

 

posted on   towboat  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示