time limit per test2 seconds memory limit per test256 megabytes
inputstandard input outputstandard output 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]
题意:确保每个连续序列的相邻两个元素的积不为平方数,求出最少需要分割成多少个序列
如果两个数相乘是平方数,那么他们的对应的因子的次数的和一定是偶数。所以只需要考虑次数为奇数的因子。对于每个数将它的奇数次的因子的乘积相乘,如果这个结果在原来的序列中出现过,那就清空前面的内容,重新开始新的序列。
//cyc
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mst(a) memset(a,0,sizeof a)
#define int long long
using namespace std;
typedef pair<int,int> pii;
const int maxn=2e5+5;
const int maxp=1e7+5;
int a[maxn];
bool s[maxp];
int cnt;
vector<int> prm;
void solve()//素数筛 存入1-1e7范围的质数
{
s[1]=1;
cnt=0;
int p;
for(int i=2;i*i<maxp;i++)
{
p=i;
if(!s[i])
{
cnt++;
prm.pb(i);
for(int j=i*i;j<maxp;j+=i)s[j]=1;
}
}
for(int i=p;i<maxp;i++){
if(!s[i]){
prm.pb(i);cnt++;
}
}
// sort(prm.begin(),prm.end());
}
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int _;
cin>>_;
solve();
// cout<<cnt<<endl;
while(_--){
map<int,int> mpk;
int n,k;
cin>>n>>k;
int ans=1;
int stt=1;
for(int i=1;i<=n;i++){
cin>>a[i];
if(!s[a[i]]){
mpk[a[i]]++;
if(mpk[a[i]]>=2){//检验是否出现过这个结果
mpk.clear();
mpk[a[i]]=1;
ans+=1;
// cout<<a[i]<<" "<<i<<endl;
}
// else if(i==n&&stt!=i)ans++;
continue;
}
int prod=1;
for(int it:prm){
if(it>a[i])break;
int cntz=0;
while(a[i]%it==0){
cntz++;
a[i]/=it;
}
if(cntz&1){
prod*=it;
}
if(!s[a[i]]){
prod*=a[i];
break;
}
}
mpk[prod]++;
if(mpk[prod]>=2){
mpk.clear();
mpk[prod]=1;
ans++;
// cout<<i<<endl;
}
}
cout<<ans<<endl;
}
}