10533 - Digit Primes
Moderator: Board moderators
10533
help please my program works fast and correct on my computer but gives WA. help pleas.[pascal]program acm10533;
var i,j,a,b,number,p,k,i1:longint;
c:array[0..1000000] of longint;
l:boolean;
procedure parz(n:longint;var h:boolean);
label 1;
var i:integer;
begin
h:=true;
for i:=2 to trunc(sqrt(n)) do
if n mod i=0 then begin h:=false;goto 1;end;
1:
end;
begin
read(number);
c[0]:=0;
for i:=1 to 1000000 do
begin
k:=0;
i1:=i;
parz(i,l);
if l=true then
begin
repeat
k:=k+(i mod 10);
i:=i div 10;
until i=0;
i:=i1;
parz(k,l);
if l=true then c:=c[i-1]+1 else c:=c[i-1];
end else c:=c[i-1];
end;
for p:=1 to number do
begin
read(a,b);
writeln(c-c[a]);
end;
end.
[/pascal]
var i,j,a,b,number,p,k,i1:longint;
c:array[0..1000000] of longint;
l:boolean;
procedure parz(n:longint;var h:boolean);
label 1;
var i:integer;
begin
h:=true;
for i:=2 to trunc(sqrt(n)) do
if n mod i=0 then begin h:=false;goto 1;end;
1:
end;
begin
read(number);
c[0]:=0;
for i:=1 to 1000000 do
begin
k:=0;
i1:=i;
parz(i,l);
if l=true then
begin
repeat
k:=k+(i mod 10);
i:=i div 10;
until i=0;
i:=i1;
parz(k,l);
if l=true then c:=c[i-1]+1 else c:=c[i-1];
end else c:=c[i-1];
end;
for p:=1 to number do
begin
read(a,b);
writeln(c-c[a]);
end;
end.
[/pascal]
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
Critical Inputs
Hi Eduard,
I am not an expert in interpreting PASCAL code, but here is some critical inputs.
What is your output for the following cases:
10
5 8
1 999999
11 25
3 3
5 6
6 7
1 100
9 10
200 300
12 13
I am not an expert in interpreting PASCAL code, but here is some critical inputs.
What is your output for the following cases:
10
5 8
1 999999
11 25
3 3
5 6
6 7
1 100
9 10
200 300
12 13
My answers are
1
30123
1
1
0
1
14
0
8
0
1
30123
1
1
0
1
14
0
8
0
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
REAL OUTPUT FOR THOSE INPUT
INPUT
11
5 8
1 999999
11 25
3 3
5 6
6 7
1 100
9 10
200 300
12 13
1 10
OUTPUT
2
56
2
1
1
1
14
0
8
0
4
11
5 8
1 999999
11 25
3 3
5 6
6 7
1 100
9 10
200 300
12 13
1 10
OUTPUT
2
56
2
1
1
1
14
0
8
0
4
this time WA
what next...............?
what next...............?
-
- Learning poster
- Posts: 57
- Joined: Wed Dec 10, 2003 7:32 pm
- Location: Russia, Saint-Petersburg
TLE :(
Hi! I have TLE on this problem, but I don't know how I can make my program faster.
I have algo that:
1) get_primes_numbers (Eratosphean)
2) get_digit_primes_numbers
It makes and d=true if i - digit prime. And this two procedures works <1 sec. !!!
It means that very slow work procedure that count how many 'true' values in array from t1 to t2. Help me please, how I can optimise this procedure:
[pascal] readln(n);
for i:=1 to n do begin
readln(n1, n2);
counter:=0;
if not odd(n1) then inc(n1);
while n1<=n2 do begin
if d[n1] then inc(counter);
inc(n1, 2);
end;
writeln(counter);
end;
[/pascal]
I have algo that:
1) get_primes_numbers (Eratosphean)
2) get_digit_primes_numbers
It makes
Code: Select all
d: array [1 .. 1000000] of boolean
It means that very slow work procedure that count how many 'true' values in array from t1 to t2. Help me please, how I can optimise this procedure:
[pascal] readln(n);
for i:=1 to n do begin
readln(n1, n2);
counter:=0;
if not odd(n1) then inc(n1);
while n1<=n2 do begin
if d[n1] then inc(counter);
inc(n1, 2);
end;
writeln(counter);
end;
[/pascal]
-
- Experienced poster
- Posts: 106
- Joined: Sun Feb 17, 2002 2:00 am
- Location: Seoul, South Korea
- Contact:
First time, I tried to find amount of digit prime to use binary search...
( Using index of digit prime stored array )
But there are some problems...
( Binary search has some problem when an index value has no
digit prime, and so on... )
So I use 1,000,000 size integer array
and accumulate all digit prime, and range value to calculate
accumulated array.
P.S ) When I use Visual C++, I can only set 250,000 size integer
array. So I use gcc, and I can declare one million size int array.
( Using index of digit prime stored array )
But there are some problems...
( Binary search has some problem when an index value has no
digit prime, and so on... )
So I use 1,000,000 size integer array
and accumulate all digit prime, and range value to calculate
accumulated array.
P.S ) When I use Visual C++, I can only set 250,000 size integer
array. So I use gcc, and I can declare one million size int array.
-
- New poster
- Posts: 10
- Joined: Wed Dec 17, 2003 3:12 pm
- Location: Dhaka
- Contact:
10533 - Digit Primes
I can not understand what is the wrong with it.Have there any critical input?
//Cutted
//Cutted
Last edited by Kamanashish on Tue Jun 15, 2004 4:14 pm, edited 1 time in total.
Work hard.
Do you think this portion will work correct...if(a[l]=='1')c++;

13 is a prime number but not a digit prime. But in that protion you increase c by one (c++) if lower bound (l) is prime only. You must test whether (l) is a digit prime...

Test these:
Input:
Output:3
3 5
13 13
11 13
Goodluck...2
0
1
All the part of your code seem correct. Cut the code if you get AC.

-
- New poster
- Posts: 10
- Joined: Wed Dec 17, 2003 3:12 pm
- Location: Dhaka
- Contact:
10533. Digit prime . Why TLE .. Somebody help me plzzzzz
i m getting time limit exceed . plz help me i dont know y
[cpp]
#include<stdio.h>
bool checkprime(int num)
{
int flag=0;
if(num==2)
{
return true;
}
else
{
int count=2;
while(count<=(num/2)+1)
{
flag = 0;
if((num%count)==0)
{
flag=1;
break;
}
count=count+1;
}
if(flag!=1)
{
return true;
}
return false;
}
}
int main()
{
bool v;
int lim;
int i;
scanf("%d",&lim);
int upper,lower;
for(int k=0;k<lim;k++)
{
int total=0;
scanf("%d",&lower);
scanf("%d",&upper);
for(i=lower;i<upper;i++)
{
v=checkprime(i);
if(v==true)
{
int sum=0;
if(i>10)
{
int n=i;
int rem;
while(n>=10)
{
rem=n%10;
n=n/10;
sum=sum+rem;
}
sum=sum+n;
if(checkprime(sum))
{
total=total+1;
}
}
}
else
{
if(checkprime(i))
{
total=total+1;
}
}
}
printf("%d",total);
printf("\n");
}
return 0;
}
[/cpp]
[cpp]
#include<stdio.h>
bool checkprime(int num)
{
int flag=0;
if(num==2)
{
return true;
}
else
{
int count=2;
while(count<=(num/2)+1)
{
flag = 0;
if((num%count)==0)
{
flag=1;
break;
}
count=count+1;
}
if(flag!=1)
{
return true;
}
return false;
}
}
int main()
{
bool v;
int lim;
int i;
scanf("%d",&lim);
int upper,lower;
for(int k=0;k<lim;k++)
{
int total=0;
scanf("%d",&lower);
scanf("%d",&upper);
for(i=lower;i<upper;i++)
{
v=checkprime(i);
if(v==true)
{
int sum=0;
if(i>10)
{
int n=i;
int rem;
while(n>=10)
{
rem=n%10;
n=n/10;
sum=sum+rem;
}
sum=sum+n;
if(checkprime(sum))
{
total=total+1;
}
}
}
else
{
if(checkprime(i))
{
total=total+1;
}
}
}
printf("%d",total);
printf("\n");
}
return 0;
}
[/cpp]