Page 8 of 12
Re: 583 - Prime Factors
Posted: Sun Jul 20, 2008 1:09 am
by x140l31
can anyone tell me why this produces a "core dumped"???
Code: Select all
int firstDiv(int &n, const VI &v)
{
if (n < 0)
{
cout << "-1";
n = -n;
return 0;
}
......
}
in concrete it crashes with n = -2147483647
-2147483647 and 2147483647 can be hold in an "int", no?
sorry about my poor english
Re: 583 - Prime Factors
Posted: Mon Jul 21, 2008 9:41 pm
by Jan
(+-)2147483647 both fit into normal int.
Re: 583 - Prime Factors
Posted: Tue Jul 22, 2008 11:02 pm
by x140l31
Jan wrote:(+-)2147483647 both fit into normal int.
then why it crashes?

Re: 583 - Prime Factors
Posted: Tue Jul 22, 2008 11:29 pm
by Jan
Can you post your full code?
Re: 583 - Prime Factors
Posted: Wed Jul 23, 2008 12:20 am
by x140l31
Jan wrote:Can you post your full code?
this is not the code of 583 problem... but the function is similar:
Code: Select all
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
int firstDiv(int &n, const VI &v)
{
if (n < 0)
{
cout << "-1";
n = -n;
return 0;
}
// here will be another portion of code
return 0;
}
int main()
{
int n;
cin >> n;
VI v(n);
cout << firstDiv (n, v) << endl;
}
you can try this program, if you try this input, it will crash
Re: 583 - Prime Factors
Posted: Fri Jul 25, 2008 12:43 am
by Jan
Did you notice this?
You are giving -2147483647 as n. vector<int> v(-2147483647). That's why your program crashed.
Re: 583 - Prime Factors
Posted: Sat Jul 26, 2008 1:53 am
by x140l31
Jan wrote:Did you notice this?
You are giving -2147483647 as n. vector<int> v(-2147483647). That's why your program crashed.
sorry sorry sorry!
i was wrong...
i give you the true code by mp
change those terrible mistake xD
Code: Select all
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
int firstDiv(int &n, const VI &v)
{
if (n < 0)
{
cout << "-1";
n = -n;
return 0;
}
// here will be another portion of code
return 0;
}
int main()
{
int n;
cin >> n;
VI v(10000);
cout << firstDiv (n, v) << endl;
}
Re: 583 - Prime Factors
Posted: Fri Aug 01, 2008 8:00 am
by ihack
Jan wrote:(+-)2147483647 both fit into normal int.
if so ...why my code isnt working for 2147483647??
Code: Select all
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,minflag=0;
while(scanf("%d",&n))
{
if(n==0)break;
printf("%d = ",n);
if (n<0)
{
minflag=1;
printf("-1 x ");
n=abs(n);
}
for(i=2;i*i<=n;i++)
{
while(n%i==0)
{
printf("%d",i);
n/=i;
if(n>1)printf(" x ");
}
}
if(n!=1)
printf("%d",n);
printf("\n");
}
return 0;
}
Re: 583 - Prime Factors
Posted: Thu Aug 21, 2008 5:06 pm
by Jan
When n = 2147483647, suppose i*i is greater than n, but since i is an 'int' so, overflow will occur. Use i as 'long long'. Hope it helps.
Re: 583 - Prime Factors TLE
Posted: Wed Sep 17, 2008 10:03 pm
by newton
why TLE plz help
whene i solved the problem after generating primes. But now if i dont handle only with primes the code gets TLE.
Code: Select all
#include <cstdio>
int absValue(int a){
if(a<0)
return -a;
else
return a;
}
int main(){
int num,i;
bool s;
while(scanf("%d",&num) == 1 && num){
s = false;
if(absValue(num) == 1){
printf("%d = %d\n",num,num);
continue;
}
printf("%d =",num);
if(num < 0){
num = -num;
s = true;
}
if(s)
printf(" -1 x");
i = 2;
while(num/i){
if(num%i == 0){
while(num%i == 0){
num /= i;
if(num/i == 0)
printf(" %d",i);
else
printf(" %d x",i);
}
}
i++;
}
if(num>1){
printf("x %d",num);
}
puts("");
}
return 0;
}
But as i am setting a condition
if(num%i == 0)
so this will process only the primes. So why still TLE?
Thanks
Re: 583 - Prime Factors TLE
Posted: Sun Sep 21, 2008 3:35 am
by DJWS
Because your algorithm is not efficient enough.
http://mathworld.wolfram.com/DirectSear ... ation.html
newton wrote:why TLE plz help
whene i solved the problem after generating primes. But now if i dont handle only with primes the code gets TLE.
Code: Select all
#include <cstdio>
int absValue(int a){
if(a<0)
return -a;
else
return a;
}
int main(){
int num,i;
bool s;
while(scanf("%d",&num) == 1 && num){
s = false;
if(absValue(num) == 1){
printf("%d = %d\n",num,num);
continue;
}
printf("%d =",num);
if(num < 0){
num = -num;
s = true;
}
if(s)
printf(" -1 x");
i = 2;
while(num/i){
if(num%i == 0){
while(num%i == 0){
num /= i;
if(num/i == 0)
printf(" %d",i);
else
printf(" %d x",i);
}
}
i++;
}
if(num>1){
printf("x %d",num);
}
puts("");
}
return 0;
}
But as i am setting a condition
if(num%i == 0)
so this will process only the primes. So why still TLE?
Thanks
Re: 583 - wht TLE ??
Posted: Wed Nov 12, 2008 7:06 pm
by abid_iut
i think my code is quite fast but why i am getting TLE??
please help me anyone
my code:
Code: Select all
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
typedef unsigned long inte;
void ptr(inte w){
inte n=w;
inte rez=0;
inte i=2;int flag=0;
while(i<=sqrt(w)+5){
if(i>=sqrt(w) && flag!=0){cout<<n;break;}
if(n%i==0){
flag++;
cout<<i;
n/=i;
if(n!=1)cout<<" x ";
else break;
}
else {
if(i==2)i++;
else i+=2;
}
}
if(flag==0)cout<<w;
}
int main()
{
while(1){
long n;cin>>n;
if(n==0)break;
cout<<n<<" = ";
if(n<0){cout<<" -1 x ";n*=-1;}
ptr(n);
cout<<endl;
}
return 0;
}
Thanx in advance
plssss help.
Re: 583 - Prime Factors
Posted: Wed Jan 07, 2009 11:27 pm
by pok
i'm getting Presentation error..
but i cant understand what is wrong ..
so pls help me..
Re: 583 - Prime Factors
Posted: Thu Jan 08, 2009 12:39 am
by mf
Avoid printing trailing spaces at the end of each line.
Re: 583 - Prime Factors
Posted: Thu Jan 08, 2009 10:19 pm
by pok
Thanks a lot
mf..
now AC..
take care..
God bless you..
