Add and Divide solution codechef

Add and Divide solution codechef

You are given two positive integers aa and bb. You also have a number xx, which is initially set to 00. At first, you can add aa to xx any number of times. After that, you can divide xx by bb any number of times as long as xx is divisible by bb.

Print YES if you can make xx equal to 11, otherwise print NO.

Input Format

  • First line of input will contain TT, the number of test cases. Then the test cases follow.
  • Each test case contains a single line, two positive integers a,ba,b.

Output Format

Add and Divide solution codechef

For each test case, output on one line YES if it is possible to make xx , or NO if it is not possible to do so.

Output is case insensitive, which means that “yes”, “Yes”, “YEs”, “no”, “nO” – all such strings will be acceptable.


  • 1T1000001≤T≤100000
  • 1a,b1091≤a,b≤109

Sample Input 1 

3 10
9 6
7 30
8 12

Sample Output 1 Add and Divide solution codechef


ExplanatioAdd and Divide solution codechef

Test case 22: We initially have xx set to 00. Add a=9a=9 to xx for 44 times to make xx equal to 3636. And then divide xx by b=6b=6 for 22 times to make xx equal to 11.

Test case 44: We initially have xx set to 00. Add a=8a=8 to xx for 1818 times to make xx equal to 144144. And then divide xx by b=12b=12 for 22 times to make xx equal to 11.

Leave a Reply

Your email address will not be published. Required fields are marked *