Hacker Rank Euler's Criterion
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define sf scanf
#define pf printf
#define limit 32000
using namespace std;
typedef long long LL;
LL bigmod(LL a, LL b , LL c)
{
if(!b) return 1;
if(!(b&1))
{
LL r = bigmod(a, b/2, c);
return ((r%c) * (r%c))%c;
}
else
{
return ((a%c) * (bigmod(a, b-1, c)%c))%c;
}
}
bool isQuadratic_residue(LL n, LL p)
{
if(bigmod(n, (p-1)/2 , p) == 1) return true;
else return false;
}
int main()
{
int t;
sf("%d", &t);
while(t--)
{
LL a,m;
sf("%lld %lld", &a, &m);
if(isQuadratic_residue(a,m) or a==0) pf("YES\n");
else pf("NO\n");
}
return 0;
}
#include<cstdio>
#include<cmath>
#include<cstring>
#define sf scanf
#define pf printf
#define limit 32000
using namespace std;
typedef long long LL;
LL bigmod(LL a, LL b , LL c)
{
if(!b) return 1;
if(!(b&1))
{
LL r = bigmod(a, b/2, c);
return ((r%c) * (r%c))%c;
}
else
{
return ((a%c) * (bigmod(a, b-1, c)%c))%c;
}
}
bool isQuadratic_residue(LL n, LL p)
{
if(bigmod(n, (p-1)/2 , p) == 1) return true;
else return false;
}
int main()
{
int t;
sf("%d", &t);
while(t--)
{
LL a,m;
sf("%lld %lld", &a, &m);
if(isQuadratic_residue(a,m) or a==0) pf("YES\n");
else pf("NO\n");
}
return 0;
}
Comments
Post a Comment