CSU 1552 Friends(二分图 + 米勒测试)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1552
Description
On an alien planet, every extraterrestrial is born with a number. If the sum of two numbers is a prime number, then two extraterrestrials can be friends. But every extraterrestrial can only has at most one friend. You are given all number of the extraterrestrials, please determining the maximum number of friend pair.
Input
There are several test cases.
Each test start with positive integers N(1 ≤ N ≤ 100), which means there are N extraterrestrials on the alien planet.
The following N lines, each line contains a positive integer pi ( 2 ≤ pi ≤10^18),indicate the i-th extraterrestrial is born with pi number.
The input will finish with the end of file.
Output
For each the case, your program will output maximum number of friend pair.
Sample Input
3
2
2
3 4
2
5
3
8
Sample Output
1
2
Hint
Source
题意:
给你n个数,两个数相加为素数的时候,就可以成为朋友,选过的数字不能重复选择。
题解:
2分图最大匹配问题,和米勒测试。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
#define LNF (1<<60)
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+;
LL a[maxn];
bool Map[maxn][maxn], vis[maxn];
int lin[maxn];
LL big_rand(LL m)
{
LL x = rand();
x*=rand();
if(x<) x-=x;
return x%=m;
}
LL mod_mul(LL x, LL y, LL n)
{
if(x == || y == ) return ;
return (((x&)*y)%n+(mod_mul(x>>, y, n)<<)%n)%n;
}
LL mod_exp(LL x, LL y, LL n)
{
LL ret = ;
while(y){
if(y&) ret = mod_mul(ret, x, n);
x = mod_mul(x, x, n);
y >>= ;
}
return ret;
}
bool Miller_Rabbin(LL n)
{
LL i, j, x, m, k;
if(n==) return true;
if(n<|| !(n&)) return false;
m = n - ;k = ;
while(!(m&)) m >>= , k++;
for(i=;i<;i++){
x = big_rand(n-) + ;
x = mod_exp(x, m, n);
if(x == ) continue;
for(j = ;j<k;j++){
if(x==n-) break;
x = mod_mul(x, x, n);
}
if(j>=k) return false;
}
return true;
}
bool dfs(int x, int n){
for(int j = ;j<=n;j++){
if(Map[x][j]&&!vis[j]){
vis[j] = ;
if(lin[j]== || dfs(lin[j], n)){
lin[j] = x;
return ;
}
}
}
return ;
}
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL int n;
while(~scanf("%d", &n)){
ms(Map, );
for(int i=;i<=n;i++) scanf("%lld", &a[i]);
for(int i=;i+<=n;i++){
for(int j=i+;j<=n;j++){
if(Miller_Rabbin(a[i]+a[j])){
Map[i][j] = Map[j][i] = ;
}
}
}
int ans = ;
ms(lin, );
for(int i=;i<=n;i++){
ms(vis, );
if(dfs(i, n)) ans++;
}
printf("%d\n", ans/);
}
return ;
}
将出2分图讲解,和米勒测试。未完待续。。XD
CSU 1552 Friends(二分图 + 米勒测试)的更多相关文章
- csu 1552: Friends 二分图 + Miller_Rabin
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1552 把那n个数写两次,分成相同的两堆,判断相加是质数的,连一条边,然后找最大匹配,ans = ...
- csu 1552(米勒拉宾素数测试+二分图匹配)
1552: Friends Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 723 Solved: 198[Submit][Status][Web Bo ...
- Project Euler 41 Pandigital prime( 米勒测试 + 生成全排列 )
题意:如果一个n位数恰好使用了1至n每个数字各一次,我们就称其为全数字的.例如,2143就是一个4位全数字数,同时它恰好也是一个素数. 最大的全数字的素数是多少? 思路: 最大全排列素数可以从 n = ...
- Project Euler 27 Quadratic primes( 米勒测试 + 推导性质 )
题意: 欧拉发现了这个著名的二次多项式: f(n) = n2 + n + 41 对于连续的整数n从0到39,这个二次多项式生成了40个素数.然而,当n = 40时402 + 40 + 41 = 40( ...
- CSU 1552: Friends 图论匹配+超级大素数判定
1552: Friends Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 163 Solved: 34[Submit][Status][Web Boa ...
- hdu2138 How many prime numbers 米勒测试
hdu2138 How many prime numbers #include <bits/stdc++.h> using namespace std; typedef long long ...
- 二分图最大匹配:匈牙利算法的python实现
二分图匹配是很常见的算法问题,一般用匈牙利算法解决二分图最大匹配问题,但是目前网上绝大多数都是C/C++实现版本,没有python版本,于是就用python实现了一下深度优先的匈牙利算法,本文使用的是 ...
- POJ Pseudoprime numbers( Miller-Rabin素数测试 )
链接:传送门 题意:题目给出费马小定理:Fermat's theorem states that for any prime number p and for any integer a > 1 ...
- 如何判断一个数是否为素数(zt)
怎么判断一个数是否为素数? 笨蛋的作法: bool IsPrime(unsigned n){ if (n<2) { //小于2的数即不是合数也不是素数 throw 0; ...
随机推荐
- python基础-6.1 match search findall group(s) 区别
import re # match findall经常用 # re.match() #从开头匹配,没有匹配到对象就返回NONE # re.search() #浏览全部字符,匹配第一个符合规则的字符串 ...
- “希希敬敬对”团队——敏捷冲刺Alpha过程总结
“希希敬敬对”团队在七天冲刺过程中每一个小组成员都尽力去完成自己的任务.在合作过程中,总算是有一些成果出现,代码功能能够实现. 对此次冲刺有如下优缺点: 优点: 团队人员合作较多,成员都能够积极响应参 ...
- Spark-Core RDD转换算子-Value型
1. map(func) 作用: 返回一个新的 RDD, 该 RDD 是由原 RDD 的每个元素经过函数转换后的值而组成. 就是对 RDD 中的数据做转换. 创建一个包含1-10的的 RDD,然后将每 ...
- java_第一年_JavaWeb(15)
Filter过滤器,Servlet API 中提供了一个Filter接口,用于实现用户在访问某个目标资源前对其进行拦截: 拦截原理:web服务器通过Filter接口调用doFilter方法,会传递一个 ...
- [2019杭电多校第六场][hdu6638]Snowy Smile(维护区间最大子段和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意为在一个平面上任意选择一个长方形,使得长方形内点权和最大. 因为长方形可以任意选择,所以上下 ...
- Codeforces 396C (DFS序+线段树)
题面 传送门 题目大意: 给定一棵树,每个点都有权值,边的长度均为1,有两种操作 操作1:将节点u的值增加x,并且对于u的子树中的任意一个点v,将它的值增加x-dist(u,v)*k, dist(u, ...
- 固定导航栏(jquery)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- SQL结构化查询语言
一.SQL 结构化查询语言 1.T-SQL 和 SQL的关系 T-SQL是SQL的增强版 2.SQL的组成 2.1 DML (数据操作语言) 增加,修改,删除等数据操作 2.2 DCL (数据控制语言 ...
- 刚新建好的动态网站项目,创建jsp页面就报错??
拿到刚刚可以运行的Eclipse,就马上想敲码了,但一创建项目之后再创建jsp页面就报错= =! 报错的内容大概为缺乏对应的jar包. 我们常用Tomcat为中间体,而他本身是带有开发jsp网站的对应 ...
- [译]理解 SwiftUI 里的属性装饰器@State, @Binding, @ObservedObject, @EnvironmentObject
原文地址:https://mecid.github.io/2019/06/12/understanding-property-wrappers-in-swiftui/ @States 通过使用 @St ...