結果
| 問題 |
No.2249 GCDistance
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-03-17 22:58:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,327 ms / 5,000 ms |
| コード長 | 1,323 bytes |
| コンパイル時間 | 1,942 ms |
| コンパイル使用メモリ | 199,712 KB |
| 最終ジャッジ日時 | 2025-02-11 14:10:32 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 10 |
ソースコード
#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;
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)-phi[N]+1;
printf("%lld\n", ans);
}
}