yea my first submission took like 4.x seconds..which is quite slow compare with others. So I wonder if there's any other improvement that can be made, so that the problem is solved in a more efficient way.
I dont know the reason for TLE but for this in put <2147483647> your program does not give any out put. WHY ? At first try to fix this problem. Hopefully you can find the TLE reason soon. I will try to reply again. I will look at ur code carefully later.
Niaz
I got it . Your code can not ganerate big PRIMES. Use sieve methods to generate PRIMES. If u do not know the method, tell me... i will send u my function which can generate big primes.
Niaz
Hi raymond85 !
I tried to solve this problem and faced the same problem that u faced. I use sieve method to find the primes from 2 to 20000000. then for bigger prime i use 'sqrt' method. My code can run quite nice in my PC for large input. such as (2^31)-1 but i got TLE. Why ? can u help me to solve this problem ? I will wait for ur reply. Thanks in advance.
Niaz
Here is my code. Please help me ! I am totally confused about this TLE !
[cpp]#include<stdio.h>
#include<math.h>
#define max 2000000
char prime[max+10];
long PRIME[90000];
/*int isprime(long n)
{
long i;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
return 0;
}
return 1;
}*/
void find_prime()
{
long i,j;
for(i=0;i<=max;i++)
prime=1;
prime[0]=0;
prime[1]=0;
for(i=2;i<=max;i++)
{
if(prime==1)
{
for(j=i+i;j<=max;j=j+i)
{
if(j>max)
break;
prime[j]=0;
}
}
}
}
int main()
{
long num3,num,num2,k,c,i,j,p,m,n;
find_prime();
while(1)
{
scanf("%ld",&num);
I don't know why I'm getting OLE from my code. I don't seem to encounter any infinite loops whatsoever...
Also what happens if you put in '1' ? Is it just 1 = 1?
Help appreciated
[cpp]#include <stdio.h>
#include <math.h>
bool *primes;
long c=46341;
long nextprime(long i)
{
for (i++;i<=c;i++)
if (primes[i-1]) return i;
return -1;
}
bool isprime(long long n) {
if (n==2 || n==3) return true;
if (n==1) return false;
for (long i=2;i<=(long)sqrt(n); i=nextprime(i))
if (n%i==0) return false;
return true;
}
int main()
{
long long x;
int i,k;
primes=new bool[c];
primes[0]=false; primes[1]=primes[2]=true;
for (i=3;i<c;i++) primes=true;
for (i=3,k=2;k*k<=c;i+=k)
{
if (!primes[k-1] || i>=c) {k++; i=k-1;}
else primes=false;
}
long counter;
bool y;
while (scanf("%lld",&x)==1)
{
if (x==0) break;
counter=2;
if (x<0) {printf("%lld = -1 x ",x); x*=-1;}
else printf("%lld = ",x);
if (x==1 || isprime(x)) { printf("%lld\n",x); continue; }
for (y=false;;counter=nextprime(counter))
{
if (counter*counter>x) { printf("%lld\n",x); break; }
while (x%counter==0) {
x/=counter;
if (x==1) {printf("%ld\n",counter); y=true; break;}
else printf("%ld x ",counter);
}
if (y) break;
}
}
delete [] primes;
[cpp]
#include <iostream>
#include <cmath>
using namespace std;
long long primes[4800];
int point;
int prime(long long n)
{
for(int i = 0;i < point;i++)
{
if (n % primes == 0)
{
return 0;
}
}
return 1;
}
int output(long long n)
{
int firstFlag = 1;
if (n == -1 || n == 0 || n == 1)
{
cout << n << " = " << n << endl;
return 1;
}
if (n < 0)
{
cout << n << " = -1";
n = n * -1;
firstFlag = 0;
}
else
{
cout << n << " =";
}
int p = 0;
while(n != 1)
{
while(n % primes[p] == 0)
{
if(firstFlag)
{
cout << ' ' << primes[p];
firstFlag = 0;
}
else {cout << " x " << primes[p];}
n /= primes[p];
}
p++;
if(p > 4791)
{
if(firstFlag)
{
cout << ' ' << n;
firstFlag = 0;
}
else {cout << " x " << n;}
break;
}
}
cout << endl;
}
int main()
{
long long n;
primes[0] = 2;
primes[1] = 3;
point = 2;
for(long long i = 5;i <= 46340;i += 2)
{
if(prime(i))
{
primes[point++] = i;
}
}
while(cin >> n)
{
if(n == 0) break;
output(n);
}
return 0;
}
[/cpp]
is there someone who can give me some input/output case?
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
49 = 7 x 7
1073741824 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2
1 = 1
-1 = -1
64 = 2 x 2 x 2 x 2 x 2 x 2
-190 = -1 x 2 x 5 x 19
-191 = -1 x 191
-192 = -1 x 2 x 2 x 2 x 2 x 2 x 2 x 3
-193 = -1 x 193
-194 = -1 x 2 x 97
195 = 3 x 5 x 13
196 = 2 x 2 x 7 x 7
197 = 197
198 = 2 x 3 x 3 x 11
199 = 199
200 = 2 x 2 x 2 x 5 x 5
2147483647 = 2147483647
2147483646 = 2 x 3 x 3 x 7 x 11 x 31 x 151 x 331
-2147483647 = -1 x 2147483647
-2147483646 = -1 x 2 x 3 x 3 x 7 x 11 x 31 x 151 x 331