数学——CodeForces #410 div.2
来源:互联网 发布:音频发生器软件 编辑:程序博客网 时间:2024/06/06 02:21
题目链接: http://codeforces.com/contest/798/problem/C
题目:见链接
分析:我们可以发现,对于相邻的两个奇数,我们对前一个做操作,会使得他们变成两个偶数,即gcd 最小为 2;再者,对于任意一个数,做2遍操作后,它和它后面的元素都会变为原来的2倍(符号不管),所以我们对于单个的奇数,做2次操作,就会也会使得gcd最小为2。这样得到的操作次数一定是最小的。首先,我们判定gcd最小为2的情况一定是最小的,因为所有偶数都满足情况,并且连续2个奇数,操作一次也能满足情况。现在看单个的奇数,如果单个的奇数包含的因子是最小gcd的话,那么要么所有数都包括,这就是操作为0的情况,可以直接验证出来;要么有的数没有包括,那么将这个数操作使得它和它后面的数包含这个奇数因子的次数肯定大于等于2(证明就是没有反例QAQ),所以单个的奇数操作2次也是最小次数。综上,gcd最小为2的情况也是最小的。
AC代码:
/************************************************************************* > File Name: test.cpp > Author: Akira > Mail: qaq.febr2.qaq@gmail.com ************************************************************************/#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>#include <bitset>#include <queue>#include <stack>#include <map>#include <cmath>#include <vector>#include <set>#include <list>#include <ctime>#include <climits>typedef long long LL;typedef unsigned long long ULL;typedef long double LD;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define Sqr(a) ((a)*(a))using namespace std;#define MaxN 100001#define MaxM MaxN*10#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626const int mod = 1E9+7;const double eps = 1e-6;#define bug cout<<88888888<<endl;#define debug(x) cout << #x" = " << x << endl;LL gcd(LL a, LL b){ return b==0?a:gcd(b, a%b);}int n;LL A[MaxN];void solve(){ int ans = 0; int GCD = A[1]; for(int i=2;i<=n;i++) { GCD = gcd(GCD, A[i]); } if(GCD>1) { cout << "YES\n" << 0 << endl; return; } for(int i=2;i<=n;i++) { if(A[i]%2 && A[i-1]%2) { LL tmp = A[i-1]; A[i-1] = abs(A[i-1] - A[i]); A[i] = abs(tmp + A[i]); ans++; } } for(int i=1;i<=n;i++) { //cout << i << " " << A[i] << endl; if(A[i]%2) { A[i] = abs(2*A[i]); A[i+1] = abs(2*A[i+1]); ans+=2; } } cout << "YES\n" << ans << endl;}int main(){ //std::ios::sync_with_stdio(false); scanf("%d", &n); for(int i=1;i<=n;i++) scanf("%lld", &A[i]); solve(); system("pause");}
0 0
- 数学——CodeForces #410 div.2
- 数学推公式——较难——Codeforces Round #187 (Div. 2)
- 组合数学——Codeforces Round #404 (Div. 2) D. Anton and School
- Codeforces Round #258 (Div. 2) D. Count Good Substrings —— 组合数学
- Codeforces Round #224 (Div. 2)(数学、dfs)
- codeforces #230 (Div. 2) B(简单数学)
- Codeforces Round #282 (Div. 2) B.(数学)
- Codeforces Round #384 (Div. 2)(数学)
- Codeforces 711 E. ZS and The Birthday Paradox(数学)——Codeforces Round #369 (Div. 2)
- Codeforces Round #410 (Div.2) C.Mike and gcd problem-数学
- Codeforces Round #109 (Div. 2) D. Colliders 数学
- Codeforces Round #133 (Div. 2) A. Tiling with Hexagons(数学)
- Codeforces Round #196 (Div. 2) / 337B Routine Problem(数学)
- Codeforces Beta Round #4 (Div. 2) / 4A Watermelon(数学)
- Codeforces #260 (Div. 2) B. Fedya and Maths(数学:水题)
- Codeforces Round #224 (Div. 2) B 数学推理
- Codeforces #261 (Div. 2) C. Pashmak and Buses(数学)
- Codeforces Round #265 (Div. 2)E. Substitutes in Number(数学)
- C++ Primer Chapter 11-2
- react native Android启动页面、修改图标、修改名字、修复启动白屏
- Android Activity或者Fragment 向Adapter实时传递参数
- 使用Maven创建Web应用程序项目(十)
- java算法实践 将list中的元素按照属性分类成树状的map
- 数学——CodeForces #410 div.2
- MTU问题,为何抓包到1514
- Android studio快捷键重写继承父类的抽象方法
- UML之了解
- C++ Primer Chapter 11-3
- Python语言程序设计
- 动态规划练习——怪盗基德的滑翔翼
- C++ Primer Chapter 12-1
- 基于 Redux 的 React Native 应用架构