Solution -「CF 510E」Fox And Dinner
\(\mathcal{Description}\)
Link.
给定正整数集合 \(\{a_n\}\),求一种把这些数放置在任意多个圆环上的方案,使得每个环的大小大于 \(2\) 且环上相邻两数之和是素数。
\(n\le200\),\(2\le a_i\le10^4\)。
\(\mathcal{Solution}\)
这题怎么也黑了呢 qwq……
考虑到 \(2\le a_i\),有 \(4\le a_i+a_j\),所以素数必然是奇素数,而一个环必然是偶环。一个常见的套路是奇偶分开建对偶图,不妨设左侧奇数右侧偶数,源点 \(S\) 向所有奇数连边,容量为 \(2\)(环上与两个数相邻);奇数向与之加和为素数的偶数连边,容量为 \(1\)(环大小大于 \(2\));偶数向汇点 \(T\) 连边,容量为 \(2\)。跑最大流再根据残余网络输出方案即可。
\(\mathcal{Code}\)
#include <queue>
#include <cstdio>
#include <vector>
const int MAXN = 200, MAXV = 2e4, INF = 0x3f3f3f3f;
int n, pn, oc, ec, S, T, ecnt = 1, a[MAXN + 5], pr[MAXV + 5];
int d[MAXN + 5], head[MAXN + 5], curh[MAXN + 5], ref[MAXN + 5];
bool vis[MAXV + 5], mtc[MAXN + 5];
std::vector<int> odd, even;
std::vector<std::vector<int> > table;
struct Edge { int to, flow, nxt; } graph[MAXN * 2 + MAXN * MAXN / 2 + 5];
inline void link ( const int s, const int t, const int f ) {
graph[++ ecnt] = { t, f, head[s] };
head[s] = ecnt;
}
inline void addEdge ( const int s, const int t, const int f ) {
link ( s, t, f ), link ( t, s, 0 );
}
inline void sieve ( const int n ) {
vis[1] = true;
for ( int i = 2; i <= n; ++ i ) {
if ( ! vis[i] ) pr[++ pn] = i;
for ( int j = 1; j <= pn && i * pr[j] <= n; ++ j ) {
vis[i * pr[j]] = true;
if ( ! ( i % pr[j] ) ) break;
}
}
}
inline int DFS ( const int u, int iflow ) {
if ( u == T ) return iflow;
int oflow = 0;
for ( int& i = curh[u], v, of; i; i = graph[i].nxt ) {
if ( d[v = graph[i].to] == d[u] + 1 && graph[i].flow ) {
of = DFS ( v, std::min ( iflow, graph[i].flow ) );
oflow += of, graph[i].flow -= of, graph[i ^ 1].flow += of;
if ( ! ( iflow -= of ) ) break;
}
}
if ( ! oflow ) d[u] = -1;
return oflow;
}
inline bool BFS () {
static std::queue<int> que;
for ( int i = 1; i <= T; ++ i ) d[i] = -1;
que.push ( S ), d[S] = 0;
for ( int u; ! que.empty (); que.pop () ) {
u = que.front ();
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ! ~ d[v = graph[i].to] && graph[i].flow ) {
que.push ( v ), d[v] = d[u] + 1;
}
}
}
return ~ d[T];
}
inline int Dinic () {
int ret = 0;
for ( ; BFS (); ret += DFS ( S, INF ) ) {
for ( int i = 1; i <= T; ++ i ) {
curh[i] = head[i];
}
}
return ret;
}
inline void match ( const int u, std::vector<int>& now ) {
now.push_back ( u ), mtc[u] = true;
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ! mtc[v = graph[i].to] && v < S
&& ( ( u <= oc && graph[i ^ 1].flow ) || ( u > oc && graph[i].flow ) ) ) {
match ( v, now );
break;
}
}
}
int main () {
scanf ( "%d", &n );
int mx = 0;
for ( int i = 1; i <= n; ++ i ) {
scanf ( "%d", &a[i] );
if ( mx < a[i] ) mx = a[i];
if ( a[i] & 1 ) odd.push_back ( a[i] );
else even.push_back ( a[i] );
}
sieve ( mx << 1 );
oc = odd.size (), ec = even.size ();
S = oc + ec + 1, T = S + 1;
for ( int i = 1, ot = 0, ct = 0; i <= n; ++ i ) {
if ( a[i] & 1 ) ref[++ ot] = i;
else ref[oc + ++ ct] = i;
}
for ( int i = 1; i <= oc; ++ i ) addEdge ( S, i, 2 );
for ( int i = 1; i <= ec; ++ i ) addEdge ( i + oc, T, 2 );
for ( int i = 0; i ^ oc; ++ i ) {
for ( int j = 0; j ^ ec; ++ j ) {
if ( ! vis[odd[i] + even[j]] ) {
addEdge ( i + 1, oc + j + 1, 1 );
}
}
}
int f = Dinic ();
if ( f < n ) return puts ( "Impossible" ), 0;
std::vector<int> now;
for ( int i = 1; i <= oc; ++ i ) {
if ( ! mtc[i] ) {
now.clear ();
match ( i, now );
table.push_back ( now );
}
}
printf ( "%d\n", ( int ) table.size () );
for ( auto ele: table ) {
printf ( "%d", ( int ) ele.size () );
for ( int v: ele ) printf ( " %d", ref[v] );
putchar ( '\n' );
}
return 0;
}
Solution -「CF 510E」Fox And Dinner的更多相关文章
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
- Solution -「CF 1622F」Quadratic Set
\(\mathscr{Description}\) Link. 求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...
- Solution -「CF 923F」Public Service
\(\mathscr{Description}\) Link. 给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...
- Solution -「CF 923E」Perpetual Subtraction
\(\mathcal{Description}\) Link. 有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...
- Solution -「CF 1586F」Defender of Childhood Dreams
\(\mathcal{Description}\) Link. 定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...
- Solution -「CF 1237E」Balanced Binary Search Trees
\(\mathcal{Description}\) Link. 定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...
- Solution -「CF 623E」Transforming Sequence
题目 题意简述 link. 有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...
- Solution -「CF 1023F」Mobile Phone Network
\(\mathcal{Description}\) Link. 有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...
- Solution -「CF 599E」Sandy and Nuts
\(\mathcal{Description}\) Link. 指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...
随机推荐
- Go语言系列之time
time包是go语言的内置库,提供了时间的显示和测量用的函数.日历的计算采用的是公历. 一.时间类型 time.Time类型表示时间.我们可以通过time.Now()函数获取当前的时间对象,然后获取时 ...
- SYCOJ157乘二加一
题目-乘二加一 (shiyancang.cn) 递归写法 #include <bits/stdc++.h> using namespace std; string f(int n) { i ...
- Sentry 企业级数据安全解决方案 - Relay 操作指南
内容整理自官方文档 本篇回顾了我们在自托管外部使用 Relay 时的操作指南,即在您的硬件上运行的 Relay 并将事件转发到 sentry.io. 系列 Sentry 企业级数据安全解决方案 - R ...
- JVM调优工具锦囊
Arthas线上 分析诊断调优工具 以前我们要排查线上问题,通常使用的是jdk自带的调优工具和命令.最常见的就是dump线上日志,然后下载到本地,导入到jvisualvm工具中.这样操作有诸多不变,现 ...
- nginx代理图片上传以及访问 nginx 图片上传完整版
nginx代理图片上传 首先需要利用nginx代理图片访问参考 https://www.cnblogs.com/TJ21/p/12609017.html 编写接受文件的controller 1 @Po ...
- Java包装类和处理对象
Java中基本类型变量和字符串之间的转换 public class Primitive2String { public static void main(String args[]) { String ...
- C++ 从&到&&
人类发展史,就是不断挖坑.填坑的过程. 语言发展史也是如此! 任何一门设计合理的语言,给你的限制或提供的什么特性,都不是没有代价的. C的指针 指针:pointer 指针的思想起源于汇编.指针思想是编 ...
- 《剑指offer》面试题67. 把字符串转换成整数
问题描述 写一个函数 StrToInt,实现把字符串转换成整数这个功能.不能使用 atoi 或者其他类似的库函数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. ...
- Parallel.For实现
static class MyParallel { //4.0及以上用Task, Task的背后的实现也是使用了线程池线程 //static List<Task> tasks = new ...
- 科技爱好者周刊(第 176 期):中国法院承认 GPL 吗?
这里记录每周值得分享的科技内容,周五发布. 本杂志开源(GitHub: ruanyf/weekly),欢迎提交 issue,投稿或推荐科技内容. 周刊讨论区的帖子<谁在招人?>,提供大量程 ...