您的位置:首页 > Web前端

uva10622 Perfect P-th Powers

2016-01-07 19:18 344 查看
留坑(p.343)

完全不知道哪里有问题qwq

从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>

using namespace std;

void setIO(const string& s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
template<typename Q> Q read(Q& x) {
static char c, f;
for(f = 0; c = getchar(), !isdigit(c); ) if(c == '-') f = 1;
for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0';
if(f) x = -x;
return x;
}
template<typename Q> Q read() {
static Q x; read(x); return x;
}

typedef long long LL;

LL qpow(LL a, LL b, LL Lim = ~0ull >> 1) {
for(LL c = 1; ; a *= a) {
if(b & 1) c *= a;
if(!(b >>= 1)) return c;
if(c > Lim || a > Lim) return -1;
}
}

bool exist(LL x, LL p) {
int l = 0, r = ~0u >> 1;
while(l <= r) {
int mid = l + (r - l) / 2;
LL res = qpow(mid, p, x);
if(res == x) return 1;
else if(res == -1 || res > x) r = mid - 1;
else l = mid + 1;
}
return 0;
}

int solve(LL n) {
if(n == 1) return 1;
int sign = 1;
if(n < 0) sign = -1, n = -n;
for(int p = 31; p >= 2; p -= (sign > 0 ? 1 : 2)) {
if(exist(n, p)) return p;
}
return 1;
}

int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n;
while(read(n)) {
printf("%d\n", solve(n));
}

return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: