洛谷 - P12142
Chthohepta · · 题解
题意
小蓝正在拷贝数据,数据是一个
思路
可以指定两个数为
将选出的
总方案为
但有些数可能相等,所以要去掉重复的贡献,设
最终新数组的贡献为
最终答案为所有贡献的和。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 5e5 + 1, mod = 1000000007;
int n;
ll a[N];
int _n, _m;
ll cnt[N];
ll s;
ll ans;
ll fr[N];
ll ny[N];
ll po(ll a, ll b, ll mod){
ll ans = 1;
while(b){
if(b & 1){
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return ans;
}
void init(){
fr[0] = 1;
for(int i = 1; i < N; i ++){
fr[i] = fr[i - 1] * i % mod;
}
ny[N - 1] = po(fr[N - 1], mod - 2, mod);
for(int i = N - 1; i >= 1; i --){
ny[i - 1] = ny[i] * i % mod;
}
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
init();
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
cnt[a[i]] ++;
}
if(n == 1){
cout << 0;
exit(0);
}
for(int i = 1; i <= n - 2; i ++){
if((n - 2) % i != 0){
continue;
}
_n = i, _m = (n - 2) / i;
if(cnt[_n] < 2 && _n == _m){
continue;
}
if(cnt[_n] == 0 || cnt[_m] == 0){
continue;
}
cnt[_n] --;
cnt[_m] --;
s = 1;
for(int i = 1; i < N; i ++){
s = s * ny[cnt[i]] % mod;
}
ans = (ans + fr[n - 2] * s % mod) % mod;
cnt[_n] ++;
cnt[_m] ++;
}
cout << ans;
return 0;
}
/***
6
2 2 1 4 3 3
***/