codeforces 798C Mike and gcd problem
来源:互联网 发布:异次元软件 编辑:程序博客网 时间:2024/06/05 16:37
题意:给出数组a[1...n],能否经过有限次变换使 gcd(a[i])>1 。能的话输出最小变换次数。 变换规则:选取i,a[i]=a[i]-a[i+1],a[i+1]=a[i]+a[i+1]
题解:
gcd(a[i])>1:特判
gcd(a[i])=1:
考虑两个数x,y。d=gcd(x-y,x+y),那么即存在p,q,( gcd(p,q)=1 )使得 x-y=pd , x+y=qd
那么有(p+q)d=2x,(q-p)d=2y,也就是说d|2x,d|2y
gcd(x-y,x+y) | gcd(2x,2y) <=> gcd(x-y,x+y) | 2gcd(x,y)
说明每次操作只能使得两个数的公因数乘以1或者乘以2
若相邻两个数同为奇数或同为偶数,那么操作后公因数乘以2
若相邻两个数为一奇一偶,那么操作后公因数不变
考虑全部n个数的话,显然一次操作同样也只能让他们总体的公因数乘以1或者乘以2
问题化为了求最少的操作次数使得他们全体为偶数
接下来问题就简单了
若相邻的两个数为奇数,那么需要1次操作让他俩都化为偶数
若相邻的两个数为偶数,那么需要0次操作让他俩都化为偶数
题解:
gcd(a[i])>1:特判
gcd(a[i])=1:
考虑两个数x,y。d=gcd(x-y,x+y),那么即存在p,q,( gcd(p,q)=1 )使得 x-y=pd , x+y=qd
那么有(p+q)d=2x,(q-p)d=2y,也就是说d|2x,d|2y
gcd(x-y,x+y) | gcd(2x,2y) <=> gcd(x-y,x+y) | 2gcd(x,y)
说明每次操作只能使得两个数的公因数乘以1或者乘以2
若相邻两个数同为奇数或同为偶数,那么操作后公因数乘以2
若相邻两个数为一奇一偶,那么操作后公因数不变
考虑全部n个数的话,显然一次操作同样也只能让他们总体的公因数乘以1或者乘以2
问题化为了求最少的操作次数使得他们全体为偶数
接下来问题就简单了
若相邻的两个数为奇数,那么需要1次操作让他俩都化为偶数
若相邻的两个数为偶数,那么需要0次操作让他俩都化为偶数
若相邻的两个数为一奇一偶,那么需要2次操作让他俩都化为偶数
参考题解:www.cnblogs.com/scidylanpno/p/6753960.html
#include<cstdio>const int N=100005;int a[N];int gcd(int a,int b) {if(b==0) return a;return gcd(b,a%b);}int main() {int n;while(~scanf("%d",&n)) {///readfor(int i=1;i<=n;++i) scanf("%d",a+i);///get dint d=a[1];for(int i=2;i<=n;++i) {d=gcd(d,a[i]);}///if(d>1) {puts("YES\n0");continue;}///solveint ans=0,now=0;for(int i=1;i<=n+1;++i) {if( a[i]%2 && a[i-1]%2 ) {++now;} else if( a[i]%2 && a[i-1]%2==0 ) {now=1;} else if( a[i]%2==0 && a[i-1]%2 ) { ans=ans+now/2;if(now&1) ans+=2;}}///printprintf("YES\n%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(思维)
- android5.1 以太网启动已经修改为静态IP
- 论微服务安全:保护微服务的两大方案
- 【编程素质】软件质量
- Airflow学习笔记 --- airflow安装
- spring注入一个实体bean的集合并指定顺序
- codeforces 798C Mike and gcd problem
- Angular JS 指令 & $scope 2 <连载> 新手必看
- pyqt4原始例程--提示_再次确认_关闭_图标_居中
- MySQL自动分区脚本
- HTTPS那些事(一)HTTPS原理
- pysctp中出现的IOError: [Errno 11] Resource temporarily unavailable以及无限重复接收的问题
- 【学习笔记】面向对象基础1
- LibSVM学习详细说明
- 简单SO加密及ELF头文件修复