E1. Square-free division (easy version)
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≤ai≤107).
It’s guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case print a single integer — the answer to the problem.
Example
inputCopy
3
5 0
18 6 2 4 1
5 0
6 8 1 24 8
1 0
1
outputCopy
3
2
1
Note
In the first test case the division may be as follows:
[18,6]
[2,4]
[1]
题目大意:把数组分成n块,每块里的数相乘不能是4,9,16这样的可开方数,求最小n。
这里我们先分析什么样的两个数相乘是可整数被开方数。
我们知道一个数可以分解成n = p1q1 *p2q2*p3q3…*pkqk。
那我们把这里的每个p都mod2后再计算得到一个k值,如果两个数相乘是可整数被开方数他们的k值一定是相同的。这样我们把原数组转换成对应的k值数组就很好分块了,对每个k值找最后出现的index,用map记录一下就好了。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<iomanip>
#include<sstream>
#define ll long long
#define int long long
// inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
const ll N = 1e7 + 50;
const ll M = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f33fll;
using namespace std;
int n, k, m;
int T;
int prime[N],vis[N];
void init(){//线筛素数
int cnt = 0;
for (int i = 2; i < N;i++){
if(!vis[i]) {
vis[i] = i;
prime[cnt++] = i;
}
for (int j = 0; j < cnt&&prime[j]*i<N;j++){
vis[prime[j] * i] = prime[j];//vis要记录当前数最早被素数整除的那个素数,只用了标记了话会超时。
if(i%prime[j]==0) break;
}
}
}
signed main()
{
ios::sync_with_stdio(false);
init();
cin >> T;
while(T--){
cin >> n >> k;
int x;
vector<int>a(n, 1);
for (int i = 0; i < n;i++){
cin >> x;
int cnt = 0;
while(x>1){
cnt = 0;
int p = vis[x];
while(x%p==0){
x /= p;
cnt++;
}
if(cnt%2){
a[i] *= p;
}
}
}
int ans = 0,l = 0;
map<int, int> last;
for (int i = 0; i < n;i++){
if(last.find(a[i])!=last.end()&&last[a[i]]>=l){
ans++;
// cout << i << " ";
l = i;
}
last[a[i]] = i;
}
cout << ans+1 << endl;
}
return 0;
}