結果

問題 No.1666 累乗数
ユーザー karinohitokarinohito
提出日時 2021-09-03 22:10:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,998 bytes
コンパイル時間 1,873 ms
コンパイル使用メモリ 173,036 KB
実行使用メモリ 21,564 KB
最終ジャッジ日時 2024-05-09 04:19:03
合計ジャッジ時間 3,057 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
19,884 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
#define all(A) A.begin(),A.end()
using vll = vector<ll>;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using Graph = vector<vector<ll>>;
vll P = { 2,3,5,7,11,13,17,19,23,29,31,37};
vector<vll> PP(P.size() - 1);
bool isOK(ll index, ll key) {

    ll k = 1;
    ll D = sqrt(index);
    D = max(0ll, D - 10);
    rep(i, 20) {
        if (D * D <= index) {
            D++;
        }
        else {
            D--;
            break;
        }
    }
    k += D - 1;


    rep(p, PP.size()) {
        auto D = upper_bound(all(PP[p]), index) - PP[p].begin();

        k += (D);
    }
    return (k >= key);


}

// 汎用的な二分探索のテンプレ
ll binary_search(ll key) {
    ll ng = 0; //「index = 0」が条件を満たすこともあるので、初期値は -1
    ll ok = key * key + 3; // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()

    /* ok と ng のどちらが大きいかわからないことを考慮 */
    while (abs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        cout << "";
        if (isOK(mid, key)) ok = mid;
        else ng = mid;
    }
    return ok;
}

int main() {
    rep(i, P.size() - 1) {
        ll p = P[i + 1];
        ll k = 2;
        while (1) {
            ll d = k;
            bool c = true;
            rep(u, p - 1) {
                if (d <= 2e18 / k) {
                    d *= k;
                }
                else {

                    c = false;
                    break;
                }
            }
            if (c) {
                PP[i].push_back(d);
            }
            else {
                PP[i].push_back(2e18);
                break;
            }
            k++;
            d = 0;
        }
    }
    ll T;
    cin >> T;
    rep(t, T) {
        ll N;
        cin >> N;
        cout << binary_search(N) << endl;
    }
}
0