Re: 10200 PRIME TIME
Posted: Fri Jun 27, 2008 7:51 am
Search the board first. Don't open a new thread if there is one already.
mf wrote:If you do preprocessing a bit cleverly, you'll be able to solve each test case in just O(1) time.
(Hint: if you know that n^2+n+41 gives A primes for interval [0,a], and B primes for [0,b], what can you tell about the interval [a+1,b]?)
Code: Select all
VI p(10001, 1);
for (int i = 0; i < 40; i++) p[i] = i + 1;
p[40] = 40;
for (int i = 41; i < 10001; i++)
p[i] = p[i - 1] + prime(formula(i));
int a, b;
while (cin >> a >> b)
{
if (a == 0) cout << (double(p[b])/double(b + 1))*100 << endl;
else
{
//calculate the result
}
}
Code: Select all
VI p(10001, 1);
p[40] = 0;
for (int i = 41; i < 10001; i++)
{
p[i] = prime(formula(i));
}
int a, b;
while (cin >> a >> b)
{
double pr = 0;
for (int i = a; i <= b; i++) pr += p[i];
//calculate the result
}
Code: Select all
Removed because Accepted!
yes i tried but failed!newton have you tried without "eps"?
some people had WA because of this...
Code: Select all
printf("%.2Lf\n",(cnt/(b - a + 1)*100.00)+eps);
Code: Select all
printf("%.2lf\n", ((cnt*100) / (b - a + 1)) + eps);
modifie toprintf("%.2Lf\n",(cnt/(b - a + 1)*100.00)+eps);
that was not the bug i think.printf("%.2lf\n", ((cnt*100) / (b - a + 1)) + eps);
Code: Select all
printf("%.2Lf\n",(cnt/(b - a + 1)*100.00)+eps);
Code: Select all
printf("%.2lf\n",((cnt*100.00)/(b - a + 1))+eps);
Code: Select all
printf("%.2f\n",((cnt*100.00)/(b - a + 1))+eps);
Code: Select all
printf("%.2lf\n",((cnt*100.00)/(b - a + 1)));
Code: Select all
printf("%.2f\n",((cnt*100.00)/(b - a + 1)));
Joseph Kurniawan wrote:ICode: Select all
suggest to use "semi-hardcode".
You are a very nice man. my idea is also like so.very nice.![]()
![]()
![]()
![]()
![]()
![]()
Code: Select all
deleted
Code: Select all
My Algorithm was
1. make an Boolean array of 10001
2. check isPrime( i*i + i + 41) for i-> 0 to MAX
3. set array[i] true if isPrime return true;
4. scan inputs and get your answer.
Code: Select all
#include<stdio.h>
#include<math.h>
long x,y,count,p,store[10009],temp;
double res;
bool isPrime(long x)
{
if(x == 2) return true;
if(x == 1) return false;
if(x % 2 == 0) return false;
for(int i = 3; i*i<=x; i+=2){
if(x % i == 0) return false;
}
return true;
}
int main()
{
count=0;
for(int i=0;i<=10000;i++)
{
p=(i*i)+i+41;
if(isPrime(p))
count++;
store[i]=count;
}
while(scanf("%ld%ld",&x,&y)==2)
{
if(x>y)
{
temp=x;
x=y;
y=temp;
}
if(x==0 && y==0)
{
printf("100.00\n");
continue;
}
if(x==y)
{
if(store[x]==store[x-1])
{
res=((store[y]-store[x])/(y-x+1.0))*100.00;
printf("%.2lf\n",res);
continue;
}
}
if(store[x]!=store[x-1])
{
res=((store[y]-store[x]+1.0)/(y-x+1.0))*100.00;
printf("%.2lf\n",res);
continue;
}
else
{
res=((store[y]-store[x])/(y-x+1.0))*100.00;
printf("%.2lf\n",res);
continue;
}
}
return 0;
}
Code: Select all
while(scanf("%d %d",&a,&b) == 2){
cnt = 0.0;
for(i = a; i<=b; i++){
if(store[i])
count = count + 1;
}
printf("%.2lf\n",(count*100.00/(b - a + 1)) + eps);
}