結果

問題 No.1261 数字集め
ユーザー magstamagsta
提出日時 2020-10-14 22:49:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,283 ms / 8,000 ms
コード長 3,937 bytes
コンパイル時間 1,635 ms
コンパイル使用メモリ 91,444 KB
実行使用メモリ 145,912 KB
最終ジャッジ日時 2023-09-28 10:11:52
合計ジャッジ時間 107,637 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,283 ms
145,416 KB
testcase_01 AC 427 ms
83,128 KB
testcase_02 AC 1,010 ms
118,536 KB
testcase_03 AC 989 ms
119,404 KB
testcase_04 AC 602 ms
94,832 KB
testcase_05 AC 1,415 ms
140,988 KB
testcase_06 AC 711 ms
100,916 KB
testcase_07 AC 1,041 ms
121,744 KB
testcase_08 AC 551 ms
90,428 KB
testcase_09 AC 753 ms
102,772 KB
testcase_10 AC 38 ms
52,728 KB
testcase_11 AC 1,424 ms
140,056 KB
testcase_12 AC 1,280 ms
130,512 KB
testcase_13 AC 1,126 ms
122,852 KB
testcase_14 AC 92 ms
58,300 KB
testcase_15 AC 488 ms
85,932 KB
testcase_16 AC 1,107 ms
122,128 KB
testcase_17 AC 856 ms
108,356 KB
testcase_18 AC 507 ms
86,800 KB
testcase_19 AC 1,063 ms
119,984 KB
testcase_20 AC 111 ms
60,136 KB
testcase_21 AC 1,530 ms
145,012 KB
testcase_22 AC 1,525 ms
144,964 KB
testcase_23 AC 1,529 ms
144,984 KB
testcase_24 AC 1,517 ms
145,024 KB
testcase_25 AC 1,529 ms
144,948 KB
testcase_26 AC 1,533 ms
145,016 KB
testcase_27 AC 1,525 ms
144,960 KB
testcase_28 AC 1,520 ms
145,008 KB
testcase_29 AC 1,506 ms
145,008 KB
testcase_30 AC 1,498 ms
145,008 KB
testcase_31 AC 1,518 ms
145,084 KB
testcase_32 AC 1,515 ms
144,944 KB
testcase_33 AC 1,517 ms
144,952 KB
testcase_34 AC 1,514 ms
145,020 KB
testcase_35 AC 1,528 ms
145,156 KB
testcase_36 AC 1,533 ms
144,944 KB
testcase_37 AC 1,542 ms
144,980 KB
testcase_38 AC 1,528 ms
145,012 KB
testcase_39 AC 1,546 ms
144,960 KB
testcase_40 AC 1,528 ms
144,968 KB
testcase_41 AC 1,511 ms
144,948 KB
testcase_42 AC 1,545 ms
145,088 KB
testcase_43 AC 1,545 ms
144,948 KB
testcase_44 AC 1,354 ms
134,328 KB
testcase_45 AC 299 ms
69,848 KB
testcase_46 AC 692 ms
96,544 KB
testcase_47 AC 490 ms
82,744 KB
testcase_48 AC 1,117 ms
120,772 KB
testcase_49 AC 1,288 ms
133,400 KB
testcase_50 AC 1,197 ms
124,884 KB
testcase_51 AC 276 ms
71,888 KB
testcase_52 AC 50 ms
52,132 KB
testcase_53 AC 318 ms
72,352 KB
testcase_54 AC 390 ms
75,484 KB
testcase_55 AC 171 ms
62,340 KB
testcase_56 AC 238 ms
65,732 KB
testcase_57 AC 1,044 ms
117,252 KB
testcase_58 AC 292 ms
70,304 KB
testcase_59 AC 1,446 ms
135,896 KB
testcase_60 AC 349 ms
74,600 KB
testcase_61 AC 477 ms
83,928 KB
testcase_62 AC 1,265 ms
127,324 KB
testcase_63 AC 170 ms
63,840 KB
testcase_64 AC 1,563 ms
142,436 KB
testcase_65 AC 937 ms
111,216 KB
testcase_66 AC 753 ms
101,364 KB
testcase_67 AC 1,204 ms
127,404 KB
testcase_68 AC 711 ms
98,704 KB
testcase_69 AC 677 ms
97,580 KB
testcase_70 AC 584 ms
88,240 KB
testcase_71 AC 1,197 ms
123,812 KB
testcase_72 AC 397 ms
77,600 KB
testcase_73 AC 466 ms
81,960 KB
testcase_74 AC 211 ms
65,660 KB
testcase_75 AC 696 ms
95,104 KB
testcase_76 AC 1,058 ms
117,356 KB
testcase_77 AC 1,252 ms
128,328 KB
testcase_78 AC 47 ms
51,028 KB
testcase_79 AC 1,283 ms
131,188 KB
testcase_80 AC 79 ms
52,376 KB
testcase_81 AC 130 ms
59,196 KB
testcase_82 AC 982 ms
113,780 KB
testcase_83 AC 447 ms
79,320 KB
testcase_84 AC 1,623 ms
145,712 KB
testcase_85 AC 1,641 ms
145,776 KB
testcase_86 AC 1,615 ms
145,780 KB
testcase_87 AC 1,612 ms
145,748 KB
testcase_88 AC 1,616 ms
145,912 KB
testcase_89 AC 1,606 ms
145,784 KB
testcase_90 AC 1,608 ms
145,844 KB
testcase_91 AC 1,572 ms
145,776 KB
testcase_92 AC 1,569 ms
145,708 KB
testcase_93 AC 1,592 ms
145,776 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 = 1; i < N; i++){
        Map[A[i]]++;
        if (Map[A[i]] > 1) cout << "errar" << endl;
        if (A[i] < 2 || A[i] > 1000000) cout << "errbr" << 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;
    if (Q < 0 || Q > 10000) cout << "errer" << endl;
    vector<long long> num[N];
    bool flag[N]; for (int i = 0; i < N; i++) flag[i] = false;
    map<long long, long long> Map_[1000001];
    for (int i = 0; i < Q; i++){
        long long X, Y; cin >> X >> Y;
        if (X >= Y || X < 1 || Y > N) cout << "errfr" << endl;
        if (Y % X == 0) cout << "errgr" << endl;
        Map_[X][Y]++;
        if (Map_[X][Y] > 1) cout << "errxr" << endl;
        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