結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,941 ms
145,396 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 1,099 ms
134,276 KB
testcase_45 AC 251 ms
69,916 KB
testcase_46 AC 504 ms
96,284 KB
testcase_47 AC 398 ms
82,808 KB
testcase_48 AC 853 ms
120,732 KB
testcase_49 AC 1,032 ms
133,256 KB
testcase_50 AC 1,012 ms
124,944 KB
testcase_51 AC 224 ms
71,872 KB
testcase_52 AC 49 ms
52,176 KB
testcase_53 AC 264 ms
72,308 KB
testcase_54 AC 349 ms
75,528 KB
testcase_55 AC 149 ms
62,476 KB
testcase_56 AC 206 ms
66,076 KB
testcase_57 AC 847 ms
117,248 KB
testcase_58 AC 235 ms
70,316 KB
testcase_59 AC 1,143 ms
135,908 KB
testcase_60 AC 288 ms
74,588 KB
testcase_61 AC 391 ms
83,940 KB
testcase_62 AC 1,096 ms
127,256 KB
testcase_63 AC 144 ms
63,892 KB
testcase_64 AC 1,272 ms
142,308 KB
testcase_65 AC 788 ms
111,252 KB
testcase_66 AC 578 ms
101,412 KB
testcase_67 AC 1,227 ms
127,356 KB
testcase_68 AC 556 ms
98,712 KB
testcase_69 AC 528 ms
97,508 KB
testcase_70 AC 454 ms
88,300 KB
testcase_71 AC 919 ms
123,868 KB
testcase_72 AC 314 ms
77,604 KB
testcase_73 AC 363 ms
81,984 KB
testcase_74 AC 169 ms
65,668 KB
testcase_75 AC 533 ms
95,184 KB
testcase_76 AC 887 ms
117,404 KB
testcase_77 AC 975 ms
128,508 KB
testcase_78 AC 44 ms
51,004 KB
testcase_79 AC 1,008 ms
131,368 KB
testcase_80 AC 72 ms
52,364 KB
testcase_81 AC 117 ms
59,212 KB
testcase_82 AC 789 ms
113,776 KB
testcase_83 AC 354 ms
79,332 KB
testcase_84 AC 1,312 ms
145,736 KB
testcase_85 AC 1,320 ms
145,772 KB
testcase_86 AC 1,309 ms
146,012 KB
testcase_87 AC 1,317 ms
145,720 KB
testcase_88 AC 1,295 ms
146,024 KB
testcase_89 AC 1,326 ms
145,720 KB
testcase_90 AC 1,313 ms
145,844 KB
testcase_91 AC 1,315 ms
145,716 KB
testcase_92 AC 1,336 ms
145,724 KB
testcase_93 AC 1,319 ms
145,732 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 < 1 || 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