codeforces798C - Mike and gcd problem (数论+思维)
原题链接:http://codeforces.com/contest/798/problem/C
题意:有一个数列A,gcd(a1,a2,a3...,an)>1 时称这个数列是“漂亮”的。存在这样的操作,使ai,ai+1变为(ai-ai+1), (ai+ai+1)。问最少进行这样的操作使数列是“漂亮”的。
思路:考虑gcd(a1,a2,a3...,an)>1 的情况:
我们对ai,ai+1进行两次操作可以得到2ai,2ai+1,也就是说一对相邻的数字最多操作2次使它们的gcd=2>1。而对于一对奇数来说,操作一次就能使它们成为偶数。
现在就是要把数列中所有的数变为偶数。先对相邻奇数进行操作(每次+1),再对单个奇数进行操作(每次+2)即可。
AC代码:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 const int MAXN=2e6+5; 6 long long num[MAXN]; 7 long long gcd(long long a, long long b){ 8 return (b==0)?a:gcd(b, a%b); 9 } 10 int main() 11 { 12 int n; 13 while(cin>>n){ 14 memset(num, 0, sizeof(num)); 15 for(int i=0;i<n;i++){ 16 cin>>num[i]; 17 } 18 long long g=gcd(num[0], num[1]); 19 for(int i=2;i<n;i++){ 20 g=gcd(g, num[i]); 21 if(g==1) 22 break; 23 } 24 if(g>1){ 25 cout<<"YES"<<endl; 26 cout<<0<<endl; 27 continue; 28 } 29 int res=0; 30 for(int i=1;i<n;i++){ 31 if(num[i-1]%2&&num[i]%2){ 32 num[i-1]++; 33 num[i]++; 34 res++; 35 } 36 } 37 for(int i=1;i<n;i++){ 38 if(num[i-1]%2&&num[i]%2==0||(num[i]%2&&num[i-1]%2==0)) { 39 num[i-1]=0; 40 num[i]=0; 41 res+=2; 42 } 43 } 44 cout<<"YES"<<endl; 45 cout<<res<<endl; 46 } 47 48 return 0; 49 }
转载于:https://www.cnblogs.com/MasterSpark/p/7436987.html
codeforces798C - Mike and gcd problem (数论+思维)相关推荐
- Mike and gcd problem(思维)
Mike has a sequence A = [a1, a2, -, an] of length n. He considers the sequence B = [b1, b2, -, bn] b ...
- 【Codeforces - 798C】 Mike and gcd problem(思维,贪心)
题干: Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, .. ...
- Codeforces 798C:Mike and gcd problem
Codeforces 798C:Mike and gcd problem 题目链接:http://codeforces.com/contest/798/problem/C 题目大意:给出一个大小为$n ...
- G - Mike and gcd problem
G - Mike and gcd problem Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the seq ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- 牛客多校第四场【B-Basic Gcd Problem】
牛客多校第四场[B-Basic Gcd Problem] 题目链接:https://ac.nowcoder.com/acm/contest/5669/B 思路:先要理解公式,多看几个数据基本就会有点想 ...
- 贡献思想 + 数论 + 思维(例题 Problem J. Prime Game)
首先说一下贡献的思想: 举个例子:已知有n个数,从 ,求 中所有质因数出现的个数. 假设当前数为 6, 7, 5, 5, 4, 9, 9, 1, 8, 12 首先写出他每个数的质因数: 每个数的质因 ...
- CodeForces798cMike and gcd problem
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., b ...
- CF--思维练习--CodeForces - 221C-H - Little Elephant and Problem (思维)
ACM思维题训练集合 The Little Elephant has got a problem - somebody has been touching his sorted by non-decr ...
最新文章
- bzoj1066 蜥蜴 (dinic)
- win2008WEB服务器集群实践(转)
- 第一章:火狐浏览器 : 环境配置: FireFox 版本38 + jdk 7 + selenium 2.53.6 + selenum-version 2.48.2...
- 协作与协同有什么区别_OA软件, BPM系统, 移动办公系统, 协同办公系统到底有什么区别...
- 成功解决UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 130: invalid continuation b
- JVM(3)——JVM类加载器
- 剪切文件_lammps模拟带缺陷镍板剪切变形(in文件及注释)
- Leetcode: One Edit Distance
- 豆瓣电影推荐系统(Ⅰ)ItemCF算法原理
- 利用DMRMAN备份时出现“管道连接失败”的错误信息的解决方法
- php网页弹窗广告,弹窗广告最多的网站
- Java file outside of source root
- bootstrap常用样式整理
- 怎么将红底照片用PS换成白底
- TCP/UDP协议常用端口号服务
- Flink常见的面试题
- 2019,2021非科班生的Android面试之路
- 终极算法——第三章:符号学派:休谟的归纳问题
- matlab基本矩阵运算,MATLAB矩阵及其运算
- RabbitMq无法连接([AMQP Connection xx.xx.xx.xx:5672-119] ERROR com.rabbitmq.client.impl.ForgivingExcept)
热门文章
- 常见的通配符_8、数据库常见操作
- 两个iphone怎么大量传照片_「唯物」传个视频到 iPhone,有了新方法
- 三体与计算机科学与技术,《三体》作者刘慈欣:AlphaGo赢了,但人工智能不可能强大到没有弱点...
- linux 停止jar 指令,Linux编辑启动、停止与重启springboot jar包脚本实例
- oracle for net,使用Oracle Developer Tools For Visual Studio .NET-.NET教程,数据库应用
- Pycharm -- RuntimeWarning: Parent module '...' not found while handling absolute import
- WINDOWS下面使用ICE操作记录
- 可以方便的将SQL语句的执行结果显示成表格结果的JAVA类,可以用于不同的数据显示
- Vue的常用指令有哪些,分别是什么作用?
- 查询商品信息报错FreeMark template error