题解:P12431 [BalticOI 2025] Gingerbread
先把
先给
有这样的结论:我们只要找到两个非空的无交的子序列且它们的和相同,那么
由抽屉原理可知,当
所以我们只要考虑
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int p[1000005],n,g;
void dfs(int x,int y,int now,int g){
if(x==n){
if(__gcd(g,p[x]+y-now)==1) cout<<y<<'\n',exit(0);
return ;
}
for(int i=0;i<=y-now;i++)
dfs(x+1,y,now+i,__gcd(g,p[x]+i));
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>p[i],g=__gcd(g,p[i]);
if(g==1) return cout<<"0\n",0;
if(n>23) return cout<<"1\n",0;
for(int i=1;;i++)
dfs(1,i,0,0);
return 0;
}