CF980D Perfect Groups
首先发现如果一个数中含有平方因子,那么去掉平方因子不会对答案产生影响。
所以对于一个串,去掉平方因子后,有多少种不同的数,权值就是多少。
#include <bits/stdc++.h>
#define reg register
#define ll long long
#define vi vector<int>
#define SZ(x) (int) x.size()
#define pb push_back
using namespace std;
const int MAXN = 5010;
int n, tot, a[MAXN], b[MAXN], ans[MAXN];
bool vis[MAXN];
map<int, int> mp;
int calc(int x) {
if(x >= -1 && x <= 1) return x;
int flag = 1;
if(x < 0) flag = -1, x *= -1;
for(reg int i = 2; i <= 10000; ++i) {
if(i * i > x) break;
while(x % (i * i) == 0) x /= i * i;
}
return x * flag;
}
void work() {
scanf("%d", &n);
for(reg int i = 1; i <= n; ++i) {
scanf("%d", &a[i]), a[i] = calc(a[i]);
if(!mp[a[i]]) mp[a[i]] = ++tot, b[i] = tot;
else b[i] = mp[a[i]];
}
for(reg int i = 1; i <= n; ++i) {
tot = 0, memset(vis, 0, sizeof(vis));
for(reg int j = i; j <= n; ++j) {
if(a[j] == 0) {
if(!tot) ++ans[1];
else ++ans[tot];
} else {
if(!vis[b[j]]) vis[b[j]] = 1, ++tot;
++ans[tot];
}
}
}
for(reg int i = 1; i <= n; ++i) printf("%d ", ans[i]);
puts("");
}
int main() {
int _ = 1;
// scanf("%d", &_);
while(_--) work();
return 0;
}