洛谷 CF798C Mike and gcd problem

Ok...

 

Topic link: https: //www.luogu.org/problemnew/show/CF798C

 

This question will be the first to write gcd .. also a genius for finding similar law now ...

 

Operational problems is carried out on the basis of two numbers:

Then we might consider only the operation of two numbers, handwritten difficult to find several sets of data, all written out two numbers AB, the task will be completed within at most two operations. Then we can consider its nature:

 

AB no more than four cases of two numbers:

Odd, odd ---------------> operation after an even number, an even number

Odd, even ---------------> After the operation becomes odd, odd

Even, odd ---------------> After the operation becomes odd, odd

An even number, the even ---------------> operation after an even number, an even number

 

and so:

If the original two numbers are even, then the operand is 0.

If the two original numbers are odd, then operand 1.

If the original two numbers is an odd one even, then the operand 2.

It will not appear result (3, 6) of this situation, unless the original sequence is like this.

 

So in the end plus a few special sentence can be:

If n == 1, which must gcd is 1, it can be output directly;

If the operation has not before gcd greater than 1, and can direct output;

Other cases can be discussed parity ... (note that this question does not exist without a solution, as already explained) ....

 

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 int n, a[100005], ans;
 7 
 8 int gcd(int a, int b){
 9     if(b == 0)
10         return a;
11     return gcd(b, a % b);
12 }
13 
14 int main(){
15     scanf("%d", &n);
16     for(int i = 1; i <= n; i++)
17         scanf("%d", &a[i]);
18     if(n == 1){
19         printf("YES\n0\n");
20         return 0;
21     }//特判 
22     int now = gcd(a[1], a[2]);
23     for(int i = 3; i <= n; i++)
24         now = gcd(a[i], now);//gcd 
25     if(now != 1){
26         printf("YES\n0\n");
27         return 0;
28     }//特判 
29     else{
30         a[n + 1] = 0;
31         for(int i = 1; i <= n; i++){
32             if(a[i] % 2 == 1 && a[i + 1] % 2 == . 1 ) {
 33 is                  ANS ++ ;
 34 is                  A [I + . 1 ] = 0 ; // has been operated into even, it can be assigned to any even number of 
35              }
 36              the else  IF (A [I]% 2 == . 1 && A [I + 1 ]% 2 == 0 ) // an odd one even 
37 [                  ANS + = 2 ;
 38 is          }
 39          the printf ( " YES \% n-D \ n- " , ANS);
 40      }
 41 is      return 0;
42 }
AC Code

 

Guess you like

Origin www.cnblogs.com/New-ljx/p/11237320.html