The way it is, I'm a new sprout, then sprout new beginner number theory. qvq
This article is about the difficulty of gcd ~ Mobius inversion, maybe I'll write a little combination count, the inclusion-exclusion principle, knowledge of linear algebra, of course, I guess I will not, because the cuckoo.
Taking mathematical proof based, code is easy to understand, all the operations of computer algorithms prevail. qvq ( other people's code the wind was not cancer )
GCD&LCM
That is the greatest common divisor and least common multiple, presented here Euclid seek gcd.
Set (a, b) represents gcd (a, b).
Prove that: gcd (a, b) = gcd (b, a% b)
prove:
Set a = k1 * c, b = k2 * c and (k1, k2) = 1.
Then (a, b) = c.
Provided k3 = a / b (according to computer algorithms, rounded down)
Provisions a% b = a-k3 * b.
There is a% b = a-k3 * b = k1 * c-k3 * k2 * c = (k1-k2 * k3) * c.
若(k2,k1-k2*k3)=1,(b,a%b)=c,则(a,b)=(b,b%a).
If (k2, k1-k2 * k3) ≠ 1, provided k2 = m1 * d, k1-k2 * k3 = m2 * d.
Then (a, b) = (k3 * m1 * d * c + m2 * d * c, m1 * d * c) = dc ≠ c, the assumption does not hold.
In summary, (a, b) = (b, a% b).
QED.
In particular, when b = 0 when, (a, b) = a.
LCM method of finding:. Lcm (a, b) = a * b / gcd (a, b) is not demonstrated.
Code:
#include<cstdio> int a,b; int gcd(int a,int b) { return (b==0)?a:gcd(b,a%b); } int lcm(int a,int b) { return a*b/gcd(a,b); } int main() { scanf("%d%d",&a,&b); printf("GCD=%d LCM=%d\n",gcd(a,b),lcm(a,b)); return 0; }