Codeforces Round 410 Div2 Problem C

Mike and gcd problem

Solution sketch

這個講解比官方解析棒很多!

大體而言,全部都是偶數,一定ok。其餘,觀察兩奇數需要一次操作即可變成偶數偶數,一奇數一偶數需要兩次操作。

注意 gcd(all number) != 1 這個特判不能省下 (Wrong answer on test 40)。

AC code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
void change(int &a, int &b)
{
int tmp = b;
b = a + b;
a = a - tmp;
}
int gcd(int a, int b)
{
return a == 0 ? b : gcd(b % a, a);
}
int main()
{
int n;
scanf("%d", &n);
vector<int> inp;
int g = 0; // default value can't be 1, come on bro
for (int i = 0; i < n; i++) {
int tmp;
scanf("%d", &tmp);
inp.push_back(tmp);
g = gcd(g, tmp);
}
int cnt = 0;
for (int i = 1; i < n; i++) {
if ((inp[i - 1] % 2 == 1) && (inp[i] % 2 == 1)) {
change(inp[i - 1], inp[i]);
cnt++;
}
}
for (int i = 1; i < n; i++) {
if ((inp[i - 1] % 2 == 1) || (inp[i] % 2 == 1)) {
change(inp[i - 1], inp[i]);
change(inp[i - 1], inp[i]);
cnt += 2;
}
}
printf("YES\n");
printf("%d\n", g > 1 ? 0 : cnt);
return 0;
}