Miller Rabin和Pollard Rho
ll rnd(ll l,ll r){
return rd()%(r-l+1)+l;
}
ll pow(ll a,ll b,ll c){
a%=c;
ll res=1;
while(b){
if(b&1)res=(__int128)1*res*a%c;
a=(__int128)1*a*a%c;
b>>=1;
}
return res%c;
}
bool check(ll n){
if(n==1)return 0;
if(n==2||n==3)return 1;
if(n%2==0||n%3==0)return 0;
ll u=n-1,t=0;
while(u%2==0){
u/=2;
t++;
}
int T=12;
while(T--){
ll a=rnd(2,n-2);
ll tmp=pow(a,u,n);
if(tmp==1)continue;
bool flag=0;
for(int i=1;i<=t;i++){
if(tmp==n-1){
flag=1;
break;
}
tmp=(__int128)1*tmp*tmp%n;
}
if(!flag)return 0;
}
return 1;
}
ll f(ll x,ll c,ll n){
return ((__int128)1*x*x%n+c)%n;
}
map<ll,int>solve(ll n){
if(n==4)return {{2,2}};
if(check(n))return {{n,1}};
while(1){
ll x=rnd(1,n-1);
ll y=x;
ll c=rnd(1,n-1);
while(1){
x=f(x,c,n);
y=f(f(y,c,n),c,n);
ll d=__gcd(n,abs(x-y));
if(d!=0&&d!=1&&d!=n){
auto t1=solve(d);
auto t2=solve(n/d);
for(auto tp:t2){
t1[tp.first]+=tp.second;
}
return t1;
}
if(x==y)break;
}
}
}二次剩余
计算 , 则是二次剩余, 则不是。
核心:随机 使得 不是二次剩余,计算 ,其中
#include<bits/stdc++.h>
#define ll long long
using namespace std;
mt19937_64 rd(time(0));
struct node{
int x,y;//x+yw
node(){
}
node(int x_,int y_){
x=x_;
y=y_;
}
};
int a,p;
int b;
int w_2;//w^2
ll rnd(ll l,ll r){
return rd()%(r-l+1)+l;
}
int pow(int a,int b,int c){
int res=1;
while(b){
if(b&1)res=1LL*res*a%c;
a=1LL*a*a%c;
b>>=1;
}
return res;
}
node operator*(node a,node b){
return node((1LL*a.x*b.x+1LL*a.y*b.y%p*w_2%p)%p,(1LL*a.x*b.y+1LL*a.y*b.x)%p);
}
node pow(node a,int b){
node res=node(1,0);
while(b){
if(b&1)res=res*a;
a=a*a;
b>>=1;
}
return res;
}
void solve(){
cin>>a>>p;
a=(a%p+p)%p;
if(p==2){
cout<<a<<"\n";
return;
}
if(a==0)cout<<0<<"\n";
else if(pow(a,(p-1)/2,p)==1){
b=rnd(0,p-1);
while(pow((1LL*b*b%p+p-a)%p,(p-1)/2,p)!=p-1)b=rnd(0,p-1);
w_2=(1LL*b*b%p+p-a)%p;
//cout<<"w_2:"<<w_2<<endl;
node tmp=node(b,1);
tmp=pow(tmp,(p+1)/2);
int x1=tmp.x,x2=(p-tmp.x)%p;
if(x1>x2)swap(x1,x2);
cout<<x1<<" "<<x2<<"\n";
}
else{
cout<<"no\n";
}
}
int main(){
ios::sync_with_stdio(0);
int tt;
cin>>tt;
while(tt--)solve();
return 0;
}后缀自动机
void add(int c){
int cur=++tot;
len[cur]=len[las]+1;
int p=las;
while(p&&son[p][c]==0){
son[p][c]=cur;
p=F[p];
}
if(p==0){
F[cur]=1;
}
else{
int q=son[p][c];
if(len[q]==len[p]+1){
F[cur]=q;
}
else{
int nq=++tot;
len[nq]=len[p]+1;
F[nq]=F[q];
for(int i=0;i<26;i++)son[nq][i]=son[q][i];
F[q]=F[cur]=nq;
while(p&&son[p][c]==q){
son[p][c]=nq;
p=F[p];
}
}
}
las=cur;
}回文自动机
int jump(int p,int i){
while(s[i-len[p]-1]!=s[i]){
p=F[p];
}
return p;
}
void add(int i){
int p=jump(las,i);
int&q=son[p][s[i]-'a'];
if(!q){
q=++idx;
len[q]=len[p]+2;
if(len[q]==1)F[q]=2;
else{
F[q]=son[jump(F[p],i)][s[i]-'a'];
}
}
las=q;
}AC自动机
void build(){
queue<int>q;
for(int i=0;i<26;i++){
if(son[0][i]){
q.push(son[0][i]);
}
}
while(q.size()){
int u=q.front();
q.pop();
for(int i=0;i<26;i++){
if(son[u][i]){
fail[son[u][i]]=son[fail[u]][i];
q.push(son[u][i]);
}
else{
son[u][i]=son[fail[u]][i];
}
}
}
}Tarjan求强连通分量
void tarjan(int u){
num[u]=low[u]=++dfn;
stk[++top]=u;
for(int i=h[u];i;i=ne[i]){
int v=e[i];
if(!num[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!belong[v]){
low[u]=min(low[u],num[v]);
}
}
if(num[u]==low[u]){
scc++;
while(1){
int v=stk[top];
top--;
belong[v]=scc;
if(u==v)break;
}
}
}Tarjan求边双
void tarjan(int u,int fa){
num[u]=low[u]=++dfn;
stk[++top]=u;
for(int i=h[u];i;i=ne[i]){
int v=e[i];
if(v==fa)continue;
if(!num[v]){
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(!belong[v]){
low[u]=min(low[u],num[v]);
}
}
if(num[u]==low[u]){
bcc++;
while(1){
int v=stk[top];
top--;
belong[v]=bcc;
if(u==v)break;
}
}
}Tarjan求点双
void tarjan(int u,int fa){
num[u]=low[u]=++dfn;
int son=0;
for(int i=h[u];i;i=ne[i]){
int v=e[i];
if(v==fa)continue;
if(!num[v]){
son++;
stk.push({u,v});
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=num[u]){
cut[u]=1;
bcc++;
while(1){
int x=stk.top().first,y=stk.top().second;
stk.pop();
st[bcc].insert(x);
st[bcc].insert(y);
if(u==x&&v==y)break;
}
}
}
else if(num[v]<num[u]){
stk.push({u,v});
low[u]=min(low[u],num[v]);
}
}
if(fa==0&&son==1){
cut[u]=0;
}
}