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;
using lint = long long;
const int MAX_N = 1e4 + 5;
const lint MOD = 1e6 + 7;
int n;
int arr[MAX_N];
lint mod(lint x) {
return x % MOD;
}
int n_unis[MAX_N]; // [1, i - 1]
void precomp() {
set<int> unis;
for (int i = 1; i <= n; i++) {
n_unis[i] = unis.size();
unis.insert(arr[i]);
}
}
lint dp[2][MAX_N];
int main() {
// freopen("calvin.in", "r", stdin);
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i];
precomp();
lint ans = 0;
for (int s = n; s >= 1; s--) {
int p = s % 2, opp_p = (s + 1) % 2;
for (int t = 1; t <= s; t++) {
if (s == n) dp[p][t] = 1;
else dp[p][t] = mod(dp[opp_p][t + 1] + mod(t * dp[opp_p][t]));
}
ans = mod(ans + mod(dp[p][n_unis[s]] * (arr[s] - 1)));
}
ans++;
cout << ans << '\n';
}
# | 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... |