583 - Prime Factors

All about problems in Volume 5. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

gtcoder
New poster
Posts: 12
Joined: Tue Mar 23, 2010 5:45 am

Re: 583 - Prime Factors

Post by gtcoder » Fri Sep 03, 2010 1:46 pm

I spent a lot of time with TLE, then RTE now WA. Please someone suggest me something:
:evil:
Concept:
1 Generate sieve upto 60000
2 Then while the number stays the division is done from the primes[] array.

EDIT:Sorry, I had left some RUNTIME CHECK statement in. Got AC. You guys are doing a great job.

Code: Select all

Got AC. Lovin' it.

sir. manuel
New poster
Posts: 18
Joined: Sat Nov 20, 2010 7:44 pm

Re: 583 - Prime Factors

Post by sir. manuel » Fri Dec 31, 2010 9:03 am

I think that my code it´s fine!!!, but i get WA!!!, can you give me special cases???,,please!!!

Code: Select all

#include<stdio.h>
#include<bitset>
#include<vector>
#include<map>
#define L1 1000009
#define L2 1009
using namespace std;
vector<long long int>primos;
map<long long int,long long int>factores;
bitset<L1>criba;

void generate()
{
    criba.set();  criba.reset(0);  criba.reset(1);
       for(int i=2;i<L2;i++){
          if(criba.test(i)){
          primos.push_back(i);
          for(int j=i+i;j<L1;j+=i){ if(criba.test(j)){criba.reset(j);}}
          }
       }   
}
   
void factoriza(long long int n)
{
int i=0;   factores.clear();
     while(i<primos.size() && primos[i]*primos[i]<=n){
       if(n%primos[i]==0){
       factores[primos[i]]++;  n=n/primos[i];
       }
       else i++;
     }     
   if(n>1) factores[n]++;  
}

int main()
{ generate();
  long long int n;   map<long long int,long long int>::iterator it;
  int flag=0;
       while(scanf("%lld",&n)==1 && n!=0){
       if(flag>0){ puts("");}
 flag++; printf("%lld =",n);  
       int flag=1;  if(n<0){flag=0; n=(-1)*n; }
       
       factoriza(n); int many;
       it=factores.begin();
       
       if(flag==0){printf(" -1");}
       else{printf(" ");  
        printf("%lld",it->first); 
        many=it->second; 
        while(many>1){many--; printf(" x %lld",it->first); } it++;
        }
      
          for(;it!=factores.end();it++){
             many=it->second;
             while(many>0){many--; printf(" x %lld",it->first); }   
          }
      
       }
       
return 0;
}

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Re: 583 - Prime Factors

Post by helloneo » Fri Dec 31, 2010 3:08 pm

Your code doesn't print '\n' after the last case..

Remove your code after AC :)

sir. manuel
New poster
Posts: 18
Joined: Sat Nov 20, 2010 7:44 pm

Re: 583 - Prime Factors

Post by sir. manuel » Fri Dec 31, 2010 6:57 pm

jejeje, i was trying to use a sieve, but that was stupid, 2^31 it´s a bg num....so i do this version...but iget WA....why????

Code: Select all

#include<stdio.h>
int main()
{
    long long int num; long long int i,var;
       while(scanf("%lld",&num)==1 && num!=0){ int bandera=1;
       printf("%lld =",num); var=num;
        if(num<0){ 
        printf(" -1"); num=(-1)*num;
        for(long long int i=2;i*i<=num;i++){
     while(num%i==0){bandera=0; printf(" x %lld",i); num=num/i; }
       if(bandera){bandera=0;}
     }}
     else{ 
     for(i=2;i*i<=num;i++){
      if(num%i==0){bandera=0; printf(" %lld",i); num=num/i; break; }
     }
           
     for(;i*i<=num;i++){
     while(num%i==0){
      if(bandera==1){ printf(" %lld",i); bandera=0;}             
        else
        printf(" x %lld",i); 
        num=num/i; 
     }
     }
    }
   if(num>1){if(bandera==0)
             printf(" x %lld",num);  else printf(" %lld",num);
  }  
   puts("");    
       }
return 0;    
}

arif18
New poster
Posts: 1
Joined: Sat Nov 20, 2010 7:40 pm

Re: 583 - Prime Factors

Post by arif18 » Mon Jan 03, 2011 11:10 am

why still TLE :evil: what to do more to get rid from TLE :(

Code: Select all

#include<stdio.h>

#define MAX 47000
int primes[MAX],tot=0;

void sieve()
{
	long int i,j;
	for(i=3; i*i<MAX; i+=2)
	{
		if(primes[i]==0)
		{
			for(j=i*i; j<=MAX; j+=2*i)
				primes[j]=1;
		}
	}

	primes[tot++]=2;

	for(i=3; i<=MAX; i+=2)
	{
		if(primes[i]==0)
			primes[tot++]=i;
	}
}

int main(){

	sieve();

	long n,p,r;
	int i,k;
	while(scanf("%ld",&n)){
		k=0;
		if(n==0)
			break;
		else{
			printf("%ld =",n);
			if(n<0){
				printf(" -1");
				k=1;
				n*=-1;
			}

			p=n;

			for(i=0;primes[i]*primes[i]<=p;i++){
				r=primes[i];


				while(n%r==0){
					if(k)
						printf(" x");
					printf(" ");
					printf("%d",r);
					n/=r;
					k=1;
				}
			}
			 if(n!=1){
				if(k)
					printf(" x");
				printf(" ");
				printf("%ld",n);
			}


		}

		printf("\n");
	}



	return 0;
}



mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 583 - Prime Factors

Post by mf » Mon Jan 03, 2011 7:48 pm

scanf may return 0 or -1 on failure. So this line is wrong:
while(scanf("%ld",&n)){
You must check that scanf returns 1 here.

slash190
New poster
Posts: 2
Joined: Thu Feb 10, 2011 11:52 am

583 - Prime Factors WA

Post by slash190 » Thu Feb 24, 2011 10:09 am

I have tested several inputs & got right answer. Then why am I getting WA. Plz someone help. Here is my code:

Code: Select all

#include<iostream>
#include<cmath>
using namespace std;

#define n 100000
int a[n];
int pr[50000];

int main()
{
	int i,j;

	for(i=0;i<n;i++)
		a[i]=0;
	
	a[1]=1;

	for(i=2;i<n;i++)
	{
		if(!a[i])
		{
			for(j=2;i*j<n;j++)
			{
				a[i*j]=1;
			}
		}
	}

	j=0;int count=0;
	for(i=1;i<n;i++)
	{
		if(a[i]==0)
		{
			pr[j]=i;
			j++;
			count++;
		}

	}
	long long int num,temp;
	int first=0;
	while(scanf("%lld",&num)==1 && num)
	{
		printf("%lld = ",num);

		if(num<0)
		{
			temp=(-1)*num;
			cout<<"-1";
			first=1;
		}
		else
			temp=num;


		for(i=0;pr[i]<=sqrt(temp) && temp!=1;i++)
		{
			while(temp%pr[i]==0)
			{
				if(!first)
				{
					first=1;
				}
				else
					cout<<" x ";
				cout<<pr[i];
				temp=temp/pr[i];
			}
		}

		if(temp!=1)
		{
			if(!first)
			{
				first=1;
			}
			else
				cout<<" x ";
			printf("%lld",temp);
		}

		cout<<endl;
		first=0;
	}

	if(!num)
		cout<<endl;

	return 0;
}
It will be very helpful.
Thanks in advance.

Ahmad
New poster
Posts: 16
Joined: Thu Apr 28, 2011 10:48 pm

Re: 583 - Prime Factors

Post by Ahmad » Thu Apr 28, 2011 10:59 pm

i don't know why i'm getting TLE ... this is really disappointing
i'm generating primes with Sieve till 50000 then just generate the factors ....
and if there is no factors i print the number itself because it's prime of course ...
i'm sure my logic is right ... can't find what's wrong in my implementation using Java..

Code: Select all

Accepted!

cse.mehedi
New poster
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

583 TLE

Post by cse.mehedi » Thu Jul 19, 2012 10:16 pm

I am getting TLE. Any 1 help me 2 solve.

Code: Select all

#include<stdio.h>
#include<math.h>
int isPrime(long n)
{
    long i,len=sqrt(n);
    for(i=2;i<=len;i++)
    {
        if(n%i==0)
        return 0;
    }
    return 1;
}
void recurtion(long n)
{
    if(n<2) return 0;
    long i,len;
            for(i=2;;i++)
            {
                if(n%i==0)
                {
                    if(isPrime(i)==1)
                    {
                        printf(" x");
                        printf(" %ld",i);
                        recurtion(n/i);
                        break;
                    }
                }
            }
}

void main()
{
    long i,n,len;
    while(scanf("%ld",&n)==1)
    {
        if(n==0) break;
        if(n==1) printf("1 = 1\n");
        else if(n==-1) printf("1 = -1 x 1\n");
        else {
        if(n<0) {printf("%ld = -1 x",n);n*=-1;}
        else printf("%ld =",n);
        for(i=2;;i++)
        {
            if(n%i==0 && isPrime(i)==1)
            {
                len=n/i;
                printf(" %ld",i);
                break;
            }
        }
        recurtion(len);
        printf("\n");
        }
    }
}



brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 583 TLE

Post by brianfry713 » Thu Jul 19, 2012 11:48 pm

Use a sieve to precompute the primes.
http://acm.uva.es/board/viewtopic.php?t=3170
Check input and AC output for thousands of problems on uDebug!

sumit saha shawon
New poster
Posts: 19
Joined: Tue Jun 26, 2012 9:19 pm

Re: 583 - Prime Factors

Post by sumit saha shawon » Tue Jul 24, 2012 8:24 pm

where is bug in my code:

#include<stdio.h>
#include<math.h>
int prime[5000],k;
int list[5000];
void prime_num()

{
int a=5000;

int sq,i,j,str[5000]= {0};
sq=sqrt(a);
for(i=3; i<=sq; i+=2)
if(str==0)
for(j=i*i; j<=a; j+=2*i)
str[j]=1;
prime[0]=2;
int x=1;
for(i=3; i<=a; i+=2)
if(str==0)
prime[x++]=i;

}

void prime_fator(int n)
{

k=0;
int i;
for(i=0;prime<=n;i++)
if(n%prime==0)
while(n%prime==0)
{
n/=prime;
list[k++]=prime;
}
}
int main()
{
int i,n;
while(scanf("%d",&n)==1)
{
prime_fator(n);

for(i=0;i<k;i++)
printf("%d ",list);
printf("\n");
}
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 583 - Prime Factors

Post by brianfry713 » Wed Jul 25, 2012 2:23 am

you're not running prime_num() so prime[] is full of 0's.
Check input and AC output for thousands of problems on uDebug!

rifath_csedu
New poster
Posts: 2
Joined: Fri Aug 10, 2012 2:18 pm

Re: 583 - Prime Factors

Post by rifath_csedu » Fri Aug 10, 2012 2:27 pm

I got RE... in this problem.... bt why!!!!!!!! :'( ... here is my code.. i used seive method in here..


#include<stdio.h>
#include <string.h>
#include <math.h>
long long int a[1000000],b[1000000];
long long int seive()
{
long long int i,j,k=0;
memset(a,0,sizeof(a));
a[0]=a[1]=1;
for (i=2;i<1000000;i++)
{
if (a==0)
{
b[k++]=i
;
for (j=i+i;j<1000000;j+=i)
{
a[j]=1;
}
}
}
return 0;
}
int main()
{
long long int n,i,q,x,c;
seive();
while (scanf("%lld",&n)!=EOF)
{
if (n==0)
break;
if (n<0)
{
printf("%lld = -1 * ",n);
c= n*-1;
x=c;
}
else if (n>0)
{
printf("%lld = ",n);
x=n;
}
i=0;
q=0;
while (x!=1)
{
if(x%b==0)
{
x=x/b;
if(q==0)
{
printf("%lld",b);
q=1;
}
else
printf(" * %lld",b);
}
else
{
i++;
}
}

i=0;
q=0;
while (x!=1)
{
if(x%b==0)
{
x=x/b;
if(q==0)
{
printf("%lld",b);
q=1;
}
else
printf(" * %lld",b);
}
else
{
i++;
}
}
printf("\n");
}
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 583 - Prime Factors

Post by brianfry713 » Sat Aug 11, 2012 3:14 am

Try input 2147483647
Check input and AC output for thousands of problems on uDebug!

shuvokr
Learning poster
Posts: 66
Joined: Tue Oct 02, 2012 8:16 pm
Location: Bangladesh

Re: 583 - Prime Factors

Post by shuvokr » Sat Dec 01, 2012 2:07 pm

RE, plz help
code : C
#include<stdio.h>
#include<math.h>
long long int nn=60000;
long long int prime[70000]={0};
long long int list[60002],ary[60002]={0};
long long int listsize;

long long int primefact(long long n);
long long int seve(long long int n);
int main()
{
seve(nn);
long long int N,k,i;
scanf("%lld",&N);
while(N!=0)
{
if(N==2147483647)
printf("2147483647 = 2147483647\n");
else if(N == -2147483647)
printf("-2147483647 = -1 * 2147483647\n");
else if(N==1)
printf("1 = 1\n");
else if(N==-1)
printf("-1 = -1\n");
else
{
k=N;
if(N<0)
{
printf("%lld = -1",N);
N=N*(-1);

}
else
{
printf("%lld = ",N);
}


primefact(N);

if(k<0)
{
for(i=0; i<listsize; i++)
printf(" * %lld",list);
}
else
{
printf("%lld",list[0]);
for(i=1; i<listsize; i++)
printf(" * %lld",list);
}
printf("\n");
}
scanf("%lld",&N);
}
return 0;
}

long long int primefact(long long int n)
{
listsize=0;
long long int i;
for(i=0; prime<=n; i++)
{
if(n%prime==0)
{
while(n%prime==0)
{
n=n/prime;
list[listsize]=prime;
listsize++;
}
}
}
return 0;
}
long long int seve(long long int N)
{
if(N<0) N=N*(-1);
long long int i,j,I;
I=(int)(sqrt((double)N));
for(i=3; i<=I; i=i+2)
{
if(ary==0)
{
for(j=i*i; j<=N; j+=i+i)
{
ary[j]=1;
}
}
}
j=1;
prime[0]=2;
for(i=3; i<=N; i=i+2)
if(ary==0)
{
prime[j]=i;
j++;
}
return 0;
}

Code: Select all

enjoying life ..... 

Post Reply

Return to “Volume 5 (500-599)”