PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4
Setter: Utkarsh Gupta
Tester: Manan Grover
Editorialist: Prakhar Kochar
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
There is a sale going on in Chefland. For every 2 items Chef pays for, he gets the third item for free.
It is given that the cost of 1 item is X rupees. Find the minimum money required by Chef to buy at least N items.
QUICK EXPLANATION:
Minimum money required = \lfloor \frac{N}{3} \rfloor \cdot (2 \cdot X) + (N mod 3 ) \cdot X
EXPLANATION:
Given that the cost of 1 item is X rupees. For every 2 items Chefs pays for, he gets the third item for free. From the above statements we can conclude that for every group of 3 items Chef needs to pay a minimum price of 2 \cdot X rupees.
Since Chef needs to buy at least N items, in order to minimize the money required it is better to divide N items into groups of 3 items each and pay 2 \cdot X price for each such group.
It may happen that N cannot be divided into complete groups of 3 items each. In this case, there will be a single group G_{single} containing less than 3 items. Each item in G_{single} must be bought at a price of X rupees.
Equation is as follows :
Minimum money required = \lfloor \frac{N}{3} \rfloor \cdot (2 \cdot X) + (N mod 3 ) \cdot X
Here, mod is modulo operator and \lfloor N \rfloor is floor(N) which represents the largest integer less than or equal to N.
Examples
- N = 10, X = 2;
Minimum money required = \lfloor \frac{10}{3} \rfloor \cdot (2 \cdot 2) + (10 mod 3 ) \cdot 2 = 14
- N = 6, X = 3;
Minimum money required = \lfloor \frac{6}{3} \rfloor \cdot (2 \cdot 3) + (6 mod 3 ) \cdot 3 = 12
TIME COMPLEXITY:
O(1) for each test case.
SOLUTION:
Setter's Solution
//Utkarsh.25dec
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
vector <int> adj[N];
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){
if(is_neg){
x= -x;
}
if(!(l <= x && x <= r))
{
cerr << l << ' ' << r << ' ' << x << '\n';
assert(1 == 0);
}
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,' ');
}
void solve()
{
int n,x;
n=readInt(1,1000,' ');
x=readInt(1,1000,'\n');
ll ans=0;
int items=2*n/3;
ans=(items*x);
int got=items+items/2;
if(got<n)
{
ans+=x;
got++;
}
assert(got==n);
cout<<ans<<'\n';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
int T=readInt(1,1000,'\n');
//cin>>T;
while(T--)
solve();
assert(getchar()==-1);
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}
Tester's Solution
#include <bits/stdc++.h>
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);
}
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
t = readInt(1, 1000, '\n');
while(t--){
int n, x;
n = readInt(1, 1000, ' ');
x = readInt(1, 1000, '\n');
int y = ((n - n % 3) / 3) * 2 + n % 3;
cout<<y * x<<"\n";
}
return 0;
}
Editorialist's Solution
/*prakhar_87*/
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define inf INT_MAX
#define mod 998244353
void f() {
int n, x;
cin >> n >> x;
int ans = (n / 3) * (2 * x) + (n % 3) * x;
cout << ans << "\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while (t--) f();
}