首页 技术 正文
技术 2022年11月20日
0 收藏 599 点赞 3,759 浏览 4756 个字

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 (CodeFroces New Assignment 二分图匹配), 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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844