Mathematically beautiful numbers
Practice
4 (30 votes)
Implementation
Basic programming
Basics of greedy algorithms
Problem
92% Success 11427 Attempts 20 Points 2s Time Limit 256MB Memory 1024 KB Max Code

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.

Task

Your task is to determine whether the number is mathematically beautiful.

Input format

  • The first line contains T denoting the number of test cases.
  • The next T lines contain \(x\)  and  \(k\)  denoting the numbers.

Output Format

For each test case, output  "YES"  if \(x\) is "mathematically beautiful" and  "NO"  otherwise.

Constraints

\(T <= 1000\)

\((1 <= x <= 10^{18})\)

\((2 <= k <= 9)\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
94 votes
Tags:
MathematicsOpenApprovedEasy
Points:20
45 votes
Tags:
Ad-HocApprovedEasyOpen
Points:20
29 votes
Tags:
Basic ProgrammingBasics of ImplementationEasyImplementation