結果

問題 No.1666 累乗数
ユーザー iiljjiiljj
提出日時 2021-09-03 23:10:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 698 ms / 2,000 ms
コード長 17,171 bytes
コンパイル時間 1,669 ms
コンパイル使用メモリ 145,120 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-09 06:50:20
合計ジャッジ時間 15,555 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
5,248 KB
testcase_01 AC 698 ms
5,376 KB
testcase_02 AC 695 ms
5,376 KB
testcase_03 AC 691 ms
5,376 KB
testcase_04 AC 694 ms
5,376 KB
testcase_05 AC 669 ms
5,376 KB
testcase_06 AC 669 ms
5,376 KB
testcase_07 AC 673 ms
5,376 KB
testcase_08 AC 664 ms
5,376 KB
testcase_09 AC 687 ms
5,376 KB
testcase_10 AC 687 ms
5,376 KB
testcase_11 AC 684 ms
5,376 KB
testcase_12 AC 681 ms
5,376 KB
testcase_13 AC 670 ms
5,376 KB
testcase_14 AC 670 ms
5,376 KB
testcase_15 AC 652 ms
5,376 KB
testcase_16 AC 648 ms
5,376 KB
testcase_17 AC 653 ms
5,376 KB
testcase_18 AC 651 ms
5,376 KB
testcase_19 AC 650 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* #region Head */

// #include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert> // assert.h
#include <cmath>   // math.h
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using vll = vc<ll>;
using vvll = vvc<ll>;
using vld = vc<ld>;
using vvld = vvc<ld>;
using vs = vc<string>;
using vvs = vvc<string>;
template <class T, class U> using um = unordered_map<T, U>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqa = priority_queue<T, vc<T>, greater<T>>;
template <class T> using us = unordered_set<T>;

#define TREP(T, i, m, n) for (T i = (m), i##_len = (T)(n); i < i##_len; ++(i))
#define TREPM(T, i, m, n) for (T i = (m), i##_max = (T)(n); i <= i##_max; ++(i))
#define TREPR(T, i, m, n) for (T i = (m), i##_min = (T)(n); i >= i##_min; --(i))
#define TREPD(T, i, m, n, d) for (T i = (m), i##_len = (T)(n); i < i##_len; i += (d))
#define TREPMD(T, i, m, n, d) for (T i = (m), i##_max = (T)(n); i <= i##_max; i += (d))

#define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i))
#define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i))
#define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i))
#define REPD(i, m, n, d) for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d))
#define REPMD(i, m, n, d) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d))
#define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define REPIR(itr, ds) for (auto itr = ds.rbegin(); itr != ds.rend(); itr++)
#define ALL(x) begin(x), end(x)
#define SIZE(x) ((ll)(x).size())
#define PERM(c)                                                                                                        \
    sort(ALL(c));                                                                                                      \
    for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define UNIQ(v) v.erase(unique(ALL(v)), v.end());
#define CEIL(a, b) (((a) + (b)-1) / (b))

#define endl '\n'

constexpr ll INF = 1'010'000'000'000'000'017LL;
constexpr int IINF = 1'000'000'007LL;
constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7
// constexpr ll MOD = 998244353;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.14159265358979323846;

template <typename T> istream &operator>>(istream &is, vc<T> &vec) { // vector 入力
    for (T &x : vec) is >> x;
    return is;
}
template <typename T> ostream &operator<<(ostream &os, const vc<T> &vec) { // vector 出力 (for dump)
    os << "{";
    REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "" : ", ");
    os << "}";
    return os;
}
template <typename T> ostream &operator>>(ostream &os, const vc<T> &vec) { // vector 出力 (inline)
    REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "\n" : " ");
    return os;
}

template <typename T, size_t _Nm> istream &operator>>(istream &is, array<T, _Nm> &arr) { // array 入力
    REP(i, 0, SIZE(arr)) is >> arr[i];
    return is;
}
template <typename T, size_t _Nm> ostream &operator<<(ostream &os, const array<T, _Nm> &arr) { // array 出力 (for dump)
    os << "{";
    REP(i, 0, SIZE(arr)) os << arr[i] << (i == i_len - 1 ? "" : ", ");
    os << "}";
    return os;
}

template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &pair_var) { // pair 入力
    is >> pair_var.first >> pair_var.second;
    return is;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &pair_var) { // pair 出力
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}

// map, um, set, us 出力
template <class T> ostream &out_iter(ostream &os, const T &map_var) {
    os << "{";
    REPI(itr, map_var) {
        os << *itr;
        auto itrcp = itr;
        if (++itrcp != map_var.end()) os << ", ";
    }
    return os << "}";
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    return out_iter(os, map_var);
}
template <typename T, typename U> ostream &operator<<(ostream &os, const um<T, U> &map_var) {
    os << "{";
    REPI(itr, map_var) {
        auto [key, value] = *itr;
        os << "(" << key << ", " << value << ")";
        auto itrcp = itr;
        if (++itrcp != map_var.end()) os << ", ";
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) { return out_iter(os, set_var); }
template <typename T> ostream &operator<<(ostream &os, const us<T> &set_var) { return out_iter(os, set_var); }
template <typename T> ostream &operator<<(ostream &os, const pq<T> &pq_var) {
    pq<T> pq_cp(pq_var);
    os << "{";
    if (!pq_cp.empty()) {
        os << pq_cp.top(), pq_cp.pop();
        while (!pq_cp.empty()) os << ", " << pq_cp.top(), pq_cp.pop();
    }
    return os << "}";
}

// tuple 出力
template <size_t N = 0, bool end_line = false, typename... Args> ostream &operator<<(ostream &os, tuple<Args...> &a) {
    if constexpr (N < std::tuple_size_v<tuple<Args...>>) {
        os << get<N>(a);
        if constexpr (N + 1 < std::tuple_size_v<tuple<Args...>>) {
            os << ' ';
        } else if constexpr (end_line) {
            os << '\n';
        }
        return operator<<<N + 1, end_line>(os, a);
    }
    return os;
}
template <typename... Args> void print_tuple(tuple<Args...> &a) { operator<<<0, true>(cout, a); }

void pprint() { cout << endl; }
template <class Head, class... Tail> void pprint(Head &&head, Tail &&...tail) {
    cout << head;
    if (sizeof...(Tail) > 0) cout << ' ';
    pprint(move(tail)...);
}

// dump
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) > 0) DUMPOUT << ", ";
    dump_func(move(tail)...);
}

// chmax (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>> bool chmax(T &xmax, const U &x, Comp comp = {}) {
    if (comp(xmax, x)) {
        xmax = x;
        return true;
    }
    return false;
}

// chmin (更新「される」かもしれない値が前)
template <typename T, typename U, typename Comp = less<>> bool chmin(T &xmin, const U &x, Comp comp = {}) {
    if (comp(x, xmin)) {
        xmin = x;
        return true;
    }
    return false;
}

// ローカル用
#ifndef ONLINE_JUDGE
#define DEBUG_
#endif

#ifdef DEBUG_
#define DEB
#define dump(...)                                                                                                      \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                                                                    \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl                                        \
            << "    ",                                                                                                 \
        dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

#define VAR(type, ...)                                                                                                 \
    type __VA_ARGS__;                                                                                                  \
    cin >> __VA_ARGS__;

template <typename T> istream &operator,(istream &is, T &rhs) { return is >> rhs; }
template <typename T> ostream &operator,(ostream &os, const T &rhs) { return os << ' ' << rhs; }

struct AtCoderInitialize {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;
    AtCoderInitialize() {
        ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
        cout << fixed << setprecision(IOS_PREC);
        if (AUTOFLUSH) cout << unitbuf;
    }
} ATCODER_INITIALIZE;

void Yn(bool p) { cout << (p ? "Yes" : "No") << endl; }
void YN(bool p) { cout << (p ? "YES" : "NO") << endl; }

/* #endregion */

// #include <atcoder/all>
// using namespace atcoder;

template <typename T = ll> vc<T> get_primes_eratosthenes(T n) {
    vc<char> iscomposite(n + 1, 0); // 合成数だとフラグが立つ
    vc<T> res;
    REPM(i, 2, n) {
        if (iscomposite[i]) continue;
        res.emplace_back(i);
        REPMD(j, i * i, n, i) iscomposite[j] = 1;
    }
    return res;
}

template <class tProposition, typename T = ll> T binarySearchIntMax(T left, T right, tProposition p) {
    if (right < left) return -1;
    T mid;
    while (left + 1 < right) {
        mid = (left + right) / 2;
        if (p(mid))
            left = mid;
        else // fn > 0
            right = mid - 1;
    }

    if (p(right))
        return right;
    else if (p(left))
        return left;
    else
        return -1;
}

template <class tProposition, typename T = ll> T binarySearchIntMin(T left, T right, tProposition p) {
    if (right < left) return -1;
    T mid;
    while (left + 1 < right) {
        mid = (left + right) / 2;
        if (p(mid))
            right = mid;
        else // fn > 0
            left = mid + 1;
    }

    if (p(left))
        return left;
    else if (p(right))
        return right;
    else
        return -1;
}

template <typename T = ll> inline constexpr T add_overflow(T x, T y) {
    T result = 0;
    if (__builtin_add_overflow(x, y, &result)) return std::numeric_limits<T>::max();
    return result;
}

template <typename T = ll> inline constexpr T mul_overflow(T x, T y) {
    T result = 0;
    if (__builtin_mul_overflow(x, y, &result)) return std::numeric_limits<T>::max();
    return result;
}

template <typename T = ll> T powll(T n, T t) {
    if (!t) return 1;
    // if (t == 1) return n;
    // dump(n, t, t >> 1);
    // assert(t >= 0);
    T a = powll(n, t >> 1);     // ⌊t/2⌋ 乗
    a = mul_overflow(a, a);     // ⌊t/2⌋*2 乗
    if (t & 1)                  // ⌊t/2⌋*2 == t-1 のとき
        a = mul_overflow(a, n); // ⌊t/2⌋*2+1 乗 => t 乗
    return a;
}

vll pn = {
    1,      4,      8,      9,      16,     25,     27,     32,     36,     49,     64,     81,     100,    121,
    125,    128,    144,    169,    196,    216,    225,    243,    256,    289,    324,    343,    361,    400,
    441,    484,    512,    529,    576,    625,    676,    729,    784,    841,    900,    961,    1000,   1024,
    1089,   1156,   1225,   1296,   1331,   1369,   1444,   1521,   1600,   1681,   1728,   1764,   1849,   1936,
    2025,   2048,   2116,   2187,   2197,   2209,   2304,   2401,   2500,   2601,   2704,   2744,   2809,   2916,
    3025,   3125,   3136,   3249,   3364,   3375,   3481,   3600,   3721,   3844,   3969,   4096,   4225,   4356,
    4489,   4624,   4761,   4900,   4913,   5041,   5184,   5329,   5476,   5625,   5776,   5832,   5929,   6084,
    6241,   6400,   6561,   6724,   6859,   6889,   7056,   7225,   7396,   7569,   7744,   7776,   7921,   8000,
    8100,   8192,   8281,   8464,   8649,   8836,   9025,   9216,   9261,   9409,   9604,   9801,   10000,  10201,
    10404,  10609,  10648,  10816,  11025,  11236,  11449,  11664,  11881,  12100,  12167,  12321,  12544,  12769,
    12996,  13225,  13456,  13689,  13824,  13924,  14161,  14400,  14641,  14884,  15129,  15376,  15625,  15876,
    16129,  16384,  16641,  16807,  16900,  17161,  17424,  17576,  17689,  17956,  18225,  18496,  18769,  19044,
    19321,  19600,  19683,  19881,  20164,  20449,  20736,  21025,  21316,  21609,  21904,  21952,  22201,  22500,
    22801,  23104,  23409,  23716,  24025,  24336,  24389,  24649,  24964,  25281,  25600,  25921,  26244,  26569,
    26896,  27000,  27225,  27556,  27889,  28224,  28561,  28900,  29241,  29584,  29791,  29929,  30276,  30625,
    30976,  31329,  31684,  32041,  32400,  32761,  32768,  33124,  33489,  33856,  34225,  34596,  34969,  35344,
    35721,  35937,  36100,  36481,  36864,  37249,  37636,  38025,  38416,  38809,  39204,  39304,  39601,  40000,
    40401,  40804,  41209,  41616,  42025,  42436,  42849,  42875,  43264,  43681,  44100,  44521,  44944,  45369,
    45796,  46225,  46656,  47089,  47524,  47961,  48400,  48841,  49284,  49729,  50176,  50625,  50653,  51076,
    51529,  51984,  52441,  52900,  53361,  53824,  54289,  54756,  54872,  55225,  55696,  56169,  56644,  57121,
    57600,  58081,  58564,  59049,  59319,  59536,  60025,  60516,  61009,  61504,  62001,  62500,  63001,  63504,
    64000,  64009,  64516,  65025,  65536,  66049,  66564,  67081,  67600,  68121,  68644,  68921,  69169,  69696,
    70225,  70756,  71289,  71824,  72361,  72900,  73441,  73984,  74088,  74529,  75076,  75625,  76176,  76729,
    77284,  77841,  78125,  78400,  78961,  79507,  79524,  80089,  80656,  81225,  81796,  82369,  82944,  83521,
    84100,  84681,  85184,  85264,  85849,  86436,  87025,  87616,  88209,  88804,  89401,  90000,  90601,  91125,
    91204,  91809,  92416,  93025,  93636,  94249,  94864,  95481,  96100,  96721,  97336,  97344,  97969,  98596,
    99225,  99856,  100000, 100489, 101124, 101761, 102400, 103041, 103684, 103823, 104329, 104976, 105625, 106276,
    106929, 107584, 108241, 108900, 109561, 110224, 110592, 110889, 111556, 112225, 112896, 113569, 114244, 114921,
    115600, 116281, 116964, 117649, 118336, 119025, 119716, 120409, 121104, 121801, 122500, 123201, 123904, 124609,
    125000, 125316, 126025, 126736, 127449, 128164, 128881, 129600, 130321, 131044, 131072, 131769, 132496, 132651,
    133225, 133956, 134689, 135424, 136161, 136900, 137641, 138384, 139129, 139876, 140608, 140625, 141376, 142129,
    142884, 143641, 144400, 145161, 145924, 146689, 147456, 148225, 148877, 148996, 149769, 150544, 151321, 152100,
    152881, 153664, 154449, 155236, 156025, 156816, 157464, 157609, 158404, 159201, 160000, 160801, 161051, 161604,
    162409, 163216, 164025, 164836, 165649, 166375, 166464, 167281, 168100, 168921, 169744, 170569, 171396, 172225,
    173056, 173889, 174724, 175561, 175616, 176400, 177147, 177241, 178084, 178929, 179776, 180625, 181476, 182329,
    183184, 184041, 184900, 185193, 185761, 186624, 187489, 188356, 189225, 190096, 190969, 191844, 192721, 193600,
    194481, 195112, 195364, 196249, 197136, 198025, 198916, 199809, 200704, 201601, 202500, 203401, 204304, 205209,
    205379, 206116, 207025, 207936, 208849, 209764, 210681, 211600, 212521, 213444, 214369, 215296, 216000, 216225,
    217156, 218089, 219024, 219961, 220900, 221841, 222784, 223729, 224676, 225625, 226576, 226981, 227529, 228484,
    229441, 230400, 231361, 232324, 233289, 234256, 235225, 236196, 237169, 238144, 238328, 239121, 240100, 241081,
    242064, 243049, 244036, 245025, 246016, 247009, 248004, 248832, 249001, 250000, 250047, 251001, 252004, 253009,
    254016, 255025, 256036, 257049, 258064, 259081, 260100, 261121, 262144, 263169, 264196, 265225, 266256, 267289,
    268324, 269361, 270400, 271441, 272484, 273529, 274576, 274625, 275625, 276676, 277729, 278784};

ll get_kth_perfect_power(const ll k, const vll &primes) {
    // a^p <= n となる a がいくつあるか数える
    // それをすべての p について足す
    // 合計個数が k 個以上になるような最小の n が答え
    ll sz = SIZE(primes);
    ll n = binarySearchIntMin(1LL, INF, [&](ll mid) -> bool {
        ll cnt = 1;
        auto dfs = [&](auto &&dfs, ll val = 1, ll cnt_p = 0, ll ptr = 0) -> void {
            if (val > 64) return;
            if (ptr == sz) {
                if (val == 1) return;
                ll bmax = binarySearchIntMax(1LL, INF, [&](ll mid2) -> bool { return powll(mid2, val) <= mid; });
                if (bmax == -1) return;
                ll dif = bmax - 1; // 1 を除く
                cnt += (cnt_p % 2 == 0 ? -dif : dif);
                return;
            } else {
                dfs(dfs, val, cnt_p, ptr + 1);
                dfs(dfs, val * primes[ptr], cnt_p + 1, ptr + 1);
            }
        };
        dfs(dfs);
        return cnt >= k;
    });
    assert(n != -1);
    return n;
}

void check(const vll &primes) {
    dump(SIZE(pn), primes);
    REPM(i, 1, 600) {
        ll target = pn[i - 1];
        ll ans = get_kth_perfect_power(i, primes);
        pprint(i, target, ans);
        std::flush(std::cout);
        assert(ans == target);
    }
}

// ★ 指数部を素数に限定しても,複数通りの表し方がある
//    包除で適切に取り除く必要がある

// Problem
void solve() {
    vll primes = get_primes_eratosthenes(64LL);

    // check(primes);
    // return;

    VAR(ll, t);
    REP(_case, 0, t) {
        VAR(ll, k); //
        ll n = get_kth_perfect_power(k, primes);
        pprint(n);
    }
}

// entry point
int main() {
    solve();
    return 0;
}
0