#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll w;
ll p=1025436931;
/*inline int rd(){
int x=0,f=1;char c=getchar();
while(c<'0' || c>'9') f=c=='-'?-1:1,c=getchar();
while(c<='9' && c>='0') x=x*10+c-'0',c=getchar();
return x*f;
}*/
struct Num{
ll x, y;
Num(){
x = y = 0;
}
Num(ll xx, ll yy){
x = xx;
y = yy;
}
Num operator * (Num const & a)const{
Num ans;
ans.x = ((x * a.x)%p + (y * a.y)%p * w %p + p)%p;
ans.y = ((x * a.y)%p + (y * a.x)%p + p)%p;
return ans;
}
};
ll pow_num(Num a, ll b){
Num ans;
ans.x=1;
ans.y=0;
while(b){
if(b&1)
ans = ans*a;
a = a*a;
b /= 2;
}
return ans.x%p;
}
ll pow(ll a, ll b){
ll ans = 1;
while(b){
if(b&1)
ans = ans*a%p;
a = a*a%p;
b /= 2;
}
return ans;
}
ll check(ll n){
ll ans = pow(n, (p-1)/2);
if(ans == p-1)
return -1;
else
return 1;
}
ll solve(ll n){
n %= p;
if(check(n) == -1)
return -1;
if(p == 2) return n;
ll a;
srand(time(NULL));
while(1){
a = rand()*13331%p;
w = ((a*a%p - n)%p + p)%p;
if(check(w) == -1)
break;
}
Num ans;
ans.x=a;
ans.y=1;
return pow_num(ans, (p+1)/2);
}
int main() {
cout<<solve(5)<<endl;
}
二次剩余模板
最新推荐文章于 2021-08-11 10:10:01 发布