Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L? Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least c...
class Solution: def subarrayLCM(self, nums: List[int], k: int) -> int: # def lcm(x, y): # 穷举法 # greater = x if x > y else y # 获取最大的数 # while(True): # if((greater % x == 0) and (greater % y == 0)): # return greater # greater += 1 # return x * ...
https://www.hackerrank.com/contests/w8/challenges/john-and-gcd-list 简单题,GCD和LCM。 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
用扩展GCD可以求出一组解,但这组解的绝对值的和不一定是最小的,那么可以通过x+=k*b/gcd,y-=k*a/gcd来尝试收缩出一组绝对值的和最小的解,之后记录res=K-abs(x)-abs(y),如果res是偶数,那么左右来回移动用完剩下的步子就行,奇数的话,讨论a/gcd+b/gcd的奇偶性,如果是奇数,那么可以一次收缩达成上一中...
ll lcm(ll a, ll b){returna * b /gcd(a, b); }voidsolve(ll x){ ll k= x - a %x;if(lcm(a + k, b + k) <ans) { ans= lcm(a + k, b +k); ansk=k; } }intmain() { cin>> a >>b; ansk=0; ans=lcm(a, b); ...