Square-free division (easy version) CodeForces - 1497E1

This is the easy version of the problem. The only difference is that in this version k = 0.

There is an array a1, a2, ……, an of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.

Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important.

What is the minimum number of continuous segments you should use if you will make changes optimally?

Input

The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains two integers n, k (1≤n≤2⋅105, k = 0).

The second line of each test case contains n integers a1, a2, ……, an(1≤a ≤107).

It’s guaranteed that the sum of nn over all test cases does not exceed 2⋅105 .

Output

For each test case print a single integer — the answer to the problem.

题意

给你一个长度为n的序列,把这序列分为若干段,使每段序列中的两个数字相乘不为完全平方数,问你最少分为多少段?

思路

完全平方数的每个质因子的个数都为偶数个,所以我们
把每个数质因子分解,记录它为奇数个的质因数相乘的结果,如果两个数记录为奇数个的质因数相乘的结果一样,那它们相乘九尾完全平方数

代码

#include "stdio.h"
#include "algorithm"
#include "string.h"
#include "iostream"
#include "map"
using namespace std;
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int n,k,t;
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		map<int,int>p;
		int ans=0,sum=1,s,f=1;
		for(int i=1; i<=n; i++)
		{
			sum=1;
			cin>>k;
			for(int j=2; j*j<=k; j++)
			{
				s=0;
				while(k%j==0)
				{
					k/=j;
					s++;
				}
				if(s&1)//这个质因子的个数为奇数个
					sum*=j;
			}
			if(k>1)//k为素数
				sum*=k;
			if(p[sum])
			{
				p.clear();//初始化
				ans++;
			}
			p[sum]=1;

		}
		ans++;//自己理解
		cout<<ans<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值