結果

問題 No.1262 グラフを作ろう!
ユーザー 👑 hitonanodehitonanode
提出日時 2020-10-17 00:08:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 552 ms / 3,000 ms
コード長 2,957 bytes
コンパイル時間 3,883 ms
コンパイル使用メモリ 224,668 KB
実行使用メモリ 23,212 KB
最終ジャッジ日時 2023-09-28 06:49:37
合計ジャッジ時間 33,898 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 292 ms
22,452 KB
testcase_01 AC 291 ms
21,652 KB
testcase_02 AC 417 ms
22,440 KB
testcase_03 AC 225 ms
22,756 KB
testcase_04 AC 70 ms
19,128 KB
testcase_05 AC 235 ms
20,920 KB
testcase_06 AC 54 ms
19,080 KB
testcase_07 AC 74 ms
19,572 KB
testcase_08 AC 307 ms
21,164 KB
testcase_09 AC 160 ms
21,688 KB
testcase_10 AC 194 ms
20,836 KB
testcase_11 AC 397 ms
21,140 KB
testcase_12 AC 346 ms
22,408 KB
testcase_13 AC 236 ms
22,608 KB
testcase_14 AC 186 ms
20,640 KB
testcase_15 AC 121 ms
21,304 KB
testcase_16 AC 299 ms
21,452 KB
testcase_17 AC 305 ms
21,448 KB
testcase_18 AC 502 ms
22,336 KB
testcase_19 AC 102 ms
19,400 KB
testcase_20 AC 235 ms
20,128 KB
testcase_21 AC 455 ms
23,128 KB
testcase_22 AC 45 ms
18,996 KB
testcase_23 AC 191 ms
21,372 KB
testcase_24 AC 307 ms
21,432 KB
testcase_25 AC 70 ms
19,252 KB
testcase_26 AC 120 ms
19,676 KB
testcase_27 AC 269 ms
20,384 KB
testcase_28 AC 230 ms
20,124 KB
testcase_29 AC 167 ms
19,848 KB
testcase_30 AC 203 ms
20,204 KB
testcase_31 AC 410 ms
22,888 KB
testcase_32 AC 305 ms
21,108 KB
testcase_33 AC 89 ms
20,316 KB
testcase_34 AC 255 ms
20,628 KB
testcase_35 AC 281 ms
22,484 KB
testcase_36 AC 97 ms
20,332 KB
testcase_37 AC 517 ms
22,652 KB
testcase_38 AC 262 ms
21,048 KB
testcase_39 AC 186 ms
21,480 KB
testcase_40 AC 232 ms
20,832 KB
testcase_41 AC 175 ms
19,888 KB
testcase_42 AC 414 ms
21,896 KB
testcase_43 AC 203 ms
20,580 KB
testcase_44 AC 295 ms
20,940 KB
testcase_45 AC 366 ms
21,636 KB
testcase_46 AC 154 ms
19,524 KB
testcase_47 AC 141 ms
21,892 KB
testcase_48 AC 296 ms
21,636 KB
testcase_49 AC 376 ms
22,820 KB
testcase_50 AC 322 ms
21,948 KB
testcase_51 AC 130 ms
21,976 KB
testcase_52 AC 138 ms
19,784 KB
testcase_53 AC 83 ms
19,528 KB
testcase_54 AC 231 ms
23,212 KB
testcase_55 AC 218 ms
21,712 KB
testcase_56 AC 146 ms
20,128 KB
testcase_57 AC 402 ms
22,952 KB
testcase_58 AC 200 ms
22,528 KB
testcase_59 AC 143 ms
19,960 KB
testcase_60 AC 384 ms
22,724 KB
testcase_61 AC 152 ms
20,660 KB
testcase_62 AC 313 ms
22,724 KB
testcase_63 AC 212 ms
23,000 KB
testcase_64 AC 207 ms
21,452 KB
testcase_65 AC 89 ms
19,604 KB
testcase_66 AC 160 ms
20,640 KB
testcase_67 AC 136 ms
19,904 KB
testcase_68 AC 96 ms
19,572 KB
testcase_69 AC 144 ms
20,116 KB
testcase_70 AC 253 ms
21,456 KB
testcase_71 AC 160 ms
21,360 KB
testcase_72 AC 375 ms
22,196 KB
testcase_73 AC 208 ms
23,060 KB
testcase_74 AC 111 ms
22,688 KB
testcase_75 AC 180 ms
22,036 KB
testcase_76 AC 144 ms
21,372 KB
testcase_77 AC 142 ms
22,340 KB
testcase_78 AC 190 ms
21,616 KB
testcase_79 AC 288 ms
22,420 KB
testcase_80 AC 253 ms
21,468 KB
testcase_81 AC 130 ms
22,436 KB
testcase_82 AC 168 ms
22,992 KB
testcase_83 AC 229 ms
21,260 KB
testcase_84 AC 150 ms
21,468 KB
testcase_85 AC 295 ms
22,948 KB
testcase_86 AC 173 ms
22,128 KB
testcase_87 AC 254 ms
22,604 KB
testcase_88 AC 128 ms
22,164 KB
testcase_89 AC 218 ms
21,208 KB
testcase_90 AC 110 ms
22,988 KB
testcase_91 AC 110 ms
23,028 KB
testcase_92 AC 108 ms
23,096 KB
testcase_93 AC 113 ms
22,964 KB
testcase_94 AC 111 ms
22,956 KB
testcase_95 AC 552 ms
22,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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';
}
0