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;
ostream& operator<<(ostream &out, string str) {
for(char c : str) out << c;
return out;
}
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
out << "{";
for(auto it = a.begin(); it != a.end(); it = next(it))
out << (it != a.begin() ? ", " : "") << *it;
return out << "}";
}
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
cerr << a << ", ";
dump(x...);
}
#ifdef DEBUG
# define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
# define debug(...) false
#endif
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
template<class T> int size(T && a) { return a.size(); }
using LL = long long;
using PII = pair<LL, LL>;
template<int mod>
struct mi {
int val;
mi() { val = 0; }
mi(const LL& v) {
val = (-mod <= v && v <= mod) ? v : v % mod;
if(val < 0) val += mod;
}
friend ostream& operator<<(ostream &os, const mi &a) {
return os << a.val;
}
friend istream& operator>>(istream &is, mi &a) {
return is >> a.val;
}
friend bool operator==(const mi &a, const mi &b) {
return a.val == b.val;
}
friend bool operator!=(const mi &a, const mi &b) {
return !(a == b);
}
friend bool operator<(const mi &a, const mi &b) {
return a.val < b.val;
}
friend bool operator<=(const mi &a, const mi &b) {
return a.val <= b.val;
}
mi operator-() const { return mi(-val); }
mi& operator+=(const mi &m) {
if((val += m.val) >= mod) val -= mod;
return *this;
}
mi& operator-=(const mi &m) {
if((val -= m.val) < 0) val += mod;
return *this;
}
mi& operator*=(const mi &m) {
val = (LL) val * m.val % mod;
return *this;
}
friend mi qpow(mi a, LL n) {
if(n == 0) return 1;
if(n % 2 == 1) return qpow(a, n - 1) * a;
return qpow(a * a, n / 2);
}
friend mi inv(const mi &a) {
assert(a != 0); return qpow(a, mod - 2);
}
mi& operator /=(const mi &m) {
return (*this) *= inv(m);
}
friend mi operator+(mi a, const mi &b) { return a += b; }
friend mi operator-(mi a, const mi &b) { return a -= b; }
friend mi operator*(mi a, const mi &b) { return a *= b; }
friend mi operator/(mi a, const mi &b) { return a /= b; }
};
using MI = mi<int(1e6 + 7)>;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n), pref(n + 1);
REP(i, n) {
cin >> a[i]; a[i]--;
pref[i + 1] = max(pref[i], a[i]);
}
MI ans = min(pref[n - 1] + 1, a[n - 1]) + 1;
vector<vector<MI>> dp(2, vector<MI>(n + 1));
REP(j, n) dp[(n - 1) & 1][j] = j + 2, debug(j, dp[n & 1 ^ 1][j]);
for(int i = n - 2; i >= 0; i--) REP(j, n) {
int x = i & 1, y = (i + 1) & 1;
dp[x][j] = dp[y][j + 1] + dp[y][j] * (j + 1);
debug(i, j, dp[x][j]);
if(j == pref[i])
ans += min(a[i], j + 1) * dp[y][j];
}
cout << ans << "\n";
}
Compilation message (stderr)
teams.cpp: In function 'int main()':
teams.cpp:117:66: warning: right operand of comma operator has no effect [-Wunused-value]
REP(j, n) dp[(n - 1) & 1][j] = j + 2, debug(j, dp[n & 1 ^ 1][j]);
^
teams.cpp:121:24: warning: statement has no effect [-Wunused-value]
debug(i, j, dp[x][j]);
^
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |