結果

問題 No.2249 GCDistance
ユーザー merom686merom686
提出日時 2023-03-17 22:46:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 975 ms / 5,000 ms
コード長 1,021 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 205,156 KB
実行使用メモリ 125,580 KB
最終ジャッジ日時 2023-10-18 15:47:45
合計ジャッジ時間 15,398 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 908 ms
125,576 KB
testcase_01 AC 975 ms
125,580 KB
testcase_02 AC 956 ms
125,580 KB
testcase_03 AC 959 ms
125,580 KB
testcase_04 AC 936 ms
125,580 KB
testcase_05 AC 961 ms
125,580 KB
testcase_06 AC 964 ms
125,580 KB
testcase_07 AC 952 ms
125,580 KB
testcase_08 AC 911 ms
125,580 KB
testcase_09 AC 950 ms
125,580 KB
testcase_10 AC 949 ms
125,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using namespace std;

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;
    }
    vector<int> l, p;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;

    int m = 1e7;
    vector<ll> a(m + 1);
    Sieve si(m);
    ll s = 0;
    for (int i = 2; i <= m; i++) {
        int t = i, u = i, p = 0;
        while (t > 1) {
            int l = si.l[t];
            if (l != p) {
                u = u / l * (l - 1);
                p = l;
            }
            t /= l;
        }
        s += (i - 1) * 2 - u;
        a[i] = s;
    }

    while (t--) {
        int n;
        cin >> n;

        cout << a[n] << '\n';
    }

    return 0;
}
0