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 题目 ...
随机推荐
- 安装glibc2.7
参考链接http://fhqdddddd.blog.163.com/blog/static/18699154201401722649441/ /lib/libc.so.6: version `glib ...
- Linux查看系统信息命令总结
系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinf ...
- 在VSTO界面中,调用xll中的函数
最近研究各种有点迷茫了,原来Xll的加载宏直接可以在C#中调用的,我又各种Out了. 先说明一下,在VBA中,如何调用吧 XLLFound = Application.RegisterXLL(This ...
- C3P0连接池使用小结
C3P0在最近的demo中也用了(我用的是0.9.2.1版本),因为单例很难应付大量并发. 用法详见文档:http://www.mchange.com/projects/c3p0/ 基本的用法在htt ...
- D. Green and Black Tea
先搞多的,搞到相等. (tmd上星期+上上星期简直是弱智,怎么也不会写,总是想着各种a/b,,,踢蹬) #include<bits/stdc++.h> #define lowbit(x) ...
- android 获取手机型号,本机电话号码,SDK版本以及firmwarw版本号(即系统版本号)
Android开发平台中,可通过TelephonyManager 获取本机号码. TelephonyManager phoneMgr=(TelephonyManager)this.getSystemS ...
- OC:继承、初始化方法、便利构造器
A继承 的作用就是为了,节省代码. 继承 :子类会继承父类 里面所有的内容 思想 :凡是继承过来的实例变量和方法 都是自己的(是从父类遗传过来的) 继承的使用场景 : 当我们多 ...
- 利用Retrofit, RxJava获取网络内容
Retrofit & RxJava 关于如何使用Retrofit和RxJava请阅读参考中的两篇文章. Retrofit处理数据 Retrofit是在什么时候处理从网络中获取到的json数据的 ...
- Builder
Builder模式的使用情景 相同的方法, 不同的执行顺序, 产生不同的事件结果 多个部件或零件, 都可以装配到一个对象中, 但是产生的运行结果又不相同 产品类比较复杂, 或者产品类中的调用顺序不同产 ...
- WinForm设置窗体默认控件焦点
winform窗口打开后文本框的默认焦点设置,进入窗口后默认聚焦到某个文本框,两种方法: ①设置tabindex 把该文本框属性里的tabIndex设为0,焦点就默认在这个文本框里了. ②Winfor ...