516 - Prime Land

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

kn
New poster
Posts: 28
Joined: Fri Apr 13, 2007 8:53 am

Post by kn » Mon Apr 30, 2007 9:45 pm

Being a newbie, this time I encounter "FLOATING POINT EXCEPTION"

According to the question, we have to read an unknown number of pairs of inputs (prime, exponent)...
By which I mean, I cannot determine the total number of inputs before hand...
I have to use "getchar()" to read the whole line, until a '\n' is discovered...

I want to ask
1) Is there any present function that allows me to read the inputs correctly, instead of writing the part for input by "getchar()"?
2) What does it mean by FLOATING POINT EXCEPTION? Where does it come from my piece of code?

THX

Here is my code

Code: Select all

OK-ed
Last edited by kn on Thu May 10, 2007 3:05 pm, edited 1 time in total.

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

Post by helloneo » Tue May 01, 2007 3:40 am

1. I usually use gets() and strtok() to parse this kind of input..
2. floating point exception usually caused by "divide by zero".. and I think your program has the chance to get it..

kn
New poster
Posts: 28
Joined: Fri Apr 13, 2007 8:53 am

Post by kn » Thu May 10, 2007 3:04 pm

helloneo wrote:1. I usually use gets() and strtok() to parse this kind of input..
2. floating point exception usually caused by "divide by zero".. and I think your program has the chance to get it..
I used "strtok" successfully to remove that complicated part..
Yes, floating pt exception is caused by "division by 0"...
Finally I got AC
THX :D

But, I just wonder whether I could get rid of those warnings.

tanmoy
New poster
Posts: 18
Joined: Wed Jun 25, 2008 2:25 pm

516

Post by tanmoy » Wed Jun 25, 2008 2:37 pm

it is saying PE :( can any one please help me?
here is my code ?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
int num;
vector<int>prime_factors(int a)
{
vector<int>pf;
int g,i;
while(((a%2)==0))
{
a /=2;
pf.push_back(2);
}
g=sqrt(a);

for(i=3;i<=g;i+=2)
{
while(a%i ==0)
{
pf.push_back(i);
a/=i;
}
}
if(a!=1)
pf.push_back(a);
return pf;

}
int getnum(int a[],int g)
{
int j=2,k=1;
int l=1;
do
{
l=int(l*(pow(a[k],a[j])));
k=j+1;
j=j+2;
}while(j<=g);
return l;
}
int main()
{
char s[1000];
char t[1000];
int f[1000];
int i=0,j=0,h,m=0,p;
vector<int>pf;
int act[100][2];
int test,prime,mm,nn;
while(1)
{
i=0,j=0,h,m=0,p=1;
do{
s=getchar();
i++;
}while(s[i-1]!='\n');
s[i-1]='\0';
i--;
i=strlen(s);
int y=0;
while(1)
{
for(h=y;h<i;h++)
{
t[j]=s[h];
if(t[j]==' '){t[j]='\0';j=0;break;}
j++;
if(h==(i-1)){t[j]='\0';break;}
}
m++;
f[m]=atoi(t);
if(f[1]==0) p=0;
y=h+1;
if(h==(i-1))break;
}
if(p==0) break;

num=getnum(f,m);
num=num-1;
pf=prime_factors(num);
mm=0;
nn=0;
int r=pf.size();
while(1)
{
test=0,prime=pf[nn];
if(prime !=0)
{
for(i=0;i<pf.size();i++)
if(prime==pf && pf !=0){pf=0;test++;}

act[mm][0]=prime;
act[mm][1]=test;
mm++;
}
nn++;
if(nn==r)break;
}
mm=mm-1;
for(i=mm;i>=0;i--)
for(j=0;j<2;j++)
printf("%d ",act[j]);
printf("\n");

}

return 0;
}

Chirag Chheda
Learning poster
Posts: 74
Joined: Sat Jun 21, 2008 12:24 pm
Location: India

Re: 516

Post by Chirag Chheda » Thu Jun 26, 2008 12:18 pm

hi,
All numbers in the line are separated by one space
There is no space after the last number.

tanmoy
New poster
Posts: 18
Joined: Wed Jun 25, 2008 2:25 pm

Re: 516

Post by tanmoy » Mon Aug 04, 2008 10:09 am

thanks a lot .
at last i get ac :) :)
thanks again for your good advice :)

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

Why PE 516!

Post by newton » Sun Sep 07, 2008 1:04 pm

please anyone point me the reason of getting PE.
i thought but couldn't find. My code:

Code: Select all

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAX 32767
using namespace std;

typedef long long LL;
bool isPrime[MAX];
int array[MAX];
int sum[MAX];
int primes[MAX];

LL getPow(int a,int b){
	LL n = a;
	for(int i = 2; i <= b; i++)
		n *= a;
	return n;
}

void setSieve(){
	int i,j,sqrtNum;
	memset(isPrime,true,sizeof(isPrime));
	isPrime[0] = false;
	isPrime[1] = false;	
	sqrtNum = sqrt(MAX);
	for(i = 2; i <= sqrt(MAX); i += 1){
		if(isPrime[i])
			for(j = i*i; j <= MAX; j += i)
				isPrime[j] = false;
	}
	int k = 0;
	for(i = 0; i<= MAX; i++)
		if(isPrime[i])
			primes[k++] = i;
}

int freq[MAX];

void callConvert(int num){
	memset(freq,0,sizeof(freq));
	int i = 0,j;
	bool f = true;
	while(num > 1 && i <= MAX){
		while(num % primes[i] == 0 && i < MAX){
			f = false;
			num /= primes[i];
			freq[i]++;
		}
		i++;
	}
	
	if(isPrime[num] && f){
		printf("%d %d ",num,1);
	}
	else if(isPrime[num]){
		printf("%d %d",num,1);
	}
		
	for(j = i; j >= 0; j--){
		if(freq[j]){
			if(j)
				printf("%d %d ",primes[j],freq[j]);
			else
				printf("%d %d",primes[j],freq[j]);
		}
	}	
	printf("\n");	
}

int main(){
	//freopen("in.txt","rt",stdin);
	char str[10000],*ptr;
	int n,e;
	LL sum;
	setSieve();

	while(true){
		sum = 0;
		gets(str);		
		ptr = strtok(str," ");
		n = atoi(ptr);
		if(n == 0)
			break;

		ptr = strtok(NULL," ");
		e = atoi(ptr);
		sum = getPow(n,e);
		
		while(true){
			ptr = strtok(NULL," ");
			if(!ptr)
				break;
			n = atoi(ptr);			
			ptr = strtok(NULL," ");
			e = atoi(ptr);
			sum *= getPow(n,e);			
		}				
		callConvert(sum-1);
	}
	
	return 0;
}
Advanced thanks

Chirag Chheda
Learning poster
Posts: 74
Joined: Sat Jun 21, 2008 12:24 pm
Location: India

Re: 516

Post by Chirag Chheda » Fri Sep 12, 2008 5:46 am

Please look at the above post that i had made in this topic regarding PE. Hope it helps. Let me know if it still shows PE

alimbubt
New poster
Posts: 39
Joined: Tue Aug 07, 2012 10:40 pm
Location: BUBT,Dhaka, Bangladesh
Contact:

Re: 516

Post by alimbubt » Tue Apr 16, 2013 10:18 pm

Input:

Code: Select all

12 3 14 2
2 3 7 3
8 3 9 3
19 4
12 2 6 3
18 5
100 2
105 2
15 2 18 2
0
Output:

Code: Select all

338687 1
211 1 13 1
751 1 71 1 7 1
181 1 5 1 3 2 2 4
1637 1 19 1
2711 1 41 1 17 1
101 1 11 1 3 2
53 1 13 1 2 4
271 1 269 1
Give me six hours to chop down a tree and I will spend the first four sharpening the axe...(BUBT ILLUSION)
http://uhunt.felix-halim.net/id/155497
http://onlyprogramming.wordpress.com/

Post Reply

Return to “Volume 5 (500-599)”