SPOJ-AMR11E - Distinct Primes
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<bitset>
#include<map>
#define N 3010
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef map<int,int> mpii;
typedef map<LL,bool>mpbll;
int prm[N], plen=0 , psz = N;
bitset<N>bs;
void sieve()
{
LL i,j;
bs.set();
bs[0] = bs[1] = 0;
for(i=2; i<N; i++)
{
if(bs[i])
{
prm[plen++] = i;
for(j=i*i;j<N;j+=i)
{
bs[j] = 0;
}
}
}
//for(i=0;i<100;i++)cout<<prm[i] << " ";
}
bool distict_prime(LL n)
{
int i, cnt=0;
for(i=0;i<plen and prm[i] * prm[i] <= n;i++)
{
if(!(n%prm[i]))
{
while(!(n%prm[i]))
{
n/=prm[i];
}
cnt++;
}
}
if(n > 1)
{
cnt++;
}
if(cnt >= 3)
{
return true;
}
else
{
return false;
}
}
LL luckyList[N], len=1;
void fun()
{
luckyList[len++] = 30;
luckyList[len++] = 42;
for(LL i=43; len <= 1002; i++)
{
if(distict_prime(i))
{
luckyList[len++] = i;
}
}
//or(int i=1; i<=1000; i++)cout << luckyList[i] << " ";
}
int main()
{
sieve();
fun();
int t;
sf("%d",&t);
while(t--)
{
int n;
sf("%d",&n);
pf("%lld\n",luckyList[n]);
}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<bitset>
#include<map>
#define N 3010
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef map<int,int> mpii;
typedef map<LL,bool>mpbll;
int prm[N], plen=0 , psz = N;
bitset<N>bs;
void sieve()
{
LL i,j;
bs.set();
bs[0] = bs[1] = 0;
for(i=2; i<N; i++)
{
if(bs[i])
{
prm[plen++] = i;
for(j=i*i;j<N;j+=i)
{
bs[j] = 0;
}
}
}
//for(i=0;i<100;i++)cout<<prm[i] << " ";
}
bool distict_prime(LL n)
{
int i, cnt=0;
for(i=0;i<plen and prm[i] * prm[i] <= n;i++)
{
if(!(n%prm[i]))
{
while(!(n%prm[i]))
{
n/=prm[i];
}
cnt++;
}
}
if(n > 1)
{
cnt++;
}
if(cnt >= 3)
{
return true;
}
else
{
return false;
}
}
LL luckyList[N], len=1;
void fun()
{
luckyList[len++] = 30;
luckyList[len++] = 42;
for(LL i=43; len <= 1002; i++)
{
if(distict_prime(i))
{
luckyList[len++] = i;
}
}
//or(int i=1; i<=1000; i++)cout << luckyList[i] << " ";
}
int main()
{
sieve();
fun();
int t;
sf("%d",&t);
while(t--)
{
int n;
sf("%d",&n);
pf("%lld\n",luckyList[n]);
}
return 0;
}
Comments
Post a Comment