CSU 1552: Friends 图论匹配+超级大素数判定
1552: Friends
Time Limit: 3 Sec Memory Limit: 256 MB
Submit: 163 Solved: 34
[Submit][Status][Web Board]
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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
#include <ctime>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int vis[MS];
int edge[MS][MS]; int lin[MS];
LL a[MS];
int n; //
LL MIN;
LL mult_mod(LL a,LL b,LL n)
{
LL s=;
while(b)
{
if(b&) s=(s+a)%n;
a=(a+a)%n;
b>>=;
}
return s;
} LL pow_mod(LL a,LL b,LL n)
{
LL s=;
while(b)
{
if(b&) s=mult_mod(s,a,n);
a=mult_mod(a,a,n);
b>>=;
}
return s;
} bool Prime(LL n)
{
LL u=n-,pre,x;
int i,j,k=;
if(n==||n==||n==||n==||n==) return ;
if(n==||(!(n%))||(!(n%))||(!(n%))||(!(n%))||(!(n%))) return ;
for(;!(u&);k++,u>>=);
srand((LL)time());
for(i=;i<;i++)
{
x=rand()%(n-)+;
x=pow_mod(x,u,n);
pre=x;
for(j=;j<k;j++)
{
x=mult_mod(x,x,n);
if(x==&&pre!=&&pre!=(n-))
return ;
pre=x;
}
if(x!=) return false;
}
return true;
} bool dfs(int x) // dfs增广路
{
for(int i=;i<=n;i++)
{
if(edge[x][i]&&!vis[i])
{
vis[i]=;
if(lin[i]==-|| dfs(lin[i])) // i没有匹配 或匹配了可以得到增广路
{
lin[i]=x;
return true;
}
}
}
return false;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(edge,,sizeof(edge));
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(Prime(a[i]+a[j]))
edge[i][j]=edge[j][i]=;
int ans=;
memset(lin,-,sizeof(lin));
for(int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
if(dfs(i))
ans++;
}
printf("%d\n",ans/);
}
return ;
}
CSU 1552: Friends 图论匹配+超级大素数判定的更多相关文章
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 公钥密码之RSA密码算法大素数判定:Miller-Rabin判定法!
公钥密码之RSA密码算法大素数判定:Miller-Rabin判定法! 先存档再说,以后实验报告还得打印上交. Miller-Rabin大素数判定对于学算法的人来讲不是什么难事,主要了解其原理. 先来灌 ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- Miller Robin大素数判定
Miller Robin算法 当要判断的数过大,以至于根n的算法不可行时,可以采用这种方法来判定素数. 用于判断大于2的奇数(2和偶数需要手动判断),是概率意义上的判定,因此需要做多次来减少出错概率. ...
- HDU 4344 大数分解大素数判定
这里贴个模板吧.反正是不太理解 看原题就可以理解用法!! #include <cstdio> #include <iostream> #include <algorith ...
- csu 1552(米勒拉宾素数测试+二分图匹配)
1552: Friends Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 723 Solved: 198[Submit][Status][Web Bo ...
- Snowflake Snow Snowflakes(哈希,大素数取模)
Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 27277 Accepted: 7197 Description You ...
- 计蒜客 18492.Upside down primes-米勒拉宾判大素数 (German Collegiate Programming Contest 2015 ACM-ICPC Asia Training League 暑假第一阶段第三场 K)
K. Upside down primes 传送门 这个题就是把大数按字符串输进去,判断一下是不是素数,然后反转180度,先判断反转之后的东西是不是一个数,如果是的话,再把这个数判一下是不是素数,如果 ...
- 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)
若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...
随机推荐
- 给Asp.net MVC Forms 验证设置角色访问控制
当我们使用Asp.net MVC Forms方式验证用户, 然后设置Controller 或 Action 的 Authorize属性时, 默认情况下只有Users属性可以设置(这里的Users通常是 ...
- 60分钟内从零起步驾驭Hive实战学习笔记
本博文的主要内容是: 1. Hive本质解析 2. Hive安装实战 3. 使用Hive操作搜索引擎数据实战 SparkSQL前身是Shark,Shark强烈依赖于Hive.Spark原来没有做SQL ...
- Docker 入门教程(转)
add by zhj: 可以简单的认为docker是对LXC(Linux Container)封装,它提供一种比LXC高级的API.Docker使用Go语言开发,利用了Linux提供的LXC,AUFS ...
- Python使用UUID库生成唯一ID(转)
原文:http://www.cnblogs.com/dkblog/archive/2011/10/10/2205200.html 资料: Python官方Doc:<20.15. uuid — U ...
- CIDR
CIDR的介绍: CIDR(Classless Inter-Domain Routing,无类域间路由选择)它消除了传统的A类.B类和C类地址以及划分子网的概念,因而可以更加有效地分配IPv4的地址空 ...
- chrome emulator use-agent 设置 chrom模拟手机客户端
谷歌升级以后,发现找不到use-agent设置了 在Element 下点击ESC 出现console,再点击Emulation就出现了
- java设计模式(装饰模式)
装饰模式实现了可以动态地为原对象扩展方法 装饰对象与被装饰的都实现了同一个接口(或抽象类) 举个例子: 工作 可以边吃东西边工作,也可以边喝东西边工作,还可以工作的时候边吃边喝 创建共同接口 Work ...
- .NET/C#- EPPLUS DEMO
强大的导出EXCEL,比NPOI更好用,更强大,可惜只有4.0版本的. 记录一下DEMO var sheet = p.Workbook.Worksheets.Add("My Sheet&qu ...
- 利用C#实现对excel的写操作
一.COM interop 首先我们要了解下何为COM Interop,它是一种服务,可以使.NET Framework对象能够与COM对象通信.Visual Studio .NET 通过引入面向公共 ...
- 如何在微博侧栏中加入自己的微博[js]
<div class="box"> <ul> <li> <object classid="clsid:D27CDB6E-AE6D ...