EJMAR21E - Editorial

PROBLEM LINK:

Practice

Author: Akash Kumar Bhagat
Tester: Shekhar Shrivastava
Editorialist: Akash Kumar Bhagat

DIFFICULTY:

Easy

PROBLEM:

Given a number N, one has to calculate a number M such that M = 2^N. Now we have to add the digits of M to get a new number. We would repeate this process of adding the digits of the number untill you get a single digit number.

EXPLANATION:

One can easily calculate 2^N because N \leq 63, 64 bit integer can store this value. After Calculating M, we can add its digit untill we get a one digit number

	while (M>=10):
		sum=0
		while(M>0):
			sum+=M%10
			M/=10
		M=sum

SOLUTIONS:

Python 3.7

for _ in range(int(input())):
    n=int(input())
    m = str(2**n)
    #print(len(m))
    while len(m)>1:
        m=str(sum(list(map(int,m))))
 
    print(m) 

CPP
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
#define range(i, start, end ,step) for(ll i=start ;i<end; i += step)
#define rep(i,s,e) for(ll i=s;i<=e;++i)
#define reparr(arr) for(auto x: arr) cout<<x<<" ";
#define repr(i,e,s) for(ll i=e; i>=s; i--)
#define vit vector<ll>
#define mid(l,r) (l+(r-l)/2)
#define endl '\n'
#define vr vector
#define ull unsigned long long
 
const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
 
ull dsum(ull x){
	ull sum=0;
	while(x){
		sum+=x%(ull)10;
		x/=(ull)10;
	}
	return sum;
}
 
void solve() {
	ull n,v=1;
	cin>>n;
	v<<=n;
	while(v>=10){
		v = dsum(v);
	}
	cout<<v<<endl;
 
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t  << ": ";
        solve();
    }
}