이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
int lenPerm, maxSum;
int dp[100][1001][101][2][2];
// dp[curPos][curSum][ccCenter][fL][fR];
int diffs[101];
void iterative() {
for (int curPos = lenPerm - 1; curPos >= 0; --curPos)
for (int _curSum = 0; _curSum <= maxSum; ++_curSum)
for (int ccCenter = 0; ccCenter <= lenPerm; ++ccCenter)
for (int fL = 0; fL < 2; ++fL)
for (int fR = 0; fR < 2; ++fR) {
int &cur = dp[curPos][_curSum][ccCenter][fL][fR];
cur = 0;
int curSum = _curSum;
curSum += fL * diffs[curPos];
curSum += fR * diffs[curPos];
curSum += ccCenter * diffs[curPos] * 2;
if (curSum > maxSum)
continue;
if (curPos == lenPerm - 1) {
if (ccCenter > 0 or (!fL and !fR))
continue;
cur = 1;
continue;
}
// Create new cc :
if (ccCenter < lenPerm)
cur += dp[curPos + 1][curSum][ccCenter + 1][fL][fR];
// Create new cc on side :
if (!fL)
cur += dp[curPos + 1][curSum][ccCenter][true][fR];
if (!fR)
cur += dp[curPos + 1][curSum][ccCenter][fL][true];
// merge center and side :
if (fL and ccCenter)
cur += ccCenter * dp[curPos + 1][curSum][ccCenter - 1][true][fR];
if (fR and ccCenter)
cur += ccCenter * dp[curPos + 1][curSum][ccCenter - 1][fL][true];
// merge centers :
if (ccCenter >= 2)
cur += ccCenter * (ccCenter - 1) *
dp[curPos + 1][curSum][ccCenter - 1][fL][fR];
// add to center :
if (ccCenter)
cur += 2 * ccCenter * dp[curPos + 1][curSum][ccCenter][fL][fR];
// add to side:
if (fL)
cur += dp[curPos + 1][curSum][ccCenter][fL][fR];
if (fR)
cur += dp[curPos + 1][curSum][ccCenter][fL][fR];
// Add to center and merge with side :
if (!fL and ccCenter)
cur += ccCenter * dp[curPos + 1][curSum][ccCenter - 1][true][fR];
if (!fR and ccCenter)
cur += ccCenter * dp[curPos + 1][curSum][ccCenter - 1][fL][true];
cur %= MOD;
}
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> lenPerm >> maxSum;
for (int i = 0; i < lenPerm; ++i) {
cin >> diffs[i];
}
sort(diffs, diffs + lenPerm);
for (int i = lenPerm - 1; i > 0; --i)
diffs[i] -= diffs[i - 1];
if (lenPerm == 1) {
cout << 1 << endl;
return 0;
}
iterative();
cout << dp[0][0][0][0][0] << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |