Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)

来源:互联网 发布:裴乐品牌知乎 编辑:程序博客网 时间:2024/06/15 06:43
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 ab (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 1000000
output
YesYesYesNoNoYes
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.


这个题是多么的有意思!开始我还想用搜索做呢,后来一想,把输入的a和b相乘,不应该就是某个数的三次方嘛!!(因为他俩总是有个人得平方,另一个人得一次方),假设进行n局,数字分别为k1,k2,k3......kn,所以他们得分相乘之后就应该是(k1*k2*k3......kn)^3。

所以我们只去看一看有没有这样一个数的三次方等于a*b就可以了。

给大家提供两种思路

①二分              ②把输入的a*b,开三次方根

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;bool find(ll m){int l = 1,r = 1e6;//1e6的三次幂就是1e18,正好是1e9的平方ll mid;while(l<= r)//二分查找{mid = (l+r)/2;if(mid*mid*mid == m)return 1;if(mid*mid*mid< m)l = mid+1;elser = mid-1;}return 0;}int main(){int n;cin>>n;while(n--){ll a,b;scanf("%lld %lld",&a,&b);if(a*a%b == 0&&b*b%a == 0&&find(a*b))//前两个条件是很重要的,这样保证了每一次round,都是按照k^2和k,这样分的printf("YES\n");             //能找到某个数的3次方等于a*b只是必要条件,还要加上上面一条才可以elseprintf("NO\n");}return 0;}

#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>using namespace std;typedef long long ll;long long a,b,c;int T;void solve(){    scanf("%lld %lld",&a,&b);    c=a*b;    c=round(pow((double)c,1.0/3));    if (c*c*c!=a*b||(a%c)>0||(b%c)>0) printf("NO\n");    else printf("YES\n");}int main(){    scanf("%d",&T);    while (T--) solve();}

精写文章

阅读全文
2 2
原创粉丝点击