On the Euclidean algorithm for finding the greatest common divisor principle (GCD) and simple application

First, the Euclidean algorithm and its proof

1. Definitions:

Euclidean algorithm known as Euclidean algorithm, a greatest common divisor of two numbers, calculated as GCD (a, b) = GCD (b, a% b);

2. Proof:

X is an integer of two set a, b (a> = b) the greatest common divisor, then x | a, x | b;

① integer division by a transitive (if divisible x a, x b divisible, then x is divisible a, b arbitrary linear combination) of known x | ab &;

② b factor x is not provided, then x ab and b are not common factors; where x is not a factor, then x ab and b are not common factors; can be derived GCD (a, b) = GCD (b, ab);

③> = b is known from a, a can be expressed as a = b * q + r; q is subtracted a remaining number is the b-r, so GCD (a, b) = GCD (b, a% b );

3. General Code:

(1) recursive form:

                        int gcd(int a,int b){return b?gcd(b,a%b):a;}

(2) iterative form:

                        int gcd(int a,int b){  
                                  for(;;) {  
                                        if(b==0)return a;  
                                        int temp=a%b;  
                                        a=b;
                                        b=temp;
                                  }  
                              }  

4. Several properties:

(1) When the GCD (a, b) = 1, then a, b two prime number.

(2)GCD(a,2a)=a;

(3)GCD(a,0)=a;

(4)GCD(a,b)=GCD(-a,b)=GCD(a,-b)=GCD(-a,-b);

(. 5) the LCM (A, B) the GCD (A, B) = A B (the LCM is a common multiple of the number two);

(6) GCD (n, n + 1) = 1;

prove:

Assuming they are not coprime, a common factor q

n = p1 * 1,n + 1 = p2 * q;n+1 - n = q(p2 - p1)

Then q (p2-p1) = 1; wherein p2, p1 are integers, q> = 2, ranging license. Proved.

Second, related topics

1. [Luo Gu P1372] is graduation season I

Description

The teacher wanted to single out the biggest individuals involved in understanding the extent k graduation party dress rehearsal. But how to choose it? The teacher listed the class of the number 1, 2, ......, n, k and I believe understanding the extent of the individual is their greatest common divisor (this is not a superstition oh). This can be difficult for him, please help out! PS: a number that is the greatest common divisor itself.

Input format: two space-separated positive integers n and k. (N> = k> = 1)

Output format: an integer, the maximum value of understanding.

Solution

1. Note: "the greatest common divisor of a number that is itself": we can consider the nature (2): When the number is a multiple of two, the greatest common divisor of the number that is smaller, then the other case opposing the same range the combination of these two relatively large number of the greatest common divisor.

2. In the discussion of some special cases: when k = 1, ans = n; k = 2, if n is even, the ans = n / 2, if n is odd, ans = (n-1) / 2 ;

3. We have found that the above discussion: satisfying k * a <n, the maximum value of a is the answer. I.e., the selected figures were a, 2a, 3a, ......, ka, so the answer is a / b;

Code

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    cout<<a/b<<"\n";
    return 0;
}

2. [Luo Gu P1170] Bugs Bunny and the Huntsman

Description

Bugs Bunny hiding in the orchard next to the woods. Orchard trees N × M, consisting of a matrix of M rows and N columns, horizontally or vertically adjacent to a distance of two trees. Bugs Bunny in under a fruit tree. Hunter carrying a shotgun into the orchard, he climbed a fruit tree, ready to kill Bugs Bunny. If no other fruit trees between Hunter and Bugs Bunny, hunters can see Bugs Bunny. Now known hunters and Bugs Bunny location, location written procedures to determine where the rabbit is safe.

Input format: first row n, expressed n (n ≤ 100,000) sets of data, each data of the first two acts of positive integers ax and ay, indicating the position of the hunter, a second behavior bx and by two positive integers, Bugs represents a position (1 ≤ ax, ay, bx, by ≤ 100,000,000).

Output Format: A total of n lines, each behavior "yes" or "no" expressed Bugs Bunny's position is safe.

Solution

1. After reading the title we can simplify the topic: whether there is another integer coordinates are seeking two points on the coordinate point integer determined straight line between two points;

2. We can then Hunter coordinates as the origin coordinate system is established, provided the hunter (x1, y1), rabbit (x2, y2), then we changed the rabbit coordinates (x2-x1, y2-y1);

3. So how do you determine that no other point integer coordinates between the origin and the point of it? By drawing we can find, as long as the coordinates of the point that is relatively prime to meet the requirements, by the nature of (1) is equivalent to the known GCD (x, y) = 1;

4. We know that by the nature of (4), two symbol number thereof does not affect the number of coordinates taking the absolute value of the greatest common divisor so recalculation;

5. Note that this problem plurality of sets of data;

Code

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}   //GCD;
int main(){
    int n,x1,x2,y1,y2,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;++i){
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if(x1==x2&&y1==y2){  //特判,两者坐标重合时,GCD=0,而兔子有危险;
            printf("no\n");
            continue;
        }
        x2=abs(x2-x1);
        y2=abs(y2-y1);
        if(gcd(x2,y2)==1)printf("no\n");
        else printf("yes\n");
    }
    return 0;
} 

3. [Luo Gu P2651] Add parentheses III

Description

Now gives an expression of the form a1 / a2 / a3 /.../ an; if computed directly, that is, except the last one, such as 1/2/1/4 = 1/8. A small fraction, however see a very uncomfortable feeling, I hope to add some brackets so that it becomes an integer. One possible approach is to (1/2) / (1/4) = 2. Now given this expression, to inquire whether you can make it an integer by adding some parentheses to change the order of operations.

Input formats: a test point there will be multiple expressions. The first line of t, indicates the number of expressions. For each expression, a first row is n, the number of second row n, denotes the i-th ai.

Output format: t output lines. For each expression, if you can make it into an integer by adding brackets to change the order, then the output "Yes", otherwise a "No"

Solution

1. We can see that in order to make the results as an integer, we should minimize the maximum denominator, molecules;

2.那么我们发现,a2无论如何都是在分母上的,那么我们这样添加括号即可:a1/(a2/a3/.../an)=a1a3...*an/a2,此时满足分母最大,分子最小;

3.那么我们需要进行约分:对每一个分子都和分母求一次GCD,每次求后令分母除以GCD,到最后一项时若分母=1,则结果为整数;

4.注意本题有多组数据;

Code

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;} //GCD
int main(){
    int t,n,i,j;
    scanf("%d",&t);
    for(i=1;i<=t;++i){
        scanf("%d",&n);
        int a[n+1]={};
        for(j=1;j<=n;++j) scanf("%d",&a[j]);
        a[2]/=gcd(a[1],a[2]);
        for(j=3;j<=n;++j) a[2]/=gcd(a[2],a[j]);
        if(a[2]==1)printf("Yes\n");
        else printf("No\n");
    }
    return 0; 
} 

[洛谷P1029]最大公约数与最小公倍数问题

题解随笔:http://www.cnblogs.com/COLIN-LIGHTNING/p/8514163.html

[CodePlus 2017 11月赛]晨跑

题解随笔:http://www.cnblogs.com/COLIN-LIGHTNING/p/8514190.html

[BZOJ 2257][JSOI 2009] 瓶子和燃料

题解随笔:http://www.cnblogs.com/COLIN-LIGHTNING/p/8995031.html

Guess you like

Origin www.cnblogs.com/lixuejian/p/12467008.html