top of page
Click here to go to the home page of AskTheCode.

Bella ciao - Codechef Solution | AskTheCode

Team ATC

Updated: Jun 14, 2021

June Long Challenge 2021 Solution | Bella ciao (CHFHEIST) Solution | AskTheCode

 

Problem:

Chef is planning a heist in the reserve bank of Chefland. They are planning to hijack the bank for D days and print the money. The initial rate of printing the currency is P dollars per day and they increase the production by Q dollars after every interval of d days. For example, after d days the rate is P+Q dollars per day, and after 2d days the rate is P+2Q dollars per day, and so on. Output the amount of money they will be able to print in the given period.

 

Input:

  • The first line contains an integer T, the number of test cases. Then the test cases follow.

  • Each test case contains a single line of input, four integers D,d,P,Q.

 

Output:

For each test case, output in a single line the answer to the problem.

 

Subtasks:

Subtask #1 (15 points):dD≤100


Subtask #2 (85 points): original constraints

 

Sample Input:

3
2 1 1 1
3 2 1 1
5 2 1 2
 

Sample Output:

3
4
13
 

EXPLANATION:

Test Case 1:

  • On the first day, the rate of production is 1 dollar per day so 1 dollar is printed on the first day.

  • On the second day, the rate of production is 1+1=2 dollars per day so 2 dollars are printed on the second day.

  • The total amount of money printed in 2 days is 1+2=3 dollars.

Test Case 2:

  • For the first two days, the rate of production is 1 dollar per day so 1⋅2=2 dollars are printed on the first two days.

  • On the third day, the rate of production is 1+1=2 dollars per day so 2 dollars are printed on the third day.

  • The total amount of money printed in 3 days is 2+2=4 dollars.

Test Case 3:

  • For the first two days, the rate of production is 1 dollar per day so 1⋅2=2 dollars are printed on the first two days.

  • On the next two days, the rate of production is 1+2=3 dollars per day so 3⋅2=6 dollars are printed on the next two days.

  • On the last day, the rate of production is 3+2=5 dollars per day so 5 dollars are printed on the last day.

  • The total amount of money printed in 5 days is 2+6+5=13 dollars.

 

Code:


In C++ :

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() 
{
	ll t;
	cin>>t;
	while(t--)
	{
	    ll D,d,P,Q,i,j,n,rem;
	    cin>>D>>d>>P>>Q;
	    n = D/d;
	    if(D%d==0)
	    {
    	    j = d*n*P ;
    	    j += Q*(n-1)*n*d/2;
    	    cout<<j<<"\n";
	    }
	    else
	    {
	        j = d*n*P ;
    	    j += Q*(n-1)*n*d/2;
    	    rem = D%d;
    	    j+=rem*(P + n*Q);
	        cout<<j<<"\n";
	    }
	}
	return 0;
}
 

In Java :

import java.util.Scanner;
import java.lang.*;
import java.io.*;

class Codechef{
  public static void main (String[] args) throws java.lang.Exception{
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        long t = Integer.parseInt(in.readLine());
        while(t-->0){
            String[] input;
            long D,d,P,Q,n,money;
            input = in.readLine().split(" ");
            D = Integer.parseInt(input[0]);
            d = Integer.parseInt(input[1]);
            P = Integer.parseInt(input[2]);
            Q = Integer.parseInt(input[3]);
            n=D/d;
            if(n%2==0){
                money=d*((n/2)*(2 * P +(n-1)*Q));
            }
            else{
                money=d * (n * (P+((n-1)/2)*Q));
            }
            money+=(D % d) * (P+(n)*Q);
            System.out.println(money);
        }
     }
}
 

In Python :

def func(D,d,p,q):
    x=D//d
    count=((x)*(2*p+(x-1)*q))//2
    return count;
    
t=int(input())
for i in range(t):
    D,d,p,q=map(int,input().split())
    y=D%d
    c=y*(p+q*(D//d))
    x=d*(func(D,d,p,q))+c
    print(x)

Recent Posts

See All

1 Komentar


bpioe36011
18 Jun 2021

Nice content keep it up<a href="https://www.igmguru.com/"> Online Training</a>

Suka
bottom of page