結果

問題 No.2249 GCDistance
ユーザー V_MelvilleV_Melville
提出日時 2023-03-21 19:13:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 571 ms / 5,000 ms
コード長 1,046 bytes
コンパイル時間 4,089 ms
コンパイル使用メモリ 275,356 KB
実行使用メモリ 162,380 KB
最終ジャッジ日時 2023-10-18 18:32:15
合計ジャッジ時間 11,222 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 197 ms
162,380 KB
testcase_01 AC 564 ms
162,380 KB
testcase_02 AC 561 ms
162,380 KB
testcase_03 AC 567 ms
162,380 KB
testcase_04 AC 331 ms
162,380 KB
testcase_05 AC 562 ms
162,380 KB
testcase_06 AC 559 ms
162,380 KB
testcase_07 AC 562 ms
162,380 KB
testcase_08 AC 208 ms
162,380 KB
testcase_09 AC 571 ms
162,380 KB
testcase_10 AC 505 ms
162,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;

// totient table
vector<int> ps, pf, phi;
void sieve(int mx) {
    pf.resize(mx+1);
    phi.resize(mx+1);
    phi[1] = 1;
    rep(i, mx+1) pf[i] = i;
    for (int i = 2; i <= mx; ++i) {
        if (pf[i] == i) {
            ps.push_back(i);
            phi[i] = i-1;
        }
        rep(j, ps.size()) {
            int x = ps[j]*i;
            if (x > mx) break;
            pf[x] = ps[j];
            if (i%ps[j] == 0) {
                phi[x] = phi[i]*ps[j];
                break;
            }
            phi[x] = phi[i]*(ps[j]-1);
        }
    }
}

int main() {
    sieve(1e7);
    vector<ll> s(1e7+1);
    rep(i, 1e7) s[i+1] = s[i]+phi[i+1];
    
    int t;
    cin >> t;
    
    while (t--) {
        int n;
        cin >> n;
        ll ans = (ll)n*(n-1) - s[n] + 1;
        cout << ans << '\n';
    }
    
    return 0;
}
0