Problem Statement

There are an integer $N$ and $M$ pairs of integers: $(a_1, b_1), (a_2, b_2), \dots, (a_M, b_M)$. Each pair $(a_i, b_i)$ satisfies $1 \leq a_i \lt b_i \leq N$.

Initally, you have all $N!$ permutations of $(1,2,\dots,N)$.

You will perform $M$ operations. The $i$-th operation is as follows.

  • Do the following for each of your $N!$ permutations.
    • Compare the $a_i$-th and $b_i$-th elements. If the former is greater, swap the two elements.

For each $1 \leq i \leq M$, let $S_i$ be the number of permutations of yours that are already sorted in ascending order at the end of the $i$-th operation.

Print $S_1, S_2, \dots, S_M$.

Here, the input gives you pairs of integers $(x_i, y_i)$ instead of $(a_i, b_i)$.

The values of $(a_i, b_i)$ can be obtained using $x_i$, $y_i$, and $S_{i-1}$ as follows. (Let $S_0 = 1$ for convenience.)

  • Let $c_i = ((x_i + S_{i-1}) \bmod N) + 1$.
  • Let $d_i = ((y_i + S_{i-1} \times 2) \bmod N) + 1$. (Here, it is guaranteed that $c_i \neq d_i$.)
  • Let $a_i = \min(c_i, d_i)$.
  • Let $b_i = \max(c_i, d_i)$.

Constraints

  • $2 \leq N \leq 15$
  • $1 \leq M \leq 5 \times 10^5$
  • $1 \leq a_i \lt b_i \leq N$
  • $0 \leq x_i, y_i \leq N - 1$

Input

The input is given from Standard Input in the following format:

$N$ $M$
$x_1$ $y_1$
$x_2$ $y_2$
$\vdots$
$x_M$ $y_M$

Output

Print $M$ lines. The $i$-th line should contain $S_i$.


Sample Input 1

2 1
1 1

Sample Output 1

2

You start with the permutations $(1, 2)$ and $(2, 1)$.

We have $(a_1, b_1) = (1, 2)$. At the end of the first operation, you have two copies of $(1, 2)$, so you should print $2$.


Sample Input 2

3 4
0 1
2 1
1 1
0 1

Sample Output 2

2
4
4
6

$(a_i, b_i)$ in order are $(1, 2), (2, 3), (1, 3), (1, 2)$.


Sample Input 3

5 5
4 4
0 4
1 1
2 4
1 2

Sample Output 3

2
4
4
8
16

$(a_i, b_i)$ in order are $(1, 2), (3, 4), (1, 5), (2, 3), (4, 5)$.

一道妙妙题。

(据说是一个套路)发现其实我们整个过程中只关心大小关系,但是又要去统计。我们把一个排列压成 \(n\) 个二进制数,第 \(i\) 个二进制数第 \(j\) 位表示 \(p_j\) 是否大于 \(i\)

这样子看起来很废话,但是仔细观察会发现,在进行题目中的交换操作后,有些二进制数会从不合法变成合法,而如果我们已经知道了那些二进制数是合法的,那么可以进行一个 \(O(2^n\times n)\) 的dp去统计答案。定义 \(dp_{S}\) 为目前到达二进制数 \(S\) 时的答案,枚举数字 \(popcount(S)+1\) 放在了哪里,转移易得。那么最终答案就是 \(dp_{2^n-1}\)

如果我们知道现在那个二进制数从不合法变成了合法,怎么更新 dp 值。由于每个二进制数最多只会进行这样一次过程,所以我们可以使用暴力重构的方式。如果 \(S\) 从不合法成为合法,那么只有 \(S\) 的超集会发生变化,对他们进行暴力重构即可。超集枚举加dp总复杂度 \(O(3^nn)\)

现在剩下的问题就是如何知道每次二进制数改完,有那些二进制数发生了变化。一个二进制数最多会被交换 \(O(n^2)\) 次,所以也是考虑维护所有的可以变换的二进制数,然后给他们暴力交换,暴力判断。维护 \(n^2\) 个队列 \(q[x][y]\),表示把 \(x\) 和 \(y\) 交换了后会有更新的二进制数,而二进制数交换完之后,把所有新增加的 \((x,y)\) ,加入队列。这样子每个二进制数最多会被交换 \(O(n^2)\) 次,而每次交换最多会新增 \(O(n)\) 个数对,他最多会被加入队列 \(O(n^3)\) 次,所以这里复杂度 \(O(n^32^n)\)

#include<bits/stdc++.h>
using namespace std;
const int N=15,P=998244353;
int n,m,x,y,st[1<<N],S,v[1<<N],q[N][N][(1<<N)*N],ll[N][N],rr[N][N],to[1<<N],is[1<<N];
long long dp[1<<N],ls=1;
void getnew(int s)
{
dp[s]=0;
for(int i=0;i<n;i++)
if(s>>i&1)
dp[s]+=dp[s^(1<<i)];
}
void ins(int s)
{
if(v[s])
return;
// printf("%d\n",s);
v[s]=1;
int tp=0;
for(int i=S-s;i;i=(i-1)&(S-s))
if(v[S-i])
st[++tp]=S-i;
st[++tp]=S;
for(int i=1;i<=tp;i++)
getnew(st[i]);
}
void newdot(int s,int x)
{
if(is[to[s]])
ins(s);
for(int j=0;j<x;j++)
if(!(to[s]>>j&1))
q[j][x][++rr[j][x]]=s;
}
void newnode(int s,int x)
{
for(int j=x+1;j<n;j++)
if((to[s]>>j&1))
q[x][j][++rr[x][j]]=s;
}
int main()
{
scanf("%d%d",&n,&m);
S=(1<<n)-1;
v[0]=dp[0]=is[0]=1;
for(int i=1;i<=n;i++)
ins((1<<i)-1),is[(1<<i)-1]=1;
for(int i=0;i<=S;i++)
{
to[i]=i;
for(int j=0;j<n;j++)
if(i>>j&1)
newdot(i,j);
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
x=(x+ls)%n;
y=(y+ls*2)%n;
if(x>y)
swap(x,y);
for(int i=ll[x][y]+1;i<=rr[x][y];i++)
{
int s=to[q[x][y][i]];
if(!(s>>x&1)&&(s>>y&1))
{
to[q[x][y][i]]^=(1<<x)^(1<<y);
newdot(q[x][y][i],x);
newnode(q[x][y][i],y);
}
}
ll[x][y]=rr[x][y];
printf("%lld\n",ls=dp[(1<<n)-1]);
}
}

[ARC160F] Count Sorted Arrays的更多相关文章

  1. No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

  2. 【leetcode】4. Median of Two Sorted Arrays

    题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...

  3. Median of Two Sorted Arrays 解答

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  4. Median of Sorted Arrays

    (http://leetcode.com/2011/03/median-of-two-sorted-arrays.html) There are two sorted arrays A and B o ...

  5. Leetcode刷题C#版之 Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  6. [Swift]LeetCode4. 两个排序数组的中位数 | Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. LeetCode--No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

  8. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  9. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  10. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. 分布式存储系统举例剖析(elasticsearch,kafka,redis-cluster)

    1. 概述 对于分布式系统,人们首先对现实中的分布式系统进行高层抽象,然后做出各种假设,发展了诸如CAP, FLP 等理论,提出了很多一致性模型,Paxos 是其中最璀璨的明珠.我们对分布式系统的时序 ...

  2. [ABC213E] Stronger Takahashi

    2023-01-17 题目 题目传送门 翻译 难度&重要性(1~10):4 题目来源 AtCoder 题目算法 bfs 解题思路 首先,这道题的问题是从家到鱼市摧毁障碍物的最少次数.我们很容易 ...

  3. Vue源码学习(二):<templete>渲染第一步,模板解析

    好家伙, 1.<template>去哪了 在正式内容之前,我们来思考一个问题, 当我们使用vue开发页面时,<tamplete>中的内容是如何变成我们网页中的内容的? 它会经历 ...

  4. spark修改控制台输出日志级别

    spark修改控制台输出日志级别 修改conf/log4j.properties cd $SPARK_HOME/conf cp log4j.properties.template ./log4j.pr ...

  5. Jupyter_Notebook_添加代码自动补全功能

    Jupyter Notebook 添加代码自动补全功能 安装 如果之前安装过显示目录功能的话,这一步骤可以跳过. pip install jupyter_contrib_nbextensions 配置 ...

  6. 2023.09.29 入门级 J2 模拟赛 赛后总结(尝试第一篇总结)

    T1:变换(change) 一道大水题. 赛场上想都没想就切掉了 不难发现,转换的过程只和a 和b 的二进制位有关,且不同二进制位之间无关.我们可以将a 和b 转化为二进制表示,每一位分别判断,如果这 ...

  7. Python - 读取CSV文件发现有重复数据,如何清洗以及保存为CSV文件,这里有完整的过程!!!! 片尾有彩蛋

    语言:Python 功能: 1.清洗CSV文件中重复数据. 2.保存为CSV文件 大体流程: 1.首先观察CSV文件中的数据布局格式如何? 2.通过csv包读取数据.并根据规则使用continue,来 ...

  8. MyBatis foreach循环批量修改数据时报错

    报错如下 org.springframework.jdbc.BadSqlGrammarException: ### Error updating database. Cause: java.sql.S ...

  9. 实战0-1,Java开发者也能看懂的大模型应用开发实践!!!

    前言 在前几天的文章<续写AI技术新篇,融汇工程化实践>中,我分享说在RAG领域,很多都是工程上的实践,做AI大模型应用的开发其实Java也能写,那么本文就一个Java开发者的立场,构建实 ...

  10. 从零用VitePress搭建博客教程(2) –VitePress默认首页和头部导航、左侧导航配置

    2. 从零用VitePress搭建博客教程(2) –VitePress默认首页和头部导航.左侧导航配置 接上一节: 从零用VitePress搭建博客教程(1) – VitePress的安装和运行 四. ...