QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#881015#3265. 组合数问题Grand_Elf100 ✓14ms3840kbC++17923b2025-02-04 09:01:372025-02-04 09:01:37

Judging History

This is the latest submission verdict.

  • [2025-02-04 09:01:37]
  • Judged
  • Verdict: 100
  • Time: 14ms
  • Memory: 3840kb
  • [2025-02-04 09:01:37]
  • Submitted

answer

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

const int K = 55;

int n, mod, k, r;

struct M {
	int a[K][K];

	M () {
		memset(a, 0, sizeof(a));
	}

	M operator * (const M &rhs) const {
		M res;
		for (int i = 0; i < k; i++) {
			for (int j = 0; j < k; j++) {
				for (int x = 0; x < k; x++) {
					res.a[i][x] += 1ll * a[i][j] * rhs.a[j][x] % mod;
					if (res.a[i][x] >= mod) {
						res.a[i][x] -= mod;
					}
				}
			}
		}
		return res;
	}
} U, I;

M qpow(M x, long long y) {
	M res = U;
	while (y) {
		if (y & 1) {
			res = res * x;
		}
		x = x * x;
		y >>= 1;
	}
	return res;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> mod >> k >> r;
	for (int i = 0; i < k; i++) {
		U.a[i][i] = 1;
	}
	for (int i = 0; i < k; i++) {
		I.a[i][i]++;
		I.a[i][(i + 1) % k]++;
	}
	M res = qpow(I, 1ll * n * k);
	cout << res.a[0][r] << '\n';

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 1ms
memory: 3712kb

input:

14 861021533 17 8

output:

109194683

result:

ok single line: '109194683'

Test #2:

score: 5
Accepted
time: 0ms
memory: 3840kb

input:

14 734575201 8 4

output:

136629647

result:

ok single line: '136629647'

Test #3:

score: 5
Accepted
time: 0ms
memory: 3840kb

input:

6 572660339 1 0

output:

64

result:

ok single line: '64'

Test #4:

score: 5
Accepted
time: 0ms
memory: 3712kb

input:

17 141616127 10 5

output:

87241654

result:

ok single line: '87241654'

Test #5:

score: 5
Accepted
time: 0ms
memory: 3712kb

input:

13 889947181 16 12

output:

100910764

result:

ok single line: '100910764'

Test #6:

score: 5
Accepted
time: 0ms
memory: 3712kb

input:

25 752392757 4 3

output:

424857239

result:

ok single line: '424857239'

Test #7:

score: 5
Accepted
time: 14ms
memory: 3712kb

input:

553999928 2 45 26

output:

0

result:

ok single line: '0'

Test #8:

score: 5
Accepted
time: 0ms
memory: 3840kb

input:

843993365 943947738 1 0

output:

470996456

result:

ok single line: '470996456'

Test #9:

score: 5
Accepted
time: 0ms
memory: 3712kb

input:

855636225 749698583 2 0

output:

545006096

result:

ok single line: '545006096'

Test #10:

score: 5
Accepted
time: 0ms
memory: 3712kb

input:

969348092 956297536 2 1

output:

679600512

result:

ok single line: '679600512'

Test #11:

score: 5
Accepted
time: 6ms
memory: 3712kb

input:

650 660260759 45 16

output:

173373801

result:

ok single line: '173373801'

Test #12:

score: 5
Accepted
time: 5ms
memory: 3840kb

input:

598 221558443 41 40

output:

199993457

result:

ok single line: '199993457'

Test #13:

score: 5
Accepted
time: 6ms
memory: 3584kb

input:

863 269455309 42 29

output:

172561433

result:

ok single line: '172561433'

Test #14:

score: 5
Accepted
time: 4ms
memory: 3712kb

input:

28627 269441503 34 24

output:

180377871

result:

ok single line: '180377871'

Test #15:

score: 5
Accepted
time: 1ms
memory: 3840kb

input:

36656 67854541 25 6

output:

2429047

result:

ok single line: '2429047'

Test #16:

score: 5
Accepted
time: 1ms
memory: 3712kb

input:

27459 172755593 27 20

output:

73460042

result:

ok single line: '73460042'

Test #17:

score: 5
Accepted
time: 5ms
memory: 3712kb

input:

743268139 705178739 33 19

output:

311756018

result:

ok single line: '311756018'

Test #18:

score: 5
Accepted
time: 14ms
memory: 3840kb

input:

934248626 620145553 43 16

output:

121876656

result:

ok single line: '121876656'

Test #19:

score: 5
Accepted
time: 8ms
memory: 3712kb

input:

619290070 907487131 38 19

output:

213941765

result:

ok single line: '213941765'

Test #20:

score: 5
Accepted
time: 3ms
memory: 3712kb

input:

507684930 711845893 28 24

output:

419183631

result:

ok single line: '419183631'

Extra Test:

score: 0
Extra Test Passed