Codeforces 980D 数论

14 篇文章 0 订阅
9 篇文章 0 订阅

题目链接: http://codeforces.com/contest/980/problem/D

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

题意:给出一个数组,对于每个连续子序列, 这个子序列(i ~ j)有(j - i + 1)个数, 把这些数分成k组,使得每组任意两个数相乘是完全平方数,要求对于一个子序列,k要最小化, 比如[1 2 8]分成2组[2 8][1], k= 2, res[2] ++.


#include <bits/stdc++.h>

using namespace std;
const int maxn = 5007;
bool isp[maxn*2];
vector<int> prime;
int a[maxn], b[maxn];
int sz, res[maxn];
void init()
{
    memset(isp, 1, sizeof(isp));
    isp[1] = 0;
    for(int i = 2;i <= 10000;i ++) {
        if(isp[i]) {
            for(int j = i + i;j <= 10000;j += i) isp[j] = 0;
        }
    }
    for(int i = 2;i <= 10000;i ++) if(isp[i]) prime.push_back(i);
    sz = prime.size();
}
int main()
{
    init();
    int n;
    scanf("%d", &n);
    for(int i = 1;i <= n;i ++) {
        scanf("%d", &a[i]);
        if(!a[i]) {
            b[i] = a[i];
            continue;
        }
        int fg = 1;
        if(a[i] < 0) a[i] = -a[i], fg = -1;
        for(int j = 0;j < sz;j ++) {
            int t = prime[j] * prime[j];
            while(a[i] % t == 0) a[i] /= t;
            if(t > a[i]) break;
        }
        b[i] = fg * a[i];
    }
    for(int i = 1;i <= n;i ++) {
        int zero = 0, cnt = 0;
        for(int j = i;j <= n;j ++) {
            if(b[j] == UP) zero = 1;
            if(bs[b[j]] == 0) {
                cnt ++;
                bs[b[j]] = 1;
                pos.push_back(b[j]);
            }
            res[max(1, cnt-zero)] ++;
        }
        for(auto i : pos) bs[i] = 0;
    }
    for(int i = 1;i <= n;i ++) printf("%d%c",res[i], " \n"[i==n]);
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值