Codeforces Round #426 (Div. 2)

C. The Meaningless Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.
假设ab是符合条件的结尾则有
S*P*P = a;
P*S*S = b;
a*b = S^3*P^3;
于是有a*b的三次根 == S*P;
于是利用二分查找S*P,并检查是否可以整除a和b;

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
using LL = long long;
int main(void)
{

    int n;
    cin>>n;
    while(n--)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        if(a==b&&a==1)
            cout<<"Yes"<<endl;
            else
        {
            LL L = (LL )a *b;
        LL Mid;
        LL Left = 1, Right = L;
        if(Right>1000000)
            Right = 1000000;//做一下优化,要不然会爆,超出long long 的范围
        while(Right>=Left)
        {
           Mid = Left+(Right-Left)/2;
           LL t = Mid*Mid*Mid;
           if(t==L)
           {
               break;
           }
           if(t>L)
            Right = Mid-1;
           else
             Left = Mid+1;
        }
        if(Mid*Mid*Mid == L&&a%Mid==0&&b%Mid==0)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
        }
    }


    return 0;
}
全部评论

相关推荐

10-05 23:02
东北大学 Java
我说句实话啊:那时候看三个月培训班视频,随便做个项目背点八股,都能说3 40w是侮辱价
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务