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; ...
随机推荐
- github项目分享
unity 项目合集:https://michidk.github.io/Unity-Script-Collection/ ugui特效:https://github.com/mob-sakai/UI ...
- YOLOV3中Darknet中cfg文件说明和理解
今天将要说明的是Darknet中的cfg文件,废话少说,直接干!(以cfg/yolov3.cfg为例,其它类似) [net] ★ [xxx]开始的行表示网 ...
- python基础-3 集合 三元运算 深浅拷贝 函数 Python作用域
上节课总结 1 运算符 in 字符串 判断 : “hello” in "asdasfhelloasdfsadf" 列表元素判断:"li" in ['li', ...
- [Python3 练习] 003 货币转换
题目:货币转换 (1) 描述 人民币和美元是世界上通用的两种货币,写一个程序进行货币间币值转换 记人民币和美元之间的汇率为:1 美元 = 6.78 人民币 程序可以接受人民币或美元输入,转换为另一种货 ...
- [19/05/05-星期日] JDBC(Java DataBase Connectivity,java数据库连接)_mysql基本知识
一.概念 (1).是一种开放源代码的关系型数据库管理系统(RDBMS,Relational Database Management System):目前有很多大公司(新浪.京东.阿里)使用: (2). ...
- 创建Maven项目时,出现系列的错误提示的修改方法
1.创建Maven项目成功之后,需要修改一些配置, (1).java版本改为“本系统中java的版本号” 问题一:(2).Dynamic Web Module的version要改为2.5以上,然而本人 ...
- Consul集群Server模式
Consul集群Server模式 架构示意图 Consul在生产环境下运行模式分为两种:Server模式和Client模式(dev模式属于开发模式不在这里讨论),我们先用Server模式搭建一个Con ...
- 修复线上bug
1,git branch new_branch 2,git push origin new_branch 以上是线上地址操作,以下是本地仓库操作 3,git fetch 4,git checkout ...
- Centos7-firewall-cmd
firewall-cmd CentOS 7 默认使用的防火墙是firewalld,不是CentOS 6的iptables 查看防火墙状态 systemctl status firewalld 也可以 ...
- [SheetJS] js-xlsx模块学习指南
简介 SheetJS是前端操作Excel以及类似的二维表的最佳选择之一,而js-xlsx是它的社区版本. js-xlsx将注意力集中到了数据转换和导出上,所以它支持相当多种类的数据解析和导出.不仅仅局 ...