who can tell me what "Output limit exceeded" is ? (closed) Edited by author 03.12.2007 01:29 Edited by author 03.12.2007 01:30 #include <cstdio> #include <cmath> #include <memory> const int MAX = 170001; bool prime[MAX]; int p[15001]; void findPrime() { int i,j; memset(prime,true,sizeof(prime)); //for(i = 0;i <MAX; i++) // prime[i] = true; for(i = 2;i < sqrt(MAX);) { if(prime[i] == true) for(j = i * i;j < MAX; j += i) prime[j] = false; if(i == 2) i++; else i += 2; } } int main() { findPrime(); int i,k,j; j = 1; for(i = 2;i < MAX; i++) { if(prime[i]) p[j++] = i; } scanf("%d",&k); while(k--) { scanf("%d",&i); printf("%d\n",p[i]); } return 0; } I got it. In the FAQ,it says that, In C/C++ you cannot call functions from header math.h with integer parameters. This code will not be compiled: sqrt(2). You can write correctly sqrt((double)2) or sqrt(2.0). 0.062 sek. 264 KB const n=15001; var a:array [1..n] of longint; t,l,m:integer; procedure Generate_array; var i:integer; root:integer; k:integer; newp:longint; prime:boolean; begin a[1]:=2; newp:=3; i:=2; while i<=n do begin root:=trunc(sqrt(newp)); k:=1; prime:=true; while (a[k]<=root) do begin if newp mod a[k] = 0 then begin prime:=false; break; end; inc(k); end; if prime then begin a[i]:=newp; inc(i); end; inc(newp); end; end; begin {$IFNDEF ONLINE_JUDGE} assign(input, 'input.txt'); reset(input); assign(output, 'output.txt'); rewrite(output); {$ENDIF} generate_array; readln(l); for t:=1 to l do begin readln(m); writeln(a[m]); end; {$IFNDEF ONLINE_JUDGE} close(input); close(output); {$ENDIF} end. Who has better results? #include<stdio.h> int main() { int i,j; bool lab[165001]; int sim[15001],c=0; sim[0]=0; j=2; while(j<=165000){ while(!lab[j]) j++; sim[++c]=j; if(c==15000) break; for(i=2*j;i<=165000;i+=j){ lab[i]=false; }j++; }int k; scanf("%d",&k); int *a; a=new int[k+1]; for(i=1;i<=k;i++) scanf("%d",&a[i]); for(i=1;i<=k;i++) printf("%d\n",sim[a[i]]); return 0; } I can't understand why I get compile error! I don't see nothing strange in my code and it works perfectly in my computer, so I don't know what to change here is my code: #include <stdio.h> #include <math.h> int p[15003],ind=5; is_prime(int n){ int i=3,l=sqrt(n)+1; while(i<l){ if(n%i==0) return 0; i+=2; } return 1; } init(){ int i; for(i=11;ind<15002;i+=2){ if(is_prime(i)){ p[ind]=i; ind++; } } } int main() { p[1]=2;p[2]=3;p[3]=5;p[4]=7; int n,i,k; init(); scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d",&k); printf("%d\n",p[k]); } return 0; } is someone can help me I would appreciate.... thanks! bye Put SQRTL instead of SQRT and you'll get AC... thanks for your reply but I try it by changing "sqrt" for "sqrtl" and it doesn't work maybe it is something else thanks anyway! byee I submitted your source with C++ and sqrtl and got AC... yes you are right I submit it but in C so I get compilation error thanks :D !!!!! (int)sqrt((double)n)-it is a true while (i*i<=n) - it's true or i = 0; while (p[i]*p[i]<=n)... What does "Crash!!!ACCESS_VIOLATION" mean?? Why this problem happen??? How to settle it??? Thanks!!! Source code: #include<iostream.h> unsigned long int is_prime(unsigned long int x) { unsigned long int mid=x/2+1,y; for(y=2;y<mid;y++) { if(x%y==0)return 0; } return 1; } unsigned long int prime_k(unsigned long int k) { unsigned long int count,l; for(l=2,count=0;count<k;) { if(is_prime(l)) { count++; l++; } else l++; } l--; return l; } int main() { unsigned long int num,a[100],i,prime,m; cin>>num; for(i=0;i<num;i++) { cin>>a[i]; } for(i=0;i<num;i++) { m=a[i]; prime=prime_k(m); m=num-1; if(i==m)cout<<prime; else cout<<prime<<" "; } } Need increase array "unsigned long a[100]" to 15001 If you got crush may be your value of K is not anought; I got crush when K=10000 then I take K=30000 and got AC. #include <fstream.h> #include <string.h> const char *Fn_Inp = "p1086.in" ; const char *Fn_Out = "p1086.out" ; const char EndLn = '\n' ; const int MaxN = 16000 ; typedef int TMark[ MaxN ] ; typedef int TPrime[ MaxN ] ; TMark Mark ; TPrime Prime ; long N , K , Tot ; ifstream Read ( Fn_Inp ) ; ofstream Write ( Fn_Out ) ; void Init () { int i , j ; memset ( Mark , 1 , sizeof ( Mark ) ) ; memset ( Prime , 0 , sizeof ( Prime ) ) ; Mark[ 1 ] = 0 ; for ( i = 2 ; i < MaxN ; i++ ) if ( Mark[ i ] ) { j = i + i ; while ( j < MaxN ) { Mark[ j ] = 0 ; j += i ; } } for ( i = 1 ; i < MaxN ; i++ ) if ( Mark[ i ] ) Prime[ Tot++ ] = i ; } void Work () { long i , Pos ; cin >> K ; for ( i = 0 ; i < K ; i++ ) { cin >> Pos ; cout << Prime[ Pos - 1 ] << EndLn ; } } int main () { Init () ; Work () ; return ( 0 ) ; } The test data should be very much. Does the judge run my program only once or run several times?
My meaning is : If the judge run only once. I can generate all prime number with at most 15000 numbers.then I read a test data n, then output the n-th prime number. otherwise,I must generate prime number with at most n numbers(where n is not knew in advance)
> The test data should be very much. > > Does the judge run my program only once > or run several times? > > My meaning is : > If the judge run only once. > I can generate all prime number with at most > 15000 numbers.then I read a test data n, then > output the n-th prime number. > otherwise,I must generate prime number with > at most n numbers(where n is not knew in advance) > > This is my solve. Judge write Time Limit Ex.. But on my PC program very fast.. Why??/? { @JUDGE_ID: 22797EM 1086 Pascal } program Cryptography_1086; const max=15000; type int=1..max; mas_find=array [int] of longint; var limit:integer; k,i,j,kolvo,n:int; simple:boolean; candidat:longint; Find:mas_find; BEGIN readln(k); for i:=1 to k do begin readln(n); if n=1 then begin writeln(2); continue; end; candidat:=3; find[1]:=2; kolvo:=1; while kolvo<n do begin limit:=trunc(sqrt(candidat)); simple:=true; for j:=1 to kolvo do begin if j=1 then continue; if Find[j]>limit then break; if candidat mod find[j]=0 then begin simple:=false; break; end; end; if simple then begin inc(kolvo); find[kolvo]:=candidat; end; inc(candidat,2); end; writeln(Find[kolvo]); end; END. #include <iostream.h> int a[15001],b[200000],i,n,m; void make() { int i,j,an; for (i=2; i<200000; i++) if (!b[i]) for (j=2; j<=200000/i; j++) if (i*j<=200000) b[i*j]=1; an=0; for (i=2; i<200000; i++) if (!b[i]) { an++; a[an]=i; } } int main() { make(); cin>>n; for (i=0; i<n; i++) { cin>>m; cout<<a[m]<<endl; } return 0; } Why the result of testing is WA? Here is my program: const max=15000; var a:array[1..max] of longint; k:integer; f:text; procedure readdata; var i,m:integer; begin assign(f,'temp.tmp'); rewrite(f); readln(k); for i:=1 to k do begin readln(m); writeln(f,a[m]); end; close(f); end;{p..readdata} function ifp(n:longint):boolean; var k:longint; begin ifp:=false; k:=2; while (n mod k<>0) and (k*k<n) do k:=k+1; if k*k>n then ifp:=true; end; procedure makep; var k:integer; p:longint; begin k:=1; p:=2; while k<=max do if ifp(p) then begin a[k]:=p; k:=k+1; p:=p+1; end else p:=p+1; end;{p.. makep} procedure print; var i:integer; k:longint; begin reset(f); for i:=1 to k do begin readln(f,k); writeln(k); end; close(f); erase(f); end; {p..print} begin makep; readdata; print; end. Here is my resources #include<stdio.h> #include<math.h> main() { long a[15000],i,j,sum; int state,n,k; a[0]=2; a[1]=3; sum=1; for(i=5;i<170000,sum<15000;i++) { for(j=2;j<=sqrt(i);j++) { state=1; if(i%j==0) { state=0; break; } } if(state) { sum++; a[sum]=i; } } scanf("%d",&n); for(i=0;i<n-1;i++) { scanf("%d",&k); printf("%ld\n",a[k]); } scanf("%d",&k); printf("%d",a[k-1]); } Thank you if anyone who can help me!!! I mean that my program is 1700 lines because in 1086 I generate all simple numberes in an array. Is there some other way to do so or do you have any idea why I receive Compilation Error when the programs runs on my computer prefectly??? you shouldn't solve the problem in that way. In this case, generate the prime array is very fast and simple, use some function to enshort your code and believe me, it won't make your prog run slower (if so, just only 0.01s ;)) Good luck ! QH@ PS: btw, Tong Hop Vo Dich, you must know that ;) I can't get it. What do you mean? Please explain it once again! Thanx See what I do I make large const array and one other where I keep the questions. It works perfectly on my computer but the Judge says Compile Error! WHY??? Maybe it is because of the 1670 lines of constant array??? const a:array[1..15000] of longint = (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,103, 107,109,113,127,131,137,139,149,151, 157,163,167,173,179,181,191,193,197, 199,211,223,227,229,233,239,241,251, 257,263,269,271,277,281,283,293,307, 311,313,317,331,337,347,349,353,359, 367,373,379,383,389,397,401,409,419, 421,431,433,439,443,449,457,461,463, 467,479,487,491,499,503,509,521,523, .... 163327,163337,163351,163363,163367,163393,163403,163409,1634 11, 163417,163433,163469,163477,163481,163483,163487,163517,1635 43, 163561,163567,163573,163601,163613,163621,163627,163633,1636 37, 163643,163661,163673,163679,163697,163729,163733,163741,1637 53, 163771,163781,163789,163811,163819,163841); var b:array[1..1000] of integer; br,k:integer; begin readln(k); br:=0; for br:=1 to k do readln(b[br]); for br:=1 to k do writeln(a[b[br]]); end. in the main program, use don't have to use array b[], just write this const a: .... var k, br, n : integer; begin read(k); for br:=1 to k do begin read(N); writeln(a[N]); end; end. if u still get Compile error, that means the error is your constant array. So, try to generate it from your function include to your program, don't attach a const array to it like yours now. i.e : var a : array [ 1..15000 ] of longint; procedure generate_prime_array; begin ... {write your procedure to generate the array here} ... end; Good luck ! QH@ Do you mean to find another way to solve the problem that is fast enought to get in the time? How can I receive Compile Error when on my computer it runs perfectly? And last it's said in the task's text that you input number k and k numbers. So your output is after you input the num. What do you think? Thanks !!! I think they have some limit on the max size of the program that you can submit. Btw generating the prime array is quite easy, if you have to check if x is prime then check the numbers only from 2 to sqrt(x) if they are x's divisors. Since you can generate the array bottom->up and it doesn't make sense checking if composite numbers are x's divisors, then you can use the already generated part (discarding when you go > sqrt(x)) to check if it divides x. If x is prime then you put it in the array. Btw you can treat 2 as special case(it is the only prime that is even) and try to add the numbers from 3-> using +2 as step. Kak se kazva tozi algoritam? Kato gledam imash 104 resheni zadaci ot koga si w sastezanieto, kolko cesto ima online sastezaniq? Blagodarq, Cao!!! Did you read that you should post in english ? The text is little above the submit button. Posting in non-english languages sux so don't do it. > Kak se kazva tozi algoritam? There is no name for this algorithm it is obvious solution. > Kato gledam imash 104 resheni zadaci > ot koga si w sastezanieto, kolko > cesto ima online sastezaniq? To see how often are the online contests check the schedule page regulary. > Blagodarq, > Cao!!! > Do you mean to find another way to solve the problem > that is fast enought to get in the time? Yes, i mean that !
> How can I receive Compile Error when on my computer it runs > perfectly? Which Pascal language you use at home ? are u sure that it's the same language and the same version with the one they use in the judge ??? you know, there's a lot of diffrence between some Pascal languages, and even if they are in the same Pascal language, there's still some difference between the versions. So that, your prog run perfectly in your computer, it does not mean that it can't get compile error in the judge ! > And last it's said in the task's text that you input number > k and k numbers. So your output is after you input the num. Do you know that "read(something)" that means "read(input, something)", and so does write function. Here, you can suppose that the word input in function read(input, ..) is a variable of Text. Output in function write(output, ..) is the same here. In the judge, you can image that, when you read, that you read from a file, and you write, you are writing to a file. That means it doesn't important you read all the input before writing to output, or you read input, then write output, read input again, .... The important thing here is only the correctness of your output ! You can determine what i said here is false or true by viewing the FAQ page, in the end of page, about sending problem, they test your output by run the command "test.exe (output of user solution) (correct output)" > What do you think? So now my question for you is the same, "WHAT DO YOU THINK" ???
> Thanks !!! You are welcome ! Good luck again ;) QH@ |
|