Codeforces 980D 数论

题目链接: http://codeforces.com/contest/980/problem/D

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).

Input

The first line of input contains a single integer n

( 1n5000

), the size of the array.

The second line contains n

integers a1, a2, , an ( 108ai108

), the values of the array.

Output

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

.

Examples
Input
Copy
2
5 5
Output
Copy
3 0
Input
Copy
5
5 -4 2 1 8
Output
Copy
5 5 3 2 0
Input
Copy
1
0
Output
Copy
1

题意:给出一个数组,对于每个连续子序列, 这个子序列(i ~ j)有(j - i + 1)个数, 把这些数分成k组,使得每组任意两个数相乘是完全平方数,要求对于一个子序列,k要最小化, 比如[1 2 8]分成2组[2 8][1], k= 2, res[2] ++.


#include 

using namespace std;
const int maxn = 5007;
bool isp[maxn*2];
vector prime;
int a[maxn], b[maxn];
int sz, res[maxn];
void init()
{
    memset(isp, 1, sizeof(isp));
    isp[1] = 0;
    for(int i = 2;i <= 10000;i ++) {
        if(isp[i]) {
            for(int j = i + i;j <= 10000;j += i) isp[j] = 0;
        }
    }
    for(int i = 2;i <= 10000;i ++) if(isp[i]) prime.push_back(i);
    sz = prime.size();
}
int main()
{
    init();
    int n;
    scanf("%d", &n);
    for(int i = 1;i <= n;i ++) {
        scanf("%d", &a[i]);
        if(!a[i]) {
            b[i] = a[i];
            continue;
        }
        int fg = 1;
        if(a[i] < 0) a[i] = -a[i], fg = -1;
        for(int j = 0;j < sz;j ++) {
            int t = prime[j] * prime[j];
            while(a[i] % t == 0) a[i] /= t;
            if(t > a[i]) break;
        }
        b[i] = fg * a[i];
    }
    for(int i = 1;i <= n;i ++) {
        int zero = 0, cnt = 0;
        for(int j = i;j <= n;j ++) {
            if(b[j] == UP) zero = 1;
            if(bs[b[j]] == 0) {
                cnt ++;
                bs[b[j]] = 1;
                pos.push_back(b[j]);
            }
            res[max(1, cnt-zero)] ++;
        }
        for(auto i : pos) bs[i] = 0;
    }
    for(int i = 1;i <= n;i ++) printf("%d%c",res[i], " \n"[i==n]);
    return 0;
}




你可能感兴趣的:(数学,数论,CF日常)