SPOJ Prime Time
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <sstream>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#define sf scanf
#define pf printf
#define sfint scanf ("%d %d",&a,&b)
#define sfl scanf ("%ld %ld",&a,&b)
#define sfll scanf ("%lld %lld",&a,&b)
#define sfd scanf ("%lf %lf",&a,&b)
#define sff scanf ("%f %f",&a,&b)
#define loop(i,n) for(i=0;i<n;i++)
#define LL long long
#define L long
#define nl puts("")
#define MX 10100
#define N 100
#define MOD 10000000007
#define pb push_back
#define pi acos(-1.0)
#define sz size()
#define gc getchar ()
#define ps push
#define clr clear
#define bn begin()
#define ed end()
using namespace std;
int prime[(MX >> 6) + 1];
typedef vector<int>vi;
typedef vector<LL>vl;
vi prm;
#define setbit(n) (prime[n>>6] |= (1 << ((n >> 1) & 31)))
#define checkbit(n) (prime[n>>6] & (1 << ((n >> 1) & 31)))
void sieve()
{
int i,j;
for(i=3; i*i<=MX; i+=2)
{
if(!checkbit(i))
{
for(j=i*i; j<=MX; j+=(2*i))
{
setbit(j);
}
}
}
prm.push_back(2);
for(i=3; i<=MX; i+=2)
{
if(!checkbit(i))
{
prm.push_back(i);
}
}
//for(i=0; i<100; i++) cout << prm[i] << " ";
}
int ret_factors(int n, int p)
{
int cnt=0;
while(n)
{
cnt+=(n / p);
n/=p;
}
return cnt;
}
int main()
{
sieve();
int num;
while(~sf("%d", &num))
{
bool f=false;
for(int i=0; prm[i] <= num; i++)
{
if(!f)
{
pf("%d^%d", prm[i], ret_factors(num, prm[i]));
f=true;
}
else
{
pf(" * %d^%d", prm[i], ret_factors(num, prm[i]));
}
}
pf("\n");
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <sstream>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#define sf scanf
#define pf printf
#define sfint scanf ("%d %d",&a,&b)
#define sfl scanf ("%ld %ld",&a,&b)
#define sfll scanf ("%lld %lld",&a,&b)
#define sfd scanf ("%lf %lf",&a,&b)
#define sff scanf ("%f %f",&a,&b)
#define loop(i,n) for(i=0;i<n;i++)
#define LL long long
#define L long
#define nl puts("")
#define MX 10100
#define N 100
#define MOD 10000000007
#define pb push_back
#define pi acos(-1.0)
#define sz size()
#define gc getchar ()
#define ps push
#define clr clear
#define bn begin()
#define ed end()
using namespace std;
int prime[(MX >> 6) + 1];
typedef vector<int>vi;
typedef vector<LL>vl;
vi prm;
#define setbit(n) (prime[n>>6] |= (1 << ((n >> 1) & 31)))
#define checkbit(n) (prime[n>>6] & (1 << ((n >> 1) & 31)))
void sieve()
{
int i,j;
for(i=3; i*i<=MX; i+=2)
{
if(!checkbit(i))
{
for(j=i*i; j<=MX; j+=(2*i))
{
setbit(j);
}
}
}
prm.push_back(2);
for(i=3; i<=MX; i+=2)
{
if(!checkbit(i))
{
prm.push_back(i);
}
}
//for(i=0; i<100; i++) cout << prm[i] << " ";
}
int ret_factors(int n, int p)
{
int cnt=0;
while(n)
{
cnt+=(n / p);
n/=p;
}
return cnt;
}
int main()
{
sieve();
int num;
while(~sf("%d", &num))
{
bool f=false;
for(int i=0; prm[i] <= num; i++)
{
if(!f)
{
pf("%d^%d", prm[i], ret_factors(num, prm[i]));
f=true;
}
else
{
pf(" * %d^%d", prm[i], ret_factors(num, prm[i]));
}
}
pf("\n");
}
return 0;
}
Comments
Post a Comment