ACM.GCD and LCM

Euclidean algorithm
modulo operation rules of operation
(A + B)% P = (P + A% B% P)% P
(A - B)% P = (% P A - P B%)% P
( B * A)% P = (P * A% B% P) P%
A ^ B% P = ((A% P) ^ B)% P
, also known as Euclidean
code for process
a non-recursive wording

int gcd(int a,int b)
{
int r=a%b;
while(r)
{a=b;
b=r;
r=a%b;
}
return b;
}

2 recursive wording

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

The least common multiple LCM (A, B) = A B / GCD (A, B)
some properties of the least common multiple and the greatest common divisor
GCD (A, B) = GCD (B, ab &)
GCD (mA, MB) m =
GCD ( a, b), m is a natural number
GCD (a + MB, B) = GCD (a, B)
m = GCD (a, B) if gcd (a / m, b / m) = gcd (a, b) / m
GCD (A, LCM (B, C)) = LCM (GCD (A, B), GCD (A, C))
LCM (A, GCD (B, C)) = GCD (LCM (A, B) , lcm (a, c))

A .NEFU-992
See the GCD
Problem: B
Time Limit: 1000ms
Memory Limit: 65536k
the Description
There are three positive integers a, b, c (0 < a, b, c <10 ^ 6), wherein c is not equal to b. If the greatest common divisor of a and c is b, it is known that a and b, the minimum required to meet the conditions of c.
Input
per line two positive integers a, b.
Output
corresponding to the output c, each set of test data row for
the Sample the Input
. 6 2
12 is. 4
the Sample Output
. 4
. 8
I is incremented each time the value is set to b

#include <bits/stdc++.h>
using namespace std;
int i;
int main()
{
    int a,b,c,g;
    while(cin>>a>>b)
    {
        int min=999999;
      for(i=b*2;;i+=b)
      {
          if(__gcd(i,a)==b){min=i;
          break;
      }}
      cout<<min<<endl;
    }
    return 0;
}

.NEFU-764 two
greatest common divisor of the number of the plurality
Problem: C
Time Limit: 1000ms
Memory Limit: 65536k
the Description
Given n (n <= 10) positive integer, your job is to find their greatest common divisor of all the data They are within the scope of long long.
Input
input data a plurality of sets, each set of two rows, first row n, represents the number of digits to be entered, then the second row has n positive integers.
Output
outputs a number, i.e., the n number of the greatest common divisor.
The Input the Sample
. 5
2. 8. 6. 4 10
2
13 is 26 is
the Sample the Output
2
13 is found sequentially every two numbers can be the greatest common factor, similar Fibonacci number

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n;
    while(cin>>n)
    {
        long long ans=1,i;
        long long a[n];
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
        }
         for(i=1;i<=n-1;i++)
         {
             a[i+1]=__gcd(a[i],a[i+1]);
         }
        cout<<a[n-1]<<endl;
    }
    return 0;
}

.NEFU-765 three
least common multiple of the number of the plurality
Problem: D
Time Limit: 1000ms
Memory Limit: 65536k
the Description
Given n (n <= 10) positive integer, your job is to find the least common multiple thereof, the range of all data They are within long long.
Input
input data a plurality of sets, each set of two rows, first row n, represents the number of digits to be entered, then the second row has n positive integers.
Output
outputs a number, i.e., the n number of the least common multiple.
The Input the Sample
. 5
2. 8. 6. 4 10
2
13 is 26 is
the Sample the Output
120
26 is
the principle above a question

#include <bits/stdc++.h>
using namespace std;
int i;
int main()
{
    long long n;
    while(cin>>n)
    {
        long long ans=1;
        long long a[n];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            ans=(ans*a[i]/__gcd(ans,a[i]));
        }

        cout<<ans<<endl;
    }
    return 0;
}

Five .NEFU-1669
Takagi students factors
Problem: G
Time Limit: 1000ms
Memory Limit: 65535K
the Description
Today Western films classmates tease the students has been Takagi, Takagi students with classmates Western films play such a game. They each want a digital mind, these two figures are x and y (1 <= x, y <= 1e18), Western films and then let the students say a number of factors both integers x total, is the factor y . Due to Western films and Takagi tacit agreement, so the two of them wanted to ensure the greatest common factor of x and y number does not exceed 1e9. This problem has stumped classmates Western films, Western films you can help the students told him the answer?
Input
single set of input
data per line, comprising two integers x and y (1 <= x, y <= 1e18), to ensure that gcd (x, y) <= 1e9.
Output
Output both x factor y is an integer number of factors. Output line representing
the Sample the Input
12 is 36
the Sample the Output
. 6

Since x and y are large numbers, calculating undesirable direct violence, the maximum common divisor of two numbers should be the greatest common factor, other factors are factor greatest common factor, so as to seek the maximum Transformation the number of factors common factor.

#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x , long long y)
{
    if(y == 0) return x;
    else return gcd(y , x % y);
}
int main()
{
       long long m,n,i;
       cin>>m>>n;
       int daan=0;
       long long q=gcd(m,n);
       for(i=1;i*i<q;i++)            //只判断一半
       {
           if(q%i==0)daan+=2;      //此处每次加上2,因为循环条件判断的是一半,每次加一对数
       }
       if(i*i==q)daan++;
       cout<<daan<<endl;
       return 0;
}
Released five original articles · won praise 0 · Views 47

Guess you like

Origin blog.csdn.net/abysswatcher1/article/details/103911326