Codeforces 798C Mike and gcd problem
来源:互联网 发布:java岗位管理制度 编辑:程序博客网 时间:2024/06/05 03:38
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .
Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so.
is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).
The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.
Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise.
If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.
21 1
YES1
36 2 4
YES0
21 3
YES1
In the first example you can simply make one move to obtain sequence [0, 2] with .
In the second example the gcd of the sequence is already greater than 1.
题意: 给定一个数组,问最少变幻多少次可以把他变成一个漂亮数组。变幻方式说明:每个数组只能和与它相邻的数组一起进行变幻。例如:a[i],a[i+1] ------> a[i] - a[i+1],a[i+1] + a[i],对漂亮数组的定义:整个数组的最大公约数大于1。
思路: 首先这个这个问题是不可能输出NO的,都是YES。然后,就是一些基本知识:1.任何数与0的最大公约数都是这个数本身。(确切的说是gcd值。)。2.而如果是负数, 那么可以把它转化成相反数来看。如果这个数组刚开始就是漂亮数组那么结果就是“0”,如果这个数组刚开始的是不是漂亮数组。然后,如果这个数组 初始中的最大公约数是1,那么最终变幻之后就只的最大公约数必然是2。然后,相邻的两个数是奇数那么变幻后就是 “奇数 - 奇数,奇数 + 奇数”。其中“奇数 - 奇数”是一个偶数,“奇数 + 奇数” 是偶数最大公约数中有2。如果是奇数与偶数相邻,则第一次变幻之后 “奇数 - 偶数,偶数 + 奇数”,(这两个数都是奇数,还需要在变幻一次才能得到两个偶数。)
基于这个思路接下来是我自己的AC代码:(个人感觉不是很好。)
#include<iostream>#define MAX 100005using namespace std;int GCD(int a,int b){return b == 0 ? a : GCD(b,a%b);}int num[MAX];int main( ){int n;while(cin >> n){for(int j = 0; j < n; j++)cin >> num[j];int gcd = GCD(num[0],num[1]);for(int j = 2; j < n; j++)gcd = GCD(gcd,num[j]);//cout << gcd << endl;if(gcd != 1){cout << "YES" << endl;cout << "0" << endl;continue;}int ans = 0;bool odd_ok;if((num[0]&1) == 0)odd_ok = false;elseodd_ok = true;for(int j = 1; j < n; j++){if((num[j]&1) == 1){if(odd_ok)ans++,odd_ok = false;elseodd_ok = true;}else{if(odd_ok)ans += 2,odd_ok = false;elseodd_ok = false;}}if(odd_ok)ans += 2;cout << "YES" << endl;cout << ans << endl;}}
#include <bits/stdc++.h>using namespace std;int main ( ) {int n, ai, d = 0, ones = 0, ans = 0;scanf ( "%d", &n );for ( int i = 0; i <= n; ++i ) {if ( i == n ) ai = 0;else scanf ( "%d", &ai );d = __gcd(d,ai);if ( ai&1 ) ones++;else ans += ones/2 + 2*(ones%2), ones = 0;}printf ( "YES\n%d\n", d < 2 ? ans : 0 );return 0;}
这个题刚开始的时候把题目理解错了,我以为这个最大公约数是只是相邻两个数之间的最大公约数。结果在我好不容易看懂题意之后感觉太后悔了。QAQ.......
- 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(思维)
- android 蓝牙的那些坑
- JQuery中的switch语句
- React-Native 之 redux 与 react-redux
- 3ds max法线贴图制作流程
- 基于 Django1.10 文档的深入学习(1)—— Settings.py 之 MEDIA_ROOT 与 MEDIA_URL
- Codeforces 798C Mike and gcd problem
- JDBC与JAVA数据库编程
- 金融数据类——债券,全球股指
- C++primer学习笔记-----4.3逻辑和关系运算符
- 网络抓包工具Charles使用详解
- android开发-验证邮箱输入是否合法
- Density Peak改进(PCA/KNN)
- bzoj 4780: [Usaco2017 Open]Modern Art 2 (模拟)
- JavaScript 中的 this 用法以及 call(apply) 的理解