結果

問題 No.2249 GCDistance
ユーザー potoooooooo
提出日時 2023-03-21 01:49:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,077 ms / 5,000 ms
コード長 711 bytes
コンパイル時間 1,711 ms
コンパイル使用メモリ 193,388 KB
最終ジャッジ日時 2025-02-11 15:46:49
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr int max_val = 1e7;
long long tot[max_val + 1];
long long ans[max_val + 1];

void solve() {
    for (int i = 1; i <= max_val; i++) tot[i] = i;
    for (int i = 2; i <= max_val; i++) {
        if (tot[i] == i) {
            tot[i]--;
            for (int j = i + i; j <= max_val; j += i) {
                tot[j] = tot[j] / i * (i - 1);
            }
        }
    }
    for (int i = 1; i < max_val; i++) {
        ans[i + 1] = ans[i] + i + i - tot[i + 1];
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int T; cin >> T;
    solve();
    while (T--) {
        int n; cin >> n; cout << ans[n] << "\n";
    }
    return 0;
}
0