Codeforces 833A The Meaningless Game - 数论 - 牛顿迭代法 - 二分法
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.
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.
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).
6
2 4
75 45
8 8
16 16
247 994
1000000000 1000000
Yes
Yes
Yes
No
No
Yes
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 - 数论 - 牛顿迭代法 - 二分法的更多相关文章
- NOIP2001 一元三次方程求解[导数+牛顿迭代法]
题目描述 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之间),且根与根之差 ...
- Atitit 迭代法 “二分法”和“牛顿迭代法 attilax总结
Atitit 迭代法 "二分法"和"牛顿迭代法 attilax总结 1.1. ."二分法"和"牛顿迭代法"属于近似迭代法1 1. ...
- 牛顿迭代法实现平方根函数sqrt
转自利用牛顿迭代法自己写平方根函数sqrt 给定一个正数a,不用库函数求其平方根. 设其平方根为x,则有x2=a,即x2-a=0.设函数f(x)= x2-a,则可得图示红色的函数曲线.在曲线上任取一点 ...
- sqrt (x) 牛顿迭代法
参考: 0开方 是 0 1的开方式 1 2的开方式 1.4 3.的开方=(1.4+3/1.4)/2 牛顿迭代法:学习自 http://blog.csdn.net/youwuwei2012/articl ...
- 【清橙A1094】【牛顿迭代法】牛顿迭代法求方程的根
问题描述 给定三次函数f(x)=ax3+bx2+cx+d的4个系数a,b,c,d,以及一个数z,请用牛顿迭代法求出函数f(x)=0在z附近的根,并给出迭代所需要次数. 牛顿迭代法的原理如下(参考下图) ...
- 基于visual Studio2013解决C语言竞赛题之0422牛顿迭代法
题目
- 牛顿迭代法解指数方程(aX + e^x解 = b )
高中好友突然问我一道这样的问题,似乎是因为他们专业要做一个计算器,其中的一道习题是要求计算器实现这样的功能. 整理一下要求:解aX + e^X = b 方程.解方程精度要求0.01,给定方程只有一解, ...
- 牛顿迭代法(Newton's Method)
牛顿迭代法(Newton's Method) 简介 牛顿迭代法(简称牛顿法)由英国著名的数学家牛顿爵士最早提出.但是,这一方法在牛顿生前并未公开发表. 牛顿法的作用是使用迭代的方法来求解函数方程的根. ...
- sqrt()平方根计算函数的实现2——牛顿迭代法
牛顿迭代法: 牛顿迭代法又称为牛顿-拉夫逊方法,它是牛顿在17世纪提出的一种在实数域和复数域上近似求解方程的方法.多数方程不存在求根公式,因此求精确根非常困难,甚至不可能,从而寻找方程的近似根就显得特 ...
随机推荐
- 从零开始一起学习SLAM | 为什么要用齐次坐标?
在涉及到计算机视觉的几何问题中,我们经常看到齐次坐标这个术语.本文介绍一下究竟为什么要用齐次坐标?使用齐次坐标到底有什么好处? 什么是齐次坐标?简单的说:齐次坐标就是在原有坐标上加上一个维度: 使用齐 ...
- cocos2d-x JS 富文本(为一段文本中的个别字体上颜色)
setWinText : function (levelStr1,levelStr2,levelStr3,color1,color2) { var imgRankingBG = this.contai ...
- qq浏览器默认字体设置
- fang99-三号线与四号线新盘
三号线与四号线新盘 http://www.fang99.com/buycenter/buildingsearch_map.aspx?projectid=0000011104 http://www.fa ...
- 谷歌Cookies无法写入
写Cookies页面加这个ok: Response.AddHeader("P3P", "CP=CAO PSA OUR");
- Spark学习之路 (十一)SparkCore的调优之Spark内存模型
摘抄自:https://www.ibm.com/developerworks/cn/analytics/library/ba-cn-apache-spark-memory-management/ind ...
- windows10 安装 mysql8.0.12 详解
[1]下载安装包 官网下载地址:https://downloads.mysql.com/archives/community/ 如下图所示: 下载完成,安装包为mysql-8.0.12-winx64. ...
- DB2的空间数据库管理复杂配置
计算机条件: a机器:linux系统 安装arcgis server b机器:windows系统 安装catalog c机器:linux系统 安装DB2 ...
- [转载]C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编
原文出处:https://www.cnblogs.com/wuhuisheng/archive/2011/03/23/1992652.html 验证电话号码的主要代码如下: public bool I ...
- [转载]SQL Server中的事务与锁
了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...