您的位置:首页 > 其它

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

2017-08-15 21:31 531 查看
/**
* 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 = 1, r = 1e6;
while(l <= r) {
LL mid = (l + r) >> 1;
if(mid * mid * mid <= x)    l = mid + 1;
else r = mid - 1;
}
return l - 1;
}

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 0;
}


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 + 1];
inline void rinit() {
for(int i = 1; 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 + 1, arr3 + limit + 1, 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 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: