解題思路
每個中心加小圈形成完全圖;完全圖的無環定向等價於一個唯一全序。中心圈先決定一個全序,有n0!種。
在中心順序固定後,加入第i小圈的人,能與中心交錯的全序數為(n0+ni)!/n0!。不同小圈沒有直接邊,只需共用中心順序,這些選擇互相獨立。
任一有向環若跨小圈,必須經過中心,會使中心順序產生矛盾;因此一致的局部全序恰好得到全圖無環定向。答案n0!×∏(n0+ni)!×(n0!)^(−t) modP。只計算需要的階乘索引。
令U=max(n0+ni),時間O(U+t log t+log P),儲存查詢與所需階乘O(t);不配置U長度表。
Step-by-step animation
共用完全子圖的無環定向|n0=1,兩個小圈各1人,中心只有一種順序。
步驟 1 / 3
中心順序
n0=1,兩個小圈各1人,中心只有一種順序。
中心A
- 中心排列
- 1
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n0;
cin >> t >> n0;
vector<int> a(t), need{n0};
for (int &x : a) {
cin >> x;
need.push_back(n0 + x);
}
sort(need.begin(), need.end());
need.erase(unique(need.begin(), need.end()), need.end());
const long long M = 1000000007;
map<int, long long> fac;
long long f = 1;
int at = 0;
for (int i = 1; i <= need.back(); i++) {
f = f * i % M;
if (i == need[at]) {
fac[i] = f;
at++;
if (at == (int)need.size())
break;
}
}
auto pw = [&](long long x, long long e) {
long long r = 1;
while (e) {
if (e & 1)
r = r * x % M;
x = x * x % M;
e /= 2;
}
return r;
};
long long ans = fac[n0];
for (int x : a)
ans = ans * fac[n0 + x] % M;
ans = ans * pw(pw(fac[n0], M - 2), t) % M;
cout << ans << "\n";
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。