hdu5594 ZYB's Prime
hdu5594 ZYB’s Prime
Problem Description
After getting 600 scores in NOIP,ZYB(ZJ−267) creates a problem:you are given N numbers,now you are asked to divide them into K groups(K≥1),the
number of each group must be no less than 3,and put all the numbers in a group into a ring,the sum of every two adjacent numbers must be a prime.ZYB want to ask
you whether the N numbers can be divided or not?Input
In the first line there is the testcase T.For each teatcase:
In the first line there is one number N.
In the next line there are N numbers Ai.1≤T≤50,1≤N≤200,1≤Ai≤200,for 60% cases N≤20.
Output
For each testcase,print the YES or NO.Sample Input
2
7
3 4 8 9 1 1 1
3
1 2 3Sample Output
YES
NOSource
BestCoder Round #65
题意:(背景)在zyb noip得了600分后..
给出n个数,让你在这n个数中组成若干个环,每个环的大小必须大于等于3,并且使得这个环相邻的两个数和为质数(每个数都是在1到200范围之内)
又是一道bc的题.. 名不虚传,bc的题就是难.. 而且很多细节
官方题解:
我的题解(有点长,请有点耐心..):
由于官方说的实在太官方,很多很细节的地方并没有说到,我在这里再补充下..
首先我们想到的是,在质数表中,只有2一个数是偶数,而且能组成2的也只有1和1,那么我们先假设如果没有1的情况,也就是2不是质数
那么所有的质数都是奇数,也就是说组成这个质数的一定是一个偶数和一个奇数,那么就是说这个环一定是奇—偶—奇—偶.. 最终整个图就是一个不存在奇环的图
我们可以看出这就是一个二分图模型,就可以把这n个数分成两半,一半为奇数一半为偶数,然后源向奇数连边,偶数向汇连边,流量都为2(因为要组成一个环)。接着在从和为质数的奇数偶数相连边,流量为1,这样就能保证每个环的长度都大于等于3(自己想想为什么)
用上面的构图做一次最大流,如果满流则YES,否则为NO。那么这样我们就得到了一个很优秀的不含1的算法,那含1的呢?
现在我们再对上面的算法进行进一步的分析,什么情况才可能满流?就是奇数的个数等于偶数的个数
然后我们再想,如果我现在有若干个1,那么这些1可以连成一条链:1—1—1—…—1—1
这个东西是成立的,因为相邻的加起来都等于2,是质数
看一下这一条链,它的内部是满足题目条件的,而且它的左右两端都是1。你想到什么——
这可以把这若干个1缩成一个数,就是——1
其实你会发现这个是等效的,所以我们就可以把这若干个1当作一个奇数1来用,这是绝对没有问题的
那么接下来就是重点了,第一步我们要使这个图能够满流,所以奇数个数必须等于偶数个数。可以发现,我们可以在奇数的那一栏补上若干个1,只要不超过1的个数。那么我们就要把奇数个数补满到偶数个数,于是也就得到下列结论:
1.如果奇数个数+1的个数<偶数个数,那么一定无解
2.如果奇数个数>偶数个数,那么一定无解
第二步,也就是关于1的问题了,因为会出现1—1—2这个满足条件的环,而当你把所有的1缩成一个1的时候是不能得到正确答案的。我想了半个多小时.. 得出这样的一个构图方法:
1.如果我补1的个数已经和1的总个数相同了,那么从所有1出发所连的边流量都为1
2.如果我补1的个数还有剩余,那么从所有1出发所连的边流量都为2
这样也就是说在1不够的时候要在里面多放一个1使其不会出现上述情况
Q:
也许你会说,这样构图如果对于这些1已经补到了极限不会错吗?
A:
不会,因为只会补一个1(自己好好想一下)
其实这样就已经完成构图了..
但是还有一种很特殊的情况.. 我一直被卡直到对拍才拍出结果
假如现在的奇数个数已经等于偶数个数了,那么正常的我们肯定不会去在奇数里补1,那么也就是说剩下的若干个1会组成一个环..
那.. 假如只有两个1呢?
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
const int Maxn = 210;
struct node {int x, y, next, c, opp;
}a[Maxn*Maxn*5]; int first[Maxn], len;
int _min ( int x, int y ){ return x < y ? x : y; }
void ins ( int x, int y, int c ){len ++; int k1 = len;a[len].x = x; a[len].y = y; a[len].c = c;a[len].next = first[x]; first[x] = len;len ++; int k2 = len;a[len].x = y; a[len].y = x; a[len].c = 0;a[len].next = first[y]; first[y] = len;a[k1].opp = k2;a[k2].opp = k1;
}
bool prime ( int x ){int i;for ( i = 2; i <= sqrt (x); i ++ ){if ( x % i == 0 ) return false;}return true;
}
int js[Maxn], os[Maxn], jsl, osl;
int st, ed, h[Maxn];
int n;
bool bfs (){queue <int> q;memset ( h, -1, sizeof (h) ); h[st] = 0;q.push (st);while ( !q.empty () ){int x = q.front (); q.pop ();for ( int k = first[x]; k; k = a[k].next ){int y = a[k].y;if ( h[y] == -1 && a[k].c > 0 ){h[y] = h[x]+1;q.push (y);}}}return h[ed] > 0;
}
int dfs ( int x, int flow ){if ( x == ed ) return flow;int delta = 0;for ( int k = first[x]; k; k = a[k].next ){int y = a[k].y;if ( h[y] == h[x]+1 && a[k].c > 0 && flow-delta > 0 ){int minf = dfs ( y, _min ( a[k].c, flow-delta ) );delta += minf;a[k].c -= minf;a[a[k].opp].c += minf;}}if ( delta == 0 ) h[x] = -1;return delta;
}
int main (){int i, j, k;int T;scanf ( "%d", &T );while ( T -- ){scanf ( "%d", &n );int sum1 = 0;jsl = osl = 0;for ( i = 1; i <= n; i ++ ){int x;scanf ( "%d", &x );if ( x % 2 == 1 ){if ( x == 1 ) sum1 ++;else js[++jsl] = x;} else os[++osl] = x;}if ( jsl+sum1 < osl || jsl > osl ){ printf ( "NO\n" ); continue; }len = 0; memset ( first, 0, sizeof (first) );st = 0; ed = osl*2+1;for ( i = 1; i <= osl; i ++ ){ins ( st, i, 2 );ins ( i+osl, ed, 2 );} for ( i = 1; i <= jsl; i ++ ){for ( j = 1; j <= osl; j ++ ){if ( prime (js[i]+os[j]) == true ){ins ( i, j+osl, 1 );}}}int pp;if ( sum1 == 2 && jsl == osl ){ printf ( "NO\n" ); continue; }if ( sum1 > osl-jsl ) pp = 2;else pp = 1;for ( i = 1; i <= osl-jsl; i ++ ){for ( j = 1; j <= osl; j ++ ){if ( prime (1+os[j]) == true ){ins ( i+jsl, j+osl, pp );}}}int ans = 0, delta;while ( bfs () ){while ( delta = dfs ( st, 0x7fffffff ) ) ans += delta;}if ( ans == 2*osl ) printf ( "YES\n" );else printf ( "NO\n" );}return 0;
}
给一下对拍程序吧..
maker
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define db double
using namespace std;
int n;
bool v[1100][1100];
int main()
{int x, last, y;srand(time(0));int t = rand () % 50+1;printf ( "%d\n", t );while ( t -- ){int n = 100;printf ( "%d\n", n );for ( int i = 1; i <= n; i ++ ){int x = rand () % 200+1;printf ( "%d ", x );}printf ( "\n" );}return 0;
}
hdu5594 ZYB's Prime相关推荐
- hdu 5594 ZYB's Prime 最大流
ZYB's Prime Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5 ...
- BC#65 T5 ZYB's Prime
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5594 完全不会啊TAT.. 其实官方题解已经说的很清楚了.. #include <cstdio> ...
- ccf 最优灌溉(prime模板)
模板题 #include<bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f int lowcost[1010]; int ...
- usaco Prime Palindromes
新技能get,可以这样遍历回文数,的确厉害.注意剪枝不然超时. /* ID: jinbo wu LANG:C++ TASK: pprime */ #include <stdio.h> #i ...
- ACMNO.23 C语言-素数判定 写一个判断素数的函数,在主函数输入一个整数,输出是否是素数的消息。 输入 一个数 输出 如果是素数输出prime 如果不是输出not prime
题目描述 写一个判断素数的函数,在主函数输入一个整数,输出是否是素数的消息. 输入 一个数 输出 如果是素数输出prime 如果不是输出not prime 样例输入 97 样例输出 prime 来源/ ...
- Prime Path(bfs)广度优先搜索
题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- F - Prime Path POJ - 3126
F - Prime Path POJ - 3126 题意修改一个四位数质数的某一位,使得该质数又变为一个质数,求从当前数变为目标质数的最少变化次数. bfs枚举每一位的每种变化 #include< ...
- 小白都能看懂最小生成树prime算法
定义不多说,说说代码的实现. 用落谷上的题目来说明代码的正确性 落谷上的题目 邻接矩阵的实现 定义一个生成树点的集合A,和图中其他点 的集合B 先任意选择一个点a加入到A中,即visit[a]=tru ...
- Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
A. Prime Permutation 题目连接: http://www.codeforces.com/contest/123/problem/A Description You are given ...
最新文章
- NSDate NSCalendar NSString之间的故事以及转换
- 基于i.MX RT电磁智能车AI算法的一些讨论
- linux c 宏判断多条件 #ifdef 和 #if defined 的区别
- python exe运行报 编码错误_python运行显示编码错误
- 图像分割之(二)Graph Cut(图割)
- linux java性能监控工具_性能监控工具以及java堆分析OOM
- ZooKeeper小结
- python集合常用方法_Python中集合类型(set)学习小结
- (4)HTML标签补充和HTML转义字符
- linux 文本 编辑 软件下载,文本编辑软件 Atom 1.5.0 已经发布下载
- POJ-1426 Find The Multiple
- 【二分】【中等难度】noip模拟赛 聪哥的工资
- 命令行构建Unity项目
- java64字节指令包,JVM 字节码指令解析
- [转载] python 语音识别 中文_python中文语音识别
- 应用程序窗口小部件App Widgets
- CellCtrl控件完美破解研究
- go-sqlite3 “database is locked”问题解决方案
- Java 汉字繁体转简体
- Could not locate call adapter for io.reactivex.Observable
热门文章
- PT100(RTD)三线制四线制测量方案
- 基于STC89C52单片机实现简易计算器
- Power query(Power BI) 自动生成贷款公司的还款计划表
- 线上支付,出款和收款
- ubuntu系统vim常用命令学习以及ubuntu软件下载安装
- 亚马逊SP-API申请,亚马逊SP-API注册,亚马逊开发者申请,PII申请怎么做?SP-API是什么?
- java怎么处理黑白bmp_Android JNI处理图片实现黑白滤镜的方法
- 技术研发方面工作经验总结
- ukf源程序 matlab,《卡尔曼滤波原理及应用-MATLAB仿真》程序-5.1UKF
- 2021年危险化学品经营单位主要负责人考试题及危险化学品经营单位主要负责人模拟试题