CF980D-Perfect Groups
题目:
题目描述:
SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be divided into, such that the product of any pair of integers in the same group is a perfect square.
Each integer must be in exactly one group. However, integers in a group do not necessarily have to be contiguous in the array.
SaMer wishes to create more cases from the test case he already has. His test case has an array $ A $ of $ n $ integers, and he needs to find the number of contiguous subarrays of $ A $ that have an answer to the problem equal to $ k $ for each integer $ k $ between $ 1 $ and $ n $ (inclusive).
输入格式:
The first line of input contains a single integer $ n $ ( $ 1 \leq n \leq 5000 $ ), the size of the array.
The second line contains $ n $ integers $ a_1 $ , $ a_2 $ , $ \dots $ , $ a_n $ ( $ -10^8 \leq a_i \leq 10^8 $ ), the values of the array.
输出格式:
Output $ n $ space-separated integers, the $ k $ -th integer should be the number of contiguous subarrays of $ A $ that have an answer to the problem equal to $ k $ .
样例:
样例输入 1:
样例输出 1:
样例输入 2:
样例输出 2:
样例输入 3:
样例输出 3:
思路:
实现:
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
| #include "ybwhead/ios.h"
const int maxn = 10010;
int n, k;
map<int, int> m;
int p[maxn];
int visp[maxn], coun[maxn], ans[maxn];
void iszhi(int x)
{
int count = 0;
for (int i = 2; i <= x; i++)
{
if (!visp[i])
{
p[count++] = i;
}
for (int j = 0;; j++)
{
int temp = p[j] * i;
if (temp >= maxn)
break;
visp[temp] = 1;
if (i % p[j] == 0)
{
break;
}
}
}
}
int makeit(int x)
{
if (!x)
return x;
for (int i = 0; p[i]; i++)
{
while (x % p[i] == 0)
x /= p[i];
}
return x;
}
int main()
{
yin >> n;
iszhi(maxn - 1);
for (int i = 0; p[i]; i++)
p[i] *= p[i];
for (int i = 1; i <= n; i++)
{
yin >> k;
k = makeit(k);
int temp = m[k];
m[k] = i;
int temp0 = m[0];
for (int j = i; j; j--)
{
if (j > temp)
coun[j]++;
if (j > temp0 || coun[j] == 1)
ans[coun[j]]++;
else
ans[coun[j] - 1]++;
}
}
for (int i = 1; i <= n; i++)
yout << ans[i] << ' ';
return 0;
}
|