10311 - Goldbach and Euler

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

Moderator: Board moderators

..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

I think it is not a good attitude to ask for extending the time limit.
As you say, there are more than 60 people got accepted within 10s. That means there is some methods to do it in reasonable time.
I think most of us come to this online judge because we want to learn by solving problems. If you don't get fast enough, just think about how to do it faster :wink:

P.S. I got it accepted with 8s
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

From other side:
why problemsetter set time-limit to 40 sec ? I think, that she/he thought that it is reasonable time for solving this problem. So I think, that if problemsetter doesn't set TIME-LIMIT, we should use 10 sec limit. But If PROBLEMSETTER SET LIMIT, we should use your limit .... (and then time-limit could be i.e. 2 sec for other problems ... ). It's my private opinion and I think that when I solve problem in 1/3 of time limit it's good time. I know, that every problem we can solve in very small amount of time, but in many cases it unnecessary ... (this is only my opinion! ) ...

DM

PS. I got Acc in 14 sec
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Farid Ahmadov
Experienced poster
Posts: 131
Joined: Thu Apr 17, 2003 8:39 am
Location: Baku, Azerbaijan

Post by Farid Ahmadov »

If there are 60 who solved this problem in less than 10 seconds then it means it is possible to solve it:). If you are a good programmer then you will try to do it. Every problem that I have met in online contests the TL for easy problems is 10 seconds or more and memory is 16MB or more. It means that a good programmer will solve it in less time and with less memory. The others will use much more memory or full search algorithm and solve it in more than 10 seconds. If you think that you program is correct then try to optimize it with maximum possible ways.

Good luck.
_____________
NO sigNature
yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post by yiuyuho »

I have a question about the problem specification:
For each line of input produce one line of output. This line should be of one of the following types:
n is not the sum of two primes!
n is the sum of p1 and p2.
For the second case, always make sure that (p2-p1) is positive and minimized.
how can we from there or other parts of the problem to deduce that for 4, or 6 we should output that they are not the sum of 2 primes?

we cannot do 4= 2+2 because 2-2 is not positive, but 4 is not the sum of two primes is also incorrect....and where does it say if it is not case 2 then output case 1?

Thanks
JonyBravo
New poster
Posts: 2
Joined: Sat Oct 30, 2004 9:39 am

10311 Why do i get OutOfMemoryError

Post by JonyBravo »

Can enyone help me

Why my code gets on high numbers OutOfMemoryError
for example on max N=100000000

100000000
Exception in thread "main" java.lang.OutOfMemoryError

My Code:

import java.io.*;
import java.util.*;

public class GoldbachAndEuler2{
final static long maxI=Integer.parseInt("100000000");
static long stevilo;
public static void main(String[] args){
BufferedReader vhod = new BufferedReader(new InputStreamReader(System.in));
while(true) {
try{
String nn = vhod.readLine().trim();
if (nn.equals("")) break;
int mm = Integer.parseInt(nn);

if(mm - stevilo > 0){
stevilo=mm;
if ((0 < mm) && (mm <= maxI)) {
problemL(mm);
}
else {
System.out.println("Stevilo "+ mm +" ni iz intervala [1, "+maxI+"].");
}
}
else{
System.out.println(stevilo +" - "+mm+" < 0 zato koncamo!" );
break;
}
}
catch(Exception ex){
}
}
}
public static void problemL(int stevilo)
{
boolean[] jePrastevilo = new boolean[stevilo];
for(int i = 2; i < stevilo; i++){jePrastevilo = true;}

for(int i = 2; i * i < stevilo; i++){
if(jePrastevilo){
for(int j = i; i * j < stevilo; j++){jePrastevilo[i*j] = false;}
}
}

int prastevilo = 0;
for (int i = 2; i < stevilo; i++){
if (jePrastevilo) prastevilo++;
}

int seznam[] = new int[prastevilo];
int k = 0;//pomik po seznamu
for(int i = 0; i < stevilo; i++){
if(jePrastevilo){seznam[k++] = i;}
}

int levo = 0, desno = prastevilo-1;
boolean nasliSum=false;
while(levo <= desno) {
if(seznam[levo] + seznam[desno] == stevilo){nasliSum=true; break;}
else if(seznam[levo] + seznam[desno] < stevilo){levo++;}
else{desno--;}
}
if (nasliSum){
System.out.println(stevilo + " is the sum of " + seznam[levo] + " and " + seznam[desno]);
}
else{
System.out.println(stevilo + " is not the sum of two primes!");
}
}
}//class
JonyBravo
New poster
Posts: 2
Joined: Sat Oct 30, 2004 9:39 am

Post by JonyBravo »

And how do I repare that???
:-? :-? :-? :-? :-? :-? :-?
Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

10311 Goldbach and Euler.... Compile Error

Post by Antonio Ocampo »

Hi, I don't know why I got Compile Error. Please help me!!!

Thanks in advance :lol:

Code: Select all


cut :)
Last edited by Antonio Ocampo on Sat Jan 22, 2005 8:25 am, edited 1 time in total.
stubbscroll
Experienced poster
Posts: 151
Joined: Tue Nov 16, 2004 7:23 pm
Location: Norway
Contact:

Re: 10311 Goldbach and Euler.... Compile Error

Post by stubbscroll »

Antonio Ocampo wrote:Hi, I don't know why I got Compile Error. Please help me!!!

Thanks in advance :lol:
I get the following compile error:

'main' must return 'int'

Simply replace 'void main' with 'int main'. Also reduce your #define max, or you'll get memory limit exceeded.
Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo »

Well, I put void main() in all my AC programs therefore that isn't the problem.
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

The judge should have sent you a mail identifying the lines it can't compile.
I just hope it wasnt as silly a mistake as clicking on C instead of C++ if you are using the web based submission tool :wink:

Regards,
Suman.

PS: a quick glance on VC.6 shows link errors, it might be due to the fact that you are using both iostream & cstdio, can you explain why you need both of them?

On a mac g++ cribs on void main, and on correcting that, the bool array seems to be too large, again cribs ... reduced it by a factor of 10 and then it compiles happily even with both the headers.So you have your hands full!
Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo »

I sent my code by e-mail and I have received this reply:

Code: Select all

Here are the compiler error messages:

03230701_24.c:39: unterminated string or character constant
03230701_24.c:39: possible real start of unterminated constant
Could someone tell me what does this mean? :(
shahriar_manzoor
System administrator & Problemsetter
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

hmm

Post by shahriar_manzoor »

You must have compiled your code before sending. The possible cause can be that the email sender program has imposed a line break in your program which has caused a compile error;

For example:

printf("jhjadhajhajahdjadhajdhadjahjahajdhajhajshajhajhajhajhajshaj\n");

will compile but

printf("jhjadhajhajahdjadhajdhadjahjahajdha
jhajshajhajhajhajhajshaj\n");

won't compile
Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo »

Thx for your reply Shahriar. I got the same answer when I submit my program by "Submit-o-matic 6.0".

This is my code, please help me!!!

Code: Select all

Cut  :lol: 
Thx in advance :)
Last edited by Antonio Ocampo on Sat Jan 22, 2005 8:24 am, edited 1 time in total.
shahriar_manzoor
System administrator & Problemsetter
Posts: 399
Joined: Sat Jan 12, 2002 2:00 am

hmm

Post by shahriar_manzoor »

The array you hav declared is too big for the judge. You will have to implement the seive using bit. Often we have the misconception that a boolean variable will always occupy 1 bit of memory space as it has two possible values. But the judge should have replied memory limit exceeded but instead it replied compile error.
Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo »

Well, I have replaced

Code: Select all

bool no_primo[max+1]={true,true,false}; 
by

Code: Select all

char no_primo[max+1]={1,1,0}; 

but it doesn't work, any suggestion??
Post Reply

Return to “Volume 103 (10300-10399)”