[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 ...
随机推荐
- nacos适配SqlServer、Oracle
继上文<nacos适配达梦.瀚高.人大金仓数据库及部分源码探究>后补充nacos适配SqlServer.Oracle的贴码,主要区别是SqlServer.Oracle的分页SQL有点不一样 ...
- AI绘画关键词Prompt:分享一些质量比较高的StableDiffusion(SD)关键词网站
今天向大家推荐一些SD(StableDiffusion)高质量的 关键词 网站.这些网站的质量可靠,能为大家在创建 AI 绘画时提供有效的参考.以下是六个推荐的网站,优缺点分析. 有几个质量还算是挺高 ...
- WPF-实现屏幕截图(一)
源码路径:https://gitee.com/LiuShuiRuoBing/wpf_screen_cut 实现功能 实现基本的截屏窗体 鼠标随意选择截图区域 鼠标抬起时弹出按钮区 快捷键Ctrl+Al ...
- influxdb 函数 non_negative_derivative 使用
转载请注明出处: 在InfluxDB中,non_negative_derivative()函数用于计算指定字段的非负导数.它可以用来计算时间序列数据的速率或增长率. 该函数的语法如下: non_neg ...
- C# winform 无边框窗口 移动
给自己留个笔记, 在用wke做界面的时候. 往往需要把winform窗口设置成无边框 但是WebUI也需要移动窗口, 所以才把以前在易语言中用的方法翻译过来使用 第零步: 设置无边框窗口 form属性 ...
- Java实践项目 - 购物车模块
Smiling & Weeping ----世界上美好的东西不太多,立秋傍晚从河对岸吹来的风, 加入购物车 1.数据创建--创建t_cart CREATE TABLE t_cart( cid ...
- Domain Admin域名和SSL证书过期监控到期提醒
基于Python3 + Vue3.js 技术栈实现的域名和SSL证书监测平台 用于解决,不同业务域名SSL证书,申请自不同的平台,到期后不能及时收到通知,导致线上访问异常,被老板责骂的问题 核心功能: ...
- Centos7使用ssh免密登陆同时禁用root密码登陆
Centos7使用ssh免密登陆同时禁用root密码登陆 首先配置免密登陆,参考:ssh免密登陆 禁用root密码登陆 修改 /etc/ssh/sshd_config 文件 找到: RSAAuthen ...
- 两种方式,创建有返回值的DB2函数
函数场景:路径信息由若干个机构编码组成,且一个机构编码是9位字符. 要求:获取路径信息,并且删除路径中包含'99'开头的机构编码. 从客户端及服务器端分别创建ignore99(pathinfo var ...
- Go 复合类型之切片类型介绍
Go 复合类型之切片类型 目录 Go 复合类型之切片类型 一.引入 二.切片(Slice)概述 2.1 基本介绍 2.2 特点 2.3 切片与数组的区别 三. 切片声明与初始化 3.1 方式一:使用切 ...