CF798 C. Mike and gcd problem
2017-04-22 02:29
344 查看
/* CF798 C. Mike and gcd problem http://codeforces.com/contest/798/problem/C 数论 贪心 题意:如果一个数列的gcd值大于1,则称之为美丽数列 给出数列a,可以将a_i 和 a_(i+1)换为其差和其和 如果可以变为美丽数列,输出YES并输出最少的变换次数 否则输出NO 思路: 如果变换a b a b -> a-b a+b -> -2b 2a 因此变换两个可以把a b乘以2 而若a b都是奇数,只需变换一次 所以每次先找出相邻是奇数的情况ans++ 然后找相邻是奇偶的情况ans+=2 然而 比赛的时候居然把ans=gcd(ans,num[i]) 写成了ans=gcd(ans,i) 这居然还过了40组数据,也是醉了,雪崩! */ #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <iostream> #include <map> #include <set> //#define test using namespace std; const int Nmax=1e6+7; int n; long long num[Nmax]; long long gcd(long long a,long long b) { if(b==0) return abs(a); return gcd(b,a%b); } int main() { #ifdef test #endif scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&num[i]); long long ans=0LL;//初始化一定要看仔细 for(int i=1;i<=n;i++) { ans=gcd(ans,num[i]);//num[i]一定不要写成i!!!!! } if(ans>1) { printf("YES\n0\n"); return 0; } ans=0LL; for(int i=1;i<n;i++) { if((num[i]&1) && (num[i+1]&1)) { ans++; num[i]=2; num[i+1]=2; } } for(int i=1;i<=n;i++) { if(num[i]&1) { ans+=2; } } printf("YES\n%I64d\n",ans); return 0; }
相关文章推荐
- Codeforces Round #410 (Div. 2) Mike and gcd problem 思维
- 798 C. Mike and gcd problem
- codeforces 798C - Mike and gcd problem 贪心
- C. Mike and gcd problem
- Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)
- Codeforces Round #410 (Div.2) C.Mike and gcd problem-数学
- Codeforces 798C:Mike and gcd problem【数论+贪心】
- Codeforces Round #410 (Div. 2) C. Mike and gcd problem 贪心
- 【codeforces 798C】Mike and gcd problem
- D - Mike and gcd problem
- 【推导】Codeforces Round #410 (Div. 2) C. Mike and gcd problem
- 【算法系列学习】codeforces C. Mike and gcd problem
- C. Mike and gcd problem-递推或者贪心
- Codefroces 410 C. Mike and gcd problem
- Codeforces-798C-Mike and gcd problem(贪心+数论)
- Mike and gcd problem Codeforces Round #410 (Div. 2)
- Mike and gcd problem-codeforces-Round 410 Div2-C
- 789C Mike and gcd problem
- #410div2C. Mike and gcd problem
- Mike and gcd problem CodeForces - 798C