結果

問題 No.2249 GCDistance
ユーザー Today03
提出日時 2025-02-13 20:18:27
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,403 ms / 5,000 ms
コード長 781 bytes
コンパイル時間 3,617 ms
コンパイル使用メモリ 275,908 KB
実行使用メモリ 159,492 KB
最終ジャッジ日時 2025-02-13 20:18:51
合計ジャッジ時間 22,401 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

vector<ll> totientTable(ll n) {
    vector<ll> ret(n + 1);
    iota(ret.begin(), ret.end(), 0);
    for (ll i = 2; i <= n; i++) {
        if (ret[i] == i) {
            for (ll j = i; j <= n; j += i) ret[j] = ret[j] / i * (i - 1);
        }
    }
    return ret;
}

/*
    考察
    dist(i,j)=
        1(gcd(i,j)=1)
        2(else)
*/

int main() {
    int T;
    cin >> T;

    const int mx = 1e7 + 5;
    auto tot = totientTable(mx);
    tot[1]--;
    vector<ll> pref(mx + 1);
    for (int i = 1; i < mx; i++) pref[i + 1] = pref[i] + tot[i] + (i - tot[i] - 1) * 2;

    while (T--) {
        int N;
        cin >> N;
        cout << pref[N + 1] << '\n';
    }
}
0