D. Perfect Groups+唯一分解+组合

本文介绍了一种算法,用于解决给定整数数组中寻找连续子数组的问题,这些子数组可以被划分成最少的组,使得每组内任意两个整数的乘积为完全平方数。文中提供了一个C++实现示例,包括输入输出处理、数据预处理及核心算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

(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
#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();
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值