1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
| #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int M = 1e6 + 7;
int prime[M], cnt, num[M];
bool st[M];
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
void get_prime(int x) {
for (int i = 2; i <= x; i++) {
if (!st[i])
prime[cnt++] = i;
for (int j = 0; prime[j] <= x / i; j++) {
st[prime[j] * i] = true;
if (i % prime[j] == 0)
break;
}
}
}
bool cmp(PII a, PII b) {
if (a.first != b.first)
return a.first > b.first;
else
return a.second < b.second;
}
int main() {
get_prime(1e6);//筛一下质数
int t;
scanf("%d", &t);
while (t--) {
map<int, int> mp;
vector<PII> vec;
int n, m = 0, g = 0;
scanf("%d", &n);
bool same = true;
for (int i = 1; i <= n; i++) {
scanf("%d", &num[i]);
if (same && i > 1 && num[i] != num[i - 1])
same = false;
if (!g)
g = num[i];
else
g = gcd(g, num[i]);
m = max(m, num[i]);
int now = num[i];//对每一个数进行质因数分解
for (int j = 0; j <= 1e3 && prime[j] <= now; j++) {
if (now % prime[j] == 0) {
mp[prime[j]]++;
while (now % prime[j] == 0) {
now /= prime[j];
}
}
}
if (now != 1)
mp[now]++;
}
if (same) {
printf("0\n");
continue;
}
for (auto it : mp) {
vec.push_back({it.second, it.first});
}
sort(vec.begin(), vec.end(), cmp);//按出现次数对质因数排序
for (int i = 1; i <= n; i++)
num[i] /= g;
int ans = 1, count_num = 0;
for (auto it : vec) {
if (count_num++ > 1e3 || it.first <= ans)
break;
int k = 0, now = 0, head = 0;
for (int j = 1; j <= n; j++) {
if (num[j] % it.second == 0)
k++;
else {
if (num[1] % it.second == 0 && !head)
head = k;
now = max(now, k);
k = 0;
}
}
if (k) {
now = max(now, k + head);
}
ans = max(ans, now);
}
cout << ans << endl;
}
return 0;
}
|