标签:div \n lin sqrt cst const typedef cout algo
bsgs
#include<cstdio> #include<algorithm> #include<iostream> #include<cmath> #include<map> typedef long long ll; inline int pow(int a,int b,const int mod){ int ans=1; for(;b;b>>=1,a=ll(a)*a%mod) if(b&1)ans=ll(ans)*a%mod; return ans; } inline int phi(int mod){ int ret=mod; for(int i=2;i*i<=mod;++i) if(mod%i==0){ ret=ret/i*(i-1); while(mod%i==0)mod/=i; } if(mod!=1)ret=ret/mod*(mod-1); return ret; } inline int inv(int x,const int mod){return pow(x,mod-2,mod);} std::map<int,int> map; inline int solve(int y,int z,const int mod){ map.clear(); int q = sqrtl(mod), q2=q+10; for(int i=0,lm=1;i<=q;++i) map[lm]=i,lm=ll(lm)*y%mod; const int p = inv(pow(y,q,mod),mod); int ans=0; while(!map.count(z) && --q2) z = ll(z)*p%mod,ans+=q; if(map.count(z)) return ans + map[z]; else return -1; } int main(){ int t,k; std::cin >> t >> k; while(t--){ int y,p,z; std::cin >> y >> z >> p; if(k == 1) std::cout << pow(y,z,p) << ‘\n‘; if(k == 2){ y%=p,z%=p; if(y==0&&z!=0){ std::cout << "Orz, I cannot find x!\n"; continue; } std::cout << ll(z)*inv(y,p)%p << ‘\n‘;; } if(k == 3){ const int mod = p; y %= p,z%=p; if(y == 0 && z == 0){ std::cout << "1\n"; continue; } if(y == 0 && z){ std::cout << "Orz, I cannot find x!\n"; continue; } int res = solve(y,z,mod); if(res == -1){ std::cout << "Orz, I cannot find x!\n"; continue; } std::cout << res << ‘\n‘; } } }
标签:div \n lin sqrt cst const typedef cout algo
原文地址:https://www.cnblogs.com/skip1978/p/10346370.html