[SDOI2011]计算器
Posted skip1978
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[SDOI2011]计算器相关的知识,希望对你有一定的参考价值。
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) << ‘ ‘; if(k == 2){ y%=p,z%=p; if(y==0&&z!=0){ std::cout << "Orz, I cannot find x! "; continue; } std::cout << ll(z)*inv(y,p)%p << ‘ ‘;; } if(k == 3){ const int mod = p; y %= p,z%=p; if(y == 0 && z == 0){ std::cout << "1 "; continue; } if(y == 0 && z){ std::cout << "Orz, I cannot find x! "; continue; } int res = solve(y,z,mod); if(res == -1){ std::cout << "Orz, I cannot find x! "; continue; } std::cout << res << ‘ ‘; } } }
以上是关于[SDOI2011]计算器的主要内容,如果未能解决你的问题,请参考以下文章