結果

問題 No.1666 累乗数
ユーザー karinohitokarinohito
提出日時 2021-09-03 23:42:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 315 ms / 2,000 ms
コード長 1,741 bytes
コンパイル時間 1,948 ms
コンパイル使用メモリ 175,336 KB
実行使用メモリ 59,864 KB
最終ジャッジ日時 2023-08-22 01:58:09
合計ジャッジ時間 9,337 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 302 ms
58,496 KB
testcase_01 AC 306 ms
59,664 KB
testcase_02 AC 305 ms
58,380 KB
testcase_03 AC 309 ms
59,864 KB
testcase_04 AC 307 ms
58,128 KB
testcase_05 AC 310 ms
59,020 KB
testcase_06 AC 312 ms
59,836 KB
testcase_07 AC 311 ms
58,368 KB
testcase_08 AC 315 ms
58,804 KB
testcase_09 AC 309 ms
58,320 KB
testcase_10 AC 307 ms
58,840 KB
testcase_11 AC 308 ms
59,004 KB
testcase_12 AC 306 ms
59,224 KB
testcase_13 AC 307 ms
59,532 KB
testcase_14 AC 312 ms
58,696 KB
testcase_15 AC 309 ms
58,636 KB
testcase_16 AC 312 ms
59,684 KB
testcase_17 AC 308 ms
58,260 KB
testcase_18 AC 310 ms
59,660 KB
testcase_19 AC 307 ms
58,508 KB
権限があれば一括ダウンロードができます

ソースコード

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 = { 3,5,7,11,13,17,19,23,29,31,37 };
vector<vll> PP(P.size());
set<ll> S;
vll PPP;
ll sqrtz(ll N) {
    ll L = 0;
    ll R = sqrt(N)+10000;
    while (abs(R - L) > 1) {
        ll mid = (R +L) / 2;
        if (mid * mid <= N)L = mid;
        else R = mid;
    }
    return L;
    
}



bool isOK(ll index, ll key) {

    ll k = 0;
    ll D = sqrtz(index);
    k += D;



    auto Ds = upper_bound(all(PPP), index) - PPP.begin();

    k += (Ds);

    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() {
    //set<ll> S;
    S.insert(2e18);
    for (ll p = 2; p <= 1e6+4; p++) {
        ll k = sqrtz(p);
        if (k * k == p)continue;
        ll d = p;
        while (d <= (1e18 + 6) / (p * p)) {
            d *= p * p;
            S.insert(d);
        }
    }
    for (auto l : S) {
        PPP.push_back(l);
    }
    ll T;
    cin >> T;
    rep(t, T) {
        ll N;
        cin >> N;
        cout << binary_search(N) << endl;
    }
}
0