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世纪提出的一种在实数域和复数域上近似求解方程的方法.多数方程不存在求根公式,因此求精确根非常困难,甚至不可能,从而寻找方程的近似根就显得特 ...
随机推荐
- Cocos Creator 音频API控制调频
*****音频的一些控制***** cc.audioEngine.playMusic(this.BGAudio,true);//播放音乐(true代表循环) cc.audioEngine.stopMu ...
- UVALi 3263 That Nice Euler Circuit(几何)
That Nice Euler Circuit [题目链接]That Nice Euler Circuit [题目类型]几何 &题解: 蓝书P260 要用欧拉定理:V+F=E+2 V是顶点数; ...
- Mac虚拟机上使用Genumotion模拟器
在Mac虚拟机系统上开发ReactNative的IOS应用非常方便,只要安装Xcode即可, 但 Android应用就需要三个步骤: 首先声明,下载Android SDK会非常慢,最好有快速的网络或 ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺7.0
1.每天举行站立式会议,提供当天站立式会议照片一张. 2.项目每个成员的昨天进展.存在问题.今天安排. 成员 昨天已完成 今天计划完成 郭炜埕 实现前端各界面的跳转连接 学习后端相关知识 郑晓丽 完善 ...
- php核心纪要 整理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- flask 在视图函数中验证表单
在视图函数中验证表单 因为现在的basic_form视图同时接受两种类型的请求:GET请求和POST请求.所以我们要根据请求方法的不同执行不同的代码.具体来说,首先是实例化表单,如果是GET请求,就渲 ...
- LoadRunner录制登录机票网址,并回放,加断言
回放录制登录过程脚本,加断言 在页面登录的过程如下: 1先进入http://127.0.0.1:1080/WebTours/index.htm 2之后获取userSession信息 3在输入信息后点击 ...
- Fabric架构:抽象的逻辑架构与实际的运行时架构
Fabric从1.X开始,在扩展性及安全性上面有了大大的提升,且新增了诸多的新特性: 多通道:支持多通道,提高隔离安全性. 可拔插的组件:支持共识组件.权限管理组件等可拔插功能. 账本数据可被存储为多 ...
- POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)
题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...
- shell脚本和python脚本实现批量ping IP测试
先建一个存放ip列表的txt文件: [root@yysslopenvpn01 ~]# cat hostip.txt 192.168.130.1 192.168.130.2 192.168.130.3 ...