結果

問題 No.1105 Many Triplets
ユーザー kokatsukokatsu
提出日時 2021-11-23 23:33:09
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,287 bytes
コンパイル時間 1,968 ms
コンパイル使用メモリ 202,728 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-04 15:07:50
合計ジャッジ時間 3,430 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,508 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 1 ms
4,388 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main() {
    long N;
    readf("%d\n", N);

    long A1, B1, C1;
    readf("%d %d %d\n", A1, B1, C1);

    long M = 10 ^^ 9 + 7;

    long[][] mat = [
        [1L, M-1L, 0L],
        [0L, 1L, M-1L],
        [M-1L, 0L, 1L]
    ];

    auto X = matPowMod(mat, N-1, M);

    long A = (X[0][0] * A1 % M + X[0][1] * B1 % M + X[0][2] * C1 % M) % M;
    long B = (X[1][0] * A1 % M + X[1][1] * B1 % M + X[1][2] * C1 % M) % M;
    long C = (X[2][0] * A1 % M + X[2][1] * B1 % M + X[2][2] * C1 % M) % M;

    writeln(A, " ", B, " ", C);
}

long[][] matMulMod(long[][] a, long[][] b, long m) {
    long l = a[0].length.to!long;

    assert(l == b.length);

    auto res = new long[][](a.length, b[0].length);
    foreach (i, r; res) {
        foreach (k; 0 .. l) {
            foreach (j, ref e; r) {
                e = (e + a[i][k] * b[k][j]) % m;
            }
        }
    }

    return res;
}

long[][] matPowMod(long[][] mat, long p, long m) {
    long l = mat[0].length.to!long;

    assert(l == mat.length);

    auto res = new long[][](l, l);
    foreach (i; 0 .. l) {
        res[i][i] = 1;
    }

    while (p > 0) {
        if (p & 1) {
            res = matMulMod(res, mat, m);
        }
        mat = matMulMod(mat, mat, m);
        p >>= 1;
    }

    return res;
}
0