結果

問題 No.1261 数字集め
ユーザー magstamagsta
提出日時 2020-10-12 22:34:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,099 ms / 8,000 ms
コード長 3,425 bytes
コンパイル時間 1,535 ms
コンパイル使用メモリ 78,396 KB
実行使用メモリ 35,708 KB
最終ジャッジ日時 2023-09-28 09:08:25
合計ジャッジ時間 31,190 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,099 ms
35,436 KB
testcase_01 AC 106 ms
14,732 KB
testcase_02 AC 214 ms
26,596 KB
testcase_03 AC 219 ms
27,084 KB
testcase_04 AC 142 ms
18,564 KB
testcase_05 AC 286 ms
34,228 KB
testcase_06 AC 159 ms
20,584 KB
testcase_07 AC 225 ms
27,672 KB
testcase_08 AC 129 ms
16,940 KB
testcase_09 AC 167 ms
21,160 KB
testcase_10 AC 10 ms
4,380 KB
testcase_11 AC 282 ms
33,804 KB
testcase_12 AC 251 ms
30,704 KB
testcase_13 AC 232 ms
28,212 KB
testcase_14 AC 28 ms
5,996 KB
testcase_15 AC 116 ms
15,432 KB
testcase_16 AC 225 ms
27,640 KB
testcase_17 AC 186 ms
23,112 KB
testcase_18 AC 118 ms
15,848 KB
testcase_19 AC 220 ms
26,976 KB
testcase_20 AC 33 ms
6,764 KB
testcase_21 AC 297 ms
35,468 KB
testcase_22 AC 297 ms
35,532 KB
testcase_23 AC 296 ms
35,640 KB
testcase_24 AC 297 ms
35,568 KB
testcase_25 AC 297 ms
35,640 KB
testcase_26 AC 299 ms
35,544 KB
testcase_27 AC 297 ms
35,576 KB
testcase_28 AC 295 ms
35,500 KB
testcase_29 AC 298 ms
35,580 KB
testcase_30 AC 297 ms
35,532 KB
testcase_31 AC 295 ms
35,644 KB
testcase_32 AC 297 ms
35,548 KB
testcase_33 AC 298 ms
35,516 KB
testcase_34 AC 297 ms
35,536 KB
testcase_35 AC 297 ms
35,580 KB
testcase_36 AC 298 ms
35,536 KB
testcase_37 AC 298 ms
35,564 KB
testcase_38 AC 297 ms
35,632 KB
testcase_39 AC 298 ms
35,632 KB
testcase_40 AC 298 ms
35,548 KB
testcase_41 AC 297 ms
35,632 KB
testcase_42 AC 298 ms
35,628 KB
testcase_43 AC 298 ms
35,532 KB
testcase_44 AC 317 ms
31,940 KB
testcase_45 AC 121 ms
9,936 KB
testcase_46 AC 145 ms
19,124 KB
testcase_47 AC 157 ms
14,320 KB
testcase_48 AC 244 ms
27,356 KB
testcase_49 AC 279 ms
31,672 KB
testcase_50 AC 315 ms
28,596 KB
testcase_51 AC 81 ms
10,652 KB
testcase_52 AC 25 ms
4,376 KB
testcase_53 AC 113 ms
10,712 KB
testcase_54 AC 158 ms
11,760 KB
testcase_55 AC 69 ms
7,404 KB
testcase_56 AC 112 ms
8,516 KB
testcase_57 AC 247 ms
26,092 KB
testcase_58 AC 106 ms
10,084 KB
testcase_59 AC 344 ms
32,560 KB
testcase_60 AC 124 ms
11,712 KB
testcase_61 AC 140 ms
14,828 KB
testcase_62 AC 336 ms
29,412 KB
testcase_63 AC 53 ms
7,920 KB
testcase_64 AC 371 ms
34,532 KB
testcase_65 AC 218 ms
24,060 KB
testcase_66 AC 174 ms
20,620 KB
testcase_67 AC 249 ms
29,536 KB
testcase_68 AC 172 ms
19,812 KB
testcase_69 AC 157 ms
19,380 KB
testcase_70 AC 184 ms
16,208 KB
testcase_71 AC 294 ms
28,256 KB
testcase_72 AC 133 ms
12,536 KB
testcase_73 AC 143 ms
14,088 KB
testcase_74 AC 73 ms
8,568 KB
testcase_75 AC 207 ms
18,672 KB
testcase_76 AC 244 ms
26,044 KB
testcase_77 AC 275 ms
29,848 KB
testcase_78 AC 28 ms
4,376 KB
testcase_79 AC 276 ms
30,992 KB
testcase_80 AC 53 ms
4,380 KB
testcase_81 AC 61 ms
6,264 KB
testcase_82 AC 250 ms
25,072 KB
testcase_83 AC 166 ms
13,072 KB
testcase_84 AC 405 ms
35,668 KB
testcase_85 AC 400 ms
35,636 KB
testcase_86 AC 401 ms
35,704 KB
testcase_87 AC 402 ms
35,704 KB
testcase_88 AC 406 ms
35,668 KB
testcase_89 AC 400 ms
35,704 KB
testcase_90 AC 402 ms
35,708 KB
testcase_91 AC 405 ms
35,668 KB
testcase_92 AC 405 ms
35,668 KB
testcase_93 AC 403 ms
35,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#define MOD 1000000007
using namespace std;

long long modpow(long long a, long long n) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

long long modinv(long long a) {
    return modpow(a, MOD - 2);
}

vector<long long> enum_divisors(long long N) {
    vector<long long> res;
    for (long long i = 1; i * i <= N; ++i) {
        if (N % i == 0) {
            res.push_back(i);
            // 重複しないならば i の相方である N/i も push
            if (N/i != i) res.push_back(N/i);
        }
    }
    // 小さい順に並び替える
    sort(res.begin(), res.end());
    return res;
}

long long p_q_r(long long p, long long q, long long r, long long M){
    if (r > p) swap(p, r);
    if (q > p) swap(p, q);
    if (r > q) swap(q, r);
    long long a, b, c, d, e;
    a = modpow(p, M - 2);
    b = modpow(q, M - 2);
    c = modpow(r, M - 2);
    d = (((a - c) * modinv(p - r) % MOD) + MOD) % MOD;
    e = (((b - c) * modinv(q - r) % MOD) + MOD) % MOD;
    d = d * p % MOD;
    e = e * q % MOD;
    return (((d - e) * modinv(p - q) % MOD) + MOD) % MOD;
}

int main() {
    long long N, M;
    cin >> N >> M;
    long long A[N];
    for (int i = 1; i < N; i++) cin >> A[i];
    const auto &res = enum_divisors(N);
    long long count = 0;
    long long ans = 0;
    for (int i = 0; i < res.size(); i++){
        for (int j = 0; j < res.size(); j++){
            if (res[i] == 1 || res[j] == 1) continue;
            if (N == (res[i] * res[j])) continue;
            if (N % (res[i] * res[j]) == 0){
                long long p = A[res[i] - 1] - 1;
                long long q = A[res[i] * res[j] - 1] - 1;
                long long r = A[N - 1] - 1;
                ans = (ans + p_q_r(p, q, r, M)) % MOD;
            }
        } 
    }
    cout << ans << endl;
    long long Q; cin >> Q;
    vector<long long> num[N];
    bool flag[N]; for (int i = 0; i < N; i++) flag[i] = false;
    for (int i = 0; i < Q; i++){
        long long X, Y; cin >> X >> Y;
        if (Y == N){
            const auto &res_ = enum_divisors(X);
            for (int i = 0; i < res_.size(); i++){
                if (res_[i] == 1 || res_[i] == X) continue;
                long long p = A[res_[i] - 1] - 1;
                long long q = A[X - 1] - 1;
                long long r = A[Y - 1] - 1;
                ans = (ans + p_q_r(p, q, r, M)) % MOD;
            }
            for (int i = 0; i < num[X - 1].size(); i++){
                long long p = A[num[X - 1][i] - 1] - 1;
                long long q = A[X - 1] - 1;
                long long r = A[Y - 1] - 1;
                ans = (ans + p_q_r(p, q, r, M)) % MOD;
            }
            flag[X - 1] = true;
        }
        if (Y != N){
            if (N % Y == 0){
                long long p = A[X - 1] - 1;
                long long q = A[Y - 1] - 1;
                long long r = A[N - 1] - 1;
                ans = (ans + p_q_r(p, q, r, M)) % MOD;
            }
            if (flag[Y - 1] == true){
                long long p = A[X - 1] - 1;
                long long q = A[Y - 1] - 1;
                long long r = A[N - 1] - 1;
                ans = (ans + p_q_r(p, q, r, M)) % MOD;
            }
            num[Y - 1].push_back(X);
        }
        cout << ans << endl;
    }
}
0