結果

問題 No.1262 グラフを作ろう!
ユーザー 👑 hitonanodehitonanode
提出日時 2020-10-16 22:23:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 625 ms / 3,000 ms
コード長 2,905 bytes
コンパイル時間 4,095 ms
コンパイル使用メモリ 212,440 KB
実行使用メモリ 23,176 KB
最終ジャッジ日時 2023-09-28 03:44:53
合計ジャッジ時間 34,540 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 353 ms
22,516 KB
testcase_01 AC 261 ms
21,436 KB
testcase_02 AC 427 ms
22,480 KB
testcase_03 AC 229 ms
22,760 KB
testcase_04 AC 73 ms
19,064 KB
testcase_05 AC 261 ms
20,932 KB
testcase_06 AC 52 ms
19,060 KB
testcase_07 AC 80 ms
19,656 KB
testcase_08 AC 350 ms
21,172 KB
testcase_09 AC 154 ms
21,604 KB
testcase_10 AC 209 ms
20,612 KB
testcase_11 AC 379 ms
21,176 KB
testcase_12 AC 312 ms
22,356 KB
testcase_13 AC 250 ms
22,592 KB
testcase_14 AC 214 ms
20,620 KB
testcase_15 AC 119 ms
21,236 KB
testcase_16 AC 364 ms
21,568 KB
testcase_17 AC 321 ms
21,240 KB
testcase_18 AC 457 ms
22,476 KB
testcase_19 AC 94 ms
19,388 KB
testcase_20 AC 215 ms
20,256 KB
testcase_21 AC 444 ms
23,136 KB
testcase_22 AC 51 ms
18,960 KB
testcase_23 AC 189 ms
21,488 KB
testcase_24 AC 368 ms
21,420 KB
testcase_25 AC 78 ms
19,452 KB
testcase_26 AC 128 ms
19,496 KB
testcase_27 AC 246 ms
20,376 KB
testcase_28 AC 216 ms
20,312 KB
testcase_29 AC 150 ms
19,784 KB
testcase_30 AC 183 ms
20,200 KB
testcase_31 AC 376 ms
22,856 KB
testcase_32 AC 281 ms
21,144 KB
testcase_33 AC 99 ms
20,124 KB
testcase_34 AC 299 ms
20,724 KB
testcase_35 AC 268 ms
22,496 KB
testcase_36 AC 93 ms
20,320 KB
testcase_37 AC 483 ms
22,604 KB
testcase_38 AC 293 ms
20,924 KB
testcase_39 AC 167 ms
21,340 KB
testcase_40 AC 205 ms
20,992 KB
testcase_41 AC 180 ms
19,960 KB
testcase_42 AC 434 ms
21,992 KB
testcase_43 AC 245 ms
20,576 KB
testcase_44 AC 266 ms
20,876 KB
testcase_45 AC 413 ms
21,612 KB
testcase_46 AC 137 ms
19,572 KB
testcase_47 AC 132 ms
21,964 KB
testcase_48 AC 374 ms
21,720 KB
testcase_49 AC 460 ms
22,820 KB
testcase_50 AC 411 ms
22,048 KB
testcase_51 AC 154 ms
21,824 KB
testcase_52 AC 169 ms
19,820 KB
testcase_53 AC 99 ms
19,504 KB
testcase_54 AC 268 ms
22,656 KB
testcase_55 AC 270 ms
21,720 KB
testcase_56 AC 187 ms
19,832 KB
testcase_57 AC 510 ms
23,120 KB
testcase_58 AC 235 ms
22,712 KB
testcase_59 AC 177 ms
19,844 KB
testcase_60 AC 481 ms
22,592 KB
testcase_61 AC 181 ms
20,704 KB
testcase_62 AC 385 ms
22,356 KB
testcase_63 AC 241 ms
23,028 KB
testcase_64 AC 246 ms
21,348 KB
testcase_65 AC 107 ms
19,768 KB
testcase_66 AC 190 ms
20,552 KB
testcase_67 AC 167 ms
20,040 KB
testcase_68 AC 119 ms
19,652 KB
testcase_69 AC 177 ms
20,188 KB
testcase_70 AC 314 ms
21,456 KB
testcase_71 AC 179 ms
21,472 KB
testcase_72 AC 323 ms
21,892 KB
testcase_73 AC 235 ms
22,856 KB
testcase_74 AC 120 ms
22,684 KB
testcase_75 AC 202 ms
21,804 KB
testcase_76 AC 169 ms
21,452 KB
testcase_77 AC 156 ms
22,292 KB
testcase_78 AC 219 ms
21,784 KB
testcase_79 AC 351 ms
22,612 KB
testcase_80 AC 309 ms
21,152 KB
testcase_81 AC 143 ms
22,312 KB
testcase_82 AC 186 ms
23,176 KB
testcase_83 AC 286 ms
21,308 KB
testcase_84 AC 173 ms
21,444 KB
testcase_85 AC 360 ms
22,668 KB
testcase_86 AC 203 ms
22,156 KB
testcase_87 AC 303 ms
22,580 KB
testcase_88 AC 139 ms
22,408 KB
testcase_89 AC 262 ms
21,156 KB
testcase_90 AC 121 ms
23,124 KB
testcase_91 AC 121 ms
22,968 KB
testcase_92 AC 120 ms
22,864 KB
testcase_93 AC 126 ms
22,984 KB
testcase_94 AC 119 ms
22,920 KB
testcase_95 AC 625 ms
22,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 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)

// 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);

int main()
{
    int N, M;
    cin >> N >> M;
    vector<int> A(M);
    for (auto &a : A) 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);
            }
        }
    }

    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