UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#169860#97. sequencewuzr1000ms1208kbC++1.0kb2023-03-11 14:41:372023-03-11 14:41:38

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
const int N = 31, mod = 1234567891;
ll dp[2][N], c[N][N], fac[N];
int cnt[1005], n, p;
signed main() {
	cin >> n >> p;
	rep(i, 1, n) {
		int x;
		cin >> x;
		++cnt[(x % p + p) % p];
	}
	fac[0] = 1;
	rep(i, 1, N - 1) fac[i] = fac[i - 1] * i % mod;
	c[0][0] = 1;
	rep(i, 1, N - 1) {
		c[i][0] = 1;
		rep(j, 1, i) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
	}
	int pr = 0, nw = 1;
	ll F = 1;
	int s = 0;
	dp[0][0] = 1;
	rep(i, 0, p - 1) if (cnt[i]) {
		for (int j = 0; j <= s; ++j) if (dp[pr][j])
			for (int k = 1; k <= cnt[i]; ++k)
				for (int h = 0; h <= k && h <= j; ++h)
					(dp[nw][j - h + cnt[i] - k]
						+= dp[pr][j] * c[j][h] % mod * c[s + 1 - j][k - h] % mod
						* c[cnt[i] - 1][k - 1] % mod) %= mod;
		s += cnt[i];
		swap(nw, pr);
		memset(dp[nw], 0, sizeof dp[nw]);
		(F *= fac[cnt[i]]) %= mod;
	}
	cout << dp[pr][0]*F % mod;
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1204kb

input:

5 2
4 6 8 -3 7

output:

12

result:

ok single line: '12'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1208kb

input:

2 4
6 2

output:

0

result:

ok single line: '0'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1204kb

input:

11 6
807395 227594 972638 777467 -103510 -71395 247016 250862 214823 -410032 357815

output:

86400

result:

ok single line: '86400'

Test #4:

score: 10
Accepted
time: 0ms
memory: 1208kb

input:

15 2
174539 95375 -729121 453926 -324496 83600 -46240 -302272 895631 -37600 -673405 168968 -596572 -...

output:

0

result:

ok single line: '0'

Test #5:

score: 10
Accepted
time: 0ms
memory: 1208kb

input:

15 484
-40771 864890 -169198 -33088 -400552 -632734 978188 684899 -243901 652877 -230890 -141430 -10...

output:

266971431

result:

ok single line: '266971431'

Test #6:

score: 10
Accepted
time: 0ms
memory: 1204kb

input:

30 4
0 -5 1 2 3 4 5 6 7 8 9 10 12 213 123 122 21 2136 4534 2312 12312 34543 2765 56756 462346 46434 ...

output:

681816692

result:

ok single line: '681816692'

Test #7:

score: 10
Accepted
time: 0ms
memory: 1204kb

input:

29 64
-252280 -368452 -303262 -374344 441497 -359356 -210649 -267136 -213643 740639 653444 951005 32...

output:

224458919

result:

ok single line: '224458919'

Test #8:

score: 10
Accepted
time: 0ms
memory: 1208kb

input:

20 5
356048 -549295 650300 69389 -367054 -934147 840023 567719 164588 633239 -149836 -710620 -388705...

output:

10088554

result:

ok single line: '10088554'

Test #9:

score: 10
Accepted
time: 0ms
memory: 1204kb

input:

29 240
866570 591971 -118828 824093 314495 -308128 413843 832694 772121 -961372 915728 742880 -19906...

output:

194500727

result:

ok single line: '194500727'

Test #10:

score: 10
Accepted
time: 0ms
memory: 1208kb

input:

29 4
181481 576347 202304 -378526 -74698 282647 776576 -246487 -240208 329660 509207 -288685 476582 ...

output:

20947336

result:

ok single line: '20947336'