BSGS
北上广深/拔山盖世算法。
yaT+b = z mod p
p为质数,Hash表存b,枚举a,复杂度p0.5
记得特判y = 0的情况。
1 inline void solve3() { 2 Hash::clear(); 3 //mp.clear(); 4 scanf("%lld%lld%lld", &Y, &Z, &MO); 5 Z %= MO; 6 Y %= MO; 7 /// BSGS 8 if(Y == 0) { 9 if(!Z) printf("1\n"); 10 else printf("Orz, I cannot find x!\n"); 11 return; 12 } 13 int T = sqrt(MO); 14 LL x = 1, y = 1; 15 for(int i = 0; i < T; i++) { 16 Hash::insert(x, i); 17 //mp[x] = i; 18 x = x * Y % MO; 19 } 20 x = qpow(x, MO - 2); /// x = 1 / (Y ^ T) 21 for(int i = 0; i <= MO / T; i++) { 22 int t = Hash::find(Z * y % MO); 23 if(t != -1) { 24 printf("%lld\n", 1ll * i * T + t); 25 return; 26 } 27 y = y * x % MO; 28 } 29 printf("Orz, I cannot find x!\n"); 30 return; 31 }