codeforces 426 Div2C 833A The Meaningless Game(二分||数学)
来源:互联网 发布:徐静蕾字体字帖淘宝 编辑:程序博客网 时间:2024/06/15 18:31
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,对于他们每次的操作只能是a/k,b/k^2或者a/k^2,b/k。(每次都必须是整除)问最后他们是否可以同时变成1。
思路:假设满足条件,则必定存在s,p满足 s^2*p = a,p^2*s = b。如果确定一个s,那么p = a/s^2。p^2*s = a^2/s^3 = b。这是单调函数,所以,就可以使用二分。
#include<iostream>#include<cmath>#include<cstdio>#define MAX 1000000#define ll long longusing namespace std;ll a,b;bool Judge(ll num){return a*a/num/num/num >= b;}int main( ){int n;scanf("%d",&n);while(n--){scanf("%d%d",&a,&b);ll left = 1,right = MAX;while((right - left) > 1){ll mid = left + ((right - left)>>1);if(Judge(mid))left = mid;elseright = mid;}int s = left,p = a/left/left;if(s*s*p == a && p*p*s == b)printf("Yes\n");elseprintf("No\n");}}复杂度 O(n*k)
优化思路:a = s^2*p,b = p^2*s。a*b = s^3*p^3。(s*p)^3 = a*b,同时 a%(s*p) == 0 && b%(s*p) == 0。所以,直接求出a*b的立方根即可。
#include<cmath>#include<cstdio>#define ll long longusing namespace std;ll a,b;int main( ){int n;scanf("%d",&n);while(n--){scanf("%lld%lld",&a,&b);ll c = pow(a*b,1.0/3) + 0.5;puts(c*c*c==a*b && a%c==0 && b%c==0 ? "Yes":"No");}}
只不过因为从小数化正数的过程中会出现各种问题。所以,如果直接写的话不会AC。
- codeforces 426 Div2C 833A The Meaningless Game(二分||数学)
- (数学或二分)Codeforces Round #426 C. The Meaningless Game
- CodeFroces 833A. The Meaningless Game(数学)
- Codeforces Round 833A-The Meaningless Game
- Codeforces 833A-The Meaningless Game
- codeforces The Meaningless Game 数学思维
- 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 833 A.The Meaningless Game(水~)
- Codeforces Round #426 (Div. 1) A. The Meaningless Game
- (CodeForces 883A) The Meaningless Game 二分答案思路+骚操作
- 【CodeForces】426Div2 C The Meaningless Game
- CodeForces #426 div2 C The Meaningless Game
- codeforce The Meaningless Game(二分+数学思维)
- Codeforces 834 C The Meaningless Game(二分)
- Codeforces Round #426 (Div. 2) C. The Meaningless Game(简单数学)
- The Meaningless Game 【codeforces】【思维】
- Shiro集成CAS登录成功跳转地址问题
- android实现图片圆角和圆形
- 【Selenium】6操作控件
- 【轻松同传】-有TA,走遍全球都不怕!
- SQL
- codeforces 426 Div2C 833A The Meaningless Game(二分||数学)
- java操作时间,将当前时间减一年,减一天,减一个月
- 带你走进多线程的世界(多线程实现方式)
- 开启mysql慢查询日志
- 网络号、主机号、子网号、子网掩码、子网划分
- Linux-进程控制
- Silver Cow Party
- emoji表情处理保存到数据库
- socialite包简单记录