题解:P2485 [SDOI2011] 计算器
fish_love_cat · · 题解
原题三合一。
- 当
K=1 ,左转 P1226。 - 当
K=2 ,x=\frac{z}{y}\pmod p ,右转 P2613。 - 当
K=3 ,右转 P3846。
嗯做完了。
但是这题一般是被当做 BSGS 例题来讲的根本原因我认为是因为 P3846 数据太弱了,所以讲几个容易写挂的地方吧。
首先输入顺序不要挂了。有个用户名里包含 Sunset 的同学因此虚空调试 BSGS 长达 1h,我不知道是不是在写这题。
取根号时要向上取整!!!不然会 TLE on #2,警钟长鸣。
填数只填一次,不要覆盖掉已经填好的数。
大常数选手用 unordered_map 跑的比 map 快。
打标记一定不要打个 0 上去,虽然答案是这样的但是这样打标记读不出来的。最好是加个一进去,返回时减一。
本题虽然
嗯差不多这些吧。
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define flc_INF LLONG_MAX
int qpow(int a,int b,int p=flc_INF){
int ans=1;
if(b==0)return 1;
while(b){
if(b&1)ans*=a,ans%=p;
a*=a,b>>=1,a%=p;
}
return ans;
}
int ni(int a,int p){return qpow(a,p-2,p);}
int BSGS(int x,int n,int p){
if(n==1)return 0;
if(x%p==n%p)return 1;
if(x%p==0)return -1;
n%=p,x%=p;
int inx=ni(x,p);
int B=sqrt(p)+1;
int Bb=qpow(x,B,p);
unordered_map<int,int>mp;
for(int i=0,flc=n;i<B;i++,flc=flc*inx%p)
if(!mp[flc])mp[flc]=i+1;
for(int i=0,flc=1;i<=B;i++,flc=flc*Bb%p)
if(mp[flc])return (i*B+mp[flc]-1)%p;
return -1;
}
signed main(){
int t,k;
cin>>t>>k;
while(t--){
int y,z,p;
cin>>y>>z>>p;
if(k==1)cout<<qpow(y,z,p);
else if(k==2){
if(y%p==0)cout<<"Orz, I cannot find x!";
else cout<<z*ni(y,p)%p;
}else{
int ans=BSGS(y,z,p);
if(ans==-1)cout<<"Orz, I cannot find x!";
else cout<<ans;
}
puts("");
}
return 0;
}
Upd:把自己 hack 了。