結果

問題 No.1262 グラフを作ろう!
ユーザー hitonanodehitonanode
提出日時 2020-10-16 22:20:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 6,606 bytes
コンパイル時間 2,469 ms
コンパイル使用メモリ 216,740 KB
実行使用メモリ 23,208 KB
最終ジャッジ日時 2024-07-20 22:13:38
合計ジャッジ時間 48,478 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 530 ms
22,308 KB
testcase_01 AC 370 ms
20,004 KB
testcase_02 AC 561 ms
22,052 KB
testcase_03 AC 528 ms
22,816 KB
testcase_04 AC 72 ms
15,780 KB
testcase_05 AC 292 ms
18,720 KB
testcase_06 AC 55 ms
15,520 KB
testcase_07 AC 88 ms
16,164 KB
testcase_08 AC 366 ms
19,492 KB
testcase_09 AC 429 ms
20,392 KB
testcase_10 AC 348 ms
18,724 KB
testcase_11 AC 400 ms
19,620 KB
testcase_12 AC 509 ms
21,924 KB
testcase_13 AC 600 ms
22,696 KB
testcase_14 AC 239 ms
18,340 KB
testcase_15 AC 307 ms
19,748 KB
testcase_16 AC 452 ms
20,004 KB
testcase_17 AC 374 ms
19,748 KB
testcase_18 AC 543 ms
22,304 KB
testcase_19 AC 90 ms
16,164 KB
testcase_20 AC 196 ms
17,440 KB
testcase_21 AC 631 ms
22,948 KB
testcase_22 AC 58 ms
15,524 KB
testcase_23 AC 361 ms
20,004 KB
testcase_24 AC 424 ms
19,880 KB
testcase_25 AC 82 ms
15,780 KB
testcase_26 AC 128 ms
16,548 KB
testcase_27 AC 260 ms
17,956 KB
testcase_28 AC 219 ms
17,572 KB
testcase_29 AC 152 ms
16,676 KB
testcase_30 AC 198 ms
17,440 KB
testcase_31 AC 610 ms
22,948 KB
testcase_32 AC 346 ms
19,620 KB
testcase_33 AC 186 ms
17,576 KB
testcase_34 AC 302 ms
18,464 KB
testcase_35 AC 503 ms
22,176 KB
testcase_36 AC 210 ms
18,212 KB
testcase_37 AC 580 ms
22,692 KB
testcase_38 AC 329 ms
19,020 KB
testcase_39 AC 333 ms
19,812 KB
testcase_40 AC 289 ms
18,980 KB
testcase_41 AC 168 ms
17,188 KB
testcase_42 AC 481 ms
21,156 KB
testcase_43 AC 259 ms
18,212 KB
testcase_44 AC 296 ms
18,856 KB
testcase_45 AC 440 ms
20,516 KB
testcase_46 AC 138 ms
16,544 KB
testcase_47 AC 405 ms
21,408 KB
testcase_48 AC 424 ms
20,256 KB
testcase_49 AC 566 ms
22,564 KB
testcase_50 AC 486 ms
20,768 KB
testcase_51 AC 428 ms
21,156 KB
testcase_52 AC 169 ms
16,932 KB
testcase_53 AC 121 ms
16,544 KB
testcase_54 AC 607 ms
22,688 KB
testcase_55 AC 426 ms
20,516 KB
testcase_56 AC 187 ms
17,188 KB
testcase_57 AC 769 ms
22,820 KB
testcase_58 AC 509 ms
22,180 KB
testcase_59 AC 174 ms
17,060 KB
testcase_60 AC 596 ms
22,564 KB
testcase_61 AC 241 ms
18,212 KB
testcase_62 AC 531 ms
22,184 KB
testcase_63 AC 569 ms
22,948 KB
testcase_64 AC 383 ms
20,008 KB
testcase_65 AC 111 ms
16,416 KB
testcase_66 AC 252 ms
18,336 KB
testcase_67 AC 179 ms
17,060 KB
testcase_68 AC 115 ms
16,292 KB
testcase_69 AC 187 ms
17,572 KB
testcase_70 AC 416 ms
20,132 KB
testcase_71 AC 391 ms
19,872 KB
testcase_72 AC 489 ms
21,416 KB
testcase_73 AC 632 ms
23,204 KB
testcase_74 AC 538 ms
22,432 KB
testcase_75 AC 458 ms
21,156 KB
testcase_76 AC 355 ms
19,748 KB
testcase_77 AC 507 ms
21,792 KB
testcase_78 AC 439 ms
20,580 KB
testcase_79 AC 551 ms
22,184 KB
testcase_80 AC 371 ms
19,880 KB
testcase_81 AC 544 ms
21,672 KB
testcase_82 AC 613 ms
23,080 KB
testcase_83 AC 387 ms
19,620 KB
testcase_84 AC 385 ms
19,876 KB
testcase_85 AC 642 ms
22,696 KB
testcase_86 AC 499 ms
21,540 KB
testcase_87 AC 581 ms
22,432 KB
testcase_88 AC 506 ms
21,796 KB
testcase_89 AC 380 ms
19,364 KB
testcase_90 AC 251 ms
23,200 KB
testcase_91 AC 243 ms
23,200 KB
testcase_92 AC 239 ms
23,076 KB
testcase_93 AC 1,097 ms
23,080 KB
testcase_94 TLE -
testcase_95 AC 660 ms
23,208 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