結果

問題 No.2709 1975 Powers
ユーザー InIn
提出日時 2024-03-31 14:03:25
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,050 ms / 2,000 ms
コード長 1,606 bytes
コンパイル時間 1,753 ms
コンパイル使用メモリ 169,196 KB
実行使用メモリ 11,968 KB
最終ジャッジ日時 2024-03-31 14:03:43
合計ジャッジ時間 16,026 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 301 ms
11,616 KB
testcase_03 AC 504 ms
6,728 KB
testcase_04 AC 882 ms
9,756 KB
testcase_05 AC 689 ms
9,848 KB
testcase_06 AC 217 ms
6,676 KB
testcase_07 AC 91 ms
9,988 KB
testcase_08 AC 642 ms
11,556 KB
testcase_09 AC 779 ms
9,640 KB
testcase_10 AC 96 ms
10,056 KB
testcase_11 AC 143 ms
6,676 KB
testcase_12 AC 513 ms
11,968 KB
testcase_13 AC 948 ms
11,728 KB
testcase_14 AC 279 ms
6,676 KB
testcase_15 AC 637 ms
11,876 KB
testcase_16 AC 389 ms
6,676 KB
testcase_17 AC 109 ms
6,676 KB
testcase_18 AC 560 ms
11,884 KB
testcase_19 AC 765 ms
9,928 KB
testcase_20 AC 289 ms
11,776 KB
testcase_21 AC 636 ms
11,556 KB
testcase_22 AC 1,050 ms
11,624 KB
testcase_23 AC 813 ms
9,956 KB
testcase_24 AC 494 ms
6,676 KB
testcase_25 AC 784 ms
9,816 KB
testcase_26 AC 902 ms
9,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N, P, Q; readln.read(N, P, Q);
    auto A = readln.split.to!(int[]);

    solve(N, P, Q, A);
}

void solve (int N, int P, int Q, int[] A) {
    // なんかMLEしたのでnextdpやる

    A.sort;
    auto dp = new long[][](5, P);
    auto ndp = new long[][](5, P);
    // dp[i][j][k] := 先頭i個からj個とってあまりがk

    dp[0][0] = 1;

    const x = [10, 9, 7, 5];

    auto memo = new long[][](4, N);
    foreach (i; 0..4) foreach (j; 0..N) memo[i][j] = mod_pow(x[i], A[j], P);

    foreach (i; 0..N) {
        foreach (d; ndp) d[] = 0;

        foreach (j; 0..5) {
            foreach (k; 0..P) {
                // 取る
                if (j + 1 <= 4) {
                    ndp[j + 1][(k + memo[j][i]) % P] += dp[j][k];
                }

                // 取らない
                ndp[j][k] += dp[j][k];
            }
        }

        swap(dp, ndp);
    }

    writeln(dp[4][Q]);
}

void read (T...) (string S, ref T args) {
    import std.conv : to;
    import std.array : split;
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}

long mod_pow (long a, long x, const long MOD)
in {
    assert(0 <= x, "x must satisfy 0 <= x");
    assert(1 <= MOD, "MOD must satisfy 1 <= MOD");
    assert(MOD <= int.max, "MOD must satisfy MOD*MOD <= long.max");
}
do {
    // normalize
    a %= MOD; a += MOD; a %= MOD;

    long res = 1L;
    long base = a;
    while (0 < x) {
        if (0 < (x&1)) (res *= base) %= MOD;
        (base *= base) %= MOD;
        x >>= 1;
    }

    return res % MOD;
}
0