Submission #948456
Source Code Expand
#include <bits/stdc++.h>
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
using ll=long long;
using R=long double;
const R EPS=1e-9; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r){return(r > EPS)-(r < -EPS);}
inline R sq(R x){return sqrt(max<R>(x,0.0));}
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
// Problem Specific Parameter:
// Description: [1,x]のクエリに対するデータ構造
// TimeComplexity: 更新$\mathcal{O}(\log n)$ クエリ$\mathcal{O}(\log n)$
// Verifyed: ARC 033 C
struct Binary_indexed_tree{
using T=ll;
int n;
vector<T> data;
Binary_indexed_tree(int _n):n(_n){data.assign(n+1,0);}
void add(int i,T x){
for(;i<=n;i+=i&-i) data[i]+=x;
}
T sum(int i){
T ret=0;
for(;i>0;i-=i&-i) ret+=data[i];
return ret;
}
};
const ll mod=1000000007LL;
const int limit=1<<20;
ll fact[limit];
bool used[limit];
int ary[limit];
int main(void){
int n;
cin >> n;
rep(i,n) cin >> ary[i];
rep(i,n) used[ary[i]]=true;
vector<ll> unuse;
rep(i,1,n+1) if(used[i]==false) unuse.push_back(i);
const ll k=ll(unuse.size());
fact[0]=1LL;
rep(i,1,n+1) fact[i]=1LL*i*fact[i-1]%mod;
ll ans=fact[k],zero=0LL,sum=0LL;
Binary_indexed_tree bit(n);
rrep(i,n){
ll cur=0LL;
if(ary[i]){
cur+=1LL*bit.sum(ary[i])*fact[k]%mod; //(i,j)=(non_zero,non_zero)
cur+=1LL*zero*(lower_bound(begin(unuse),end(unuse),ary[i])-begin(unuse))%mod*fact[k-1]%mod; //(i,j)=(non_zero,zero)
bit.add(ary[i],1);
sum+=1LL*(end(unuse)-lower_bound(begin(unuse),end(unuse),ary[i]))*fact[k-1]%mod; //(i,j)=(zero,non_zero)
sum%=mod;
}else{
ll allsum=(k*(k-1)/2LL)%mod;
cur+=1LL*allsum*fact[k-2]%mod*zero%mod; //(i,j)=(zero,zero)
cur+=sum; //(i,j)=(zero,non_zero)
zero++;
}
ans+=cur%mod*fact[n-1-i]%mod;
ans%=mod;
}
cout << ans << endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
E - Encyclopedia of Permutations |
User |
Hec |
Language |
C++14 (GCC 5.4.1) |
Score |
1200 |
Code Size |
2696 Byte |
Status |
AC |
Exec Time |
273 ms |
Memory |
14060 KB |
Judge Result
Set Name |
Sample |
Subtask |
All |
Score / Max Score |
0 / 0 |
500 / 500 |
700 / 700 |
Status |
|
|
|
Set Name |
Test Cases |
Sample |
0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt |
Subtask |
0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt |
All |
0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 2_033.txt, 2_034.txt, 2_035.txt, 2_036.txt, 2_037.txt, 2_038.txt, 2_039.txt, 2_040.txt, 2_041.txt, 2_042.txt, 2_043.txt, 2_044.txt, 2_045.txt, 2_046.txt, 2_047.txt, 2_048.txt, 2_049.txt, 2_050.txt, 2_051.txt, 2_052.txt, 2_053.txt, 2_054.txt, 2_055.txt, 2_056.txt, 2_057.txt |
Case Name |
Status |
Exec Time |
Memory |
0_000.txt |
AC |
3 ms |
256 KB |
0_001.txt |
AC |
3 ms |
256 KB |
0_002.txt |
AC |
3 ms |
256 KB |
0_003.txt |
AC |
3 ms |
256 KB |
0_004.txt |
AC |
3 ms |
256 KB |
1_005.txt |
AC |
3 ms |
256 KB |
1_006.txt |
AC |
3 ms |
256 KB |
1_007.txt |
AC |
3 ms |
256 KB |
1_008.txt |
AC |
3 ms |
384 KB |
1_009.txt |
AC |
3 ms |
256 KB |
1_010.txt |
AC |
3 ms |
384 KB |
1_011.txt |
AC |
3 ms |
384 KB |
1_012.txt |
AC |
3 ms |
384 KB |
1_013.txt |
AC |
3 ms |
256 KB |
1_014.txt |
AC |
3 ms |
384 KB |
1_015.txt |
AC |
3 ms |
256 KB |
1_016.txt |
AC |
3 ms |
384 KB |
1_017.txt |
AC |
3 ms |
256 KB |
1_018.txt |
AC |
4 ms |
384 KB |
1_019.txt |
AC |
3 ms |
256 KB |
1_020.txt |
AC |
4 ms |
384 KB |
1_021.txt |
AC |
3 ms |
256 KB |
1_022.txt |
AC |
4 ms |
384 KB |
1_023.txt |
AC |
3 ms |
256 KB |
1_024.txt |
AC |
4 ms |
256 KB |
1_025.txt |
AC |
3 ms |
256 KB |
1_026.txt |
AC |
3 ms |
256 KB |
1_027.txt |
AC |
3 ms |
256 KB |
1_028.txt |
AC |
3 ms |
384 KB |
1_029.txt |
AC |
4 ms |
256 KB |
1_030.txt |
AC |
3 ms |
256 KB |
1_031.txt |
AC |
3 ms |
256 KB |
1_032.txt |
AC |
4 ms |
384 KB |
2_033.txt |
AC |
110 ms |
14060 KB |
2_034.txt |
AC |
56 ms |
7152 KB |
2_035.txt |
AC |
136 ms |
14060 KB |
2_036.txt |
AC |
125 ms |
13036 KB |
2_037.txt |
AC |
161 ms |
13676 KB |
2_038.txt |
AC |
142 ms |
12012 KB |
2_039.txt |
AC |
185 ms |
13292 KB |
2_040.txt |
AC |
91 ms |
6768 KB |
2_041.txt |
AC |
208 ms |
12780 KB |
2_042.txt |
AC |
37 ms |
2808 KB |
2_043.txt |
AC |
225 ms |
12400 KB |
2_044.txt |
AC |
146 ms |
8176 KB |
2_045.txt |
AC |
244 ms |
11888 KB |
2_046.txt |
AC |
233 ms |
11504 KB |
2_047.txt |
AC |
258 ms |
11508 KB |
2_048.txt |
AC |
109 ms |
5240 KB |
2_049.txt |
AC |
273 ms |
11128 KB |
2_050.txt |
AC |
171 ms |
7416 KB |
2_051.txt |
AC |
198 ms |
10496 KB |
2_052.txt |
AC |
180 ms |
9472 KB |
2_053.txt |
AC |
110 ms |
14060 KB |
2_054.txt |
AC |
203 ms |
10496 KB |
2_055.txt |
AC |
184 ms |
10496 KB |
2_056.txt |
AC |
184 ms |
10496 KB |
2_057.txt |
AC |
174 ms |
12528 KB |