D. Perfect Groups
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
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
(1≤n≤5000
), the size of the array.
The second line contains n
integers a1,a2,…,an (−108≤ai≤108
), 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
2
5 5
output
3 0
input
5
5 -4 2 1 8
output
5 5 3 2 0
input
1
0
output
1
题意:
已知有这么一道题:给你n个数字,你要将这n个数字打乱后分成k组,使得对于同一个组中的任意一对数字满足两个数相乘一定是个完全平方数,求出最小的k
而这道题的意思是:给你n个数字,这n个数字一共有(1+n)*n/2个连续子序列,对于连续每个子序列你都要求出上面的那个k,最后统计k=1的子序列有多少个,k=2的子序列有多少个……k=n的子序列有多少个
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define pll pair<ll,ll>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
#define rson rt<<1|1,m+1,r
#define lson rt<<1,l,m
#define mod 323232323
using namespace std;
/*
枚举每个i
i只会对以当前位置开始的区间产生贡献
那么令d[i];//表示该数字上一个出现的位置
对于i,枚举j(j>=i)
如果d[j]>=i,说明当前数字已经在i之后出现过一次,
组数并不会增加
零也不会增加贡献,可以分到任一组,没有组自成一组
*/
const int N=1e4+10;
int app[N];
int ans[N];
int d[N];//表示该数字上一个出现的位置
int main()
{
#ifdef LOCAL_DEFINE
freopen("D:\\rush.txt","r",stdin);
#endif
ios::sync_with_stdio(false),cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>app[i];
if(app[i]!=0)
{
for(int j=N;j>=1;j--)
{
if(app[i]%(j*j)==0)
app[i]/=(j*j);
}
}
}
int pp=0;
for(int i=1;i<=n;i++)
{
for(int j=i-1;j>0;j--)
{
if(app[j]==app[i])
{d[i]=j;break;}
}
}
for (int i=1;i<=n;i++)
{
int curAns=0;
int curZero=0;//纪录零的个数,零可以分到任一组
for (int j=i;j<=n;j++)
if (d[j]>=i)//如果app[j]这个数字已经出现过,就不用curAns++了
{
if (curZero>0 && curAns>1)
ans[curAns-1]++;
else
ans[curAns]++;
}
else
{
if (app[j]==0)
curZero++;
curAns++;
if (curZero>0 && curAns>1)
ans[curAns-1]++;
else
ans[curAns]++;
}
}
for(int i=1;i<=n;i++)
cout<<ans[i]<<' ';
cout<<endl;
return 0;
}