求解 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); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!