[dp] ARC160F Count Sorted Arrays
排列个数有足足
有套路:将排列
也就是说,考虑二进制数
而且
但是本题
那么有效操作是什么量级呢?先记
我们将证明操作前后有效操作数量单调递减:考虑
证明结束?上述讨论只考虑了
初始时
然后拿个 vector 存
一个 01 串产生变化的
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ADD(a, b) a = (a + b)
#define chk(x) (!(x) || __builtin_ctz(x) == n - __builtin_popcount(x))
#define id(x, p) (((x) >> p) & 1)
#define ID(x, y) ((x) * n + (y))
//x=0时ctz出错
const int N = 15;
int n, m, x, y, a[1 << N], f[1 << N], ans;
bool vis[1 << N];
vector<int> buc[N * N];
inline void ins(int a, int b, int x) {buc[ID(a, b)].push_back(x);}
inline void upd(){
memset(f, 0, sizeof f), f[0] = 1;
for(int i = 1; i < 1 << n; ++i)
if(vis[i])
for(int j = 0; j < n; ++j)
if(id(i, j)) ADD(f[i], f[i ^ (1 << j)]);
ans = f[(1 << n) - 1];
}
signed main(){
cin >> n >> m;
for(int i = 0; i < 1 << n; ++i){
vis[i] = chk(a[i] = i);
for(int x = 0; x < n; ++x)
for(int y = x + 1; y < n; ++y)
if(id(i, x) && !id(i, y)) ins(x, y, i);
}
upd(), ans = 1;
while(m--){
scanf("%lld%lld", &x, &y);
x = (x + ans) % n, y = (y + 2 * ans) % n;
if(x > y) swap(x, y);
bool fl = false;
for(auto i : buc[ID(x, y)])
if(id(a[i], x) && !id(a[i], y)){
a[i] ^= (1 << x) | (1 << y);
if(chk(a[i])) vis[i] = true, fl = true;
else{
for(int p = 0; p < x; ++p) if(id(a[i], p)) ins(p, x, i);
for(int p = y + 1; p < n; ++p) if(!id(a[i], p)) ins(y, p, i);
}
}
buc[ID(x, y)].clear();
if(fl) upd();
printf("%lld\n", ans);
}
return 0;
}