Skip to main content
  1. Problem Solving Solutions/

Chef and Cakes CodeChef Solution

·1 min
codechef-solutions
Mayukh Datta
Author
Mayukh Datta
Table of Contents

Problem: https://www.codechef.com/QFUN2020/problems/CHEFCAKE

We have to find all possible n-digits numbers that can be formed by n distinct digits and sum them all. Read the explanation here.

Solution in C++ #

#include<bits/stdc++.h> using namespace std;

long factorial(long num) { long fact = 1;

if(num == 0 || num == 1) {
    return 1;
}
for(long i=2;i<=num;i++){
    fact = fact \* i;
}
return fact;

}

int main() { long t, n, y;

cin >> t;
while(t--) {
    cin >> n;

    long digits\_sum = 0;
    long ones = 0;

    for(long i=0;i<n;i++) {
        cin >> y;
        digits\_sum += y;
        ones = (ones \* 10) + 1;
    }
    
    cout << factorial(n-1) \* digits\_sum \* ones << endl;
}
return 0;

}