There is a class consisting of n students, in which each one has a number representing his/her personality. The teacher gives the students a new assignment and asks them to solve it in groups so that each group can contain two students at most.

Students cannot create groups as they please because the teacher gives the following rules that must be met in order for a group to be valid:

  • The group can be‎由‎of one male student, one female student, or male and female students.
  • If the number of students in the group is two, these students must share common interests. Two students i and j share interests if and only if their numbers ai and aj share common divisor d > 1.

Since this is a really diverse class, no triple of students share a common interest, therefore all triples ai, aj, ak are co-primes (i.e. gcd(ai, aj, ak) ≡ 1).

Your task is to distribute the students into groups such that each student must join exactly one group, and the number of groups is as minimal as possible. Can you?

Input

The first line contains an integer T (1 ≤ T ≤ 100), in which T is the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 104), in which n is the number of students in the class.

Then a line follows containing n integers a1, a2, ..., an (1 ≤ ai ≤ 106), in which ai if the personality of the ith student. Then a line follows containing n space-separated characters p1, p2, ..., pn (), in which pi is "M" if the ith student is male, and "F" if she is female.

‎ ‎‎n‎‎ ‎‎总测试用例的总和不超过‎ 3 × 105.

Output

For each test case, print a single line containing the minimum number of groups that can be formed in the class.

Example

Input
2
2
3 6
M F
5
5 6 7 10 21
F F F M M
Output
1
3

Note

In the second test case, the minimum number of groups is 3, in which the first group consists of the 1st and 4th students, the second group consists of the 2nd student, and the third group consists of the 3rd and 5th students.

思路:第一反应是二分图匹配找到最大匹配数,再看数据n有1e4,而且T有1e2,用匈牙利可能会超时,所以用网络流写法;再思考如何建图连边,暴力n²会超时,只能想如何优化建边了。

想到题目要求两人gcd > 1,所以把每个人对应的数字拆分质因数,我这里先将女生的数的质因数通过df函数(作用就是得出该数的所有质因数且不记录重复的质因数)放入vector数组k[素数(质因数)][i]=女生对应的编号,在通过同样意义的df'数组将男生的数分解质因数,此时不用保存在数组里了,可以直接吧有该质因数的vector数组里的女生连边。

这样就能建图然后利用网络流跑一遍最大匹配,用总数减去最大匹配数得到结果了。

注:vector数组k[素数][i]里的素数并不是直接对应的素数,因为a[i]最大有1e6,所以直接存会很浪费空间,于是首先用素数筛将素数全部找出并标号,k[i][j]的第一维表示的是第i个素数。

贴一发优化llw大佬后的代码

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P; #define bug printf("*********\n");
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define mid (l+r)/2
#define chl 2*k+1
#define chr 2*k+2
#define lson l,mid,chl
#define rson mid,r,chr
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)); const long long mod=;
const int maxn=1e6+;
const int INF=0x7fffffff;
const int inf=0x3f3f3f3f;
const double eps=1e-;
const double pi=acos(-); struct edge {
int to,cap,rev;
};
vector <edge> G[maxn];
int level[maxn];
int iter[maxn]; void init(int _n) {
for(int i=; i<=_n; i++) {
G[i].clear();
}
} void bfs(int s) {
memset(level,-,sizeof(level));
queue<int> que;
level[s]=;
que.push(s);
while(!que.empty()) {
int v= que.front();
que.pop();
for(int i=; i<G[v].size(); i++) {
edge & e=G[v][i];
if(e.cap>&&level[e.to]<) {
level[e.to]=level[v] + ;
que.push(e.to);
}
}
}
} void add(int from,int to,int cap) {
edge eg;
eg.to=to;
eg.cap=cap;
eg.rev=G[to].size();
G[from].push_back(eg);
eg.to=from;
eg.cap=;
eg.rev=G[from].size()-;
G[to].push_back(eg);
} int dfs(int v,int t,int f) {
if(v == t)return f;
for(int &i = iter[v]; i < G[v].size(); i++) {
edge &e=G[v][i];
if(e.cap> && level[v]<level[e.to]) {
int d=dfs(e.to,t,min(f,e.cap));
if(d>) {
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return ;
}
int maxflow(int s,int t) {
int flow=;
for(;;) {
bfs(s);
if(level[t]<)return flow;
memset(iter,,sizeof(iter));
int f;
while((f = dfs(s,t,INF))>) {
flow +=f;
}
}
} int a[maxn],b[maxn];
int prime[maxn];//存这是第几个素数
vector<int> v;//存素数
void sieve(int _n) {
int tot=;
memset(prime,,sizeof(prime));
prime[]=-;
prime[]=-;
for(int i=; i<=_n; i++) {
if (prime[i]==) {
prime[i]=tot++;
v.push_back(i);
for(int j=i*; j<=_n; j+=i) {
prime[j]=-;
}
}
}
} vector<int> k[maxn/];//大概估计一下区间内素数个数不足总数1/10 void df(int x,int pos) {//x为本身的数,pos为标号
if(x==)return;//1与任何数gcd都是1所以直接不考虑
for(int i=; i<v.size(); i++) {
if(x<v[i]) {//任何数的质因数肯定小于等于本身,大于就return了
return ;
}
if(prime[x]!=-) {//为素数
k[prime[x]].push_back(pos);
return ;
}
if(x%v[i]==)k[i].push_back(pos);//如果是合数就判断当前枚举素数是不是该合数的质因数
while(x%v[i]==) {//去掉重复质因数
x/=v[i];
}
}
}
void df2(int x,int pos) {
if(x==)return;
for(int i=; i<v.size(); i++) {
if(x<v[i]) {
return ;
}
if(prime[x]!=-) {
i=prime[x];
for(int j=; j<k[i].size(); j++) {//与有相同质因数的女生全部连边
add(k[i][j],pos,);
}
return ;
}
if(x%v[i]==) {
for(int j=; j<k[i].size(); j++) {//同上
add(k[i][j],pos,);
}
}
while(x%v[i]==) {
x/=v[i];
}
}
} int main() {
sieve(1e6+);//预处理素数筛
int t,n;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
for(int i=; i<v.size(); i++) {
k[i].clear();
}
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);
}
for(int i=; i<=n; i++) {
char s[];
scanf("%s",s);
if(s[]=='F') {
b[i]=;//女1男0
} else b[i]=;
}
init(n+);
for(int i=; i<=n; i++) {
if(b[i]==) {
df(a[i],i);
}
}
for(int i=; i<=n; i++) {
if(b[i]==) {
df2(a[i],i);
}
}
for(int i=; i<=n; i++) { if(b[i]==) {
add(,i,);
} else add(i,n+,);
}
printf("%d\n",n-maxflow(,n+));
}
return ;
}

CodeFroces New Assignment 二分图匹配的更多相关文章

  1. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  2. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  3. BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2375  Solved: 1005[Submit][Sta ...

  4. HDU1281-棋盘游戏-二分图匹配

    先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...

  5. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

  6. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  7. BZOJ 1059 & 二分图匹配

    题意: 判断一个黑白染色的棋盘能否通过交换行或列使对角线上都是黑色. SOL: 真是有点醉...这种问题要么很神要么很水...第一眼感觉很水但就是不造怎么做...想了10分钟怎么感觉就是判断个数够不够 ...

  8. 【POJ 3020】Antenna Placement(二分图匹配)

    相当于用1*2的板覆盖给定的h*w的格子里的点,求最少的板.可以把格子相邻的分成两个集合,如下图,0为一个集合,1的为一个,也就是(行数+列数)为奇数的是一个集合,为偶数的为另一个集合.1010101 ...

  9. BZOJ-1143&&BZOJ-2718 祭祀river&&毕业旅行 最长反链(Floyed传递闭包+二分图匹配)

    蛋蛋安利的双倍经验题 1143: [CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1901 Solved: 951 ...

随机推荐

  1. POJ-2561 Network Saboteur(DFS)

    题目: A university network is composed of N computers. System administrators gathered information on t ...

  2. PAT甲级——1153.Decode Registration Card of PAT(25分)

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  3. mongo客户端升级导致pymongo中使用聚合函数时出现异常

    一.异常信息 The 'cursor' option is required, except for aggregate with the explain argument 二.解决办法 #部分源代码 ...

  4. ubuntu14.04安装32位库

    sudo dpkg --add-architecture i386 sudo apt update

  5. python的稀疏矩阵计算

    尽量避免稀疏矩阵, 加快计算. 比如计算稀疏矩阵S的F范数 a = norm(S, 'fro'), 方法1效率比方法2高很多. 方法 1 import numpy as np a = np.linal ...

  6. APUE 书中 toll 函数

    今天看unix环境高级编程时,随着书上的源码打了一遍,编译时提示 toll函数未定义, 找了半天(恕我对上下文不了解).看了英文版和源代码文件才知道, 中文版打印错了: toll => atol ...

  7. Pooled genome sequence strategies |representative genome assembly approaches|Domestication|GERP|selective sweep|Hybridization|Introgression|iHS|SNP genotyping arrays|haplotype

    Design based on biology 通过比较基因组学的方法,将脊椎动物基因组的数据,解决生物学各方面问题.新的调控注释(在脊椎动物的进化过程中的出现的)可以丰富物种树(比如不同功能蛋白质进 ...

  8. kaggle注册获取数据

    安装谷歌访问助手,主要参考下面的作者写的 https://segmentfault.com/a/1190000020548973?utm_source=tag-newest 安装之后,打开蓝灯或其他翻 ...

  9. 量化投资_MATLAB在时间序列建模预测及程序代码

    1 ARMA时间序列机器特性 下面介绍一种重要的平稳时间序列——ARMA时间序列. ARMA时间序列分为三种: AR模型,auto regressiv model MA模型,moving averag ...

  10. F. Maximum Weight Subset(贪心or树形dp解法)

    题:https://codeforces.com/contest/1249/problem/F 题意:给一颗树,边权为1,节点有点权,问取到一个点集,俩俩之间路径超过k,是点权和最大 思路:贪心地取点 ...