Puppy and Sum||codechef||beginner solution ||programming info

Puppy and Sum

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.

SOLUTION USING C LANGUAGE:-
#include <stdio.h>
    int SUM(int D,int N)
        {
          int s,r,m=N;
          while(D!=0)
          {
            r=(m*(m+1))/2;
            s=r;
            m=s;
            D--;
          }
          return m;
        }
int main(void)
    {
        int T,D,N,i,p;
        scanf("%d\n",&T);
        for(i=0;i<T;i++)
            {
                scanf("%d %d\n",&D,&N);
                p=SUM(D,N);
                printf("%d\n",p);
            }
    	// your code goes here
    	return 0;
    }

Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support