Codeforces Round #426 The Meaningless Game

题目网址:http://codeforces.com/contest/834/problem/C

题目:

C. The Meaningless Game

Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.

The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is multiplied by k2, and the loser's score is multiplied by k. In the beginning of the game, both Slastyona and Pushok have scores equal to one.

Unfortunately, Slastyona had lost her notepad where the history of all n games was recorded. She managed to recall the final results for each games, though, but all of her memories of them are vague. Help Slastyona verify their correctness, or, to put it another way, for each given pair of scores determine whether it was possible for a game to finish with such result or not.

Input

In the first string, the number of games n (1 ≤ n ≤ 350000) is given.

Each game is represented by a pair of scores ab (1 ≤ a, b ≤ 109) – the results of Slastyona and Pushok, correspondingly.

Output

For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise.

You can output each letter in arbitrary case (upper or lower).

Example
input
6
2 4
75 45
8 8
16 16
247 994
1000000000 1000000
output
Yes
Yes
Yes
No
No
Yes

思路:

1.将两人得分相乘(最坏情况是1e9*1e9要用long long否则会爆精度)再开立方根一定是一个整数,且该数等于k1*k2…*kn。
2.两人分别mod该立方根会等于0。

代码:
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4 using namespace std;
 5 int main(){
 6     int t,a,b;
 7     long long mul,nmul;
 8     int j;
 9     scanf("%d",&t);
10     while (t--) {
11         scanf("%d%d",&a,&b);
12         mul=1LL*a*b;//用1LL转换精度
13         j=(int)(pow(mul, 1*1.0/3)+0.5);//pow运算的是浮点型,结果要加0.5再取整
14         nmul=1LL*j*j*j;
15         if(mul!=nmul)   printf("No\n");
16         else if(a%j!=0 || b%j!=0)   printf("No\n");
17         else printf("Yes\n");
18     }
19     return 0;
20 }

 

 
posted @ 2017-07-31 09:04  ventricle  阅读(244)  评论(0编辑  收藏  举报