結果

問題 No.1262 グラフを作ろう!
ユーザー 👑 hitonanodehitonanode
提出日時 2020-10-16 22:20:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 6,606 bytes
コンパイル時間 2,489 ms
コンパイル使用メモリ 215,336 KB
実行使用メモリ 23,220 KB
最終ジャッジ日時 2023-09-28 03:39:35
合計ジャッジ時間 49,728 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 509 ms
22,184 KB
testcase_01 AC 389 ms
19,972 KB
testcase_02 AC 631 ms
21,972 KB
testcase_03 AC 523 ms
22,768 KB
testcase_04 AC 68 ms
15,672 KB
testcase_05 AC 273 ms
18,612 KB
testcase_06 AC 51 ms
15,180 KB
testcase_07 AC 84 ms
15,984 KB
testcase_08 AC 345 ms
19,340 KB
testcase_09 AC 356 ms
20,468 KB
testcase_10 AC 271 ms
18,780 KB
testcase_11 AC 362 ms
19,324 KB
testcase_12 AC 501 ms
21,712 KB
testcase_13 AC 522 ms
22,444 KB
testcase_14 AC 234 ms
18,244 KB
testcase_15 AC 296 ms
19,616 KB
testcase_16 AC 424 ms
19,708 KB
testcase_17 AC 452 ms
19,592 KB
testcase_18 AC 714 ms
21,892 KB
testcase_19 AC 95 ms
15,968 KB
testcase_20 AC 197 ms
17,288 KB
testcase_21 AC 631 ms
22,628 KB
testcase_22 AC 58 ms
15,172 KB
testcase_23 AC 344 ms
19,648 KB
testcase_24 AC 387 ms
19,584 KB
testcase_25 AC 86 ms
15,856 KB
testcase_26 AC 119 ms
16,228 KB
testcase_27 AC 240 ms
17,744 KB
testcase_28 AC 237 ms
17,556 KB
testcase_29 AC 165 ms
16,552 KB
testcase_30 AC 191 ms
17,440 KB
testcase_31 AC 568 ms
22,748 KB
testcase_32 AC 326 ms
19,524 KB
testcase_33 AC 175 ms
17,740 KB
testcase_34 AC 288 ms
18,268 KB
testcase_35 AC 479 ms
21,784 KB
testcase_36 AC 199 ms
17,808 KB
testcase_37 AC 600 ms
22,360 KB
testcase_38 AC 317 ms
18,800 KB
testcase_39 AC 323 ms
19,920 KB
testcase_40 AC 285 ms
18,800 KB
testcase_41 AC 163 ms
17,020 KB
testcase_42 AC 477 ms
21,128 KB
testcase_43 AC 244 ms
18,200 KB
testcase_44 AC 297 ms
18,796 KB
testcase_45 AC 423 ms
20,384 KB
testcase_46 AC 123 ms
16,340 KB
testcase_47 AC 396 ms
21,180 KB
testcase_48 AC 411 ms
20,048 KB
testcase_49 AC 580 ms
22,424 KB
testcase_50 AC 459 ms
20,724 KB
testcase_51 AC 435 ms
21,040 KB
testcase_52 AC 152 ms
16,868 KB
testcase_53 AC 110 ms
16,224 KB
testcase_54 AC 574 ms
22,764 KB
testcase_55 AC 423 ms
20,112 KB
testcase_56 AC 166 ms
17,076 KB
testcase_57 AC 632 ms
22,848 KB
testcase_58 AC 503 ms
22,012 KB
testcase_59 AC 165 ms
16,908 KB
testcase_60 AC 580 ms
22,576 KB
testcase_61 AC 245 ms
18,084 KB
testcase_62 AC 531 ms
22,064 KB
testcase_63 AC 578 ms
22,856 KB
testcase_64 AC 366 ms
19,596 KB
testcase_65 AC 105 ms
16,048 KB
testcase_66 AC 255 ms
18,080 KB
testcase_67 AC 170 ms
17,084 KB
testcase_68 AC 110 ms
16,120 KB
testcase_69 AC 188 ms
17,280 KB
testcase_70 AC 397 ms
20,004 KB
testcase_71 AC 375 ms
20,008 KB
testcase_72 AC 533 ms
21,240 KB
testcase_73 AC 630 ms
23,092 KB
testcase_74 AC 529 ms
22,336 KB
testcase_75 AC 448 ms
20,644 KB
testcase_76 AC 357 ms
19,728 KB
testcase_77 AC 501 ms
21,648 KB
testcase_78 AC 427 ms
20,316 KB
testcase_79 AC 550 ms
22,056 KB
testcase_80 AC 374 ms
19,612 KB
testcase_81 AC 498 ms
21,744 KB
testcase_82 AC 596 ms
22,944 KB
testcase_83 AC 380 ms
19,704 KB
testcase_84 AC 365 ms
19,668 KB
testcase_85 AC 589 ms
22,804 KB
testcase_86 AC 495 ms
21,296 KB
testcase_87 AC 562 ms
22,412 KB
testcase_88 AC 490 ms
21,440 KB
testcase_89 AC 344 ms
19,468 KB
testcase_90 AC 232 ms
22,960 KB
testcase_91 AC 234 ms
22,964 KB
testcase_92 AC 238 ms
23,220 KB
testcase_93 AC 1,071 ms
23,136 KB
testcase_94 TLE -
testcase_95 AC 575 ms
23,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl
#else
#define dbg(x) {}
#endif

// 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
    }
    // Moebius function Table
    // return: [0=>0, 1=>1, 2=>-1, 3=>-1, 4=>0, 5=>-1, 6=>1, 7=>-1, 8=>0, ...]
    std::vector<int> GenerateMoebiusFunctionTable() {
        std::vector<int> ret(size());
        for (int i = 1; i < int(size()); i++) {
            if (i == 1) ret[i] = 1;
            else if ((i / (*this)[i]) % (*this)[i] == 0) ret[i] = 0;
            else ret[i] = -ret[i / (*this)[i]];
        }
        return ret;
    }
};
SieveOfEratosthenes sieve(1000010);

int main()
{
    int N, M;
    cin >> N >> M;
    vector<lint> A(M);
    cin >> A;
    lint ret = -accumulate(ALL(A), 0LL);

    constexpr int L = 1e6 + 10;
    vector<int> Phi(L + 1);
    iota(ALL(Phi), 0);
    FOR(p, 2, L + 1) {
        if (Phi[p] == p) {
            Phi[p] = p - 1;
            for (int i = p * 2; i <= L; i += p) {
                Phi[i] = Phi[i] / p * (p - 1);
            }
        }
    }

    const auto moebius = sieve.GenerateMoebiusFunctionTable();
    for (auto a : A) {
        auto v = sieve.Divisors(a);
        dbg(a);
        dbg(v);
        for (auto d : v) ret += d * Phi[a / d];
    }

    // for (auto a : A) v1[a]++;
    // for (auto a : A) {
    //     FOR(j, 1, a + 1) {
    //         ret += __gcd<lint>(a, j);
    //     }
    // }
    cout << ret << '\n';
}
0