160 - Factors and Factorials

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

Moderator: Board moderators

rana_cse_ruet
New poster
Posts: 7
Joined: Mon Mar 05, 2007 9:59 am

160 PE; Please help

Post by rana_cse_ruet » Sun Mar 18, 2007 3:32 am

here is the output

[
1
1!=
53
53!= 49 23 12 8 4 4 3 2 2 1 1 1 1 1 1
1
2
2!= 1
100
100!= 97 48 24 16 9 7 5 5 4 3 3 2 2 2 2
1 1 1 1 1 1 1 1 1 1
]

should the above output show presentation error?
please help......
[/code]

abdullah<cse du>
New poster
Posts: 39
Joined: Mon Dec 04, 2006 2:18 pm
Location: Bangladesh(CSE DU)
Contact:

Post by abdullah<cse du> » Mon Mar 19, 2007 4:34 am

Rana,

I think you don't read the output section clearly.
Output will consist of a series of blocks of lines, one block for each line of the input. Each block will start with the number N, right justified in a field of width 3, and the chracters `!', space, and `='. This will be followed by a list of the number of times each prime number occurs in N!.
Your posted o/p does not match with the problem's o/p section.

Thanks.
ABDULLAH

mohidul18
New poster
Posts: 2
Joined: Sat Mar 10, 2007 4:21 pm
Location: Bogra,Bangladesh
Contact:

can any one known me,why 160 problem is CE

Post by mohidul18 » Tue Mar 27, 2007 5:26 am

#include<stdio.h>
int pr[26]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79, 83,89,97,101};
void factor(int c,int a[])
{ if(c==1)return ;
factor(c-1,a);
int i;
for( i=0;pr<=c;i++){
while(!(c%pr)){a++;c=c/pr;}}
}

void main()
{
int n,i ;
while(scanf("%d ",&n)&&n)
{printf("%3d! = ",n);
int p[26]={0};
factor(n,p);
i=0;
printf("%3d",p[i++]);
while(p){
if(i%15)printf("%3d",p[i++]);
else printf("\n%10d",p[i++]);
} printf("\n");

}}
i dun like them who likes me

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Mon Apr 16, 2007 10:20 pm

Search your problem first. Don't open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage

henry1007
New poster
Posts: 4
Joined: Thu Apr 19, 2007 7:28 pm

Post by henry1007 » Thu Apr 19, 2007 7:32 pm

I also got a PE...I just can't figure out y.
I've tried inputting all the way from 2 to 100 and check the output - unless I missed sth, there should be no problem.
Here's my code, it would be great if someone can help me out :(

Code: Select all

Code Deleted
P.S. I will delete the code once it's got modified and accepted.
Last edited by henry1007 on Sun Apr 22, 2007 10:46 pm, edited 1 time in total.

abdullah<cse du>
New poster
Posts: 39
Joined: Mon Dec 04, 2006 2:18 pm
Location: Bangladesh(CSE DU)
Contact:

Post by abdullah<cse du> » Fri Apr 20, 2007 6:30 am

henry1007,

Your output section is not correct. Another wrong is that- you take all the input and after getting the zero input you give output. It is not necessary. Take a input and give output. I give some input for test.

Code: Select all

80
70
30
50
100
0

Code: Select all

 80! = 78 36 19 12  7  6  4  4  3  2  2  2  1  1  1
        1  1  1  1  1  1  1
 70! = 67 32 16 11  6  5  4  3  3  2  2  1  1  1  1
        1  1  1  1
 30! = 26 14  7  4  2  2  1  1  1  1
 50! = 47 22 12  8  4  3  2  2  2  1  1  1  1  1  1
100! = 97 48 24 16  9  7  5  5  4  3  3  2  2  2  2
        1  1  1  1  1  1  1  1  1  1

henry1007
New poster
Posts: 4
Joined: Thu Apr 19, 2007 7:28 pm

Post by henry1007 » Fri Apr 20, 2007 3:37 pm

abdullah<cse du> wrote:henry1007,

Your output section is not correct. Another wrong is that- you take all the input and after getting the zero input you give output. It is not necessary. Take a input and give output. I give some input for test.

Code: Select all

80
70
30
50
100
0

Code: Select all

 80! = 78 36 19 12  7  6  4  4  3  2  2  2  1  1  1
        1  1  1  1  1  1  1
 70! = 67 32 16 11  6  5  4  3  3  2  2  1  1  1  1
        1  1  1  1
 30! = 26 14  7  4  2  2  1  1  1  1
 50! = 47 22 12  8  4  3  2  2  2  1  1  1  1  1  1
100! = 97 48 24 16  9  7  5  5  4  3  3  2  2  2  2
        1  1  1  1  1  1  1  1  1  1
Thx for considering my problem. However, I still can't find out wt's wrong with my output section - and for the test case you've given, my program gives exactly the same output.

Edit: I finally resort to rewriting in C (replacing those cout with printf, etc) and got accepted. Still hv no idea y original C++ code has PE. Maybe problem with setw in iomanip, I suppose.

hridoy
New poster
Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:

acm-160

Post by hridoy » Fri May 25, 2007 8:31 pm

can anyone please tell why I m getting PE in acm-160. I read the previos topics but still I m getting PE

#include<stdio.h>
long prime (long n)
{
long i,z=0;
for(i=2;i<=n/2;i++)
if(n%i==0)
z=1;
if(z==0)
return 1;
else
return -1;
}
main()
{
long x[100],i,j,n,k,z,m;
while(1)
{
scanf("%ld",&n);
if(n==0)
break;
for(i=0;i<100;i++)
x=0;
z=1;
m=n;
for(;n>1&&n<=100;n--)
{
z*=n;
j=0;
for(i=2;i<=n;i++)
{
k=prime(i);
if(k==1)
{
while(z%i==0)
{
z/=i;
x[j]++;
}
j++;
}
}
}
printf("%3ld! =",m);
i=0;
do
{
if(i==15)
printf("%9ld",x);
else
printf("%3ld",x);
i++;
if(i==15)
printf("\n");
}while(x!=0);
printf("\n");
}
}

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Sat May 26, 2007 3:05 pm

Ami ekhono shopno dekhi...
HomePage

User avatar
newton
Experienced poster
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh
Contact:

Post by newton » Wed Jul 18, 2007 2:59 pm

thanx a lot.


it is accepted.
Last edited by newton on Wed Jul 18, 2007 3:57 pm, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Wed Jul 18, 2007 3:31 pm

Why are you calculating prime upto 100000? Check your code with 48, 49. Your code prints an extra line with some spaces.

Hope it helps.
Ami ekhono shopno dekhi...
HomePage

Sayeef
New poster
Posts: 12
Joined: Sun Jun 18, 2006 3:06 am

I get PE 160 plz help

Post by Sayeef » Sun Dec 23, 2007 8:15 am

Code: Select all

#include<stdio.h>
#include<math.h>
int prime[10000]={0};
void gen()
	{
		  long i=0,j=0,chk=1,index=3;
		  prime[0]=1;
		  prime[1]=2;
		  prime[2]=3;
		for(j=5;j<=10000;j++)
			{
			for(i=1;prime[i]<=sqrt(j);i++)
				{
					chk=j%prime[i];
					if(chk==0)
					break;

				}
				   if(chk)
						prime[index++]=j;
			}
  }

int main()
{
	int i,k,p,j;
	int fact,sum=0,count=0;
	gen();
	while(scanf("%d",&fact)&&fact!=0)
	{
		
		count=0;
		printf("%3d ! = ",fact);
		for(i=1;prime[i]<=fact;i++)
		{
			sum=0;
			for(j=1;j<10000;j++)	
			{	
				p=pow(prime[i],j);
				if(p>fact)break;
				else
				{
					sum+=floor(fact/p);
				}
			}
			if(count>=15)
			{
				printf("\n");
				printf("        ");	
				count=0;
			}
			printf("%3d ",sum);
			count++;
		}
		printf("\n");
	}
	
return 0;
}

Samiul
New poster
Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm

Post by Samiul » Sun Dec 23, 2007 1:06 pm

Your output format is not ok.

Input:
5
53
0

Your output: ("0" denotes space)

0050!0=0003000100010
0530!0=0049002300120008000400040003000200020001000100010001000100010
000000000010

Correct output:

005!0=003001001
053!0=049023012008004004003002002001001001001001001
000000001

Brainless
New poster
Posts: 11
Joined: Sat Dec 29, 2007 2:39 pm

Desperate WA

Post by Brainless » Sun Dec 30, 2007 11:48 pm

Hi,

I heve followed your suggestions, but I still get WA.

My code :

Code: Select all

#include <iostream>
#include <vector>
#include <map>
#include <iomanip>
#include <list>
using namespace std;

void get_prime_factors(const int& n, vector<int>& v)
{
	int k(n);
	for(int i=2; k!=1; ++i)
	{
		while(k%i == 0)
		{
			v.push_back(i);
			k/=i;
		}
	}
}

map<int, map<int, int> > _dec;

void set_dec()
{
	_dec[1][2] = 0;
	_dec[2][2] = 1;
	vector<int> v;
	for(int n=3; n<=100; ++n)
	{
		get_prime_factors(n, v);
		const int SIZE(v.size());
		_dec[n] = _dec[n-1];
		for(int j=0; j<SIZE; ++j)
		{
			_dec[n][v[j]]++;
		}
		v.clear();
	}
}

int main()
{
	set_dec();
	int n;
	cin >> n;
	vector<int> v;
	while(n!=0)
	{
		v.push_back(n);
		cin >> n;
	}

	const int SIZE(v.size());
	
	for(n=0; n<SIZE; ++n)
	{
		cout.setf(ios::right);
		cout << setw(3) << v[n] << "! =";
		int count(0);
		int space(3);
		for(map<int, int>::iterator i(_dec[v[n]].begin());
			i!= _dec[v[n]].end(); ++i)
		{
			cout << setw(space) << i->second << flush;
			count++;
			if(count % 15 == 0)
			{
				cout << endl;
				space = 9;
			}
			else
			{
				space = 3;
			}
		}
		cout << endl;
		cout.unsetf(ios::right);
	}
	return 0;
}
I need help, please !

Brainless
New poster
Posts: 11
Joined: Sat Dec 29, 2007 2:39 pm

Post by Brainless » Mon Dec 31, 2007 12:13 am

Ok, finally I found :

The output was incorrect with 50! : my program output a new line, which should not be .

:wink:

Post Reply

Return to “Volume 1 (100-199)”