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. Git操作说明

    Git操作说明 1.将本地项目上传到GitHub 1)    首先在GitHub上注册帐户 2)    在GitHub上创建仓库 3)    Pc安装Git客户端(Git Bach) 4)    打开 ...

  2. 线程间操作无效: 从不是创建控件“button1”的线程访问它。

    .net2后是不能跨线程访问控件的.,窗体上的控件是当前线程创建的,当用户异步执行一个方法:在该方法中给窗体上的控件赋值,记住:当执行一个异步委托的时候,其实 就是开了一个线程去执行那个方法,这样就会 ...

  3. “无效数字” ;java.lang.Integer cannot be cast to java.lang.String

    今天页面上突然查询不出数据,大致的sql语句是 select xx ,xxx from table a where a.lrmb in ( 6101060033, 61010503300, 61016 ...

  4. RSA 加解密 秘钥对说明

    rsa非对称加密, 加解密需要不同的秘钥,称作一对. rsa加解密分两种,第一:公钥加密私钥解密.第二:私钥加密公钥解密. 需要注意的是,公加私解得到的密文是变化的,而私加公解的得到的密文是固定的. ...

  5. vmvare 将主机的文件复制到虚拟机系统中 安装WMware tools

    在虚拟机里的ubuntu这里找到VMware tools包

  6. 《大话设计模式》c++实现 之工厂模式

    工厂模式 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,我们在创建对象时不会对客户端 ...

  7. css 文字样式

    Gradient 3D text 代码区域 /*css */ body { background-color: #272727; } h1 { font-family: "Arial&quo ...

  8. 导入javax.servlet。伺服登记无法解决:The import javax.servlet.MultipartConfigElement cannot be resolved

    解决办法:

  9. IO model

    上节的问题: 协程:遇到IO操作就切换. 但什么时候切回去呢?怎么确定IO操作完了? 很多程序员可能会考虑使用“线程池”或“连接池”.“线程池”旨在减少创建和销毁线程的频率,其维持一定合理数量的线程, ...

  10. windows下配置lua环境

    1.进入lua官网http://www.lua.org/ 2.点击download 3.点击get a binary 4.点击[Lua - joedf's Builds] 5.选择适合自己的版本下载, ...