题意:

有这样一个问题,给出一个数组,把里面的数字分组,使得每一个组里面的数两两相乘都是完全平方数。

问最少可以分成的组数k是多少。

现在一个人有一个数组,他想知道这个数组的连续子数组中,使得上面的问题答案分别为1到n的数组有多少个。

第一个样例

2

5 5

子数组有[5],[5],[5 5]三个,这三个组最少可以分别分为1 1 1组,使得每个组的任意两个数相乘都是平方数。

思路:

感谢js帮本智障debug。

首先对于一个不为0的数,如果把它的所有完全平方数的因子去掉,那么是不会影响结果的。

所以首先把每个数的完全平方数因子去掉,注意负数的处理

然后剩下的数都可以表示为一个或者多个质数的乘积,那么两两相乘为平方数只有一种情况,就是两个数字相等。

所以问题转化为了求一个数组的连续子数组中有多少个不相等的数字,那么这个子数组就可以最少分为几个组。

离散化统计每个区间内的不同的数字就行了,用set或者map会T,不知道为什么。。。复杂度O(n^2)。

注意有0的时候,除非每个元素都是0,否则0可以加入任何一个分组,得特殊处理一下,具体看代码。

代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <math.h>
using namespace std;
const int N = ;
int a[N];
int b[N];
int c[N];
bool vis[N];
int mabs(int x)
{
return x >= ? x : -x;
}
int main()
{
int n;
int cnt = ;
scanf("%d",&n);
for (int i = ;i < n;i++)
{
scanf("%d",&a[i]);
if (a[i] != ) cnt++;
}
if (cnt == )
{
printf("%d ",n * (n + ) / );
for (int i = ;i < n;i++) printf("0 ");
}
else
{
for (int i = ;i < n;i++)
{
if (a[i] == ) continue;
int tmp = mabs(a[i]);
for (int j = ;j * j <= tmp;j++)
{
int t = j * j;
while (a[i] % t == )
{
a[i] /= t;
}
//if (a[i] < t) break;加了这个就wa,卡了一晚上,考虑的应该是绝对值的情况
}
}
for (int i = ;i < n;i++) c[i] = a[i];
sort(c,c+n);
int js = unique(c,c+n) - c;
for (int i = ;i < n;i++)
{
if (a[i] == ) continue;
int p = lower_bound(c,c+js,a[i]) - c + ;
a[i] = p;
}
for (int i = ;i < n;i++)
{
memset(vis,,sizeof(vis));
int num = ;
for (int j = i;j < n;j++)
{
if (!vis[a[j]] && a[j] != )
{
num++;
vis[a[j]] = ;
}
int tt = max(num,);
b[tt]++;
}
}
for (int i = ;i <= n;i++)
{
printf("%d ",b[i]);
}
}
return ;
}

codeforces 980D Perfect Groups的更多相关文章

  1. Codeforces 980D Perfect Groups 计数

    原文链接https://www.cnblogs.com/zhouzhendong/p/9074164.html 题目传送门 - Codeforces 980D 题意 $\rm Codeforces$ ...

  2. CF 980D Perfect Groups(数论)

    CF 980D Perfect Groups(数论) 一个数组a的子序列划分仅当这样是合法的:每个划分中的任意两个数乘积是完全平方数.定义a的权值为a的最小子序列划分个数.现在给出一个数组b,问权值为 ...

  3. Codeforces 980 D. Perfect Groups

    \(>Codeforces\space980 D. Perfect Groups<\) 题目大意 : 设 \(F(S)\) 表示在集合\(S\)中把元素划分成若干组,使得每组内元素两两相乘 ...

  4. Perfect Groups CodeForces - 980D

    链接 题目大意: 定义一个问题: 求集合$S$的最小划分数,使得每个划分内任意两个元素积均为完全平方数. 给定$n$元素序列$a$, 对$a$的所有子区间, 求出上述问题的结果, 最后要求输出所有结果 ...

  5. CodeForces 173E Camping Groups 离线线段树 树状数组

    Camping Groups 题目连接: http://codeforces.com/problemset/problem/173/E Description A club wants to take ...

  6. Codeforces 986D Perfect Encoding FFT 分治 高精度

    原文链接https://www.cnblogs.com/zhouzhendong/p/9161557.html 题目传送门 - Codeforces 986D 题意 给定一个数 $n(n\leq 10 ...

  7. [CodeForces - 919B] Perfect Number

    题目链接:http://codeforces.com/problemset/problem/919/B AC代码: #include<cstdio> using namespace std ...

  8. Codeforces 948D Perfect Security(字典树)

    题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...

  9. CodeForces - 233A Perfect Permutation

    A. Perfect Permutation time limit per test: 2 seconds memory limit per test: 256 megabytes input: st ...

随机推荐

  1. python操作rabbitMQ小结

    1.安装rabbitMQ(与python无关) https://www.cnblogs.com/libra0920/p/7920698.html 2.python+rabbitMQ实现生产者和消费者模 ...

  2. opencv手工编译

    opencv手工编译方法1.下载cmake gui2.在where is the source code路径下配置opencv根目录,在where to build the binaries路径下配置 ...

  3. 前端 HTML body标签相关内容

    想要在网页上展示出来的内容一定要放在body标签中. 常用标签: 标题标签 h1-h6 段落标签 p标签 超链接标签 a标签 列表标签 ul,ol,li 定义列表<dl> 子标签 div ...

  4. wamp设置本地访问路径为a.com

    我们在用wamp进行本地建站时经常会碰到页面样式无法正常加载,这是因为没有正确加载css路径,那我们就用wamp设置本地访问路径为a.com指向本地的一个虚拟空间,如何操作呢?下面就跟随ytkah一起 ...

  5. Sql server 函数--取值年月

    GetDate()是获取当前时间 1.例如获取年月类似 201706 需要改为语句: Select Datename(year,GetDate())+Datename(month,GetDate())

  6. React篇-报错信息:warning: Can't call setState (or forceUpdate) on an unmounted component.

    报错信息是: Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but ...

  7. js篇-json字符串与json对象相互转化

    回调返回结果是 json字符串还是json对象一定要看清楚哦,状态不好的时候,感觉眼神也不好使了, var a = "{"name":"jenny", ...

  8. golang 中创建daemon的方法

    https://github.com/takama/daemon https://github.com/immortal/immortal/blob/master/fork.go            ...

  9. Python 全栈开发九 日志模块

    日志是一种可以追踪某些软件运行时所发生事件的方法.软件开发人员可以向他们的代码中调用日志记录相关的方法来表明发生了某些事情.一个事件可以用一个可包含可选变量数据的消息来描述.此外,事件也有重要性的概念 ...

  10. git 查看某个文件的修改记录

    有几种方式, (1)如果是在linux环境下,比如centos,ubuntu之类的,建议安装tig命令 炒鸡好用,tig后面可以跟文件或者文件夹,比如: (1.1)tig  dir_name (1.2 ...