https://codeforces.com/problemset/problem/798/C
Question: Given a sequence of numbers, you can perform the following operations, select two adjacent numbers to perform xy, x+y operations. Ask at least how many times to make the entire sequence gcd>1;
Ideas:
The simulation can find that two numbers of gcd==2 can be output at most twice.
And if it turns out to be gcd! =1, it will not pass once and become gcd>1;
Suppose t|x,t|y--->t|ax+by; so if gcd can be >1 after 1 time, it can be >1. So this will not happen.
We continue to explore why we can gcd==2 twice
Two numbers AB are nothing more than four cases:
Odd number, odd number--------------->After operation, it becomes even number, even number +1
Odd number, even number --------------->Odd number after operation, odd number----even even +2
Even number, odd number --------------->Odd number after operation, odd number ---even even+2
Even number, even number --------------->After operation, it becomes even number, even number --- +0
Therefore, for three numbers and above, consider whether the current odd number becomes even or unchanged. Perform dp.
dp[i][0/1]:前i个数且当前数字修改为偶数/奇数时,前i个数字的gcd>1的最小代价
0 0--->0 (不会有偶数变成奇数的状态)
0 1--->2
1 0--->2
1 1--->1
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
const LL inf=0x3f3f3f3f;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn];
LL dp[maxn][2];
///dp[i][0/1]:前i个数且当前数字修改为偶数/奇数时,前i个数字的gcd>1的最小代价
///0 0--->0 (不会有偶数变成奇数的状态)
///0 1--->2
///1 0--->2
///1 1--->1
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n;cin>>n;
LL gcd=0;
for(LL i=1;i<=n;i++){
cin>>a[i];
gcd=__gcd(gcd,a[i]);
}
if(gcd>1){
cout<<"YES"<<"\n";
cout<<"0"<<"\n";
}
else if(gcd==1){
dp[1][!(a[1]%2)]=inf;///第一个数偶数,奇数就无法变;第一个数是奇数,偶数就不会变
for(LL i=2;i<=n;i++){
if(a[i]%2==0){
dp[i][0]=min(dp[i-1][0],dp[i-1][1]+2);
dp[i][1]=inf;///当前偶数不会变成奇数的
}
else if(a[i]&1){
///把当前的奇数改成偶数
///和前面的偶数改,和前面变成的奇数改
dp[i][0]=min(dp[i-1][0]+2,dp[i-1][1]+1);
dp[i][1]=min(inf,dp[i-1][0]);///当前仍然奇数不动,继承前面改好的偶数状态
}
}
///总会yes的
if(dp[n][0]!=inf){
cout<<"YES"<<"\n";
cout<<dp[n][0]<<"\n";
}
else cout<<"NO"<<"\n";
}
return 0;
}
Of course, dp may be slightly troublesome. We found that we can be greedy, the odd number is the best +1, and the odd number is changed first. Then change the rest. Won't be worse
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn];
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n;cin>>n;
LL gcd=0;
for(LL i=1;i<=n;i++){
cin>>a[i];gcd=__gcd(gcd,a[i]);
}
if(gcd>1){
cout<<"YES"<<"\n";
cout<<"0"<<"\n";
}
else if(gcd==1){
for(LL i=1;i<=n;i++){
if(a[i]&1) a[i]=1;
else if(a[i]%2==0) a[i]=0;
}
LL ans=0;
for(LL i=1;i<n;i++){
if(a[i]==1&&a[i+1]==1) ans+=1,a[i]=0,a[i+1]=0;
}
for(LL i=1;i<n;i++){
if(a[i]==1&&a[i+1]==0) a[i]=0,a[i+1]=0,ans+=2;
else if(a[i]==0&&a[i+1]==1) a[i]=0,a[i+1]=0,ans+=2;
}
cout<<"YES"<<"\n";
cout<<ans<<"\n";
}
return 0;
}