[ABC263G] Erasing Prime Pairs
Problem Statement
There are integers with $N$ different values written on a blackboard. The $i$-th value is $A_i$ and is written $B_i$ times.
You may repeat the following operation as many times as possible:
- Choose two integers $x$ and $y$ written on the blackboard such that $x+y$ is prime. Erase these two integers.
Find the maximum number of times the operation can be performed.
Constraints
- $1 \leq N \leq 100$
- $1 \leq A_i \leq 10^7$
- $1 \leq B_i \leq 10^9$
- All $A_i$ are distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_N$ $B_N$
Output
Print the answer.
Sample Input 1
3
3 3
2 4
6 2
Sample Output 1
3
We have $2 + 3 = 5$, and $5$ is prime, so you can choose $2$ and $3$ to erase them, but nothing else. Since there are four $2$s and three $3$s, you can do the operation three times.
Sample Input 2
1
1 4
Sample Output 2
2
We have $1 + 1 = 2$, and $2$ is prime, so you can choose $1$ and $1$ to erase them. Since there are four $1$s, you can do the operation twice.
既然要判断质数,一个肯定要做的操作就是把 \(2\times10^7\) 之内的质数筛出来。不妨把和为质数的两个点连条边。边相连的两个点的点权可以同时减去一个数,要保证点权为正数,问最多操作次数。
手动模拟一下,有网络流的感觉。于是考虑最大流解决。把第 \(i\) 种数拆成两个点,第一个点编号为 \(i\),第二个点编号为 \(n+i\)。对于第 \(i\) 个数,从源点 \(s\) 连一条流量为 \(b_i\) 的边,从 \(i+n\) 连一条流量为 \(b_i\) 的边至汇点 \(t\)。如果 \(a_i+a_j\) 为质数,从 \(i\) 连一条流量为 \(+\infty\) 的边至 \(j+n\)。然后跑一次最大流,得出答案后除以 \(2\) 就是答案。
为什么要除以 \(2\)?由于我们计算最大流时,\(a_i+a_j\) 若为质数,那么 \(i\) 到 \(j+n\) 的边中算了一次,\(j+n\) 至 \(i\) 的边中算了一次。所以算了两次。
但是我们没考虑 \(1+1=2\)?其实计算最大流时已经计算。须要两个 \(1\) 才能有一次删除机会,而我们最后也除以了 \(2\)。所以不需要特殊判断。
#include<bits/stdc++.h>
using namespace std;
const int N=205,M=40005,P=2e7+5;
int n,m,s,t,k,g,hd[N],l,r,v[N],q[N],vhd[N],uu,vv,ww,e_num=1,p[P],p_num,pri[P],a[N],b[N],ee;
struct edge{
int v,nxt,w;
}e[M<<1];
void add_edge(int u,int v,int w)
{
e[++e_num]=(edge){v,hd[u],w};
hd[u]=e_num;
}
long long ans;
int bfs()
{
memset(v,0,sizeof(v));
memcpy(hd,vhd,sizeof(hd));
q[l=r=1]=s,v[s]=1;
while(l<=r)
{
if(q[l]==t)
break;
for(int i=hd[q[l]];i;i=e[i].nxt)
if(e[i].w>0&&!v[e[i].v])
q[++r]=e[i].v,v[e[i].v]=v[q[l]]+1;
++l;
}
return v[t];
}
int dfs(int x,int s)
{
// printf("%d %d\n",x,s);
if(x==t)
return s;
int b,c;
for(int&i=hd[x];i;i=e[i].nxt)
{
b=e[i].v,c=min(s,e[i].w);
if(v[b]==v[x]+1&&c>0)
{
g=dfs(b,c);
if(g)
{
e[i].w-=g;
e[i^1].w+=g;
return g;
}
else
v[b]=0;
}
}
return 0;
}
signed main()
{
scanf("%d",&n);
s=0,t=2*n+1;
for(int i=2;i<P;i++)
{
if(!pri[i])
p[++p_num]=i;
for(int j=1;j<=p_num&&1LL*p[j]*i<P;j++)
{
pri[p[j]*i]=1;
if(i%p[j]==0)
break;
}
}
for(int i=1;i<=n;i++)
scanf("%lld%lld",a+i,b+i);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(!pri[a[i]+a[j]])
{
add_edge(i,n+j,2e9);
add_edge(j+n,i,0);
}
}
}
for(int i=1;i<=n;i++)
{
add_edge(s,i,b[i]);
add_edge(i,s,0);
}
for(int i=1;i<=n;i++)
{
add_edge(i+n,t,b[i]);
add_edge(t,i+n,0);
}
memcpy(vhd,hd,sizeof(hd));
while(bfs())
{
while(k=dfs(s,2e9))
ans+=k;
}
printf("%lld",ans/2);
return 0;
}
[ABC263G] Erasing Prime Pairs的更多相关文章
- F. Count Prime Pairs
单点时限: 2.0 sec 内存限制: 512 MB 对于数组a,如果i≠j并且ai+aj是一个质数,那么我们就称(i,j)为质数对,计算数组中质数对的个数. 输入格式 第一行输入一个n,表示数组的长 ...
- 2018.9.20 Educational Codeforces Round 51
蒟蒻就切了四道水题,然后EF看着可写然而并不会,中间还WA了一次,我太菜了.jpg =.= A.Vasya And Password 一开始看着有点虚没敢立刻写,后来写完第二题发现可以暴力讨论,因为保 ...
- Educational Codeforces Round 51 (Rated for Div. 2)
做了四个题.. A. Vasya And Password 直接特判即可,,为啥泥萌都说难写,,,, 这个子串实际上是忽悠人的,因为每次改一个字符就可以 我靠我居然被hack了???? %……& ...
- HPU暑期集训积分赛1
A. Nth power of n 单点时限: 1.0 sec 内存限制: 512 MB 求 nn 的个位数. 输入格式 多组输入,处理到文件结束.每组数据输入一个 n.(1≤n≤109) 输出格式 ...
- CodeForces Educational Codeforces Round 51 (Rated for Div. 2)
A:Vasya And Password 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen(&q ...
- Codeforces Edu Round 51 A-D
A. Vasya And Password 模拟题,将所缺的种类依次填入"出现次数$ >1 $"的位置,替换掉Ta即可. #include <iostream> ...
- Pairs Forming LCM(素因子分解)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/B 全题在文末. 题意:在a,b中(a,b<=n) ...
- LightOJ 1236 - Pairs Forming LCM(素因子分解)
B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS Memor ...
随机推荐
- Kettle实例(获取Token并带入请求接口拉取数据到本地)
背景 近期工作中遇到许多需要协同的表单文档被放到云文档,那么我们本地做数据分析就需要先抽取云文档实时数据到本地数据库,根据接口文档我们需要先获取Token,再将返回值带到接口中发起请求拉取数据,因为在 ...
- 「BJWC2012」冻结题解
「BJWC2012」冻结题解 一.题目 "我要成为魔法少女!" "那么,以灵魂为代价,你希望得到什么?" "我要将有关魔法和奇迹的一切,封印于卡片之中 ...
- [ABC140F] Many Slimes
2023-02-13 题目 题目传送门 翻译 翻译 难度&重要性(1~10):6 题目来源 AtCoder 题目算法 贪心 解题思路 用了两个 multiset a 和一个 set s,一个 ...
- 使用.NET Jieba.NET 的 PosSegmenter 实现中文分词匹配
目录 引言 1. 什么是中文分词 2. Jieba.NET简介 3. PosSegmenter介绍 4. 实现中文分词匹配 4.1 安装Jieba.NET库 4.2 创建PosSegmenter实 ...
- Electron创建项目并打包生成exe
安装nodejs 访问这个网站去下载 http://nodejs.cn/download/ 创建项目 创建项目 git clone https://github.com/electron/electr ...
- SpringBoot使用@Async注解8大坑点
前言 SpringBoot中,@Async注解可以实现异步线程调用,用法简单,体验舒适. 但是你一定碰到过异步调用不生效的情况,今天,我就列出90%的人都可能会遇到的8大坑点. 正文 1.未启用异步支 ...
- 搭一下 Stable Diffusion WebUI
Preface 前不久看到好多朋友用上Stable Diffusion来做原画,然后又配合上了Chatgpt. 一直以来都想尝试一下,奈何2014款的双核mac跑个idea都发出了拖拉机的轰鸣声. 所 ...
- Ds100p -「数据结构百题」31~40
31.P2163 [SHOI2007]园丁的烦恼] 很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草. 有一天国王漫步在花园里 ...
- WPF 笔迹算法 从点集转笔迹轮廓
本文将告诉大家一些笔迹算法,从用户输入的点集,即鼠标轨迹点或触摸轨迹点等,转换为一个可在界面绘制显示笔迹画面的基础数学算法.尽管本文标记的是 WPF 的笔迹算法,然而实际上本文更侧重基础数学计算,理论 ...
- umicv cv-summary1-全连接神经网络模块化实现
全连接神经网络模块化实现 Linear与Relu单层实现 LossLayer实现 多层神经网络 不同梯度下降方法 Dropout层 今天这篇博文针对Assignment3的全连接网络作业,对前面学习的 ...