Puppy and Sum | CodeChef Solution

Hello coders, today we are going to solve Puppy and Sum CodeChef Solution whose Problem Code is PPSUM.

Puppy and Sum

Task

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 Format

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 Format

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

Constraints

  • 1 ≤ T ≤ 16
  • 1 ≤ D, N ≤ 4

Example

Sample Input

2
1 4
2 3

Sample 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.

Solution – Puppy and Sum | CodeChef Solution

C++

#include <iostream>
using namespace::std;
int main(int argc, const char * argv[]) {
    int t;
    cin >> t;
    while (t--) {
        int D,N;
        cin >> D >> N;
        for (int j = 1; j <= D; j++) {
            int ans = 0;
            for (int i = 1 ; i <= N; i++) {
                ans += i;
            }
            N = ans;
        }
        cout << N << endl;
    }
    return 0;
}

Python

#Solution Provided by Sloth Coders 
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)

Java

/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
    	int t = sc.nextInt();
    	int s = 0;
    	for(int i=0; i<t; i++) {
    		int n1 = sc.nextInt();
    		int n2 = sc.nextInt();
    		
    		
    		for(int x=1; x<=n1; x++) {
    			s = 0;
    			for(int j=1; j<=n2; j++) {
        			s += j;
        		}
    			n2 = s;
    		}
    		System.out.println(s);
	    }
	    sc.close();
	}
}

Disclaimer: The above Problem (Puppy and Sum) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *