The Meaningless Game CodeForces - 834C (思维题)

The Meaningless Game

CodeForces - 834C

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,赢的人分数乘K^2,输的人分数乘k,看每场比赛分数是不是可以通过这种方式得来,能就输出YES,否则输出NO


思路:在满足题意的情况下,首先我们发现每次一个数K一共出现了三次,一个数两次,另一个数一次,那么两个乘在一起就应该是k^3,那么总分相乘后首先满足的应该是这个数可分解成某个基数的三次方,那么我们先判断开三次方后乘回去能否和原来的乘积相等。

a = p1^2 p2 p3^2 p4

b = p1 p2^2 p3 p4^2

a * b = p1^3 p2^3 p3^3 p4^3 = (p1p2p3p4)^3

我们要得到的就是p1p2p3p4这个基数

但是这样并不能完全说明它是符合情况的,因为并不能判断出K分在两个人的分数中,如果一个K^3全都在一个人的分数中,相乘后仍然可以分成某个数的三次方,但却是不满足题意的。

a = p1^3 p2 p3^2 p4

b = p2^2 p3 p4^2

a * b = p1^3 p2^3 p3^3 p4^3 = (p1p2p3p4)^3

这种情况即使a*b满足条件但是却无法得到,这个时候我们发现基值是p1p2p3p4如果按照规则得到的分数,每个分数肯定都包含了p1p2p3p4这几个数字,那么直接每个人的分数再检验下是否能被整除就好了

当时做的时候死活想不出怎么求开立方了,妈的用幂函数n^1/3...

注意精度

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
ll a,b,n;
double num;
int t;
bool flag;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld",&a,&b);
        num = 1.0 * a * b;
        flag = 0;
        n = (ll)pow(num,1.0/3);
        for(ll i = n-1; i <= n+1; i++){
            if(i * i * i == a * b && a % i ==0 && b % i == 0){
                flag = 1;
                break;
            }
        }
        if(flag)
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/80559132