結果

問題 No.2249 GCDistance
ユーザー umimelumimel
提出日時 2023-03-17 23:42:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,205 ms / 5,000 ms
コード長 2,475 bytes
コンパイル時間 1,805 ms
コンパイル使用メモリ 175,508 KB
実行使用メモリ 121,708 KB
最終ジャッジ日時 2023-10-18 16:36:58
合計ジャッジ時間 28,928 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,916 ms
121,708 KB
testcase_01 AC 2,182 ms
121,708 KB
testcase_02 AC 2,205 ms
121,708 KB
testcase_03 AC 2,185 ms
121,708 KB
testcase_04 AC 2,009 ms
121,708 KB
testcase_05 AC 2,179 ms
121,708 KB
testcase_06 AC 2,185 ms
121,708 KB
testcase_07 AC 2,187 ms
121,708 KB
testcase_08 AC 1,899 ms
121,708 KB
testcase_09 AC 2,193 ms
121,708 KB
testcase_10 AC 2,146 ms
121,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

#include <iostream>
#include <vector>
using namespace std;

// エラトステネスの篩
struct Eratosthenes {
    // テーブル
    vector<bool> isprime;

    // 整数 i を割り切る最小の素数
    vector<int> minfactor;

    // コンストラクタで篩を回す
    Eratosthenes(int N) : isprime(N+1, true),
                          minfactor(N+1, -1) {
        // 1 は予めふるい落としておく
        isprime[1] = false;
        minfactor[1] = 1;

        // 篩
        for (int p = 2; p <= N; ++p) {
            // すでに合成数であるものはスキップする
            if (!isprime[p]) continue;

            // p についての情報更新
            minfactor[p] = p;

            // p 以外の p の倍数から素数ラベルを剥奪
            for (int q = p * 2; q <= N; q += p) {
                // q は合成数なのでふるい落とす
                isprime[q] = false;

                // q は p で割り切れる旨を更新
                if (minfactor[q] == -1) minfactor[q] = p;
            }
        }
    }

    // 高速素因数分解
    // pair (素因子, 指数) の vector を返す
    vector<pair<int,int>> factorize(int n) {
        vector<pair<int,int>> res;
        while (n > 1) {
            int p = minfactor[n];
            int exp = 0;

            // n で割り切れる限り割る
            while (minfactor[n] == p) {
                n /= p;
                ++exp;
            }
            res.emplace_back(p, exp);
        }
        return res;
    }  
};

int MAX = 1e7;
vector<ll> sum(MAX+1, 0);
void solve(){
    ll n; cin >> n;
    cout << n*(n-1) - sum[n] << endl;
}

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

    Eratosthenes er(MAX+1);
    for(ll i=2; i<=MAX; i++){
        vector<pair<int, int>> fac = er.factorize(i);
        int euler = i;
        for(pair<int, int> tmp : fac){
            int p = tmp.fi;
            euler /= p;
            euler *= (p-1);
        }
        sum[i] = sum[i-1] + euler;
    }

    int t; cin >> t;
    rep(i, t) solve();
}
0