Posted: Wed Jul 25, 2007 2:40 pm
Your code doesnt even pass the samples. Check the input format.
Code: Select all
#include<stdio.h>
#include<math.h>
#define NUMBER 1000000
#define SIZE 1000000
int prime[ SIZE ] ;
void generate_prime( void )
{
int i , j ,flag ;
prime[0] = 2 ; prime[1] = 3 ; prime[2] = 5 ; prime[3] = 7 ;
prime[4] = 11 ; prime[5] = 13 ; prime[6] = 17 ; prime[7] = 19 ;
int index = 7 ;
for(i=21 ; i < NUMBER ; i += 2)
{
flag = 1 ;
for(j = 0 ; i * prime[j] <= i ; j++)
{
if(i % prime[j] == 0)
{
flag = 0 ;
break ;
}
}
if( flag )
prime[++ index] = i ;
}
}
void generate_almost_prime( long long start , long long end )
{
int i=0 , flag = 0 ;
int sum_start , sum_end ;
if( start != end)
{
sum_start = sum_end = 0;
for(i= 0 ; prime[i] * prime[i] <= start ; i ++)
sum_start += (int)( log10( start ) / log10( prime[i] ) ) - 1;
for(i= 0 ; prime[i] * prime[i] <= end ; i ++)
sum_end += (int)( log10( end ) / log10( prime[i] ) ) - 1;
printf("%d\n" , sum_end - sum_start ) ;
}
else
{
for(i= 0 ; prime[i] * prime[i] <= start ; i ++)
if(start % prime[i])
{
flag++;
if(flag == 2 )
break ;
}
if(flag == 1 )
printf("1\n");
else
printf("0\n");
}
}
int main()
{
int cas_no ,i ;
long long start , end ;
generate_prime() ;
scanf("%d",&cas_no);
for(i= 1 ; i <= cas_no ; i++)
{
scanf("%lld %lld",&start ,&end);
generate_almost_prime( start , end );
}
return 0;
}
Code: Select all
Code removed after getting AC
Code: Select all
Code removed after AC
RuanZheng wrote:I got the same problem with you but now solved. Just don't use log.sharklu2000 wrote:I first calculate all the prime numbers between 1 and 10<sup>6</sup>
then for every prime numbers below low and high, I add all the log below and high to the base of the prime number as a and b. Then b-a is the answer.
But it is really inefficient, can anyone tell me some efficient algorithm.
I will be very grateful.
Just calculate the lower bound and upper bound by brute force![]()
I guess the log costs too much time.
I used log and got accepted in 0.408.zobayer wrote:for p and n, there are floor(log(n) / log(p)) powers of p from p to n. this formula can be used as well.
however, in implementation, log and log10 turn out to be too slow, even manual loop takes much much less time. also, one more thing is to note, although it doesn't have any effect on my machine, uva gives WA for log and AC for log10.
so be careful if you like "more mathematical accent" in this problem.
So i think it is efficient enough to get acc below 1second.sharklu2000 wrote:I first calculate all the prime numbers between 1 and 10<sup>6</sup>
then for every prime numbers below low and high, I add all the log below and high to the base of the prime number as a and b. Then b-a is the answer.
But it is really inefficient, can anyone tell me some efficient algorithm.
I will be very grateful.