結果
| 問題 |
No.1262 グラフを作ろう!
|
| コンテスト | |
| ユーザー |
hitonanode
|
| 提出日時 | 2020-10-17 00:08:34 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 683 ms / 3,000 ms |
| コード長 | 2,957 bytes |
| コンパイル時間 | 2,970 ms |
| コンパイル使用メモリ | 226,932 KB |
| 最終ジャッジ日時 | 2025-01-15 10:23:01 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 96 |
ソースコード
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
// Sieve of Eratosthenes
// (*this)[i] = (divisor of i, greater than 1)
// Example: [0, 1, 2, 3, 2, 5, 3, 7, 2, 3, 2, 11, ...]
// Complexity: Space O(MAXN), Time (construction) O(MAXNloglogMAXN)
struct SieveOfEratosthenes : std::vector<int>
{
std::vector<int> primes;
SieveOfEratosthenes(int MAXN) : std::vector<int>(MAXN + 1) {
std::iota(begin(), end(), 0);
for (int i = 2; i <= MAXN; i++) {
if ((*this)[i] == i) {
primes.push_back(i);
for (int j = i; j <= MAXN; j += i) (*this)[j] = i;
}
}
}
using T = long long int;
// Prime factorization for x <= MAXN^2
// Complexity: O(log x) (x <= MAXN)
// O(MAXN / logMAXN) (MAXN < x <= MAXN^2)
std::map<T, int> Factorize(T x) {
assert(x <= 1LL * (int(size()) - 1) * (int(size()) - 1));
std::map<T, int> ret;
if (x < int(size())) {
while (x > 1) {
ret[(*this)[x]]++;
x /= (*this)[x];
}
}
else {
for (auto p : primes) {
while (!(x % p)) x /= p, ret[p]++;
if (x == 1) break;
}
if (x > 1) ret[x]++;
}
return ret;
}
std::vector<T> Divisors(T x) {
std::vector<T> ret{1};
for (auto p : Factorize(x)) {
int n = ret.size();
for (int i = 0; i < n; i++) {
for (T a = 1, d = 1; d <= p.second; d++) {
a *= p.first;
ret.push_back(ret[i] * a);
}
}
}
return ret; // Not sorted
}
};
SieveOfEratosthenes sieve(1000010);
// Euler's totient function
// Complexity: O(NlgN)
std::vector<int> euler_phi(int N)
{
std::vector<int> ret(N + 1);
std::iota(ret.begin(), ret.end(), 0);
for (int p = 2; p <= N; p++) {
if (ret[p] == p) {
ret[p] = p - 1;
for (int i = p * 2; i <= N; i += p) {
ret[i] = ret[i] / p * (p - 1);
}
}
}
return ret;
}
int main()
{
int N, M;
cin >> N >> M;
vector<int> A(M);
for (auto &a : A) cin >> a;
lint ret = -accumulate(A.begin(), A.end(), 0LL);
constexpr int L = 1e6;
auto Phi = euler_phi(L);
vector<lint> memo(L + 1, -1);
for (auto a : A) {
lint ans = memo[a];
if (ans < 0) {
ans = 0;
auto v = sieve.Divisors(a);
for (auto d : v) ans += d * Phi[a / d];
}
memo[a] = ans;
ret += ans;
}
cout << ret << '\n';
}
hitonanode