Puppy and Sum Codechef Solution

Puppy and Sum Codechef Solution

Problem

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 Solution 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 Solution 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 Solution 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();
    }
}

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

Leave a Comment