10200 - Prime Time

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

Moderator: Board moderators

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo »

Read the previous posts.. (about EPS)
ankit.arora
New poster
Posts: 11
Joined: Tue May 22, 2007 10:09 pm
Location: India

Post by ankit.arora »

I m gettin WA but i have checked my output for all inputs provided in this thread!..... Please help me!!!

Code: Select all

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    unsigned long long i,j,count,t;
    int set;
    int arr[100001]={0};
    arr[0]=1;
    arr[1]=1;
    for(i=2;i<100001;i++)
    {
           if(arr[i]==0)
           {
                   j=2;     
                   while(i*j<100001)
                   {
                           arr[i*j]=1;
                           j++;
                   }
           }
    }
    
    unsigned long long num[10001];
    count=0;
    for(i=0;i<=10000;i++)
    {
           t=(i*i)+i+41;
           if(t<=100000)
           {
                   if(arr[t]==0)     
                   count++;
                   num[i]=count;
           }
           
           else
           {
                   set=0;
                   for(j=3;j<=sqrt((double)t+0.5);j+=2)
                   {
                          if(t%j==0)
                          {
                                 set=1;
                                 break;
                          }
                   }
                   
                   if(set==0)
                   count++;
                   num[i]=count;
           }
    }
    
    int a,b;
    double per;

    while(cin>>a>>b)
    {
           if(a==0)         
           per=100*((double)(num[b])/(double)(b-a+1));
           else
           per=100*((double)(num[b]-num[a-1])/(double)(b-a+1));
           
           t=(long long)(per*100);
           if((per*100)-t>0.5 ||((per*100)-t==0.5 && t%2!=0))
           t++;
           
           if(t%100==0)
           cout<<(double)t/(double)100<<".00\n";
           else if(t%10==0)
           cout<<(double)t/(double)100<<"0\n";
           else
           cout<<(double)t/(double)100<<"\n";
    }         
}                                                                         
Trying to learn!!!
mmonish
Experienced poster
Posts: 109
Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST

Post by mmonish »

>>ankit.arora
did u check the sample inputs. for the following input 1423 2222 ur output is 44.12 but the correct output is 44.13.
use double calculation and output like this
printf(".2lf\n" , res);
and don't forget about EPS.

Hope this helps.
ankit.arora
New poster
Posts: 11
Joined: Tue May 22, 2007 10:09 pm
Location: India

Post by ankit.arora »

mmonish wrote:>>ankit.arora
did u check the sample inputs. for the following input 1423 2222 ur output is 44.12 but the correct output is 44.13.
use double calculation and output like this
printf(".2lf\n" , res);
and don't forget about EPS.

Hope this helps.
Actually for this input the actual floating answer is 44.125(as per my program) which when rounded to two decimal places produces 44.12, this is the rule which i use in all my rounding calculations ..... can you please explain me that why it should be 44.13 instead.

Thanks a lot for helping!!!!
Trying to learn!!!
shaiful
New poster
Posts: 1
Joined: Mon Jun 18, 2007 9:11 am

Post by shaiful »

I got 20 WA in this problem
Can any one help me on my code?

Code: Select all

#include<stdio.h>
#include<math.h>


bool prime(long long n)
{
       long long i,sq;
       sq=(long long)sqrt((double)n);

       for(i=2;i<=sq;i++)
       {
               if(n%i==0) return false;
       }
       return true;
}

int main()
{

       long long dif,count,temp,t,i,j,n1,n2,k;

       bool p[10005];

       for(j=0;j<10005;j++) p[j]=false;
       for(j=0;j<10005;j++)
       {
               temp=j;
               k=temp*temp+temp+41;
               if(prime(k)==true)
                       p[j]=true;
       }
       double per;

       while(scanf("%lld %lld",&n1,&n2)==2)
       {
               count=0;
               if(n1>n2)
               {
                       t=n1;
                       n1=n2;
                       n2=t;
               }

               for(i=n1;i<=n2;i++)
               {
                       if(p[i]==true) count++;
               }
               dif=n2-n1+1;
               per=(double)((double)count/(double)dif)*100.0;

               printf("%.2lf\n",per);
       }
       return 0;
}

emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind »

This problem can be solved without using float or double..
That will be more safe.
WingletE
New poster
Posts: 35
Joined: Sun Aug 13, 2006 1:34 pm
Location: Taipei, Taiwan
Contact:

Post by WingletE »

I've passed all the test case above but still got WA, I use long double.
What could the problem be?

This problem can be solved without using float or double..
then how?
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

You could round and output a fraction p/q, for example with this code:

Code: Select all

int t = (p * 200 + q) / (2 * q);
printf("%d.%.2d\n", t / 100, t % 100);
ishtiaq ahmed
Learning poster
Posts: 53
Joined: Sat Jul 29, 2006 7:33 am
Location: (CSE,DU), Dhaka,Bangladesh

WA(10200)[precision error]

Post by ishtiaq ahmed »

Code: Select all

cut after AC
Last edited by ishtiaq ahmed on Sun Aug 19, 2007 10:35 pm, edited 3 times in total.
No venture no gain

with best regards
------------------------
ishtiaq ahmed
WingletE
New poster
Posts: 35
Joined: Sun Aug 13, 2006 1:34 pm
Location: Taipei, Taiwan
Contact:

Post by WingletE »

I've run your program. Your answer is not right.

Code: Select all

9999 10000
9998 10000
9998 9999
9996 10000
500 600

Code: Select all

0.00
0.00
0.00
20.00
56.44
ishtiaq ahmed
Learning poster
Posts: 53
Joined: Sat Jul 29, 2006 7:33 am
Location: (CSE,DU), Dhaka,Bangladesh

post reply of 10220 [prime time]

Post by ishtiaq ahmed »

i rewrite my code as follows but stll facing WA.

Code: Select all

cut after AC
waiting for your reply
No venture no gain

with best regards
------------------------
ishtiaq ahmed
Fuad Hassan EWU
New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

....

Post by Fuad Hassan EWU »

WA!!! i can't find the bug. i used very simple and i think simple logic coz this seems to be eay problem. but where is the bug? I matched the provided critical input output. plz help...

Code: Select all

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define N 120000000

using namespace std;


char a[N/2+10];

void genprime()
{

	long i,j;

	for(i=3;i*i<=N;i+=2)
	{
		if(a[i/2]==1)
			continue;
		for(j=(i*i)/2;j<=N/2;j+=i)
		{
			a[j]=1;
		}
	}

	

}




int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	genprime();
	double prmcnt;
	double result;
	long i,first,b, f;
	while(cin>>first>>b)
	{
	
	prmcnt=0;
	

		for(i=first;i<=b;i++)
		{
		

			f=(i*i)+i+41;

			if(f==2)
				prmcnt++;

			else if(f%2==0&&f!=2)
				continue;

			else
			{
				if(a[f/2]==0)
					prmcnt++;
				else
					continue;
			}
		}


		result=(100*(double)prmcnt)/(double)((b-first)+1);
		printf("%.2lf\n",result);


	}
	
	
	return 0;
}

Eagle er moto daana meley urbo
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Your code is taking too much memory.
Ami ekhono shopno dekhi...
HomePage
Fuad Hassan EWU
New poster
Posts: 38
Joined: Tue Jul 17, 2007 3:21 pm
Location: East West University

...

Post by Fuad Hassan EWU »

jan vai i used the size 100010041, because 10000^10000+10000+41=100010041. and to check whether it is prime or not i need to run a the outer of my seive method up to sqrt(100010041). am i right? plz make me clear about this issue. thanks.
Eagle er moto daana meley urbo
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

int (sqrt(100010041)) = 10000. There are less than 2460 primes in [2,10000]. So, in worst case you have to divide 2460 times to check whether a number is prime or not. And this isn't too much. It uses less memory, but takes a little more time. So, you can generate primes upto 10000, and make your primarity testing. Hope it helps.
Ami ekhono shopno dekhi...
HomePage
Post Reply

Return to “Volume 102 (10200-10299)”