結果

問題 No.1666 累乗数
ユーザー sapphire__15sapphire__15
提出日時 2021-09-03 22:45:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 3,855 bytes
コンパイル時間 1,296 ms
コンパイル使用メモリ 136,496 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 23:48:27
合計ジャッジ時間 6,640 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 66 ms
4,376 KB
testcase_02 AC 86 ms
4,380 KB
testcase_03 AC 93 ms
4,376 KB
testcase_04 AC 98 ms
4,380 KB
testcase_05 AC 339 ms
4,376 KB
testcase_06 AC 339 ms
4,380 KB
testcase_07 AC 339 ms
4,376 KB
testcase_08 AC 339 ms
4,376 KB
testcase_09 AC 166 ms
4,380 KB
testcase_10 AC 178 ms
4,380 KB
testcase_11 AC 182 ms
4,376 KB
testcase_12 AC 182 ms
4,380 KB
testcase_13 AC 317 ms
4,376 KB
testcase_14 AC 317 ms
4,376 KB
testcase_15 AC 318 ms
4,380 KB
testcase_16 AC 318 ms
4,380 KB
testcase_17 AC 239 ms
4,376 KB
testcase_18 AC 263 ms
4,376 KB
testcase_19 AC 257 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>
#include <iomanip>
#include <limits>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <cstring>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<ll, ll> Pll;
typedef std::pair<double, double> Pdd;

#define rip(_i, _n, _s) for (int _i = (_s); _i < (int)(_n); _i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<

template<typename _T>
using MaxHeap = std::priority_queue<_T>;
template<typename _T>
using MinHeap = std::priority_queue<_T, std::vector<_T>, std::greater<_T>>;

template<typename _T>
inline bool chmax(_T &_l, const _T _b) {
    if (_b > _l) {
        _l = _b;
        return true;
    }
    return false;
}
template<typename _T>
inline bool chmin(_T &_l, const _T _b) {
    if (_l > _b) {
        _l = _b;
        return true;
    }
    return false;
}

template<typename _T>
void vdeb(const std::vector<_T> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        if (i == bb.size() - 1) std::cout << bb[i];
        else std::cout << bb[i] << ' ';
    }
    std::cout << '\n';
}
template<typename _T>
void vdeb(const std::vector<std::vector<_T>> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        std::cout << i << ": ";
        vdeb(bb[i]);
    }
    std::cout << '\n';
}

using namespace std;

struct Solver{

    vector<int> pris, mebius;
    
    Solver() {
        const int M = 60;
        vector<int> da(M, 1), ps(M, 1);
        rip(i,M,2) {
            if(ps[i]) {
                int j = i*2;
                da[i] *= -1;
                while(j < M) {
                    da[j] *= -1;
                    ps[j] = 0;
                    j += i;
                }
                j = i*i;
                while(j < M) {
                    da[j] = 0;
                    j += i*i;
                }
            }
        }
        pris = vector<int>();
        mebius = vector<int>();
        rip(i, M, 2) {
            if(da[i]) {
                pris.push_back(i);
                mebius.push_back(da[i]);
            }
        }
    }

    ll power(ll b, ll p, ll n) {
        ll ret = 1, now = b;
        while(p) {
            if(p&1) {
                if(n/now < ret) {
                    return true;
                } else {
                    ret *= now;
                }
            }
            p /= 2;
            if(p) {
                if(now > n/now) {
                    return true;
                }else {
                    now = now * now;
                }
            }
                
        }
        return ret > n;
    }

    ll cal(ll n, ll p) {
        ll y = pow((double)n, 1.0/p);
        ll ok = y+4, ng = max(y - 4, 0LL);
        while(ok-ng > 1) {
            ll now = (ok + ng)/2;
            // if(power(now, p) >= n)
            if(power(now, p, n)) {
                ok = now;
            } else {
                ng = now;
            }
        }
        // cerr << n MM p MM ng << endl;
        return ng;
    }

    ll cnt(ll x) {
        ll ret = 1;
        rip(i, mebius.size(), 0) {
            ret -= (cal(x, pris[i])-1) * mebius[i];
        }
        // cerr << x MM ret << endl;
        return ret;
    }

    ll solve() {
        ll k; cin >> k;
        ll ok = k*k, ng = 1;
        while(ok - ng > 1) {
            ll now = (ok + ng) / 2;
            if(cnt(now) < k) ng = now;
            else ok = now;
        }
        return ok;
    }
};

int main() {
    Solver sol;
    int t; cin >> t;
    vector<ll> ans(t);
    rip(i,t,0) ans[i] = sol.solve();
    for(auto &i : ans) cout << i << endl;
}
0