Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
来源:互联网 发布:裴乐品牌知乎 编辑:程序博客网 时间:2024/06/15 06:43
![](http://codeforces.com/predownloaded/90/3a/903ab1fdd929cfd0e0b359f5548a09b88e417265.png)
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).
62 475 458 816 16247 9941000000000 1000000
YesYesYesNoNoYes
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.
这个题是多么的有意思!开始我还想用搜索做呢,后来一想,把输入的a和b相乘,不应该就是某个数的三次方嘛!!(因为他俩总是有个人得平方,另一个人得一次方),假设进行n局,数字分别为k1,k2,k3......kn,所以他们得分相乘之后就应该是(k1*k2*k3......kn)^3。
所以我们只去看一看有没有这样一个数的三次方等于a*b就可以了。
给大家提供两种思路
①二分 ②把输入的a*b,开三次方根
①
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;bool find(ll m){int l = 1,r = 1e6;//1e6的三次幂就是1e18,正好是1e9的平方ll mid;while(l<= r)//二分查找{mid = (l+r)/2;if(mid*mid*mid == m)return 1;if(mid*mid*mid< m)l = mid+1;elser = mid-1;}return 0;}int main(){int n;cin>>n;while(n--){ll a,b;scanf("%lld %lld",&a,&b);if(a*a%b == 0&&b*b%a == 0&&find(a*b))//前两个条件是很重要的,这样保证了每一次round,都是按照k^2和k,这样分的printf("YES\n"); //能找到某个数的3次方等于a*b只是必要条件,还要加上上面一条才可以elseprintf("NO\n");}return 0;}
②
#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>using namespace std;typedef long long ll;long long a,b,c;int T;void solve(){ scanf("%lld %lld",&a,&b); c=a*b; c=round(pow((double)c,1.0/3)); if (c*c*c!=a*b||(a%c)>0||(b%c)>0) printf("NO\n"); else printf("YES\n");}int main(){ scanf("%d",&T); while (T--) solve();}
精写文章
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- Codeforces Round #426 (Div. 2) C. The Meaningless Game 思维 D. The Bakery dp
- 好题-Round #426 (Div. 2)C-二分&&思维||质因数分解-The Meaningless Game
- Codeforces Round #426 (Div. 2) C:The Meaningless Game(思维)
- Codeforces Round #426 (Div. 2) C. The Meaningless Game C. The Meaningless Game
- Codeforces Round #426 (Div. 2) C:The Meaningless Game The Meaningless Game
- (Codeforces Round #426 (Div. 2)) C.The Meaningless Game
- Codeforces Round #426 (Div. 2) C. The Meaningless Game
- Codeforces Round #426 (Div. 2)-C. The Meaningless Game
- Codeforces Round #426 (Div. 2) C The Meaningless Game
- Codeforces Round #426 (Div. 2) C. The Meaningless Game
- Codeforces Round #426 (Div. 2) C. The Meaningless Game
- Codeforces Round #426 (Div. 2)The Meaningless Game【数学题】【水题】
- Codeforces Round #426 (Div. 2) C The Meaningless Game
- (数学或二分)Codeforces Round #426 C. The Meaningless Game
- Codeforces Round #426 (Div. 1) A. The Meaningless Game
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- 问题总结
- A. Vasya and Robot----思维题
- java使用SAX解析XML
- HDFS
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- C. Vasya and Beautiful Arrays----思维题
- [PAT乙级]1022. D进制的A+B (20)
- (Codeforces Round #426 (Div. 2)) C.The Meaningless Game
- Codeforces Round #426 (Div. 2) B. The Festive Evening(思维)
- java操作Redis(哨兵或单机模式)
- Linux下Nginx的安装
- 在MVC 中使用 autofac 实现自动注册
- Luogu P2196 [NOIP1996]挖地雷