Atcoder Beginner Contest 321 G - Electric Circuit 题解 - 状压dp | 指定最低位
为了更好的阅读体验,请点击这里
题目链接:G - Electric Circuit
看到了 \(N\) 的数据范围,因此是显然的状压 dp。
不妨设 \(f_S\) 为仅使用 \(S\) 集合中的所有点,能够连成恰好 \(1\) 个连通块的方案数。\(g_S\) 为仅使用 \(S\) 集合中的所有点的方案数,其中 \(cntr(S)\) 在 \(S\) 中为 red 的个数,\(cntb(S)\) 为在 \(S\) 中 blue 的个数。
不难发现对于某一集合 \(S\) 而言,只有在 \(cntr(S) = cntb(S)\) 时才能连成恰好 \(1\) 个连通块,对于答案才有贡献。因此最终答案为:
\]
且容易观察到 \(g_S = cntr(S)!\)
再想一下 \(f_S\) 和 \(g_S\) 的关系,如何求得 \(f_S\) 呢?枚举 \(S\) 的子集 \(T\),以 \(f_T\) 加权和求得 \(g_S\),即恰好用 \(T\) 这个集合构成 \(1\) 个连通块,而剩下的随意排布,方案数即为排列数。(下式是个错误式子)
\]
上式的问题之处在于,如果 \(T\) 和 \(S \setminus T\) 同时可以构成恰好 \(1\) 个连通块,那么这种方案数就被算了两遍。因此,可以指定最低位的数 \(a\),钦定它在集合 \(T\) 中,再推导一下,有:
\]
这个题就做完了,最后我们证明一下为什么指定最低位的数 \(a\) 转移能不重不漏,将下列四种情况代入回上面式子有:
- 当 \(f_T=0, f_{S\setminus T}=0\)时,无影响
- 当 \(f_T \not =0, f_{S\setminus T}=0\)时,无影响,且这种情况不可能出现
- 当 \(f_T=0, f_{S\setminus T} \not =0\)时,这种情况不可能出现
- 当 \(f_T \not =0, f_{S\setminus T} \not =0\)时,无影响
唯一能影响到答案的情况 3 在当前 \(f_S \not = 0\) 的情况下不可能出现,因此成立。
应用这种指定最低位的数 \(a\) 的方法(泛化一下是任意指定某个数的方法)应当满足如下几个要素:
- 求方案数(也许求别的也可以应用)
- 对于某个集合 \(S\),将其分割为两个集合 \(T\) 和 \(S\setminus T\) 时,满足都同 \(0\) 或都不同 \(0\),形式化地为以下两个条件中的一个:
- \(f_T=0且f_{S\setminus T}=0\)
- \(f_T \not =0且f_{S\setminus T} \not =0\)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ld;
#define IL inline
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define dbg1(x) cout << #x << " = " << x << ", "
#define dbg2(x) cout << #x << " = " << x << endl
template <typename T>
void _debug(const char* format, T t) {
cerr << format << '=' << t << endl;
}
template <class First, class... Rest>
void _debug(const char* format, First first, Rest... rest) {
while (*format != ',') cerr << *format++;
cerr << '=' << first << ',';
_debug(format + 1, rest...);
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& V) {
os << "[ ";
for (const auto& vv : V) os << vv << ", ";
os << ']';
return os;
}
#ifdef LOCAL
#define dbg(...) _debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#endif
template<typename Tp> IL void read(Tp &x) {
x=0; int f=1; char ch=getchar();
while(!isdigit(ch)) {if(ch == '-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
x *= f;
}
template<typename First, typename... Rest> IL void read(First &first, Rest&... rest) {
read(first); read(rest...);
}
int buf[42];
template<typename Tp> IL void write(Tp x) {
int p = 0;
if(x < 0) { putchar('-'); x=-x;}
if(x == 0) { putchar('0'); return;}
while(x) {
buf[++p] = x % 10;
x /= 10;
}
for(int i=p;i;i--) putchar('0' + buf[i]);
}
template<typename First, typename... Rest> IL void write(const First& first, const Rest&... rest) {
write(first); putchar(32); write(rest...);
}
#include <atcoder/modint.hpp>
using mint = atcoder::modint998244353;
void solve() {
int n, m; read(n, m);
vector<int> cntr(1 << n), cntb(1 << n);
for (int i = 0; i < m; i++) {
int r; read(r); r--;
cntr[1 << r]++;
}
for (int i = 0; i < m; i++) {
int b; read(b); b--;
cntb[1 << b]++;
}
for (int S = 2; S < (1 << n); S++) {
if (__builtin_popcount(S) < 2) continue;
for (int i = 0; i < n; i++) if (S >> i & 1) {
cntr[S] += cntr[1 << i];
cntb[S] += cntb[1 << i];
}
}
vector<mint> f(1 << n), g(1 << n);
vector<mint> J(m + 1);
J[0] = 1;
for (int i = 1; i <= m; i++) J[i] = J[i-1] * i;
mint ans = 0;
for (int S = 1; S < (1 << n); S++) {
if (cntr[S] != cntb[S]) continue;
f[S] = g[S] = J[cntr[S]];
for (int T = (S - 1) & S; T > S - T; T = (T - 1) & S) {
f[S] -= f[T] * g[S - T];
}
ans += f[S] * J[m - cntr[S]];
}
ans /= J[m];
write(ans.val()); putchar(10);
}
int main() {
#ifdef LOCAL
freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
#endif
int T = 1;
// read(T);
while(T--) solve();
return 0;
}
Atcoder Beginner Contest 321 G - Electric Circuit 题解 - 状压dp | 指定最低位的更多相关文章
- AtCoder Beginner Contest 272 - G - Yet Another mod M
随机 + 数论 题意 Submission #35524126 - AtCoder Beginner Contest 272 给一个长度为 \(n\;(1<=n<=5000)\) 的数组 ...
- AtCoder Beginner Contest 260 G // imos(累积和算法)
题目传送门:G - Scalene Triangle Area (atcoder.jp) 题意: 给定大小为N*N的OX矩阵,若矩阵的(s,t)处为O,其覆盖范围为:满足以下条件的所有位置(i,j) ...
- AtCoder Beginner Contest 282 G - Similar Permutation
套路题 题意 求有多少个 \(1\) 到 \(n\) 的排列满足恰有 \(k\) 对在排列中相邻的数满足前小于后 \(2 \leq n \leq 500, 0 \leq k \leq (n - 1)\ ...
- AtCoder Beginner Contest 178 E - Dist Max 题解(推公式)
题目链接 题目大意 给你n个点(n<=2e5)要你求所有点中两个点最短的曼哈顿距离 曼哈顿距离定义为d(i,j)=|x1-x2|+|y1-y2|. 题目思路 想了很久也没有什么思路,其实就是一个 ...
- 【AtCoder Beginner Contest 181】A~F题解
越学越菜系列 于2020.11.2,我绿了(错乱) A - Heavy Rotation 签到题,奇数Black,偶数White. code: #include<bits/stdc++.h> ...
- [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)
[BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...
- O - Matching 题解(状压dp)
题目链接 题目大意 给你一个方形矩阵mp,边长为n(n<=21) 有n个男生和女生,如果\(mp[i][j]=1\) 代表第i个男生可以和第j个女生配对 问有多少种两两配对的方式,使得所有男生和 ...
- NOIP2017 宝藏 题解报告【状压dp】
题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的 m 条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋中的宝藏.但是 ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 177 题解
AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...
随机推荐
- WPF 将控件放入到 UserControl 里获取 HwndSource 为空的情况
本文记录将 WPF 控件放入到 UserControl 里,如果此 UserControl 没有被设置 Visibility 为可见过,那么放在此 UserControl 内的控件将获取不到 Hwnd ...
- Fiddler对安卓模拟器中的app抓包
工具资源 Fiddler: https://www.telerik.com/download/fiddler Xposed Installer: https://repo.xposed.info/mo ...
- vue中使用vue-b2wordcloud创建词云
安装使用 安装:使用npm install vue-b2wordcloud --save或者直接在vue ui中添加vue-b2wordcloud运行依赖 使用:在main.js中导入使用 impor ...
- 优秀的 RocketMQ 可视化管理工具 GUI 客户端
优秀的 RocketMQ 可视化管理工具 GUI 客户端 官网地址:http://www.redisant.cn/rocketmq 快速查看所有 RocketMQ 集群,包括Brokers.Topic ...
- 【GUI软件】小红书详情数据批量采集,含笔记内容、转评赞藏等,支持多笔记同时采集!
目录 一.背景介绍 1.1 爬取目标 1.2 演示视频 1.3 软件说明 二.代码讲解 2.1 爬虫采集模块 2.2 软件界面模块 2.3 日志模块 三.获取源码及软件 一.背景介绍 1.1 爬取目标 ...
- ITIL是标准吗?
ITIL不是标准 OGC:是一个推荐的管理框架,一个模版,可根据运维实践自由裁量落地 itil诞生环境:欧美思维.欧美文化.欧美制度.欧美人文习惯.... 对欧美来说可能是最佳实践,但是对中国特色文化 ...
- Nginx教程+笔记
Nginx 学习视频: 2020最新 Nginx教程全面讲解(Nginx快速上手) https://www.bilibili.com/video/BV1W54y1z7GM?t=553&p=14 ...
- 【原创】不同RTOS POSIX接口的实现差异
目录 前言 POSIX简介 RTOS对POSIX的实现情况 Zephyr FreeRTOS RTOS提供的POSIX接口实时吗? nanosleep Timer-不同linux版本和xenomai的实 ...
- nim 1. 安装、IDE、HelloWorld
2015年,某大神写过nim的教程,请参阅: Nim教程[一] - liulun - 博客园 (cnblogs.com) 七年过去了, nim应该更成熟了. 1.安装 下载页面:Windows ins ...
- 智慧城市three.js数字孪生三维展示城市建筑轮廓数据获取方法分析和AI算法方案剖析
一.目前世面上有的2种免费方案是 1.OpenStreetMap开源网站下载, 2.blender的gis插件获取城市建筑轮廓. 缺点是:只有重点城市和省会城市,一些地级市或者县级市基本没有数据. 二 ...