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;
}
全部评论

相关推荐

湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务