Codeforces Round #410 (Div.2) C.Mike and gcd problem-数学
来源:互联网 发布:网络创业项目 编辑:程序博客网 时间:2024/06/05 23:55
题目链接:请自行搜索
题目大意:给定n个数,每次可以将A_i和A_(i+1)这两个数字修改成A_i - A_(i+1)和A_i + A_(i+1)这两个数字。
请问至少要修改多少次才能使得GCD(A1,A2,A3,...,An)>1。特别的,我们认为GCD(0,x)=x,GCD(-a,b)=GCD(a,b)。(即正负不影响)
题解:
首先如果所有数的gcd>1那么一次都不用修改,直接输出0即可。
否则,设修改最后GCD(A1',A2',A3',...,An')=d。则d|(A_i - A_(i+1)),d|(A_i + A_(i+1))。即d|2*Ai,d|2*A(i+1)。
又因为GCD(A1,A2,A3,...,An)=1,所以d|2。又因为d>1所以d=2.
所以目标是就是让所有数都变成偶数。
我们注意到相邻两个数如果是奇数,那么一次修改就可以让两个数变成偶数
如果使一奇一偶,那么需要操作两次。
于是我们的算法是:先检查所有数的gcd是否>1。如果是就输出0并推出。
否则从第一个数开始到第n-1个数,如果当前这个数是偶数就跳过不看,如果是奇数:
如果下一个数字也是奇数,cnt++。并把下一个数字改为偶数。
否则cnt+=2。
最后看一下最后一个是奇数还是偶数。
如果是前者就cnt+=2。
最后输出cnt即可。
代码:
//Codeforces Round #401 (Div.2) C.Mike and gcd problem#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define MAXN 100010using namespace std;int a[MAXN];int gcd(int a,int b){if(a==0) return b;else return gcd(b%a,a);}int main(){int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);printf("YES\n");int g=a[1];for(int i=2;i<=n;i++)g=gcd(g,a[i]);if(g>1){printf("0\n");return 0;}int cnt=0;for(int i=1;i<n;i++)if(a[i]%2){if(a[i+1]%2){cnt++;a[i]++;a[i+1]++;}else{cnt++;cnt++;a[i]++;}}if(a[n]%2) cnt++,cnt++;printf("%d\n",cnt);return 0;}
0 0
- Codeforces Round #410 (Div.2) C.Mike and gcd problem-数学
- Codeforces Round #410 (Div. 2) Mike and gcd problem 思维
- Mike and gcd problem Codeforces Round #410 (Div. 2)
- Codeforces Round #410 (Div. 2) C. Mike and gcd problem 贪心
- Mike and gcd problem-codeforces-Round 410 Div2-C
- codeforces round410 div2 C.Mike and gcd problem (数学)
- Codeforces Round #305 (Div. 1)C. Mike and Foam 数学
- Codeforces Round #305 (Div. 2), problem: (A) Mike and Fax
- Codeforces Round #361 (Div. 2)E. Mike and Geometry 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 Round #305 (Div. 2)C. Mike and Frog 数学(循环节)
- Codeforces Round #305 (Div. 2) C. Mike and Frog
- Codeforces Round #305 (Div. 2) C. Mike and Frog
- Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves
- Codeforces Round #361 (Div. 2) C - Mike and Chocolate Thieves
- 不能将“this”指针从“const CPaopao”转换为“CPaopao &
- SGE:作业调度系统安装和使用简要说明
- Anaconda安装tensorflow后,spyder无法调用,终端可以。
- Hibernate命名策略自动改变表名或字段名的问题
- 中文排序
- Codeforces Round #410 (Div.2) C.Mike and gcd problem-数学
- 遇见的异常以及其解决方法整理
- 有关UIView、subview的几个基础知识点-IOS开发 (实例)
- 那些被你忽略的Android字符串使用姿势!!!
- 电脑信息监控模块:Sigar介绍(适用于JAVA、PYTHON、C#等)
- js中cookie的使用详细分析
- Evaluate Division
- jQuery页面滚动元素进入视口发生动画特效插件
- ThinkPHP的使用(四)Excel文件导出