結果

問題 No.2249 GCDistance
ユーザー ruthenruthen
提出日時 2023-03-17 23:52:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 464 ms / 5,000 ms
コード長 2,032 bytes
コンパイル時間 2,335 ms
コンパイル使用メモリ 205,616 KB
実行使用メモリ 198,676 KB
最終ジャッジ日時 2023-10-18 16:38:57
合計ジャッジ時間 9,188 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 397 ms
198,676 KB
testcase_01 AC 447 ms
198,676 KB
testcase_02 AC 450 ms
198,676 KB
testcase_03 AC 445 ms
198,676 KB
testcase_04 AC 425 ms
198,676 KB
testcase_05 AC 452 ms
198,676 KB
testcase_06 AC 447 ms
198,676 KB
testcase_07 AC 464 ms
198,676 KB
testcase_08 AC 399 ms
198,676 KB
testcase_09 AC 453 ms
198,676 KB
testcase_10 AC 431 ms
198,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

std::vector<int> totient_table(int n) {
    std::vector<int> res(n + 1);
    std::iota(res.begin(), res.end(), 0);
    for (int p = 2; p <= n; p++) {
        if (res[p] != p) continue;
        for (int i = p; i <= n; i += p) {
            res[i] /= p;
            res[i] *= p - 1;
        }
    }
    return res;
}

template <class T> struct CumulativeSum {
    int n;
    std::vector<T> seg;

    CumulativeSum() = default;

    CumulativeSum(int n) : n(n), seg(n + 1, T(0)) {}

    CumulativeSum(std::vector<T> &a) {
        n = (int)a.size();
        seg.assign(n + 1, T(0));
        for (int i = 0; i < n; i++) seg[i + 1] = seg[i] + a[i];
    }

    // [l, r)
    T sum(int l, int r) const {
        assert(0 <= l and l <= r and r <= n);
        return seg[r] - seg[l];
    }

    // A[l] += x, A[l + 1] += x, ... , A[r - 1] += x
    void imos(int l, int r, T x = T(1)) {
        assert(0 <= l and l <= r and r <= n);
        seg[l] += x;
        seg[r] -= x;
    }

    void build() {
        for (int i = 0; i < n; i++) seg[i + 1] += seg[i];
    }

    // return A[p]
    T get(int p) const {
        assert(0 <= p and p < n);
        return seg[p];
    }

    // output
    friend std::ostream &operator<<(std::ostream &os, const CumulativeSum &A) {
        os << "n = " << A.n << "\n";
        for (int i = 0; i <= A.n; i++) os << A.seg[i] << " \n"[i == A.n];
        return os;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    auto ttt = totient_table(10000000);
    V<long long> tl(10000001);
    rep(i, 10000001) tl[i] = ttt[i];
    CumulativeSum<long long> seg(tl);
    int tt;
    cin >> tt;
    while (tt--) {
        long long N;
        cin >> N;
        long long ans = N * (N - 1) - seg.sum(0, N + 1) + 1;
        cout << ans << '\n';
    }
    return 0;
}
0