結果

問題 No.1058 素敵な数
ユーザー merom686merom686
提出日時 2020-05-22 21:43:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,254 bytes
コンパイル時間 1,369 ms
コンパイル使用メモリ 101,692 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 16:24:51
合計ジャッジ時間 1,911 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,248 KB
testcase_01 AC 4 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

constexpr int K = (int)1e5;

struct Sieve {
    Sieve(int n) : l(n + 1, 0) {
        for (int i = 2; i <= n; i++) {
            if (!l[i]) p.push_back(l[i] = i);
            for (int q : p) {
                if (q > l[i]) break;
                if (int j = i * q; j <= n) l[j] = q; else break;
            }
        }
    }
    bool is_prime(int i) {
        return l[i] == i;
    }
    void solve() {
        int n;
        cin >> n;

        n--;
        if (n == 0) {
            cout << 1 << endl;
            exit(0);
        }

        int m = n;

        vector<int> a;
        for (int i = K + 1;; i++) {
            if (is_prime(i)) {
                a.push_back(i);
                if (--m == 0) break;
            }
        }

        vector<ll> b;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                b.push_back((ll)a[i] * a[j]);
            }
        }
        sort(b.begin(), b.end());

        cout << b[n - 1] << endl;
    }
    vector<int> l, p;
};

int main() {
    Sieve si(K * 2);

    si.solve();

    return 0;
}
0