This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
#define ing long long
const int MOD = 1e9 + 7;
const int N = 2e4 + 5;
int bPow(int a, int b) {
ll ans = 1;
while (b) {
if (b & 1) ans = (1LL * ans * a) % MOD;
a = (1LL * a * a) % MOD;
b >>= 1;
}
return ans;
}
int fac[N], inv[N];
void prepare() {
fac[0] = 1;
for (int i = 1; i < N; ++i)
fac[i] = (1LL * fac[i - 1] * i) % MOD;
inv[N - 1] = bPow(fac[N - 1], MOD - 2);
for (int i = N - 2; i >= 0; --i)
inv[i] = (1LL * inv[i + 1] * (i + 1)) % MOD;
}
int C(int k, int n) {
if (k > n) return 0;
return ((1LL * fac[n] * inv[n - k]) % MOD * (inv[k])) % MOD;
}
int dp[51][51][10001];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#ifdef JASPER
freopen("in1", "r", stdin);
#endif
prepare();
int n, l;
cin >> n >> l;
vector <int> a(n + 1);
for (int i = 1; i <= n; ++i) cin >> a[i];
sort(a.begin(), a.end());
if (a[1] == a[n]) {
// sub1
int p = (n - 1) * a[1] + 1; // min length for arrangement (empty slots taken)
ll ans = (1LL * fac[n] * C(n, n + l - p)) % MOD;
cout << ans << "\n";
}
else {
// dp(i, j, p): number of ways to place first i magnets into j groups, length of arrangement = p;
dp[0][0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
for (int p = 1; p <= l; ++p) {
// create new group;
dp[i][j][p] = (dp[i][j][p] + dp[i - 1][j - 1][p - 1]) % MOD;
// place to the end of one in j groups
if (p >= a[i]) {
int ways = 2 * j; // j groups, each groups can be placed at the begin or at the end
dp[i][j][p] = (dp[i][j][p] + (dp[i - 1][j][p - a[i]] * ways) % MOD) % MOD;
}
// place between two groups
if (p >= 2 * a[i] - 1) {
int ways = j * (j + 1); // from (j + 1) groups, choose two to merge into one, each pair (x, y) has two ways to place: (x__a(i)__y) or (y__a(i)__x)
dp[i][j][p] = (dp[i][j][p] + (dp[i - 1][j + 1][p - 2 * a[i] + 1] * ways) % MOD) % MOD;
}
}
}
}
ll ans = 0;
for (int p = 1; p <= l; ++p) {
debug(dp[n][1][p]);
ans += (1LL * C(n, n + l - p) * dp[n][1][p]) % MOD;
ans %= MOD;
}
cout << ans << "\n";
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:9:20: warning: statement has no effect [-Wunused-value]
9 | #define debug(...) 166
| ^~~
Main.cpp:91:13: note: in expansion of macro 'debug'
91 | debug(dp[n][1][p]);
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |