[ERT #5] B.良集(GOODSET)
题目描述
有两个正整数集合 A A A 和 B B B,给定良集(GOODSET) C C C 的定义如下:
-
对于所有 x ∈ A x \in A x∈A 且 x ∈ B x \in B x∈B,满足 x ∈ C x \in C x∈C。
-
对于所有 $x \notin A $ 且 x ∉ B x \notin B x∈/B,满足 x ∉ C x \notin C x∈/C。
现在给出 A A A 和 B B B,请输出所有良集 C C C 的长度的可能数。
答案请对 1 0 9 + 7 10^9+7 109+7 取余。
输入格式
一共 3 3 3 行。
第一行,两个整数 n , m n,m n,m,分别表示集合 A A A 和 B B B 的长度。
第二行,一个序列 a 1 , a 2 , . . . , a n a_1,a_2,...,a_n a1,a2,...,an,用空格隔开,表示 A A A 内的所有元素。
第三行,一个序列 b 1 , b 2 , . . . , b m b_1,b_2,...,b_m b1,b2,...,bm,用空格隔开,表示 B B B 内的所有元素。
输出格式
一行,一个整数,表示良集的个数。
样例 #1
样例输入 #1
5 4
1 3 4 5 7
1 2 3 6
样例输出 #1
6
样例 #2
样例输入 #2
6 6
1 4 5 9 8 7
1 5 4 6 2 3
样例输出 #2
7
提示
对于 100 % 100\% 100% 的数据, 1 ≤ n , m ≤ 1 0 6 , 1 ≤ a i , b i ≤ 1 0 9 1 \le n,m \le 10^6,1 \le a_i,b_i \le 10^9 1≤n,m≤106,1≤ai,bi≤109,且每个集合的每种元素单一。
思路:
良集的定义中可以知道
1. ab同时有的数x 所以这个数出现两次 必定在良集中 所以默认一种长度
2. ab都没有的数x 所以这个数不会出现 必定不在良集中
3. 其余的数 会出现一次 选择性出现在良集中
最终统计的结果是 第三种情况的所有情况加1
Code:(c++)
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
unordered_map<int , bool> a;
int main(){
int n, m, ans = 0;
scanf("%d %d", &n, &m);
int i;
for(i = 1; i <= n+m; i++){
int x;
scanf("%d", &x);
if(!a[x]) a[x] = true, ans++;
else ans--;
}
printf("%d", ans+1);
return 0;
}
Code:(c语言)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(){
int n, m, ans = 0;
bool a[1000005];
scanf("%d %d", &n, &m);
int i;
for(i = 1; i <= n+m; i++){
int x;
scanf("%d", &x);
if(!a[x]) a[x] = true, ans++;
else ans--;
}
printf("%d", ans+1);
return 0;
}