There are given 0 ≤ k ≤ n ≤ 500000
, 0 ≤ l ≤ m ≤ 500000
.
I need co calculate GCD(C(n, k), C(m, l))
modulo 10^9 + 7.
My attempt:
I thought about tricks with fourmula:
C(n, k) = n*(n-1)*...*(n-k+1) / k!
For example, suppose l >= k:
GCD( C(n, k), C(m, l) ) =
= GCD( n*(n-1)*...*(n-k+1) / k!, m*(m-1)*...*(m-l+1) / l! ) =
= GCD( n*(n-1)*...*(n-k+1)*(k + 1)*...*l/ l!, m*(m-1)*...*(m-l+1) / l! ) =
= GCD( n*(n-1)*...*(n-k+1)*(k + 1)*...*l, m*(m-1)*...*(m-l+1) ) / l!
Inversing l!
with binary exponentiation to 10^9 + 5 is fine, but I don't know how to continue.
This (k + 1)*...*l
part ruins everything. I can find some benefit if there is intersection between multipliers of
n*(n-1)*...*(n-k+1)
and m*(m-1)*...*(m-l+1)
,
but if not, whole GCD must be contained in this (k + 1)*...*l
part.
And what's next? Using native GCD algorithm for remaining multipliers? Too long again because of need to calculate product of them, so that manipulations above look meaningless.
Am I on a right way? Is there some trick to come up with this problem?