题解:P15288 「YLLOI-R3-T3」龙卷风
本人在比赛时,一开始一直只有
本题意为给出
不过,要小心所有数相等的情况,此时
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 1e4 + 5;
int t;
int n, m, a[MAXN], ans, k;
bool state, num[MAXN];
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> t;
for (int _ = 1; _ <= t; _++) {
memset(a, 0, sizeof(a));
memset(num, false, sizeof(num));
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a+1, a+n+1);
ans = 0, k = 1, state = false;
for (int i = 2; i <= n; i++) {
if (a[i] == a[i-1]) k++;
else {
if (!ans) ans = k;
else ans = gcd(ans, k);
k = 1;
}
}
if (!ans) ans = k, state = true;
else ans = gcd(ans, k);
if (state) {
cout << m << endl;
continue;
}
k = 0;
for (int i = 1; i <= m; i++) {
if (num[i]) continue;
if ((n % i == 0 && ans % (n / i) == 0) || n == i) {
for (int j = i; j <= m; j += i) num[j] = true, k++;
}
}
cout << k << endl;
}
return 0;
}