zoukankan      html  css  js  c++  java
  • uva10622 Perfect P-th Powers

    留坑(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
    ", solve(n));
    64     }
    65     
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    poj 3273 Monthly Expense(贪心+二分)
    codeforces 235 div2 C Team
    ZOJ 3607 Lazier Salesgirl(贪心)
    poj 1185 炮兵阵地(三维状态压缩dP)
    poj 2411 Mondriaan's Dream(状态压缩dP)
    sdut 2819 比赛排名(边表 拓扑排序)
    hdu 1421 搬寝室(dp)
    hdu 1243 反恐训练营(dp 最大公共子序列变形)
    Codeforces Round #232 (Div. 2) B. On Corruption and Numbers
    hdu 1559 最大子矩阵 (简单dp)
  • 原文地址:https://www.cnblogs.com/showson/p/5110962.html
Copyright © 2011-2022 走看看