题意
给一个长度为 () 的序列 。执行 () 次操作,每次操作为交换两个位置上的数。求对操作的每个子集,执行完集合中的所有操作后序列的逆序数的和。
题解
怎么又是 sb 题。
记 为 的方案数,暴力。
时间复杂度 。
代码
#include <bits/stdc++.h>
using namespace std;
constexpr int P = 1'000'000'007, INV2 = 500'000'004;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
vector<vector<int>> f(n, vector<int>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
f[i][j] = a[i] > a[j];
for (int i = 0; i < q; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
for (int j = 0; j < n; ++j) {
if (j != x && j != y) {
int v = 1LL * (f[x][j] + f[y][j]) * INV2 % P;
f[x][j] = f[y][j] = v;
v = 1LL * (f[j][x] + f[j][y]) * INV2 % P;
f[j][x] = f[j][y] = v;
}
}
int v = 1LL * (f[x][y] + f[y][x]) * INV2 % P;
f[x][y] = f[y][x] = v;
}
int ans = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < i; ++j)
ans = (ans + f[j][i]) % P;
for (int i = 0; i < q; ++i)
ans = 2 * ans % P;
cout << ans << endl;
return 0;
}