题
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;
}
}