Header Ad

HackerEarth Mathematically beautiful numbers problem solution

In this HackerEarth Mathematically beautiful numbers problem solution While playing a mental math game, you realize that the number k is mathematically beautiful.

You then realize that the number x can be mathematically beautiful if it is represented as a sum of a sequence where each element is a power of k and all the numbers in the sequence are different. Your task is to determine whether the number is mathematically beautiful.


hackerEarth Mathematically beautiful numbers problem solution

HackerEarth Mathematically beautiful numbers problem solution.

#include<iostream>
using namespace std;
int m[40], t;
int main (){
cin >> t;
while(t--){
for(int i = 0; i < 40; i++)
m[i] = 0;
long long int x;
int k, cnt = 0;
cin >> x >> k;
while(x){
cnt++;
m[x % k]++;
x /= k;
}
if(m[0] + m[1] == cnt)
cout << "YES\n";
else
cout << "NO\n";
}
}

second solution

t = int(input())
while t > 0:
t -= 1
x, k = map(int, input().split())
good = True
while x > 0:
good &= x % k < 2
x //= k
print("YES" if good else "NO")


Post a Comment

0 Comments