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
(inclusive).
The first line of input contains a single integer n
), the size of the array.
The second line contains n
integers a1, a2, …, an ( −108≤ai≤108), the values of the array.
Output n
.
2 5 5
3 0
5 5 -4 2 1 8
5 5 3 2 0
1 0
1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define pb push_back
ll rd(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int P=1e5;
const int N=5000+10;
bool np[P];
vi pset;
void build(){
rep(i,2,P-1)if(not np[i]){
pset.push_back(i);
for(int j=i;j<P;j+=i)np[j]=true;
}
}
int __=1;
int n,a[N],aa[N];
int mask(int val){
if(val==0)return val;
int ret=val;
val=abs(val);
for(auto i:pset){
if((ll)i*i>val)break;
while(val%(i*i)==0){
val/=(i*i);
ret/=(i*i);
}
}
return ret;
}
void init(){
n=rd();
vi li;
rep(i,0,n-1){
a[i]=mask(rd());
li.push_back(a[i]);
}
sort(all(li));
li.resize(unique(li.begin(),li.end())-li.begin());
rep(i,0,n-1)
aa[i]=lower_bound(all(li),a[i])-li.begin();
}
int cnt[N],ans[N],ac;
void solve(){
rep(i,0,n){
ac=0;
rep(j,0,n-1)cnt[j]=0;
rep(j,i,n-1){
if(a[j]){
cnt[aa[j]]++;
if(cnt[aa[j]]==1)
ac++;
}
ans[max(1,ac)]++;
}
}
rep(i,1,n)
printf("%d ",ans[i]);
puts("");
}
int main(){
//freopen("in.txt","r",stdin);
build();
while(__--){
init();
solve();
}
}