HPU第七周周练 I

来源:互联网 发布:手机淘宝如何联系卖家 编辑:程序博客网 时间:2024/05/19 13:16
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

62 475 458 816 16247 9941000000000 1000000OutputYesYesYesNoNoYes

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.

题意:有一个狗和他的主人在玩游戏,有n回合,谁赢的话谁的分数就乘以k^2,输的话就乘以k,然而主人忘了他的分数,现在给你一对数,让你判断这一对数是否合法;
思路:a=k1^2(k1) * k2^2(k2) * k3^2(k3)…….kn^2(kn);
b=k1^2(k1) * k2^2(k2) * k3^2(k3)…….kn^2(kn);
a*b=(k1 * k2 * k3 * …… * kn)^3;
所以这个问题就转化为了判断是否有一个整数的三次方等于a * b;
a*b的范围为1~1e18,所以只要判断到1e6就行了,
打表+二分,复杂度 O(n);
下面附上代码:

#include<bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN=1e6+5;LL a,b;LL s[MAXN];void init(){    for(LL i=1;i<=MAXN;i++)        s[i]=i*i*i;}int main(){    int n;    cin>>n;    init();    while(n--)    {        scanf("%lld %lld",&a,&b);         LL sum=a*b;        LL p=lower_bound(s,s+MAXN,sum)-s;        //printf("%lld %lld %lld %lld\n",p,s[p],sum,s[p-1]);        if(s[p]==sum&&(a*a%b==0)&&(b*b%a==0))            puts("Yes");        else             puts("No");    }    return 0;}