結果

問題 No.2249 GCDistance
ユーザー roaris
提出日時 2023-03-17 22:56:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,646 ms / 5,000 ms
コード長 1,541 bytes
コンパイル時間 2,174 ms
コンパイル使用メモリ 200,332 KB
最終ジャッジ日時 2025-02-11 14:07:53
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
typedef long long ll;

int64_t euler_phi(int64_t n) {
  int64_t ret = n;
  for(int64_t i = 2; i * i <= n; i++) {
    if(n % i == 0) {
      ret -= ret / i;
      while(n % i == 0) n /= i;
    }
  }
  if(n > 1) ret -= ret / n;
  return ret;
}

struct Fast_factorize {
    vector<int> p;
    
    Fast_factorize(int n) {
        p.resize(n+1);
        fill(p.begin(), p.end(), -1);
        for (int i=2; i<=n; i++) if (p[i]==-1) {
            for (int j=i; j<=n; j+=i) {
                if (p[j]==-1) p[j] = i;
            }
        }
    }
    
    vector<int> factorize(int n) {
        vector<int> res;
        while (n>1) {
            res.pb(p[n]);
            n /= p[n];
        }
        return res;
    }
};


int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    
    int MAX = 10000010;
    Fast_factorize ff(MAX);
    ll phi[MAX];
    phi[0] = 0ll;
    
    for (int i=1; i<MAX; i++) {
        phi[i] = (ll)(i);
        vector<int> ps = ff.factorize(i);
        sort(ps.begin(), ps.end());
        ps.erase(unique(ps.begin(), ps.end()), ps.end());
        int x = i;
        for (int p : ps) {
            phi[i] -= phi[i]/p;
            while (x%p==0) x /= p;
        }
        if (x>1) phi[i] -= phi[i]/x;
    }
    
    for (int i=1; i<MAX; i++) phi[i] += phi[i-1];
    
    int T; cin >> T;
    while (T--) {
        int N; cin >> N;
        ll ans = (ll)(N)*(N-1);
        cout << ans-phi[N]+1 << endl;
    }
}
0