CHOWRAP-Editorial

PROBLEM LINK:

Chocolate Wrapper

Author: Priya Doshi
Tester: Priya Doshi
Editorialist: Priya Doshi

DIFFICULTY:

Easy

PREREQUISITES:

Math

PROBLEM:

There is a new store in the city which believes in recycling chocolate wrappers. They came up with a new method of collecting chocolate wrappers. The offer is such that for every 10 chocolate wrappers you return back to the store, you get one extra chocolate.

You buy N chocolates and also believe in recycling.

Find the total number of chocolates you will receive.

Quick Overview:

We need to find number of maximum chocolates we can have after recycling the wrappers.

Explanation:

There are two methods to Implement this question, brute-force and formula.

A naive method is to continuously count the number of chocolates by returning wrappers until wrappers left is less than 10.

Instead of recursively dividing the number of chocolates by 10,until we left with 1 chocolate. We are recomputing the values i.e. ((choc/wrap + choc%wrap)/wrap until we get 1.
It is observed that we can get the result by just reducing the values of chocolates and wrappers by 1 and then divide them to get the result (choc-1)/(wrap-1), in this case choc+=(choc-1)/9.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        ll choc;
        cin>>choc;
        ll total = choc;
        while(choc>=10) {
            ll ans = choc;
            total+=choc/10;
            choc=ans/10+ans%10;
        }
        cout<<total<<endl;
    }
}
1 Like