Description
众所周知,我是好人!所以不会出太难的题,题意很简单
给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍数
Input
第1行是一个整数T,表示共T组数据。 接下来是T组数据,每组数据第1行是正整数n(n<=50),接下来是n个正整数(小于等于1000),任意两数用1个空格隔开,最前数前面与最后数后面无空格
Output
输出T行,对应T组数据。(T<=10) 每行输出这样的正整数有多少个
Sample Input
3
4
2 3 5 7
1
2
13
854 101 143 282 538 922 946 286 681 977 892 656 907
Sample Output
228571428
500000000
968701719
HINT
提示:数据是随机生成的,尔等尽可随意乱搞
题解
容斥原理可以描述如下:
要计算几个集合并集的大小,我们要先将所有单个集合的大小计算出来,然后减去所有两个集合相交的部分,再加回所有三个集合相交的部分,再减去所有四个集合相交的部分,依此类推,一直计算到所有集合相交的部分。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL INF = 1e9;
int t, n, prime[100];
LL multiple;
int GetPrime() {
sort(prime, prime + n);
bool nots[100] = {false};
int cnt = 0;
for (int i = 0; i < n; ++i) {
if (!nots[i]) {
prime[cnt++] = prime[i];
for (int j = i + 1; j < n; ++j) {
if (!nots[j] && prime[j] % prime[i] == 0) nots[j] = true;
}
}
}
return cnt;
}
LL Gcd(LL a, LL b) {
return b == 0 ? a : Gcd(b, a % b);
}
LL Lcm(LL a, LL b) {
return a / Gcd(a, b) * b;
}
void Dfs(int cnt, int cnt2, LL temp) {
if (temp > INF) return;
if (cnt == n) {
cnt2 & 1 ? multiple -= INF / temp : multiple += INF / temp;
return;
}
Dfs(cnt + 1, cnt2, temp);
Dfs(cnt + 1, cnt2 + 1, Lcm(temp, prime[cnt]));
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &prime[i]);
}
n = GetPrime();
multiple = 0;
Dfs(0, 0, 1);
printf("%lld\n", multiple);
}
return 0;
}