Digit cube

3

22 votes
Basic Programming, C++
Problem

Let n be an integer. We define a function f(n) which returns the cube of sum of digits of n.

You are given two integers n and k. You have to find the value of the integer that is returned when the function is recursively applied k times on n.

Formally, you have to find the value of fk(n).

Input format

  • The first line of each test case consists of an integer T denoting the number of test cases.
  • Each of the next T lines consists of two space-separated integers n and k.

Output format

The output should consist of T lines each containing a single integer corresponding to the required value fk(n).

Constraints

1T1061n,k1015

Sample Input
2
3 1
3 2
Sample Output
27
729
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The Sum of digits of 3 is 3.

  • So, f1(3)=f(3)=33=27

The Sum of digits of 27 is 9.

  • So, f2(3)=f(f(3))=f(27)=93=729
Editor Image

?