题解:P7244 章节划分
下文令
首先不难发现最大值一定会参与取最大公因数运算,所以先枚举最大值的因数,考虑去检查其是否合法,令枚举的因数为
首先合并两段后最大值是原来两段最大值其中更大者,所以划分成不少于
令
#include<bits/stdc++.h>
using namespace std;
//#define lowbit(x) (x&(-x))
//#define bp push_back
//#define sz size
//#define cl clear
const int maxn = 1e5+114;
int a[maxn],n,k;
int stk[maxn],val[maxn],Max[maxn],tp;
int mx;
bool check(int x){
tp=0;
stk[0]=0;
val[0]=0;
Max[0]=-INT_MAX;
int dp=0;
for(int i=1;i<=n;i++){
int pop=-INT_MAX;
while(a[stk[tp]]<a[i]&&tp>0){
pop=max(pop,val[tp]);
tp--;
}
stk[++tp]=i;
val[tp]=max(pop,dp+1);
Max[tp]=Max[tp-1];
if(a[stk[tp]]%x==0) Max[tp]=max(Max[tp],val[tp]);
dp=Max[tp];
}
return dp>=k;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
mx=max(mx,a[i]);
}
for(int i=mx;i>=1;i--){
if(mx%i==0){
if(check(i)==true){
cout<<i<<'\n';
return 0;
}
}
}
return 0;
}