POTATOES - Editorial

why this code is giving wrong answer.Working fine on my system.

#include<stdio.h>

int main(void)
{
int x,y,z;
int n;
int i,j;
int set=0;
int *ans;
int temp,k;

  scanf("%d",&n);
  ans = (int*)malloc(n*sizeof(int));
  
  for(i=0;i<n;i++)
  {
                  scanf("%d %d",&x,&y);
                  temp = x + y;
                  for(j=1; ; j++)
                  {
                           temp = temp + 1;
                           //printf("%d\n",temp);
                           for(k=2;k<temp/2;k++)
                           {
                                                if(temp%k==0)
                                                {
                                                             set=1;
                                                             break;
                                                }
                           }
                           if(set==1) 
                           {set=0; continue; }
                           if(set==0)
                            {         *(ans+i) = j;
                                      //printf("%d\n",*(ans+i));
                                      break;
                            }                     
                  }
  }
  
  for(i=0;i<n;i++)
  {
                  printf("%d\n",*(ans+i));
  }
  
  //getch();
  return 0;

}

I am getting NZEC error.Can anyone help me code here

#include<bits/stdc++.h>
using namespace std;

bool isPrime(int sum)
{
int count=1;
for(int i=2;i<sum/2;i++)
{
if(sum%i==0)
return false;
}
return true;
}

int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int f, s;
cin>>f>>s;
int sum=f+s+1;
while(!isPrime(sum))
sum++;

cout << sum-f-s<<endl;
}

}

why is it not working??? i am getting wrong answer

One of the simple solution to this problem is pre-computation.

Here is my code (in C)-

Link-
https://www.codechef.com/viewsolution/12622044

Commends are also included. :slight_smile:

(for those too lazy to copy paste the link-)

#include <stdio.h>
 
int main(void) {
// your code goes here
int t,x,y,i=0,j,count=0,flag;
int arr[700]={0}; /First 500 prime numbers should be more than enough, given the constraints.
however, we i took 700 just to be sure.
/
arr[0]=2;
count=1;//count is number of prime numbers in array.
for(i=3;i<7000 && count<700;i+=2)//precomputing prime numbers till 7000.
{
for(j=2;j<i;j++)
{
flag=0;
if(i%j==0)
{
flag=1;
break;
}
}
if (flag==0)
{
arr[count]=i;
count++;
}

}
scanf ("%d",&t); //inputting T
for(i=0;i<t;i++)
{
    scanf ("%d %d",&x,&y);
    int sum=x+y;
    for (j=0; x+y>=arr[j] && j<700;j++); /*gives the smallest prime number greater than x+y. 
    This prime number is stored in arr[j]. Please take a moment to note the significance of '=' sign
    in x+y>=arr[j]*/
    
    int z= arr[j]-sum;
    printf ("%d\n",z);
    
}
return 0;

}

1 Like

Can anyone what wrong in this code
I m getting wrong answer
#include<stdio.h>
#include<math.h>
int prime(int a );
int main(){
long int n ,i ,t,x,y,z,q,g;
scanf("%ld" , &t);
while(t–){
n=0;
scanf("%ld%ld" , &x , &y);
q=x+y;
if(q==0){
printf(“not possible”);
}else
{
for(i=1; i<=41; i++){
z=q+i;
g=prime(z);
if(g){
n=i;
break;
}

    }
    z=0;
    printf("%ld" , n);}
}

}
int prime(int a ){
int i ,m=0 ;
for(i=2; i<=sqrt(a); i++){
if(a%i==0){
m=m+1;}

}
if(m>=1){
    return 0;
}
else
return 1;

}

in (x+y+i) i will never exceed 40 since x+y <= 2000.

could any one explain me this…?

1 Like

https://www.codechef.com/viewsolution/13385218

can anyone tell me whats wrong in the above code ?
I am geeting wrong answer even though I’ve checked every thing (i think),I’ve applied fermat’s little theorem to check if the number is prime or not.

just find sum of given two integers as first and second harvest and find just next prime number , then answer will be that prime number - sum(sum of first and second harvest).

Your program fails to find 2 and 3 as prime numbers. If x=1 and y=1 your program outputs 3, but the correct answer is 1. You can correct it by making value of test as true before entering loop and no need of else part within the loop.

1 Like

there were accepted answers that took more time than this… but this showed a TLE. please help.

thanks jawad, it solved the problem

why am i gettin wrong answer for this code?

I’ve tried most of the test cases with this code, it works fine in my local.

#include<stdio.h>
void main()
{
int t,a,b,c[1000],i=0,t1;
scanf("%d",&t);
t1=t;
while(t>0) // t test cases
{
int ct=0,p=0,j; //3 feild count
scanf("%d",&a);
scanf("%d",&b);
a+=b;
while(p!=2)
{
p=0;
ct++;
a++;
for( j=2;j<(a/2);j++)
{
if(a%j==0)
{
p=1;
}
}
if(p!=1)
{
c[i]=ct;
p=2;
i++;
}
}
t–;
}
for( i=0;i<t1;i++)
{
printf("%d\n",c[i]);
}
}

Your program fails for the input x=1326, y=1, the correct answer is 34(since 1361 is the next prime after 1327). Your program finds the answers only up to 30, make the condition of the loop in i as i<35

thanks jawad

http://www.codechef.com/viewsolution/3947463

Please check out this solution. Works perfectly fine for all the test cases, still I’m getting a wrong answer!

just print new line after each output. i.e:
printf("%d\n",A[i]-(x+y));

1 Like

@achaitanyasai Thank you so much! It did run now!
It would be a great help if you could check out my question on BNGAME as well.
Here’s the link to it-
http://discuss.codechef.com/questions/43413/bngame-problem
Thanks a lot! :slight_smile:

1 Like

I din’t understand the point that “i never exceeds 40” can explain it again?

May be it means that they are no two adjacent prime numbers till 2000 that differ by more than 40, for instance 2003 is the nearest prime to 2000.

1 Like

The potatoes in the last farm will never exceed 40 as the sum of the potatoes in the previous two farms will always be less than 2000. And there lies a prime number in every 40 numbers before 2000.

1 Like