結果

問題 No.1261 数字集め
ユーザー magstamagsta
提出日時 2020-10-14 22:37:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,305 ms / 8,000 ms
コード長 3,587 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 90,336 KB
実行使用メモリ 98,392 KB
最終ジャッジ日時 2023-09-28 10:06:44
合計ジャッジ時間 106,483 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,305 ms
97,952 KB
testcase_01 AC 418 ms
36,424 KB
testcase_02 AC 990 ms
71,708 KB
testcase_03 AC 1,030 ms
72,500 KB
testcase_04 AC 603 ms
48,004 KB
testcase_05 AC 1,394 ms
94,104 KB
testcase_06 AC 708 ms
54,096 KB
testcase_07 AC 1,065 ms
74,868 KB
testcase_08 AC 557 ms
43,400 KB
testcase_09 AC 740 ms
55,812 KB
testcase_10 AC 20 ms
5,700 KB
testcase_11 AC 1,419 ms
92,884 KB
testcase_12 AC 1,242 ms
83,652 KB
testcase_13 AC 1,102 ms
75,908 KB
testcase_14 AC 72 ms
11,364 KB
testcase_15 AC 471 ms
39,024 KB
testcase_16 AC 1,066 ms
75,040 KB
testcase_17 AC 830 ms
61,264 KB
testcase_18 AC 452 ms
39,720 KB
testcase_19 AC 1,006 ms
72,976 KB
testcase_20 AC 94 ms
13,224 KB
testcase_21 AC 1,531 ms
98,072 KB
testcase_22 AC 1,537 ms
98,204 KB
testcase_23 AC 1,507 ms
98,140 KB
testcase_24 AC 1,530 ms
98,004 KB
testcase_25 AC 1,515 ms
98,060 KB
testcase_26 AC 1,511 ms
98,068 KB
testcase_27 AC 1,506 ms
98,256 KB
testcase_28 AC 1,523 ms
98,028 KB
testcase_29 AC 1,534 ms
98,064 KB
testcase_30 AC 1,529 ms
98,024 KB
testcase_31 AC 1,499 ms
98,132 KB
testcase_32 AC 1,561 ms
98,072 KB
testcase_33 AC 1,480 ms
98,268 KB
testcase_34 AC 1,496 ms
98,004 KB
testcase_35 AC 1,525 ms
98,080 KB
testcase_36 AC 1,496 ms
98,132 KB
testcase_37 AC 1,503 ms
98,132 KB
testcase_38 AC 1,504 ms
98,072 KB
testcase_39 AC 1,540 ms
98,084 KB
testcase_40 AC 1,507 ms
98,132 KB
testcase_41 AC 1,504 ms
98,132 KB
testcase_42 AC 1,521 ms
98,024 KB
testcase_43 AC 1,538 ms
98,108 KB
testcase_44 AC 1,327 ms
86,992 KB
testcase_45 AC 292 ms
22,452 KB
testcase_46 AC 636 ms
49,356 KB
testcase_47 AC 443 ms
35,460 KB
testcase_48 AC 1,071 ms
73,680 KB
testcase_49 AC 1,287 ms
86,188 KB
testcase_50 AC 1,184 ms
77,468 KB
testcase_51 AC 264 ms
25,076 KB
testcase_52 AC 32 ms
4,984 KB
testcase_53 AC 294 ms
25,068 KB
testcase_54 AC 372 ms
27,996 KB
testcase_55 AC 145 ms
15,172 KB
testcase_56 AC 220 ms
18,400 KB
testcase_57 AC 1,033 ms
70,056 KB
testcase_58 AC 266 ms
23,024 KB
testcase_59 AC 1,395 ms
88,492 KB
testcase_60 AC 325 ms
27,340 KB
testcase_61 AC 465 ms
36,788 KB
testcase_62 AC 1,242 ms
79,804 KB
testcase_63 AC 145 ms
16,932 KB
testcase_64 AC 1,522 ms
94,980 KB
testcase_65 AC 907 ms
64,236 KB
testcase_66 AC 703 ms
54,292 KB
testcase_67 AC 1,164 ms
80,472 KB
testcase_68 AC 692 ms
51,812 KB
testcase_69 AC 664 ms
50,504 KB
testcase_70 AC 556 ms
40,964 KB
testcase_71 AC 1,166 ms
76,412 KB
testcase_72 AC 371 ms
30,328 KB
testcase_73 AC 442 ms
34,732 KB
testcase_74 AC 189 ms
18,552 KB
testcase_75 AC 683 ms
47,708 KB
testcase_76 AC 1,008 ms
70,252 KB
testcase_77 AC 1,208 ms
81,112 KB
testcase_78 AC 29 ms
4,380 KB
testcase_79 AC 1,276 ms
84,044 KB
testcase_80 AC 59 ms
4,892 KB
testcase_81 AC 118 ms
12,080 KB
testcase_82 AC 984 ms
66,508 KB
testcase_83 AC 419 ms
31,904 KB
testcase_84 AC 1,596 ms
98,208 KB
testcase_85 AC 1,624 ms
98,204 KB
testcase_86 AC 1,623 ms
98,288 KB
testcase_87 AC 1,634 ms
98,280 KB
testcase_88 AC 1,556 ms
98,204 KB
testcase_89 AC 1,584 ms
98,232 KB
testcase_90 AC 1,615 ms
98,232 KB
testcase_91 AC 1,576 ms
98,392 KB
testcase_92 AC 1,608 ms
98,348 KB
testcase_93 AC 1,557 ms
98,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#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];
    map<long long, long long> Map;
    for (int i = 0; i < N; i++){
        Map[A[i]]++;
        if (Map[A[i]] > 1) cout << "error" << endl;
    }
    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