結果

問題 No.2396 等差二項展開
ユーザー ecotteaecottea
提出日時 2023-02-26 03:56:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,484 ms / 6,000 ms
コード長 1,734 bytes
コンパイル時間 4,690 ms
コンパイル使用メモリ 259,556 KB
実行使用メモリ 19,108 KB
最終ジャッジ日時 2024-04-16 04:38:36
合計ジャッジ時間 16,577 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 11 ms
5,376 KB
testcase_16 AC 144 ms
5,632 KB
testcase_17 AC 689 ms
11,184 KB
testcase_18 AC 1,221 ms
14,336 KB
testcase_19 AC 1,484 ms
19,108 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 617 ms
12,720 KB
testcase_24 AC 1,009 ms
15,104 KB
testcase_25 AC 1,223 ms
15,104 KB
testcase_26 AC 849 ms
13,824 KB
testcase_27 AC 990 ms
14,976 KB
testcase_28 AC 1,041 ms
14,004 KB
testcase_29 AC 998 ms
14,976 KB
testcase_30 AC 688 ms
13,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std; 

#include <atcoder/all>
using namespace atcoder;
using mint = modint;

// 巡回行列の下三角部分を M 倍した行列(M-巡回行列)
using Mat = vector<vector<mint>>;

// M-巡回行列 a[0..L)[0..L), b[0..L)[0..L) の積を返す。(戻り値も M-巡回行列になる。)
Mat mul(int L, const Mat& a, const Mat& b, long long M) {
	Mat res(L, vector<mint>(L));

	// 1 行目だけちゃんと計算する。
	for (int i = 0; i < 1; i++) {
		for (int j = 0; j < L; j++) {
			for (int k = 0; k < L; k++) {
				res[i][j] += a[i][k] * b[k][j];
			}
		}
	}

	// 2 行目以降は 1 行目を参照して埋める。
	for (int i = 1; i < L; i++) {
		for (int j = 0; j < L; j++) {
			if (i <= j) res[i][j] = res[0][(j - i + L) % L];
			else res[i][j] = M * res[0][(j - i + L) % L];
		}
	}

	return res;
}


// M-巡回行列 a[0..L)[0..L) の N 乗を返す。(戻り値も M-巡回行列になる。)
Mat pow(int L, const Mat& a, long long N, long long M) {
	Mat res(L, vector<mint>(L));
	for (int i = 0; i < L; i++) res[i][i] = 1;

	Mat pow2(a);

	// 繰り返し二乗法
	while (N > 0) {
		if (N & 1) {
			res = mul(L, res, pow2, M);
		}
		pow2 = mul(L, pow2, pow2, M);
		N /= 2;
	}

	return res;
}


int main() {	
	long long N, M; int L, K, B;
	cin >> N >> M >> L >> K >> B;

	mint::set_mod(B);

	// L = 1 のときは二項定理
	if (L == 1) {
		cout << mint(1 + M).pow(N).val() << endl;
		return 0;
	}

	// a : 遷移行列(M-巡回行列)
	Mat a(L, vector<mint>(L));
	for (int i = 0; i < L; i++) a[i][i] = 1;
	for (int i = 0; i < L - 1; i++) a[i][i + 1] = 1;
	a[L - 1][0] = M;

	// N 回遷移する
	a = pow(L, a, N, M);
	
	cout << a[0][K].val() << endl;
}
0