留坑(p.343)
完全不知道哪里有问题qwq
从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<iostream> 6 7 using namespace std; 8 9 void setIO(const string& s) { 10 freopen((s + ".in").c_str(), "r", stdin); 11 freopen((s + ".out").c_str(), "w", stdout); 12 } 13 template<typename Q> Q read(Q& x) { 14 static char c, f; 15 for(f = 0; c = getchar(), !isdigit(c); ) if(c == '-') f = 1; 16 for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0'; 17 if(f) x = -x; 18 return x; 19 } 20 template<typename Q> Q read() { 21 static Q x; read(x); return x; 22 } 23 24 typedef long long LL; 25 26 LL qpow(LL a, LL b, LL Lim = ~0ull >> 1) { 27 for(LL c = 1; ; a *= a) { 28 if(b & 1) c *= a; 29 if(!(b >>= 1)) return c; 30 if(c > Lim || a > Lim) return -1; 31 } 32 } 33 34 bool exist(LL x, LL p) { 35 int l = 0, r = ~0u >> 1; 36 while(l <= r) { 37 int mid = l + (r - l) / 2; 38 LL res = qpow(mid, p, x); 39 if(res == x) return 1; 40 else if(res == -1 || res > x) r = mid - 1; 41 else l = mid + 1; 42 } 43 return 0; 44 } 45 46 int solve(LL n) { 47 if(n == 1) return 1; 48 int sign = 1; 49 if(n < 0) sign = -1, n = -n; 50 for(int p = 31; p >= 2; p -= (sign > 0 ? 1 : 2)) { 51 if(exist(n, p)) return p; 52 } 53 return 1; 54 } 55 56 int main() { 57 #ifdef DEBUG 58 freopen("in.txt", "r", stdin); 59 freopen("out.txt", "w", stdout); 60 #endif 61 int n; 62 while(read(n)) { 63 printf("%d\n", solve(n)); 64 } 65 66 return 0; 67 }