You are not logged in. Please login at www.codechef.com to post your questions!

×

RECIPE - Editorial

0
2

PROBLEM LINKS

Practice
Contest

DIFFICULTY

EASY

EXPLANATION

The problem here is divide all numbers by some constant so that the divisions have no remainder. We produce the smallest result by dividing by a number that is as large as possible, that is the greatest common divisor. The greatest common divisor can be computed efficiently by Euclid's algorithm, but in this case it was fast enough to simply check all numbers from 1 to 1000 for divisibility.

SETTER'S SOLUTION

Can be found here.

TESTER'S SOLUTION

Can be found here.

This question is marked "community wiki".

asked 27 Nov '12, 16:36

admin's gravatar image

0★admin ♦♦
19.7k350498541
accept rate: 35%

edited 27 Nov '12, 16:45


link

answered 28 Dec '12, 12:12

gc_nit's gravatar image

5★gc_nit
4514
accept rate: 0%

http://garakchy.blogspot.com/2013/12/codechef-recipe-cutting-recipe-problem.html

note on my code: the gcd block is taken from wiki url i have shown above. and main block is my c++ code. i initialised arr of 50 ingredients and saved ingredients in it. and then i have taken cmmn - the greatest common divisor for ingredients in the array. if cmmn==1 then the array has no gcd, so i just printed out the arr itself. if cmmn is equal to ingredients of arr then yes==1, which means that all ingredients in the arr are same numbers, for ex 4 as given in the codechef sample output, if so i printed out 1 for every ingredient of arr. and if there exist gcd(here cmmn) other than 1 and ingredints itself, then print out arr[i]/cmmn. that is it.

NOTE: i still have difficulty in understanding the binary gcd algo. can anyone pls post the easier way to learn binary gcd algo. tnx

link

answered 22 Dec '13, 09:19

garakchy's gravatar image

1★garakchy
1.1k163048
accept rate: 1%

Can anyone tell me what is wrong with this code? ideone

link

answered 29 Jul '14, 09:23

tech_boy's gravatar image

2★tech_boy
1.2k41931
accept rate: 7%

1

it's not necessary that the no. with minimum value will always be gcd of all...

try this : 8 16 20

ur output: 8 16 20

correct answer : 2 4 5

(29 Jul '14, 11:17) grayhathacker5★

thank you!

(29 Jul '14, 15:30) tech_boy2★

Can anyone please tell me :- I have run my code several times in my laptop with several cases and does not encountered any problem.But after submitting it I got wrong answer... Please Check my code and please inform me about my mistake and the particular case .. Waiting for your reply..... http://www.codechef.com/viewsolution/7242074

link

answered 20 Jun '15, 18:01

kislaya123's gravatar image

2★kislaya123
111
accept rate: 0%

edited 20 Jun '15, 18:03

@kislaya Why did u write so lengthy code.

U can exploit the fact that GCD cannot be greater than the minimum element of array.

link

answered 20 Jun '15, 19:43

shivam9753's gravatar image

4★shivam9753
696112
accept rate: 17%

showing runtime error SIGFPE in this problem .... please can someone tell my mistake in this code....

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

link

answered 27 Jul '15, 22:53

deeksha_garg's gravatar image

3★deeksha_garg
254
accept rate: 0%

If someone can help me figuring out what the problem is https://www.codechef.com/viewsolution/9210591

link

answered 23 Jan '16, 01:56

dravit's gravatar image

2★dravit
1
accept rate: 0%

what is wrong with my code?? my ans is correct but still it is showing wrong answer

include <stdio.h>

int main(void) {

int n,i=0,j,min=0,count=0,m,k,gcd;

scanf("%d",&n);

int a[n][49],b[50];

while(i<n)
{
    gcd=0;
    scanf("%d",&m);
    j=0;
    a[i][j]=m;

    scanf("%d",&b[j]);
    min=b[j];
    j++;


    while(j<m)
    {
        scanf("%d",&b[j]);
        if(min>b[j])
        min=b[j];
        j++;
    }

    while(min>1)
    {
        count=0;
        j=0;
        while(j<m)
        {
            if((b[j]%min)==0)
            count++;
            j++;
        }

        if(count==m)
        {
            gcd=min;
            break;
        }
        min--;

    }

    j=1;
    k=0;
    if(gcd!=0)
    {
        while(j<=m)
        {
            a[i][j]=b[k]/gcd;
            j++;
            k++;
        }
    }
    else
    {
        while(j<=m)
        {
            a[i][j]=b[k];
            j++;
            k++;
        }
    }
    i++;

    count=0;
}

for(i=0;i<n;i++)
{
    for(j=1;j<=a[i][0];j++)
    {
        if(j==a[i][0])
        printf("%d",a[i][j]);
        else
        printf("%d ",a[i][j]);
    }
    if(i!=(n-1))
    printf("\n");
}


return(0);

}

link

answered 14 Aug '16, 20:42

shreya_0296's gravatar image

0★shreya_0296
11
accept rate: 0%

my code is giving correct answers but in uploadinong your compiler is saying wrong plasee check it or tell me the example which is giving wrong answer

include <stdio.h>

include <stdlib.h>

void input(void); int main() { int n,i; printf("enter the number the test case\n"); scanf("%d",&n); for (i=0;i<n;i++) { input(); }

return 0;

}

void input(void) { int i,n,b,a[50]; int j=0,count=0 ; printf("enter the number of items between 2 to 50 :"); scanf("%d",&n); printf("enter the numbers "); for(i=0;i<n;i++) {="" scanf("%d",&a[i]);="" if(i="=0)" {="" b="a[0];" }="" if(i="">0) { if(b>a[i]) b=a[i]; } } for(i=0;i<n;i++) {="" while(1)="" {="" j++;="" if(b*j<="a[i])" {="" if(b*j="=a[i])" break;="" }="" if(b*j="">a[i]) { for(i=0;i<n;i++) printf("%d ",a[i]); count ++; break; } } if (count==1) break;

}
         if(count==0)
        {
            for(i=0;i<n;i++)
            printf("%d   ",a[i]/b);
        }

}

link

answered 04 Oct '16, 08:52

saurabh2104's gravatar image

1★saurabh2104
11
accept rate: 0%

my code is giving correct answers but when i am uploading in your compiler its saying wrong answer please check it or tell me the example which is giving wrong answer

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

link

answered 15 Nov '16, 11:43

ab1494's gravatar image

2★ab1494
1
accept rate: 0%

what wrong with my code???

include <stdio.h>

include <stdlib.h>

int minimum(int [],int);

int minimum(int arr[],int N){

    int i=0,min_val=arr[0];
    for(i=1;i<N-1;i++){
        if(min_val > arr[i])
            min_val=arr[i];
    }
        return min_val;

}

int main() { int T;

scanf("%d",&T);

while(T){
    int N,flag;
    scanf("%d",&N);
    if(N>=2 && N<=50){
    int arr[N],min,tem=N;
    int i=0;
    while(tem){
        scanf("%d",&arr[i]);
        tem--;  i++;
    }

    min=minimum(arr,N);
           tem=N;
    while(tem){
        if(((arr[tem-1])%min)==0) flag=1;
        else {flag=0; break;}
    tem--;
    }
    i=0;
    if(flag==1){
        while(N){
            printf("%d ",arr[i]/min);
         i++;N--;}
    }
    else{
            while(N){
                printf("%d ",arr[i]);
                i++; N--; }
    } }

T--;

printf("\n");

} return 0; }

link

answered 19 Mar '17, 10:41

alphazeta0's gravatar image

0★alphazeta0
1
accept rate: 0%

@alphazeta0

Your code fails for this test case-

Input-
    1
    3 15 35 55

Output-

15 35 55

Expected Output-

3 7 11

Add a check that if minimum element isn't gcd, check if any factor of it is GCD of all. (Eg, here 15 isn't GCD, but its factor 5 is GCD of all elements).

link

answered 19 Mar '17, 11:48

vijju123's gravatar image

4★vijju123 ♦♦
15.2k11859
accept rate: 18%

edited 19 Mar '17, 11:48

what's wrong with my code?it's running correctly on my compiler but giving prompt of wrong answer here.plz check . https://www.codechef.com/viewsolution/13565846

link

answered 13 May '17, 12:45

japper's gravatar image

1★japper
1
accept rate: 0%

Hi, can someone please help me find the test case where my code fails for this problem? Here is my answer : https://www.codechef.com/viewsolution/15469463

link

answered 19 Sep '17, 09:06

akwa_blue's gravatar image

4★akwa_blue
1
accept rate: 0%

My code is running correctly but don't know why it's showing wrong answer. Anyone please help? https://www.codechef.com/viewsolution/15887411

link

answered 17 Oct '17, 23:00

ujjwalmishra21's gravatar image

2★ujjwalmishra21
1
accept rate: 0%

Can someone tell me whats wrong in my code..!!!

` code here

import java.util.Scanner;

public class RECIPE_2 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    while(T>0){
        int ingredients = sc.nextInt();
        if(ingredients == 0) return;
        if(ingredients == 1){
            System.out.println(sc.next());
            return;
        }
        int[]arr = new int[ingredients];
        for(int i=0;i<ingredients;i++){
            arr[i] = sc.nextInt();
        }
        //answer
        int result = gcd(arr[0], arr[1]);
        for(int i=2;i<ingredients;i++){
            result = gcd(arr[i], result);
        }
        printArray(arr, result);

        T--;
    }

}

public static int gcd(int a, int b){
    if(a==0){
        return b;
    }else{
        return gcd(b%a, a);
    }
}

public static void printArray(int[] arr, int divNo){
    for(int i=0;i<arr.length; i++){
        System.out.print(arr[i]/divNo);
        if(i!=arr.length-1)
        System.out.print(" ");
    }
}

} ` I solved this code in another way but still it is failing. anyone what is the case when it got failed.?

link

answered 20 Oct '17, 23:53

vikalp91's gravatar image

1★vikalp91
1
accept rate: 0%

Why i am getting WA(wrong answer) ??

I am getting right answers for all the TEST cases, incuding (8,16,20) ans=(2,4,5).Given in comment. Can anyone help me.

My solution link https://www.codechef.com/viewsolution/16701304

@grayhathacker

link

answered 30 Dec '17, 13:34

premang1's gravatar image

1★premang1
01
accept rate: 0%

include<stdio.h>

int main() { int t,n,i,j,k,m,a[1000],s[1000],gcd; scanf("%d",&t); if(t<=100) { for(i=1;i<=t;i++) { scanf("%d",&n); if(n>=2&&n<=50) { for(j=0;j<n;j++) { scanf("%d",&a[j]); } } gcd=a[0]; for(k=1;k<n;k++) { if(a[k]%gcd==0) { k++; } else{ gcd=a[k]%gcd; } } for(m=0;m<n;m++) { s[m]=a[m]/gcd;
printf("%d \n",s[m]); }

}

} return 0; }

please tell me what is wrong with my code it's working on my system but when I submit it show me wrong answer

link
This answer is marked "community wiki".

answered 07 Aug '18, 02:03

bittu_19007's gravatar image

1★bittu_19007
1
accept rate: 0%

include<stdio.h>

int main() { int t,n,i,j,k,m,a[1000],s[1000],gcd; scanf("%d",&t); if(t<=100) { for(i=1;i<=t;i++) { scanf("%d",&n); if(n>=2&&n<=50) { for(j=0;j<n;j++) { scanf("%d",&a[j]); } } gcd=a[0]; for(k=1;k<n;k++) { if(a[k]%gcd==0) { k++; } else{ gcd=a[k]%gcd; } } for(m=0;m<n;m++) { s[m]=a[m]/gcd;
printf("%d \n",s[m]); }

}

} return 0; }

please tell me what is wrong with my code it's working on my system but when I submit it show me wrong answer

link
This answer is marked "community wiki".

answered 07 Aug '18, 02:03

bittu_19007's gravatar image

1★bittu_19007
1
accept rate: 0%

include<iostream>

using namespace std; int main(){ int n; cin>>n; for(int i=0;i<n;i++){ int="" q,c="0;" cin="">>q; int a[q]; for(int i=0;i<q;i++) {="" cin="">>a[i]; } int m=a[i]; for(int i=0;i<q;i++) {="" if(a[i]<m)="" {="" m="a[i];" }="" }for(int="" j="m;j">0;j--) {c=0; for(int i=0;i<q;i++) { if(a[i]%j==0) { c++; } if(c==q) { for(int i=0;i<q;i++) cout<<a[i]/j<<" "; }

}
if(c==q){
    cout<<endl;
    break;
}
}

} } what is wrong in this my compiler is giving right answer?

link

answered 19 Oct '18, 20:14

abhi_426's gravatar image

0★abhi_426
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,482
×3,705
×28
×10

question asked: 27 Nov '12, 16:36

question was seen: 9,577 times

last updated: 19 Oct '18, 20:14