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
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.


  题目大意 (题目太简洁不需要大意,看原文吧)

  对于每组询问等于求这么一个方程组的一组解,判断解是否是整数:

  首先可以得到,设它为x,那么有

  显然解是整数解的条件是x为整数且a,b均能被x整除。前半个条件又等价于ab为完全立方数。

  这个判断嘛。。牛顿迭代去开立方,二分法,HashMap都可以。

  为了装逼先写了个牛顿迭代,然而大概是我的牛顿迭代常数逆天,所以跑得比较慢。

Code

 /**
* Codeforces
* Problem#833A
* Accepted
* Time: 468ms
* Memory: 2100k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define double long double const double eps = 1e-;
double sqrt3(double x) {
double r = x;
double f = , k;
do {
f = r * r * r - x;
k = * r * r;
r -= f / k;
} while (f > eps);
return r;
} int a, b;
long long P;
inline void init() {
scanf("%d%d", &a, &b);
P = a * 1LL * b;
} inline boolean solve() {
long long x = sqrt3(P);
if(x * x * x != P) return false;
return !(a % x || b % x);
} int T;
int main() {
scanf("%d", &T);
while(T--) {
init();
puts(solve() ? ("Yes") : ("No"));
}
return ;
}

The Meaningless Game(Newton's Methon)

  然后写了一个二分法,快了将近一倍。

Code

 /**
* Codeforces
* Problem#833A
* Accepted
* Time: 218ms
* Memory: 2052k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define LL long long int sqrt3(LL x) {
int l = , r = 1e6;
while(l <= r) {
LL mid = (l + r) >> ;
if(mid * mid * mid <= x) l = mid + ;
else r = mid - ;
}
return l - ;
} int a, b;
long long P;
inline void init() {
scanf("%d%d", &a, &b);
P = a * 1LL * b;
} inline boolean solve() {
long long x = sqrt3(P);
if(x * x * x != P) return false;
return !(a % x || b % x);
} int T;
int main() {
scanf("%d", &T);
while(T--) {
init();
puts(solve() ? ("Yes") : ("No"));
}
return ;
}

The Meaningless Game(Binary Search)

  最后写了一个可以用Hash的二分法(Excuse me?新型long long开方向下取整?)

Code

 /**
* Codeforces
* Problem#833A
* Accepted
* Time: 233ms
* Memory: 9900k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define LL long long const int limit = 1e6;
LL arr3[limit + ];
inline void rinit() {
for(int i = ; i <= limit; i++)
arr3[i] = i * 1LL * i * i;
} int a, b;
long long P;
inline void init() {
scanf("%d%d", &a, &b);
P = a * 1LL * b;
} inline boolean solve() {
int x = lower_bound(arr3 + , arr3 + limit + , P) - arr3;
if(arr3[x] != P) return false;
return !(a % x || b % x);
} int T;
int main() {
rinit();
scanf("%d", &T);
while(T--) {
init();
puts(solve() ? ("Yes") : ("No"));
}
return ;
}

Codeforces 833A The Meaningless Game - 数论 - 牛顿迭代法 - 二分法的更多相关文章

  1. NOIP2001 一元三次方程求解[导数+牛顿迭代法]

    题目描述 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之间),且根与根之差 ...

  2. Atitit 迭代法  “二分法”和“牛顿迭代法 attilax总结

    Atitit 迭代法  "二分法"和"牛顿迭代法 attilax总结 1.1. ."二分法"和"牛顿迭代法"属于近似迭代法1 1. ...

  3. 牛顿迭代法实现平方根函数sqrt

    转自利用牛顿迭代法自己写平方根函数sqrt 给定一个正数a,不用库函数求其平方根. 设其平方根为x,则有x2=a,即x2-a=0.设函数f(x)= x2-a,则可得图示红色的函数曲线.在曲线上任取一点 ...

  4. sqrt (x) 牛顿迭代法

    参考: 0开方 是 0 1的开方式 1 2的开方式 1.4 3.的开方=(1.4+3/1.4)/2 牛顿迭代法:学习自 http://blog.csdn.net/youwuwei2012/articl ...

  5. 【清橙A1094】【牛顿迭代法】牛顿迭代法求方程的根

    问题描述 给定三次函数f(x)=ax3+bx2+cx+d的4个系数a,b,c,d,以及一个数z,请用牛顿迭代法求出函数f(x)=0在z附近的根,并给出迭代所需要次数. 牛顿迭代法的原理如下(参考下图) ...

  6. 基于visual Studio2013解决C语言竞赛题之0422牛顿迭代法

      题目

  7. 牛顿迭代法解指数方程(aX + e^x解 = b )

    高中好友突然问我一道这样的问题,似乎是因为他们专业要做一个计算器,其中的一道习题是要求计算器实现这样的功能. 整理一下要求:解aX + e^X = b 方程.解方程精度要求0.01,给定方程只有一解, ...

  8. 牛顿迭代法(Newton's Method)

    牛顿迭代法(Newton's Method) 简介 牛顿迭代法(简称牛顿法)由英国著名的数学家牛顿爵士最早提出.但是,这一方法在牛顿生前并未公开发表. 牛顿法的作用是使用迭代的方法来求解函数方程的根. ...

  9. sqrt()平方根计算函数的实现2——牛顿迭代法

    牛顿迭代法: 牛顿迭代法又称为牛顿-拉夫逊方法,它是牛顿在17世纪提出的一种在实数域和复数域上近似求解方程的方法.多数方程不存在求根公式,因此求精确根非常困难,甚至不可能,从而寻找方程的近似根就显得特 ...

随机推荐

  1. vue中使用echarts来绘制世界地图和中国地图

    第一步,下载echarts cnpm install echarts --save-dev 第二步,在main.js中全局引入 //引入echarts import echarts from 'ech ...

  2. TensorFlow的封装

    TensorFlow是比较底层的深度学习API,TF-Slim.TFLearn.Keras和TensorLayer均尝试简化TensorFlow/Theano,它们对TensorFlow/Theano ...

  3. 10.for

    要遍历一个范围(如数组或表),使用 for 循环.在数组上迭代时,当前数组元素存储在循环变量中.在遍历字典时, index 存储在循环变量中. (in 内容测试) for x in [5, 7, 11 ...

  4. html5-css渐变色

    div{    width: 300px;    height: 100px;    margin: 50px;    padding: 50px;    border:5px groove rgba ...

  5. grad-cam 、cam 和热力图,基于keras的实现

    http://bindog.github.io/blog/2018/02/10/model-explanation/ http://www.sohu.com/a/216216094_473283 ht ...

  6. Robot framework selenium driver download

    Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads http://npm.taobao.org/mirrors ...

  7. Necklace (全排列 + 匈牙利)

    #include<bits/stdc++.h> using namespace std; ][], Gra[][]; ]; ]; ]; bool dfs(int u, int vN) { ...

  8. python 试题归纳及答疑 更新中.....

    一.Python基础篇(80题) 1.你为什么学习Python? 一.答题思路 1.阐述 python 优缺点 2.Python应用领域说明 3.根据自身工作情况阐述为什么会使用python 1)py ...

  9. npm 代理的设置和取消

    管理员权限下的控制台输入: 设置代理 npm config set proxy=http://127.0.0.1:8080 npm config set registry=http://registr ...

  10. python 将文件描述符包装成文件对象

    有一个对应于操作系统上一个已打开的I/O 通道(比如文件.管道.套接字等)的整型文件描述符,你想将它包装成一个更高层的Python 文件对象. 一个文件描述符和一个打开的普通文件是不一样的.文件描述符 ...