二次剩余
MoCaRabbit · · 个人记录
P5491
随机找到一个
定义
可以证明
可以证明
于是可以推出
于是
可以证明,这一定是整数。
写个复数类就行了。
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,p;
int dt;
struct fcp{
int x,y;
inline fcp(int _x=0,int _y=0){x=_x,y=_y;}
};
inline fcp operator * (fcp x,fcp y){return fcp((x.x*y.x%p+dt*x.y%p*y.y%p)%p,(x.x*y.y%p+x.y*y.x%p)%p);}
inline fcp operator + (fcp x,fcp y){return fcp((x.x+y.x)%p,(x.y+y.y)%p);}
inline int power(int x,int y){
if(y==0) return 1;
int tot=power(x,y>>1);
if(y&1) return tot*tot%p*x%p;
else return tot*tot%p;
}
inline fcp fpower(fcp x,int y){
if(y==0) return fcp(1,0);
fcp tot=fpower(x,y>>1);
if(y&1) return tot*tot*x;
else return tot*tot;
}
inline void solve(){
cin>>n>>p;
n%=p;
if(power(n,(p-1)>>1)==p-1){
cout<<"Hola!\n";
return;
}
int a=(int)(rand())*rand()%p;
while(power((a*a%p-n+p)%p,(p-1)>>1)==1) a=(int)(rand())*rand()%p;
dt=(a*a%p-n+p)%p;
int x=fpower(fcp(a,1),(p+1)>>1).x;
int y=(p-x)%p;
if(x>y) swap(x,y);
if(x!=y) cout<<x<<" "<<y<<"\n";
else cout<<x<<"\n";
}
signed main() {
int t;
cin>>t;
while(t--) solve();
return 0;
}