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>
using namespace std;
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template <typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
template<int MOD>
struct ModInt {
long long v;
ModInt(long long _v = 0) {v = (-MOD < _v && _v < MOD) ? _v : _v % MOD; if (v < 0) v += MOD;}
ModInt& operator += (const ModInt &other) {v += other.v; if (v >= MOD) v -= MOD; return *this;}
ModInt& operator -= (const ModInt &other) {v -= other.v; if (v < 0) v += MOD; return *this;}
ModInt& operator *= (const ModInt &other) {v = v * other.v % MOD; return *this;}
ModInt& operator /= (const ModInt &other) {return *this *= inverse(other);}
bool operator == (const ModInt &other) const {return v == other.v;}
bool operator != (const ModInt &other) const {return v != other.v;}
friend ModInt operator + (ModInt a, const ModInt &b) {return a += b;}
friend ModInt operator - (ModInt a, const ModInt &b) {return a -= b;}
friend ModInt operator * (ModInt a, const ModInt &b) {return a *= b;}
friend ModInt operator / (ModInt a, const ModInt &b) {return a /= b;}
friend ModInt operator - (const ModInt &a) {return 0 - a;}
friend ModInt power(ModInt a, long long b) {ModInt ret(1); while (b > 0) {if (b & 1) ret *= a; a *= a; b >>= 1;} return ret;}
friend ModInt inverse(ModInt a) {return power(a, MOD - 2);}
friend ostream& operator << (ostream &os, const ModInt &m) {return os << m.v;}
};
typedef ModInt<1000000007> M;
// number of placed values, number of components, sum of abs(f_i - f_{i+1}), number of end gaps filled
M dp[105][105][1005][3];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, l;
cin >> n >> l;
vector<int> a(n + 1);
for (int i=0; i<n; i++)
cin >> a[i];
a[n] = 1e4;
if (n == 1) {
cout << "1\n";
return 0;
}
sort(a.begin(), a.end());
// form new individual component, joined to one of ends
if (a[1] - a[0] <= l)
dp[1][1][a[1] - a[0]][1] = 2;
// form new individual component, not joined to end
if (2 * (a[1] - a[0]) <= l)
dp[1][1][2 * (a[1] - a[0])][0] = 1;
for (int i=1; i<n; i++)
for (int j=1; j<=i; j++)
for (int k=0; k<=l; k++)
for (int m=0; m<3; m++) {
int diff = a[i+1] - a[i];
// joined to one of ends
if (m < 2 && k + (2 * j - m + 1) * diff <= l)
dp[i+1][j+1][k+(2*j-m+1)*diff][m+1] += dp[i][j][k][m] * (2 - m);
// joined to an existing component
if (k + (2 * j - m) * diff <= l)
dp[i+1][j][k+(2*j-m)*diff][m] += dp[i][j][k][m] * (2 * j - m);
// merge two existing components together
if (j > 1 && k + (2 * (j - 1) - m) * diff <= l)
dp[i+1][j-1][k+(2*(j-1)-m)*diff][m] += dp[i][j][k][m] * (j - 1);
// merge a component with an end
if (m < 2 && k + (2 * j - m - 1) * diff <= l)
dp[i+1][j][k+(2*j-m-1)*diff][m+1] += dp[i][j][k][m] * (2 - m);
// not joined to anything
if (k + (2 * (j + 1) - m) * diff <= l)
dp[i+1][j+1][k+(2*(j+1)-m)*diff][m] += dp[i][j][k][m] * (j + 1 - m);
}
M ret = 0;
for (int k=0; k<=l; k++)
ret += dp[n][1][k][2];
cout << ret << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |