CodeForces 798C Mike and gcd problem
来源:互联网 发布:div js 显示隐藏 编辑:程序博客网 时间:2024/06/11 23:49
题目链接:http://codeforces.com/contest/798/problem/C
题意:给你一个定义如果B序列,gcd(b[0],b[1],b[2]……)>1,那么称B为漂亮序列,现在给你一个序列A,让你对他进行操作,操作时,对于两个元素a[i],a[i+1],你可以把他替换成a[i+1]-a[i],a[i+1]+a[i],现问你,要最少多少次操作能把A序列变为漂亮序列,如果可以则输出YES和最少操作数,如果不行则输出NO
解析:首先是一定是可以通过操作来使得他变漂亮的,因为奇数+奇数等于偶数,所以如果相邻的是奇数那么你就需要花费一步操作,如果相邻的是一奇一偶,那么你需要对他操作两次,所以先操作奇数相邻的,在操作奇偶相邻的,记得操作完后得把原数组变为偶数
#include <bits/stdc++.h>using namespace std;const int inf = 0x7fffffff;const int maxn = 1e5+100;int a[maxn];int main(void){ int n; scanf("%d",&n); int ans = 0; for(int i=0;i<n;i++) scanf("%d",&a[i]); int tmp = __gcd(a[0],a[1]); for(int i=2;i<n;i++) tmp = __gcd(tmp,a[i]); if(tmp>1) puts("YES\n0"); else { for(int i=1;i<n;i++) { if(a[i]%2 && a[i-1]%2) { a[i] = 2; a[i-1] = 2; ans++; } } for(int i=1;i<n;i++) { if(a[i]%2==0 && a[i-1]%2!=0) { a[i] = 2; a[i-1] =2; ans += 2; } else if(a[i]%2!=0 && a[i-1]%2==0) { a[i] = 2; a[i-1] = 2; ans += 2; } } puts("YES"); printf("%d\n",ans); } return 0;}
0 0
- 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 gcd+贪心
- 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
- Mike and gcd problem-codeforces-Round 410 Div2-C
- codeforces round410 div2 C.Mike and gcd problem (数学)
- Codeforces 798C Mike and gcd problem【思维+贪心】好题!
- codeforce 798C Mike and gcd problem (简单数学)
- CF#798 C. Mike and gcd problem(思维)
- 抽象类、接口(包括Java8新特性)
- 界面让table居中的办法
- 【每日一问】数组 new问题
- java注解
- curl使用
- CodeForces 798C Mike and gcd problem
- 坦克大战(bfs)
- 我的 React Native 技能树点亮计划 の 为 React Native 开发准备的 VS Code 插件
- G
- Java 移位
- iOS键盘高度获取并使输入框上弹的坑
- 常见排序算法整理
- 摄像机模型
- Oh, my goddess(bfs)