csu 1552(米勒拉宾素数测试+二分图匹配)
1552: Friends
Time Limit: 3 Sec Memory Limit: 256 MB
Submit: 723 Solved: 198
[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 题意:有一些外星人想要找朋友玩,每个外星人都有一个value,当另一个外星人的 value' + value 是素数时,他们就可以成为朋友,但是每个人只能有一个朋友,问最多能够有多少朋友?
题解米勒拉宾大素数判断+二分图匹配
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 505
typedef long long LL;
//拉宾米勒测试
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;
}
int n;
int graph[N][N];
int linker[N];
bool vis[N];
LL a[N];
bool dfs(int u){
for(int i=;i<=n;i++){
if(graph[u][i]&&!vis[i]){
vis[i] = true;
if(linker[i]==-||dfs(linker[i])){
linker[i] = u;
return true;
}
}
}
return false;
}
int main()
{
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++) graph[i][j] = ;
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(Prime(a[i]+a[j])){
graph[i][j] = graph[j][i] = ;
}
}
}
int ans = ;
memset(linker,-,sizeof(linker));
for(int i=;i<=n;i++){
memset(vis,false,sizeof(vis));
if(dfs(i)) ans++;
}
printf("%d\n",ans/);
}
return ;
}
csu 1552(米勒拉宾素数测试+二分图匹配)的更多相关文章
- Miller_Rabin(米勒拉宾)素数测试
2018-03-12 17:22:48 米勒-拉宾素性检验是一种素数判定法则,利用随机化算法判断一个数是合数还是可能是素数.卡内基梅隆大学的计算机系教授Gary Lee Miller首先提出了基于广义 ...
- Miller_Rabin(米勒拉宾)素数测试算法
首先需要知道两个定理: 1: 费马小定理: 假如p是素数,且gcd(a,p)=1,那么 a(p-1)≡1(mod p). 2:二次探测定理:如果p是素数,x是小于p的正整数,且,那么要么x=1,要么x ...
- POJ 1811Prime Test(米勒拉宾素数测试)
直接套用模板,以后接着用 这里还有一个素因子分解的模板 #include <map> #include <set> #include <stack> #includ ...
- Miller_Rabin (米勒-拉宾) 素性测试
之前一直对于这个神奇的素性判定方法感到痴迷而又没有时间去了解.借着学习<信息安全数学基础>将素性这一判定方法学习一遍. 首先证明一下费马小定理. 若p为素数,且gcd(a, p)=1, 则 ...
- GCDLCM 【米勒_拉宾素数检验 (判断大素数)】
GCDLCM 题目链接(点击) 题目描述 In FZU ACM team, BroterJ and Silchen are good friends, and they often play some ...
- 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)
若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 2138 How many prime numbers (判素数,米勒拉宾算法)
题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...
- HDU2138 & 米勒拉宾模板
题意: 给出n个数,判断它是不是素数. SOL: 米勒拉宾裸题,思想方法略懂,并不能完全理解,所以实现只能靠背模板.... 好在不是很长... Code: /*==================== ...
随机推荐
- Implement Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- hihocoder[Offer收割]编程练习赛19 D 相交的铁路线(树上路径交)
傻逼题... 裸的树上路径交 两条树上的路径$[a,b]$和$[c,d]$有交,则有$lca(a,b)$在$[c,d]$上或$lca(c,d)$在$[a,b]$上. 其实只要深度大的$lca$在另一条 ...
- PID控制算法的C语言实现九
(1)微分先行PID控制算法 微分先行PID控制的特点是只对输出量yout(k)进行微分,而对给定值rin(k)不进行微分.这样,在改变给定值时,输出不会改变,而被控量的变化通常是比较缓和的.这种输出 ...
- poj2115 C Looooops
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29262 Accepted: 8441 Descr ...
- Python2和Python3共存安装
记录下: 先下载Python2.7.6,安装完成,不要添加到path中: 再下载Python3.4.3,安装,不要添加到path中. 进入 Python2: py -2 进入Python3: py - ...
- tf.session.run()单函数运行和多函数运行区别
tf.session.run()单函数运行和多函数运行区别 觉得有用的话,欢迎一起讨论相互学习~Follow Me problem instruction sess.run([a,b]) # (1)同 ...
- sweetAlert2
SweetAlert2一个前端最好用的弹窗
- HDU 2298 三分
斜抛从(0,0)到(x,y),问其角度. 首先观察下就知道抛物线上横坐标为x的点与给定的点的距离与角度关系并不是线性的,当角度大于一定值时可能会时距离单调递减,所以先三分求个角度范围,保证其点一定在抛 ...
- 复制自身程序到windows目录和system32目录下
功能:复制自身到windows目录和system32目录下. 参考代码: #include <stdio.h> #include <windows.h> void CopySe ...
- Jugs(回溯法)
ZOJ Problem Set - 1005 Jugs Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In ...