java - calculate the modulus of a fraction with big numbers -
i have 2 big numbers (<10^9) n
, m
. have calculate [(n-1+m)!]/[(n-1)!*m!]
if answer big have modulus (10^9+7)
. (ex : n%mod if n large mod).
int mod = 1000000000+7;
i calculated below. (i calculated upper half , bottom half separately)
long up=1; (long = a+b-1; i>=math.max(a, b); i--) { up*=i%mod; } up=up%mod; long down=1; (long = math.min(a, b); i>0; i--) { down*=i%mod; }
then print answer
system.out.println((up%mod/down%mod)%mod);
is approach correct. gives correct out put.
i know (a*b*c)%d == [(a%d)*(b%d)*(c%d)]%d
(correct me if i'm wrong)
so there way calculate [a/b]%d
?
(a/b) % c != ((a%c) / (b%c) % c)
instead use recursive formula of binomial coefficient calculate modulus
(n, k) = (n - 1, k - 1) + (n - 1, k)
in case be
(n - 1 + m, m) % mod = ((n - 2 + m, m - 1) % mod + (n - 2 + m, m) % mod) % mod
use above recursion result.
Comments
Post a Comment