time limit per test
2 seconds
memory limit per test
256 megabytes
Initially you have a single pile with nn gold nuggets. In an operation you can do the following:
- Take any pile and split it into two piles, so that one of the resulting piles has exactly twice as many gold nuggets as the other. (All piles should have an integer number of nuggets.)
One possible move is to take a pile of size 66 and split it into piles of sizes 22 and 44, which is valid since 44 is twice as large as 22.
Can you make a pile with exactly mm gold nuggets using zero or more operations?
Input
The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
The only line of each test case contains two integers nn and mm (1≤n,m≤1071≤n,m≤107) — the starting and target pile sizes, respectively.
Output
For each test case, output "YES" if you can make a pile of size exactly mm, and "NO" otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
Example
Input
Copy
11
6 4
9 4
4 2
18 27
27 4
27 2
27 10
1 1
3 1
5 1
746001 2984004
Output
Copy
YES YES NO NO YES YES NO YES YES NO NO
Note
The first test case is pictured in the statement. We can make a pile of size 44.
In the second test case, we can perform the following operations: {9}→{6,3}→{4,2,3}{9}→{6,3}→{4,2,3}. The pile that is split apart is colored red before each operation.
In the third test case, we can't perform a single operation.
In the fourth test case, we can't end up with a larger pile than we started with.
解题说明:此题需要采用递归算法,每次针对n拆分只能拆分为2部分,一部分是另一部分的2被,直到找到m。
#include<stdio.h>
int ans, m;
void dfs(int n)
{
if (n == m)
{
ans = 1;
return;
}
if (n < m)
{
return;
}
if (n % 3 == 0)
{
dfs(n / 3);
dfs((n / 3) * 2);
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d %d", &n, &m);
ans = 0;
dfs(n);
if (ans)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}