Sum of Powers of 3
Practice
5 (1 votes)
Problem
16% Success 371 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

In this problem you have to answer \(Q\) queries. In each query you are given two integers \(N\) and \(K\). You have to decide if it is possible to represent \(N\) as the sum of exactly \(K\) powers of 3 (they may or may not be distinct). If it is possible to represent \(N\) in such a way then the answer for that query is "YES"(without quotes) otherwise it is a "NO" (without quotes).

Input Format

The first line contains a postive integer \(Q\) - the number of queries. 

Each of the next \(Q\) lines contains two space separated integers \(N\) and \(K\) .

Constraints

  • \(1 \leq Q \leq 10^7\)
  • \(1 \leq N \leq 10^{18}\)
  • \(1 \leq K \leq 10^{18}\)

Output Format

For each query, print the answer to that query i.e. "YES"(without quotes) if it is possible to write \(N\) as the sum of exactly \(K\) powers of 3 else print "NO"(without quotes).

 

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:30
6 votes
Tags:
ArraysNumber TheoryData Structures1-DMathModulus Arithmetic
Points:30
3 votes
Tags:
Medium
Points:30
1 votes
Tags:
MediumMathNumber TheoryMedium