Codeforces 834(426 Div.2) C.The Meaningless Game(pow函数)

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 a, b (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

Note

First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won.

The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3.

因为赢了乘k^2,输了乘k,每次输赢不定,不过不管两者谁输谁赢,两者相乘一定是(k1*k2*k3*…*kn)^3。
所以我们可以通过求出这两个数乘积的1/3次方,若这个数能被两个数分别整除就说明是满足题目要求的~

扫描二维码关注公众号,回复: 1440888 查看本文章

注意:double强转时精度的问题, 要向上取整才可以~

#include<cstdio>
#include<cmath>
using namespace std;

#define ll long long

int main(){
    ll n, x, y, t, k;
    scanf("%lld", &n);
    while(n--){
        scanf("%lld%lld", &x, &y);
        t = x * y;
        k = round(pow((double)t, 1.0/3));//由于double经经经经常精度缺失~所以必须向上取整
        if(x%k != 0 || y%k != 0 || k*k*k != t)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ling_wang/article/details/80548513