小水题???但是时间限制异常鬼畜,跑了2min
(P | (2^m)*(3^n)-1)的意思就是(2^m 3^n = 1 ( ext{mod }P))
设f[i]表示3^k=i的最小的k
然后枚举2的次幂即可
#include<bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il int gi(){
int x=0,f=1;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-')f=-1;
ch=getchar();
}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}
int inv,P,ans,ans_m,f[10000010];
il int pow(int x,int y){
int ret=1;
while(y){
if(y&1)ret=1ll*ret*x%P;
x=1ll*x*x%P;y>>=1;
}
return ret;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("186b.in","r",stdin);
freopen("186b.out","w",stdout);
#endif
while(scanf("%d",&P)==1){
memset(f,63,sizeof f);
for(ll i=1,j=3;i<P;++i,j=j*3%P)f[j]=std::min(f[j],(int)i);
ans=1e9;
inv=pow(2,P-2);
for(ll i=1,j=inv;i<P;++i,j=j*inv%P)if(i+f[j]<ans)ans=i+f[j],ans_m=i;
printf("%d %d
",ans_m,ans-ans_m);
}
return 0;
}