Puppy and Sum Codechef Solution

Hello Programmers In this post, you will know how to solve the Puppy and Sum Codechef Solution.

Puppy and Sum Codechef Solution
Puppy and Sum Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Ezoicreport this adProblem

Yesterday, puppy Tuzik learned a magically efficient method to find the sum of the integers from 1 to N. He denotes it as sum(N). But today, as a true explorer, he defined his own new function: sum(D, N), which means the operation sum applied D times: the first time to N, and each subsequent time to the result of the previous operation.

For example, if D = 2 and N = 3, then sum(2, 3) equals to sum(sum(3)) = sum(1 + 2 + 3) = sum(6) = 21.

Tuzik wants to calculate some values of the sum(D, N) function. Will you help him with that?

Input

The first line contains a single integer T, the number of test cases. Each test case is described by a single line containing two integers D and N.

Output

For each testcase, output one integer on a separate line.

Constraints

  • 1 <= T <= 16
  • 1 <= D, N <= 4

Example

Input:

2
1 4
2 3

Output:

10
21

Explanation 

The first test case: sum(1, 4) = sum(4) = 1 + 2 + 3 + 4 = 10.

The second test case: sum(2, 3) = sum(sum(3)) = sum(1 + 2 + 3) = sum(6) = 1 + 2 + 3 + 4 + 5 + 6 = 21.

Puppy and Sum CodeChef Solutions in Python

T = int(input())
for _ in range(T):
    a, b = map(int, input().split())
    for i in range(0, a):
        b = (b *(b +1)) // 2
    print(b)

Puppy and Sum CodeChef Solutions in CPP

#include <iostream>
using namespace std;
int main() {
    int t,n,k,instanceMax,maxx=0;
    cin>>t;
    while(t--){
        cin>>n>>k;
        maxx=0;
        for(int i=1;i<=k;i++){
            instanceMax = n%i;
            if(instanceMax > maxx){
                maxx = instanceMax;
            }
        }
        cout<<maxx<<endl;
    }
    return 0;

Puppy and Sum CodeChef Solutions in JAVA

import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-- > 0) {
            int n = sc.nextInt();
            int k = sc.nextInt();
            int maxMod = -1;
            int j = 1;
            while(j <= k) { // go from 1 till k
                if(n%j > maxMod) {
                    maxMod = n%j;
                }
                j++;
            }
            System.out.println(maxMod);
        }
        sc.close();
    }
}
Ezoicreport this ad

Disclaimer: The above Problem (Puppy and Sum) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Fit Squares in Triangle Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad