COCONUT - Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Practice

Setter: Daanish Mahajan
Tester: Istvan Nagy
Editorialist: Taranpreet Singh

DIFFICULTY

Cakewalk

PREREQUISITES

None

PROBLEM

Chefland has 2 different types of coconut, type A and type B. Type A contains only x_a milliliters of coconut water and type B contains only x_b grams of coconut pulp. Chef’s nutritionist has advised him to consume X_a milliliters of coconut water and X_b grams of coconut pulp every week in the summer. Find the total number of coconuts (type A + type B) that the Chef should buy each week to keep himself active in the hot weather.

QUICK EXPLANATION

Chef must eat X_a/x_a coconuts of type A and X_b/x_b coconuts of type B.

EXPLANATION

Chef gets x_a milliliters of coconut water from coconuts of type A, and he needs X_a milliliters of coconut water, then how many coconuts of type A does the chef need?

Since 1 coconut provides x_a milliliters of coconut water, P coconuts would provide P*x_a milliliters. Chef need P such that P*x_a = X_a, which implies P = X_a / x_a. Hence, Chef consume P coconuts of type A.

By similar reasoning, we can see that Chef consumes Q = X_b/x_b coconuts of type B.

This way, the total number of coconuts consumed is P+Q = X_a/x_a + X_b/x_b.

Following C++ code depicts the above idea

    int T;
    cin>>T;
    while(T-->0){
        int Xa, xa, Xb, xb;
        cin>>xa>>xb>>Xa>>Xb;
        int count = Xa/xa + Xb/xb;
        cout<<count<<"\n";
    }

TIME COMPLEXITY

The time complexity is O(1) per test case.

SOLUTIONS

Setter's Solution
#include<bits/stdc++.h>
# define pb push_back 
#define pii pair<int, int>
#define mp make_pair
# define ll long long int

using namespace std;
  
int main()
{   
    int t; cin >> t;
    int a, b, c, d;
    while(t--){ 
        cin >> a >> b >> c >> d;
        int ans = c / a + d / b;
        cout << ans << endl;       
    }
} 
Tester's Solution
#include <iostream>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <random>

#ifdef HOME
    #include <windows.h>
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

template<class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; }

using namespace std;


long long readInt(long long l, long long r, char endd) {
    long long x = 0;
    int cnt = 0;
    int fi = -1;
    bool is_neg = false;
    while (true) {
	    char g = getchar();
	    if (g == '-') {
		    assert(fi == -1);
		    is_neg = true;
		    continue;
	    }
	    if ('0' <= g && g <= '9') {
		    x *= 10;
		    x += g - '0';
		    if (cnt == 0) {
			    fi = g - '0';
		    }
		    cnt++;
		    assert(fi != 0 || cnt == 1);
		    assert(fi != 0 || is_neg == false);

		    assert(!(cnt > 19 || (cnt == 19 && fi > 1)));
	    }
	    else if (g == endd) {
		    assert(cnt > 0);
		    if (is_neg) {
			    x = -x;
		    }
		    assert(l <= x && x <= r);
		    return x;
	    }
	    else {
		    //assert(false);
	    }
    }
}

string readString(int l, int r, char endd) {
    string ret = "";
    int cnt = 0;
    while (true) {
	    char g = getchar();
	    assert(g != -1);
	    if (g == endd) {
		    break;
	    }
	    cnt++;
	    ret += g;
    }
    assert(l <= cnt && cnt <= r);
    return ret;
}
long long readIntSp(long long l, long long r) {
    return readInt(l, r, ' ');
}
long long readIntLn(long long l, long long r) {
    return readInt(l, r, '\n');
}
string readStringLn(int l, int r) {
    return readString(l, r, '\n');
}
string readStringSp(int l, int r) {
    return readString(l, r, ' ');
}

int main(int argc, char** argv) 
{
#ifdef HOME
    if(IsDebuggerPresent())
    {
	    freopen("../in.txt", "rb", stdin);
	    freopen("../out.txt", "wb", stdout);
    }
#endif
    int T = readIntLn(1, 15'000);
    forn(tc, T)
    {
	    int xa = readIntSp(100, 200);
	    int xb = readIntSp(400, 500);

	    int Xa = readIntSp(1'000, 1'200);
	    int Xb = readIntLn(1'000, 1'500);
	    assert(Xa % xa == 0);
	    assert(Xb % xb == 0);
	    printf("%d\n", Xa / xa + Xb / xb);
    }
    assert(getchar() == -1);
    return 0;
}
Editorialist's Solution
import java.util.*;
import java.io.*;
class COCONUT{
    //SOLUTION BEGIN
    void pre() throws Exception{}
    void solve(int TC) throws Exception{
        int xa = ni(), xb = ni(), Xa = ni(), Xb = ni();
        pn(Xa/xa + Xb/xb);
    }
    //SOLUTION END
    void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
    static boolean multipleTC = true;
    FastReader in;PrintWriter out;
    void run() throws Exception{
        in = new FastReader();
        out = new PrintWriter(System.out);
        //Solution Credits: Taranpreet Singh
        int T = (multipleTC)?ni():1;
        pre();for(int t = 1; t<= T; t++)solve(t);
        out.flush();
        out.close();
    }
    public static void main(String[] args) throws Exception{
        new COCONUT().run();
    }
    int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
    void p(Object o){out.print(o);}
    void pn(Object o){out.println(o);}
    void pni(Object o){out.println(o);out.flush();}
    String n()throws Exception{return in.next();}
    String nln()throws Exception{return in.nextLine();}
    int ni()throws Exception{return Integer.parseInt(in.next());}
    long nl()throws Exception{return Long.parseLong(in.next());}
    double nd()throws Exception{return Double.parseDouble(in.next());}

    class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws Exception{
            br = new BufferedReader(new FileReader(s));
        }

        String next() throws Exception{
            while (st == null || !st.hasMoreElements()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch (IOException  e){
                    throw new Exception(e.toString());
                }
            }
            return st.nextToken();
        }

        String nextLine() throws Exception{
            String str = "";
            try{   
                str = br.readLine();
            }catch (IOException e){
                throw new Exception(e.toString());
            }  
            return str;
        }
    }
}

Feel free to share your approach. Suggestions are welcomed as always. :slight_smile:

2 Likes

Hi can the tester or setter help me with this CodeChef: Practical coding for everyone my Soln is in the link when I am running without the t test case codechef compiler is working fine but when i use the test case in the while loop it is showing NZEC error i have to improve the code for proper input any suggestion…

think of a scenario
14 ml of c.water is required for the chef and each coconut produces 3ml,
hence if we take count as int then it will show 4 coconut of type A is required but its not, we need 5 right??
so I think we should use a (while) condition to check
while (count *xa<Xa) {
count++; //count only counts how many coconut type A is req.
}