#pragma GCC optimize("O3") #include 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 { std::vector primes; SieveOfEratosthenes(int MAXN) : std::vector(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 Factorize(T x) { assert(x <= 1LL * (int(size()) - 1) * (int(size()) - 1)); std::map 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 Divisors(T x) { std::vector 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 euler_phi(int N) { std::vector 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 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 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'; }