codeforces 834C (The Meaningless Game)

来源:互联网 发布:ubuntu ssh 安装 编辑:程序博客网 时间:2024/06/08 05:28

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.

题意时非常明显的,就是两人玩游戏,不,一个人一个狗一起玩游戏,然后每次选择一个数k,赢得人的分数会乘上k^2,输的人的分数会乘上k,然后给你a,b两个数,然后你判断这是否可能是结束后两人的分数.

**思路:我最开始是用分解质因数做的,时间复杂度是O(n*Max(sqrt(n),log(n)));//很显示是超时的(当然我是测过后的。。。),tle8,后来听大佬讲解,这题的正确姿势应该是先求出a*b,a*b = k1^3*k2^3*k^3……就是这样因为每次都是一个乘上k^2另一个乘上k,所以两个相乘后每一个都是k^3。然后我们判断a*b是否能开三次方,然后再判断结果是否是a和b的因子,就是判断是否能整除a和b,如果是就是yes。

代码如下:

#include<iostream>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;ll n,a,b;//这里都是longlong类型bool solve(){    ll c = a*b;    ll d = round(pow(c,1.0/3));    if(a % d == 0 && b % d == 0 && (d*d*d == c))        return true;    return false;}int main(void){    scanf("%lld",&n);    for(int i=1;i<=n;i++){        scanf("%lld %lld",&a,&b);        if(solve()) printf("Yes\n");        else printf("No\n");    }    return 0;}

话说我看到一个大佬的代码是二分求三次根式过得,大致思路很接近,,代码也贴在下面了

#include <iostream>#include <stdio.h>#include <string.h>#include <queue>#include <set>#include <map>#include <stack>#include <stdlib.h>#include <algorithm>#include <vector>#include <math.h>using namespace std;#define LL long long#define MX 1000006int main(){    int t;    cin>>t;    while (t--)    {        LL aa,bb;        scanf("%I64d%I64d",&aa,&bb);        LL l=1,r=1000000;        LL k=aa+1;        while (l<=r)        {            LL mid = (l+r)/2;            if (mid*mid*mid==aa*bb)            {                //printf("%lld\n",mid);                k=mid;                break;            }            else if (mid*mid*mid<aa*bb)                l=mid+1;            else if (mid*mid*mid>aa*bb)                r=mid-1;        }        if (aa%k==0&&bb%k==0)            printf("Yes\n");        else            printf("No\n");    }    return 0;}
原创粉丝点击