SPOJ Number theory Problem PSYCHON Psycho Solution
Solve in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE
///Islamic University,Bangladesh
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
#define M 10001000
#define ll long long
bool marked[M];
vector<ll>vec;
void sieve()
{
for(ll i=3; i*i<=10000000; i+=2)
{
if(marked[i]==false)
{
for(ll j=i*i; j<=10000000; j+=2*i)
{
marked[j]=true;
}
}
}
vec.push_back(2);
for(ll i=3; i<=10000000; i=i+2)
{
if(marked[i]==false)
{
vec.push_back(i);
}
}
}
int main()
{
long long int t;
sieve();
cin>>t;
while(t--)
{
long long int n,even=0,c=0,odd=0;
cin>>n;
for(int i=0; i<=sqrt(n); i++)
{
if(n%vec[i]==0)
{
c=0;
while(n%vec[i]==0)
{
n=n/vec[i];
c++;
}
if(c%2==0)
even++;
else
odd++;
}
}
if(n>1)
odd=odd+1;
if(even>odd)
printf("Psycho Number\n");
else
printf("Ordinary Number\n");
}
}
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE
///Islamic University,Bangladesh
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
#define M 10001000
#define ll long long
bool marked[M];
vector<ll>vec;
void sieve()
{
for(ll i=3; i*i<=10000000; i+=2)
{
if(marked[i]==false)
{
for(ll j=i*i; j<=10000000; j+=2*i)
{
marked[j]=true;
}
}
}
vec.push_back(2);
for(ll i=3; i<=10000000; i=i+2)
{
if(marked[i]==false)
{
vec.push_back(i);
}
}
}
int main()
{
long long int t;
sieve();
cin>>t;
while(t--)
{
long long int n,even=0,c=0,odd=0;
cin>>n;
for(int i=0; i<=sqrt(n); i++)
{
if(n%vec[i]==0)
{
c=0;
while(n%vec[i]==0)
{
n=n/vec[i];
c++;
}
if(c%2==0)
even++;
else
odd++;
}
}
if(n>1)
odd=odd+1;
if(even>odd)
printf("Psycho Number\n");
else
printf("Ordinary Number\n");
}
}
No comments