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. xml--myeclipse用快捷键注释xml语句

    7.5以上版本才可以ctrl+shift+/ 撤销注释:CTRL + SHIFT + \ 参考:https://blog.csdn.net/tengdazhang770960436/article/d ...

  2. filename

    package com.enjoyor.soa.traffic.server.tms.controller; import java.io.BufferedReader;import java.io. ...

  3. VC6.0 error LNK2001: unresolved external symbol __imp__ntohl@4

    --------------------Configuration: oxToint1 - Win32 Debug-------------------- Linking... main.obj : ...

  4. supervison

    http://blog.csdn.net/kongxx/article/details/50452357

  5. c# 调试模式下Swaggerf附加接口参数

    c# 调试模式下Swaggerf附加接口参数,如:每个接口Header中附加参数appId 1.新增过滤器: public class GlobalHttpHeaderFilter : IOperat ...

  6. asp.net webapi 404/或无效控制器/或无效请求 截取处理统一输出格式

    public static class PreRouteHandler     {         public static void HttpPreRoute(this HttpConfigura ...

  7. tensorflow tensor 索引

    问题: self.q_eval4next: (100,2) ix=[0,1,0,1---0,1](100,1) 我想取q_eval4next[:,idx] #use_doubleQ 切片用!!!! s ...

  8. HTML/HTML5 Input类型&&表单

    1.HTML 中"不常用"input类型中的属性值: disabled:输入字段禁用: maxlength:输入字段的最大字符长度: readonly:输入字符只读,无法修改: s ...

  9. Spark学习之路 (五)Spark伪分布式安装

    一.JDK的安装 JDK使用root用户安装 1.1 上传安装包并解压 [root@hadoop1 soft]# tar -zxvf jdk-8u73-linux-x64.tar.gz -C /usr ...

  10. Factory Method

    Question:Based on the previous article,what could you do if we must add an extra  function? For exam ...