This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of god
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x))
#define debug(x) cerr << #x << " : " << x << "\n"
#define usefile freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef string str;
const int maxn = 1e4 + 5;
const int mod = 1e6 + 7;
const ll inf = 1e18;
int n, dp[maxn][2], ans, num, cnt[maxn];
ll a[maxn];
bool mrk[maxn], mrk2[maxn];
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 0; i < n; i ++)cin >> a[i];
for(int i = 1; i <= n; i ++)dp[i][1] = i + 1;
mrk[1] = 1; num = 1; cnt[0] = 1;
for(int i = 1; i < n; i ++){
if(!mrk[a[i]]){
mrk[a[i]] = 1;
mrk2[i] = 1;
num ++;
}
cnt[i] = num;
}
int t = 0, m = n;
for(int j = 2; j < n - 1; j ++){
for(int i = 1; i < m; i ++)dp[i][t] = (1ll * i * dp[i][t ^ 1] + dp[i + 1][t ^ 1]) % mod;
m --;
int ind = n - j - 1, bl = 0;
if(mrk2[ind])bl = 1;
ans = (ans + (a[ind] - 1) * dp[cnt[ind] - bl][t] % mod) % mod;
t ^= 1;
}
ans += a[n - 1];
if(n > 2)ans += (a[n - 2] - 1) * (cnt[n - 3] + 1);
cout << ans % mod;
}
# | 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... |