码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 833A The Meaningless Game - 数论 - 牛顿迭代法 - 二分法

时间:2017-08-15 21:49:22      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:let   1.4   been   call   final   display   ges   ini   ext   

技术分享

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

技术分享
 1 /**
 2  * Codeforces
 3  * Problem#833A
 4  * Accepted
 5  * Time: 468ms
 6  * Memory: 2100k 
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 typedef bool boolean;
11 #define double long double
12 
13 const double eps = 1e-1;
14 double sqrt3(double x) {
15     double r = x;
16     double f = 1, k;
17     do {
18         f = r * r * r - x;
19         k = 3 * r * r;
20         r -= f / k;
21     } while (f > eps);
22     return r;
23 }
24 
25 int a, b;
26 long long P;
27 inline void init() {
28     scanf("%d%d", &a, &b);
29     P = a * 1LL * b;
30 }
31 
32 inline boolean solve() {
33     long long x = sqrt3(P);
34     if(x * x * x != P)    return false;
35     return !(a % x || b % x);
36 }
37 
38 int T;
39 int main() {
40     scanf("%d", &T);
41     while(T--) {
42         init();
43         puts(solve() ? ("Yes") : ("No"));
44     }
45     return 0;
46 }
The Meaningless Game(Newton‘s Methon)

  然后写了一个二分法,快了将近一倍。

Code

技术分享
 1 /**
 2  * Codeforces
 3  * Problem#833A
 4  * Accepted
 5  * Time: 218ms
 6  * Memory: 2052k
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 typedef bool boolean;
11 #define LL long long
12 
13 int sqrt3(LL x) {
14     int l = 1, r = 1e6;
15     while(l <= r) {
16         LL mid = (l + r) >> 1;
17         if(mid * mid * mid <= x)    l = mid + 1;
18         else r = mid - 1;
19     }
20     return l - 1;
21 }
22 
23 int a, b;
24 long long P;
25 inline void init() {
26     scanf("%d%d", &a, &b);
27     P = a * 1LL * b;
28 }
29 
30 inline boolean solve() {
31     long long x = sqrt3(P);
32     if(x * x * x != P)    return false;
33     return !(a % x || b % x);
34 }
35 
36 int T;
37 int main() {
38     scanf("%d", &T);
39     while(T--) {
40         init();
41         puts(solve() ? ("Yes") : ("No"));
42     }
43     return 0;
44 }
The Meaningless Game(Binary Search)

  最后写了一个可以用Hash的二分法(Excuse me?新型long long开方向下取整?)

Code

 1 /**
 2  * Codeforces
 3  * Problem#833A
 4  * Accepted
 5  * Time: 233ms
 6  * Memory: 9900k
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 typedef bool boolean;
11 #define LL long long
12 
13 const int limit = 1e6;
14 LL arr3[limit + 1];
15 inline void rinit() {
16     for(int i = 1; i <= limit; i++)
17         arr3[i] = i * 1LL * i * i;
18 } 
19 
20 int a, b;
21 long long P;
22 inline void init() {
23     scanf("%d%d", &a, &b);
24     P = a * 1LL * b;
25 }
26 
27 inline boolean solve() {
28     int x = lower_bound(arr3 + 1, arr3 + limit + 1, P) - arr3; 
29     if(arr3[x] != P)    return false;
30     return !(a % x || b % x);
31 }
32 
33 int T;
34 int main() {
35     rinit();
36     scanf("%d", &T);
37     while(T--) {
38         init();
39         puts(solve() ? ("Yes") : ("No"));
40     }
41     return 0;
42 }

Codeforces 833A The Meaningless Game - 数论 - 牛顿迭代法 - 二分法

标签:let   1.4   been   call   final   display   ges   ini   ext   

原文地址:http://www.cnblogs.com/yyf0309/p/7367888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!