I have done a lot of work on this but couldnt find the answer for larger test cases
In mathematics, binomial coefficients are a family of positive integers that occur as coefficients in the binomial theorem. C(n,k) denotes the number of ways of choosing k objects from n different objects.
However when n and k are too large, we often save them after modulo operation by a prime number P. Please calculate how many binomial coefficients of n become to 0 after modulo by P.
The first of input is an integer T , the number of test cases.
Each of the following T lines contains 2 integers, n and prime P.
For each test case, output a line contains the number of \tbinom nks (0<=k<=n) each of which after modulo operation by P is 0.
3
2 2
3 2
4 3
1
0
1
i have completed this by using remainder theorem in binomial coeffecients
#C(n,m) mod p
#n=l*p+t
#m=m*p+s
#then C(n,r) mod p=0 when (t<s or t=0)
inp1 = raw_input()
N=int(inp1)
result=[]
for i in range(N):
inp = raw_input()
a, b = [long(x) for x in inp.split(' ')]
j=0
#n=lp+t
t=a%b
t=t+1
l=a/b
j=l*(b-t)
result.append(j)
for i in range(N):
print result[i]
above condition is satisfied for small numbers
sample test case
n=18794630773460178101742670493883191390743597826923079533199667903991430393463990462500322752062869664969026409174076912867222446746310051635510258172105034070506806228555577773599018819952185016092141574603857551738968553782672643049704163674318579695215402562964641111900657274032612661770435202254364177910753450214277150377049334509093906874400306682949871260040370515062243982543271073443613028133844603853807066311479739789908983610180228625059956919930500586048799830730348503994503184106117058
p= 177080341
my output is
2296508200406431043037217853758906667313789305876262916681342008001095232916608835588093082038358975456171743147798282523487485386336553910277635713985851142371010771392102277877640275384064735398164190968282400640398659343189330639672472613876688344609533340662724884373078840434716174167873375700938597411315754265893890741730938202927739246687866166994143001482839656000969674716959277820008958538229366474207686428750934149471409162993083541475267772950721250234982686128039722553219836725588488
expected output is
18794630773460178101742635959946548665553041135822283621364103266511586625905046107130878283695016799933475657268010472422112556606280021574002866456544310584537519228161286450725015989697306855581489155139723025246780552510467580791551824827637581156204185887378181074365453150481221030356075255000460025095384537510111086396988416046942446776262625161326885418101128327416784858513888616089287333560469336094431461981368825028447505354473183546488856594449627370807707483671453574074503184106117059
You can look at it from the other end: How many nCr
are not divisible by p
? There's a rather simple formula for that.
The binomial coefficient nCr
is given by
nCr = n! / (r! * (n-r)!)
so the multiplicity v_p(nCr)
of p
in nCr
- the exponent of p
in the prime factorisation of nCr
- is
v_p(nCr) = v_p(n!) - v_p(r!) - v_p((n-r)!)
The multiplicity of p
in n!
can be easily determined, a well known way to calculate it is
v_p(n!) = ∑ floor(n/p^k)
k > 0
If you look at this formula considering the base-p
expansion of n
, you can see that
v_p(n!) = (n - σ_p(n)) / (p - 1)
where σ_p(k)
is the sum of the digits of the base-p
representation of k
. Thus
v_p(nCr) = (n - σ_p(n) - r + σ_p(r) - (n-r) + σ_p(n-r)) / (p - 1)
= (σ_p(r) + σ_p(n-r) - σ_p(n)) / (p - 1)
nCr
is not divisible by the prime p
if and only if the addition of r
and n-r
has no carry in base p
.
Because that is exactly when σ_p(a + b) = σ_p(a) + σ_p(b)
. A carry in the addition occurs when the sum of corresponding digits of a
and b
(plus possibly the carry-in if there was already a carry produced for less significant digits) is >= p
, then the corresponding digit in the sum is reduced by p
and the next more significant digit increased by 1, reducing the digital sum by p - 1
.
We have a carry-free addition n = r + (n-r)
in base p
if for each digit d_k
in n
's base-p
expansion, the corresponding digit of r
is less than or equal to d_k
. The admissible choices of the digits of r
are independent, hence the total number is the product of the count of choices for the individual digits.
The number of nCr
not divisible by the prime p
is
ND(n,p) = ∏(d_k + 1)
where the d_k
are the digits in n
's base p
expansion,
m
n = ∑ d_k * p^k
k=0
Since there are n+1
(nonzero) binomial coefficients nCr
for a given n
, the number of (nonzero) binomial coefficients nCr
divisible by p
is
m
n + 1 - ∏ (d_k + 1)
k=0
with the above base p
expansion of n
.
Using Michael's example n = 10
, p = 3
,
10 = 1*3^0 + 0*3^1 + 1*3^2
so there are (1+1)*(0+1)*(1+1) = 4
coefficients 10Cr
not divisible by 3 and 10 + 1 - 4 = 7
divisible by 3.
def divisibleCoefficients(n,p):
m, nondiv = n, 1
while m > 0:
nondiv = nondiv * ((m % p) + 1)
m = m // p
return (n + 1 - nondiv)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With