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

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 
Outpu
Copy
1

#include <bits/stdc++.h>

using namespace std;
map<int,int> pos;
const int maxn=1e4+10;

int val[maxn],ans[maxn];
int vis[maxn],pri[maxn];
int cnt=0;
void init(){
    for(int i=2;i<maxn;i++){
        if(!vis[i]){
            pri[cnt++]=i;
            for(int j=i*i;j<maxn;j+=i)
                vis[j]=1;
        }
    }
}

int pre[maxn];
/*
我知道为什么错了,因为有可能是负数,导致直接跳出。。。。。
自己还是太菜了
还有就是找区间内不相同数字的个数(除了0以外,当然如果只有1个的话,必须要算上0)
应该怎样来处理,真的是很666,主要是set超时,真是把我弄蒙了
最重要的一点,就是直接除以j*j,然后我们再去找相同的,这样才可以,真是 神奇
*/

int main()
{
    init();
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&val[i]);
        for(int j=0;pri[j]*pri[j]<=abs(val[i])&&j<cnt;j++){
            while(val[i]!=0&&val[i]%(pri[j]*pri[j])==0)
                val[i]/=(pri[j]*pri[j]);
            if(val[i]==0||val[i]==1||val[i]==-1)break;
        }
    }

    for(int i=1;i<=n;i++){
        pre[i]=pos[val[i]];
        pos[val[i]]=i;
    }
    for(int i=1;i<=n;i++){
        int tmp=0;
        for(int j=i;j<=n;j++){
            if(val[j]&&pre[j]<i)tmp++;
            ans[max(1,tmp)]++;
        }
    }
    for(int i=1;i<=n;i++){
        if(i==1) printf("%d",ans[i]);
        else printf(" %d",ans[i]);
    }
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/80273963