LightOj 1220 - Mysterious Bacteria
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define sf scanf
#define pf printf
#define limit 1000005
#define psb push_back
using namespace std;
typedef long long LL;
int prime[(limit >> 6) + 1] , plen;
#define setbit(n) (prime[n>>6] |= (1 << ((n>>1) & 31)))
#define checkbit(n) (prime[n>>6] & (1 << ((n>>1) & 31)))
vector<LL>prm;
void sieve()
{
LL i,j;
for(i=3; i*i<=limit; i+=2)
{
if(!checkbit(i))
{
for(j=i*i;j<=limit;j+=i+i)
{
setbit(j);
}
}
}
prm.psb(2);
for(i=3; i<=limit; i+=2)
{
if(!checkbit(i))
{
prm.psb(i);
}
}
plen = prm.size();
}
int factorize(LL n)
{
int count_factor=0, mini=100000;
for(int i=0;i<plen and prm[i]*prm[i]<=n; i++)
{
if(!(n%prm[i]))
{
count_factor=0;
while(!(n%prm[i]))
{
count_factor++;
n/=prm[i];
if(n==0 or n==1)break;
}
mini = min(mini, count_factor);
}
}
if(n > 1)
{
mini=1;
}
return mini;
}
int main()
{
sieve();
int t,tc=0;
sf("%d",&t);
LL num;
while(t--)
{
sf("%lld",&num);
bool fl=false;
if(num < 0)
{
fl=true;
num*=-1;
}
int res = factorize(num);
if(fl)
{
while(!(res&1))
{
res/=2;
}
}
pf("Case %d: %d\n",++tc,res);
}
return 0;
}
#include<cstring>
#include<cmath>
#include<vector>
#define sf scanf
#define pf printf
#define limit 1000005
#define psb push_back
using namespace std;
typedef long long LL;
int prime[(limit >> 6) + 1] , plen;
#define setbit(n) (prime[n>>6] |= (1 << ((n>>1) & 31)))
#define checkbit(n) (prime[n>>6] & (1 << ((n>>1) & 31)))
vector<LL>prm;
void sieve()
{
LL i,j;
for(i=3; i*i<=limit; i+=2)
{
if(!checkbit(i))
{
for(j=i*i;j<=limit;j+=i+i)
{
setbit(j);
}
}
}
prm.psb(2);
for(i=3; i<=limit; i+=2)
{
if(!checkbit(i))
{
prm.psb(i);
}
}
plen = prm.size();
}
int factorize(LL n)
{
int count_factor=0, mini=100000;
for(int i=0;i<plen and prm[i]*prm[i]<=n; i++)
{
if(!(n%prm[i]))
{
count_factor=0;
while(!(n%prm[i]))
{
count_factor++;
n/=prm[i];
if(n==0 or n==1)break;
}
mini = min(mini, count_factor);
}
}
if(n > 1)
{
mini=1;
}
return mini;
}
int main()
{
sieve();
int t,tc=0;
sf("%d",&t);
LL num;
while(t--)
{
sf("%lld",&num);
bool fl=false;
if(num < 0)
{
fl=true;
num*=-1;
}
int res = factorize(num);
if(fl)
{
while(!(res&1))
{
res/=2;
}
}
pf("Case %d: %d\n",++tc,res);
}
return 0;
}
Comments
Post a Comment