Uva 583 Prime Factors
/* Find the Prime Numbers using Seieve Eratonthenis. And then find the Prime factorization of your desired number. */
/*
Uva 583 Prime Factors
Verdict:: Accepted
*/
#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 1000005
#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;
bool flag[MX]; vector<int> p; int plen; vector<int>prf; int pflen;
void prime()
{
int i,j,qrt;
flag[0]=flag[1]=true;
for(i=4;i<=MX;i+=2) flag[i]=true;
qrt=(int) sqrt(double(MX));
for(i=3;i<=qrt;i+=2)
{
if(!flag[i])
{
for(j=i*i;j<=MX;j+=i+i)
{
flag[j]=true;
}
}
}
p.push_back(2);
for(i=3;i<=MX;i+=2)
{
if(!flag[i])
{
p.push_back(i);
}
}
plen=p.size();
}
void primefacto(int n)
{
int i;
for(i=0;i<plen and p[i]*p[i]<=n;i++)
{
if(!(n%p[i]))
{
while(!(n%p[i]))
{
prf.push_back(p[i]);
n/=p[i];
if(n==0 or n==1) break;
}
}
}
if(n!=1)
{
prf.push_back(n);
}
}
int main()
{
int n,i,len;
prime();
while(sf("%d",&n)==1 and n)
{
pf("%d = ",n); bool f=false;
if(n<0)
{
f=true;
n=abs(n);
}
primefacto(n);
sort(prf.begin(),prf.end()); pflen=prf.size(); len=pflen;
//loop(i,pflen) cout << prf[i] << " ";
if(f) pf("-1 x ");
//else
//{
loop(i,pflen)
{
pf("%d",prf[i]);
if(len>1)
{
pf(" x "); len--;
}
} nl;
//}
prf.clear();
}
return 0;
}
Comments
Post a Comment