Codeforces Round #426 (Div. 2) C. The Meaningless Game C. The Meaningless Game

来源:互联网 发布:没有网站外链好优化么 编辑:程序博客网 时间:2024/06/06 01:01

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.



Source

Codeforces Round #426 (Div. 2)


My Solution

题意:给出n(1 <= n <= 3.5e5)个询问,每个询问给出a、b(1 <= a, b <= 1e9),A和B 2个人每一轮选择一个数K,如果A先说出就a' * k^2 且B :b'*K。反之a'*K , b'* K^2, 经过x轮后游戏结束,A最终得分a分,B最中得分为b分(x >= 0),问得到的a,b是否合理,即是否存在一系列游戏情况使得最终得到a和b值。


数论、推公式、分解因数

a和b初始为1,然后每次一个*k 另一个*K^2,所以最终的结果是a*b = (kk)^3, 其中kk为所以ki的乘积,如果kk存在则合理否则不合理。

这里可以直接使用cmath里的kk = pow((LL)a*b, 1.0 / 3.0), 然后由于pow的各种小误差所以把y = kk和y = kk+1和y = kk-1都带入到公式 a / y * b / y == y这个公式中检查,

只要有其中一个成立则合理,否则不合理。

此外最开始的时候想到了一个笨方法,即所以ki的乘积必定是a,b的gcd的约数,所以每次求出a,b的gcd,然后sqrt(gcd(a,b))的分解因数,最终时间复杂度是O(n*sqrt(1e9))的,开始没有算常数所以以为是1e9的复杂度,后来算上常数发现是1e10的复杂度⊙﹏⊙‖∣。后来才相出这个开3次方的正确方法。

时间复杂度 O(n)

空间复杂度 O(1)


#include <iostream>#include <cstdio>#include <cmath>using namespace std;typedef long long LL;const int MAXN = 1e6 + 8;template <class T>inline void cinn(T &ret){    char c=getchar();    while(c<'0'||c>'9')        c=getchar();    ret=c-'0';    while(c=getchar(),c>='0'&&c<='9')        ret=ret*10+(c-'0');}int main(){    #ifdef LOCAL    freopen("c.txt", "r", stdin);    //freopen("c.out", "w", stdout);    int T = 1;    while(T--){    #endif // LOCAL    //ios::sync_with_stdio(false); cin.tie(0);    LL n, a, b, gcdab, i, ab, len, x;    bool ans;    cinn(n);    while(n--){        cinn(a); cinn(b);        //gcdab = gcd(a, b);        ab = a*b;        ans  = false;        x = pow(ab, 1.0/3.0);        if(a % x == 0 && b % x == 0 && a / x * b / x == x){            ans = true;        }        x++;        if(a % x == 0 && b % x == 0 && a / x * b / x == x){            ans = true;        }        x -= 2;        if(x > 0 && a % x == 0 && b % x == 0 && a / x * b / x == x){            ans = true;        }        if(ans) printf("Yes\n");        else printf("No\n");    }    #ifdef LOCAL    cout << endl;    }    #endif // LOCAL    return 0;}


  Thank you!

                                                                                                                                             ------from ProLights

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