您的位置:首页 > 其它

【Codeforces】834C. The Meaningless Game

2017-07-31 09:08 435 查看
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.

题目链接:http://codeforces.com/contest/834/problem/C

题目大意: 有n场游戏, 对于每一场游戏x,y两个人初始分数都为一,  他俩猜数字, 每回合系统给出一个数字k,谁先猜对分数*k^2, 另一个人*k, 每一场比赛可以是任意回合。 给你每一场最终的分数x,y 问你xy是否合法。

解题思路: 对于一场比赛, 我们不知道回合数, 也不知道每一回合的k。 

首先我们可以想到对于x和y来说, 可以看成 a^2*b*c*d*... = x,   那么 a * b^2 * c^2 * d^2*... = y。 (abcd理解为每一回合的k)   对于当前回合的一个k, x和y肯定是一个*k 一个*k^2。 所以如果xy合法, 肯定可以化为上面的形式。

那么 x*y = (a + b + c + d + ...) ^3。   如果存在一个num = (a + b + c + d + ...)   并且x和y都是是num的倍数, 输出Yes。

至于num怎么求, 有很多方法,  我是用map预处理存下来的, 跑了800ms。。。 有点险。  学长用二分去查找, 还有人的代码直接开三次根号

//因为这道题上蓝了  纪念一下哈哈哈 (下把必掉  T A T)

       
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;

map<ULL, int> num;
int n;

int main(){
for(LL i = 1LL; i <= 1e6; i++) num[i * i * i] = i;
scanf("%d", &n);
while(n--){
ULL a, b;
scanf("%lld %lld", &a, &b);
LL x = num[a * b];
if(x != 0 && (a % x == 0) && (b % x == 0)) printf("Yes\n");
else printf("No\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces