codeforces 980D Perfect Groups
题意:
有这样一个问题,给出一个数组,把里面的数字分组,使得每一个组里面的数两两相乘都是完全平方数。
问最少可以分成的组数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的更多相关文章
- Codeforces 980D Perfect Groups 计数
原文链接https://www.cnblogs.com/zhouzhendong/p/9074164.html 题目传送门 - Codeforces 980D 题意 $\rm Codeforces$ ...
- CF 980D Perfect Groups(数论)
CF 980D Perfect Groups(数论) 一个数组a的子序列划分仅当这样是合法的:每个划分中的任意两个数乘积是完全平方数.定义a的权值为a的最小子序列划分个数.现在给出一个数组b,问权值为 ...
- Codeforces 980 D. Perfect Groups
\(>Codeforces\space980 D. Perfect Groups<\) 题目大意 : 设 \(F(S)\) 表示在集合\(S\)中把元素划分成若干组,使得每组内元素两两相乘 ...
- Perfect Groups CodeForces - 980D
链接 题目大意: 定义一个问题: 求集合$S$的最小划分数,使得每个划分内任意两个元素积均为完全平方数. 给定$n$元素序列$a$, 对$a$的所有子区间, 求出上述问题的结果, 最后要求输出所有结果 ...
- CodeForces 173E Camping Groups 离线线段树 树状数组
Camping Groups 题目连接: http://codeforces.com/problemset/problem/173/E Description A club wants to take ...
- Codeforces 986D Perfect Encoding FFT 分治 高精度
原文链接https://www.cnblogs.com/zhouzhendong/p/9161557.html 题目传送门 - Codeforces 986D 题意 给定一个数 $n(n\leq 10 ...
- [CodeForces - 919B] Perfect Number
题目链接:http://codeforces.com/problemset/problem/919/B AC代码: #include<cstdio> using namespace std ...
- Codeforces 948D Perfect Security(字典树)
题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...
- CodeForces - 233A Perfect Permutation
A. Perfect Permutation time limit per test: 2 seconds memory limit per test: 256 megabytes input: st ...
随机推荐
- 修改.net core MVC默认端口
默认端口是5000,更改端口修改launchSettings.json.如图:
- 【PyQt5-Qt Designer】工具箱(QToolBox)用法讲解
QToolBox() 实现抽屉效果 总体介绍: QToolBox类提供了一列选项卡的小部件(选项卡内含项目). 工具箱是一个小部件,它将选项卡一个一个的显示,当前项目显示在当前选项卡下方.每个选项卡在 ...
- C语音读写文件
1.fopen() fopen的原型是:FILE *fopen(const char *filename,const char *mode),fopen实现三个功能:为使用而打开一个流,把一个文件和此 ...
- C语言编程中pid, tid以及真实pid的关系(转)
add by zhj: 下面是我对pid,tgid,ppid的个人理解 对于ubuntu14.04操作系统,可以在/usr/src/linux-headers-4.4.0-31/include/lin ...
- 在asp.net中使用瀑布流,无限加载
页面中代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1 ...
- TensorFlow安装之后导入报错:libcudnn.so.6:cannot open sharedobject file: No such file or directory
转载自:http://blog.csdn.net/silent56_th/article/details/77587792 系统环境:Ubuntu16.04 + GTX1060 目的:配置一下pyth ...
- CSS学习(二)
<!DOCTYPE html> <html> <head> <meta charset="{CHARSET}"> <title ...
- JsonDataObjects基本演示
下载地址https://github.com/ahausladen/JsonDataObjects 执行程序截图 Json数据 { "name": "张三", ...
- react +webpack 配置px2rem
项目背景需要适配ipad 以及手机端,这时候当然要告别刀耕火种时代啦(自己算rem),因为已经有成熟的工具啦,即px2rem(https://www.npmjs.com/package/px2rem) ...
- vant - 头部 - header【Layout 布局】【Icon 图标】
安装 npm i vant -S [main.js] import Vant from 'vant'; import 'vant/lib/index.css'; Vue.use(Vant); [ind ...