Codeforces 798C Mike and gcd problem gcd+贪心
来源:互联网 发布:loadrunner java 录制 编辑:程序博客网 时间:2024/06/07 10:42
点击打开链接
题意:n个数,n<=1e5,操作:把ai,ai+1 替换成 a[i]-a[i+1],a[i]+a[i+1],问gcd(a1,a2..an)>1的最少操作次数
若初始d=gcd(a1..an)==1,肯定使用操作,则使用操作后->d|2a[i] d|2a[i+1],可以看出最后的d一定整除偶数,所以n个数必须都为偶数.
问题转化为把n个数变为偶数的最小操作次数 a[i]-a[i+1],a[i]+a[i+1],相邻两个odd需要一次操作,相邻odd,even 需要两次操作
贪心:先处理(odd,odd) 在处理(o,e),(e,o)即可
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N=2e5+20;ll a[N],n;ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}int main(){while(cin>>n){for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);ll d=gcd(a[1],a[2]);for(int i=3;i<=n;i++)d=gcd(d,a[i]);if(d>1){puts("YES");puts("0");continue;}int ans=0;for(int i=1;i<n;i++){if(a[i]%2&&a[i+1]%2)a[i]=0,a[i+1]=0,ans++;//变为任意偶数即可 } for(int i=1;i<n;i++){if(a[i]%2&&a[i+1]%2==0||(a[i]%2==0&&a[i+1]%2))a[i]=0,a[i+1]=0,ans+=2;}puts("YES");cout<<ans<<endl;}return 0;}
0 0
- Codeforces 798C Mike and gcd problem gcd+贪心
- Codeforces-798C-Mike and gcd problem(贪心+数论)
- Codeforces 798C Mike and gcd problem (贪心)
- Codeforces 798C:Mike and gcd problem【数论+贪心】
- Codeforces 798C Mike and gcd problem
- codeforces 798C Mike and gcd problem
- CodeForces 798C Mike and gcd problem
- Codeforces 798C Mike and GCD problem
- Codeforces 798C Mike and gcd problem【思维+贪心】好题!
- Mike and gcd problem CodeForces
- cf 798c Mike and gcd problem
- 798 C. Mike and gcd problem
- C. Mike and gcd problem
- C. Mike and gcd problem
- Codeforces Round #410 (Div. 2) C. Mike and gcd problem 贪心
- C. Mike and gcd problem-递推或者贪心
- Mike and gcd problem-codeforces-Round 410 Div2-C
- codeforces round410 div2 C.Mike and gcd problem (数学)
- React Native Linking跨app的通信方法
- 模板_SPFA算法
- 【BNUOJ】Captcha Cracker
- 指令于动作之forward动作
- MySQL分页查询,关键字(limit)
- Codeforces 798C Mike and gcd problem gcd+贪心
- 关于Xcode编译性能优化的研究工作总结
- Redis 在Window下集群部署
- 2017.4.22 进制转换 思考记录
- [LeetCode]11. Container With Most Water
- HDU 1027 Ignatius and the Princess II (水题,排列)
- 获取A-B数字之间的质数
- 51NOD 1140 矩阵相乘结果的判断(随机化算法+矩阵结合率)
- Matlab求一元函数极值