#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define MAX_VAL (pow(2.0,60))
#define LL __int64
/*LL mod_mul(LL x,LL y,LL mo) //计算x * y % mo
{
LL t;
x %= mo;
for(t = 0; y; x = (x<<1)%mo,y>>=1)
if(y & 1)
t = (t+x) %mo;
return t;
}*/
LL mod_mul(LL x,LL y,LL mo) //计算x * y % mo
{
LL t,T,a,b,c,d,e,f,g,h,v,ans;
T = (LL)(sqrt(double(mo)+0.5));
t = T*T - mo;
a = x / T;
b = x % T;
c = y / T;
d = y % T;
e = a*c / T;
f = a*c % T;
v = ((a*d+b*c)%mo + e*t) % mo;
g = v / T;
h = v % T;
ans = (((f+g)*t%mo + b*d)% mo + h*T)%mo;
while(ans < 0)
ans += mo;
return ans;
}
LL mod_exp(LL num,LL t,LL mo) //计算num^t % mo
{
LL ret = 1, temp = num % mo;
for(; t; t >>=1,temp=mod_mul(temp,temp,mo))
if(t & 1)
ret = mod_mul(ret,temp,mo);
return ret;
}
bool miller_rabbin(LL n) //miller_rabbin素性测试
{
if(n == 2) return true;
if(n < 2 || !(n&1)) return false;
int t = 0;
LL a,x,y,u = n-1;
while((u & 1) == 0)
{
t++;
u >>= 1;
}
for(int i = 0; i < 50; i++)
{
a = rand() % (n-1)+1;
x = mod_exp(a,u,n);
for(int j = 0; j < t; j++)
{
y = mod_mul(x,x,n);
if(y == 1 && x != 1 && x != n-1) return false;
x = y;
}
if(x != 1) return false;
}
return true;
}