結果
| 問題 | No.2249 GCDistance | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-03-17 23:42:16 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,930 ms / 5,000 ms | 
| コード長 | 2,475 bytes | 
| コンパイル時間 | 1,616 ms | 
| コンパイル使用メモリ | 175,884 KB | 
| 実行使用メモリ | 121,600 KB | 
| 最終ジャッジ日時 | 2024-09-18 12:38:06 | 
| 合計ジャッジ時間 | 24,626 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 10 | 
ソースコード
#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();
}
            
            
            
        