結果

問題 No.1261 数字集め
ユーザー magstamagsta
提出日時 2020-10-08 22:42:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,388 bytes
コンパイル時間 1,173 ms
コンパイル使用メモリ 78,188 KB
実行使用メモリ 35,832 KB
最終ジャッジ日時 2023-09-28 09:10:19
合計ジャッジ時間 28,629 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 987 ms
35,508 KB
testcase_01 AC 97 ms
14,788 KB
testcase_02 AC 196 ms
26,660 KB
testcase_03 AC 200 ms
26,928 KB
testcase_04 AC 129 ms
18,580 KB
testcase_05 AC 261 ms
34,224 KB
testcase_06 AC 148 ms
20,796 KB
testcase_07 AC 200 ms
27,804 KB
testcase_08 AC 113 ms
17,060 KB
testcase_09 AC 150 ms
21,208 KB
testcase_10 AC 9 ms
4,380 KB
testcase_11 AC 257 ms
33,872 KB
testcase_12 AC 234 ms
30,776 KB
testcase_13 AC 205 ms
28,080 KB
testcase_14 AC 25 ms
6,060 KB
testcase_15 AC 101 ms
15,488 KB
testcase_16 AC 202 ms
27,728 KB
testcase_17 AC 169 ms
23,116 KB
testcase_18 AC 108 ms
15,716 KB
testcase_19 AC 209 ms
26,972 KB
testcase_20 AC 32 ms
6,696 KB
testcase_21 AC 280 ms
35,564 KB
testcase_22 AC 267 ms
35,568 KB
testcase_23 AC 264 ms
35,568 KB
testcase_24 AC 270 ms
35,588 KB
testcase_25 AC 272 ms
35,596 KB
testcase_26 AC 266 ms
35,580 KB
testcase_27 AC 267 ms
35,616 KB
testcase_28 AC 285 ms
35,592 KB
testcase_29 AC 277 ms
35,644 KB
testcase_30 AC 273 ms
35,636 KB
testcase_31 AC 267 ms
35,820 KB
testcase_32 AC 279 ms
35,568 KB
testcase_33 AC 279 ms
35,576 KB
testcase_34 AC 267 ms
35,632 KB
testcase_35 AC 267 ms
35,584 KB
testcase_36 AC 267 ms
35,588 KB
testcase_37 AC 272 ms
35,620 KB
testcase_38 AC 275 ms
35,588 KB
testcase_39 AC 275 ms
35,588 KB
testcase_40 AC 269 ms
35,576 KB
testcase_41 AC 266 ms
35,636 KB
testcase_42 AC 275 ms
35,584 KB
testcase_43 AC 270 ms
35,568 KB
testcase_44 AC 283 ms
31,796 KB
testcase_45 WA -
testcase_46 AC 131 ms
19,124 KB
testcase_47 WA -
testcase_48 AC 223 ms
27,272 KB
testcase_49 AC 250 ms
31,580 KB
testcase_50 WA -
testcase_51 AC 75 ms
10,660 KB
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 AC 222 ms
26,052 KB
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 AC 47 ms
8,004 KB
testcase_64 WA -
testcase_65 WA -
testcase_66 AC 156 ms
20,688 KB
testcase_67 AC 225 ms
29,632 KB
testcase_68 WA -
testcase_69 AC 140 ms
19,412 KB
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 AC 242 ms
26,344 KB
testcase_77 AC 250 ms
29,792 KB
testcase_78 WA -
testcase_79 AC 264 ms
30,856 KB
testcase_80 WA -
testcase_81 WA -
testcase_82 WA -
testcase_83 WA -
testcase_84 WA -
testcase_85 WA -
testcase_86 WA -
testcase_87 WA -
testcase_88 WA -
testcase_89 WA -
testcase_90 WA -
testcase_91 WA -
testcase_92 WA -
testcase_93 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
            }
        }
        cout << ans << endl;
    }
}
0