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(米勒拉宾素数测试+二分图匹配)的更多相关文章

  1. Miller_Rabin(米勒拉宾)素数测试

    2018-03-12 17:22:48 米勒-拉宾素性检验是一种素数判定法则,利用随机化算法判断一个数是合数还是可能是素数.卡内基梅隆大学的计算机系教授Gary Lee Miller首先提出了基于广义 ...

  2. Miller_Rabin(米勒拉宾)素数测试算法

    首先需要知道两个定理: 1: 费马小定理: 假如p是素数,且gcd(a,p)=1,那么 a(p-1)≡1(mod p). 2:二次探测定理:如果p是素数,x是小于p的正整数,且,那么要么x=1,要么x ...

  3. POJ 1811Prime Test(米勒拉宾素数测试)

    直接套用模板,以后接着用 这里还有一个素因子分解的模板 #include <map> #include <set> #include <stack> #includ ...

  4. Miller_Rabin (米勒-拉宾) 素性测试

    之前一直对于这个神奇的素性判定方法感到痴迷而又没有时间去了解.借着学习<信息安全数学基础>将素性这一判定方法学习一遍. 首先证明一下费马小定理. 若p为素数,且gcd(a, p)=1, 则 ...

  5. GCDLCM 【米勒_拉宾素数检验 (判断大素数)】

    GCDLCM 题目链接(点击) 题目描述 In FZU ACM team, BroterJ and Silchen are good friends, and they often play some ...

  6. 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)

    若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...

  7. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  8. HDU 2138 How many prime numbers (判素数,米勒拉宾算法)

    题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...

  9. HDU2138 & 米勒拉宾模板

    题意: 给出n个数,判断它是不是素数. SOL: 米勒拉宾裸题,思想方法略懂,并不能完全理解,所以实现只能靠背模板.... 好在不是很长... Code: /*==================== ...

随机推荐

  1. Linux内核分析4

    周子轩原创作品转载请注明出处  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 使用库函数API和C代码 ...

  2. NOIP2015Day1T3斗地主(DFS)

    这类题...真的写不动T T 首先可以发现没有顺子的话出牌次数是一定的, 换句话说只有顺子会影响出牌次数. 所以可以暴搜出所有顺子的方案, 搜完之后记忆化搜索求一下a张1张同色牌, b张2张同色牌,c ...

  3. MATLAB2010安装方法

    MATLAB2010安装方法 第一步选择无网络安装. 选择yes,然后点击next 激活序列号在crack文件夹中的txt文档中 这一步按照图片上的显示操作就可以 选择经典安装 按提示操作,这一步事激 ...

  4. 《剑指offer》— JavaScript(6)旋转数组的最小数字

    旋转数组的最小数字 题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2, ...

  5. libevent学习文档(二)eventbase相关接口和参数

    Setting up a default event_base The event_base_new() function allocates and returns a new event base ...

  6. Qt ------ 我定义的规则 之 对象命名规则

    类型 + 特性,比如  button_closeLigth 非公有的变量前面要加上小写m_ (指的修饰符为private时) 静态变量前面加上小写s_ 其它变量以小写字母开头 静态变量全大写 (sta ...

  7. 装饰器--decorator3

    装饰器添加返回值 import time def timer(func): def wrapper(*args,**kwargs): #wrapper包装的意思 start_time = time.t ...

  8. i针对网段开放端口 (命令行设置)

    针对网段开放端口 -A INPUT -s 192.168.1.0/24 -p tcp -m tcp --dport 1234 -j ACCEPT 命令行设置iptables iptables -I I ...

  9. 区间->点,点->区间,线段树优化建图+dijstra Codeforces Round #406 (Div. 2) D

    http://codeforces.com/contest/787/problem/D 题目大意:有n个点,三种有向边,这三种有向边一共加在一起有m个,然后起点是s,问,从s到所有点的最短路是多少? ...

  10. 使用scikit-learn进行建模预测和评估操作_泰坦尼克号获救预测

    # coding: utf-8 # In[142]: import pandas as pd import numpy as np import matplotlib.pyplot as plt # ...