[Seeking Help]Binomial Coefficients with Large Mod

Правка en64, от sibillalazzerini, 2023-01-17 11:41:30

I have been exploring the idea of computing Binomial Coefficients $$$\binom{n}{k}\mod m$$$ where $$$n$$$,$$$k$$$ and $$$m$$$ are all 64 bit integers.ie $$$m$$$ is any integer rather than being a prime. I have found few solutions here where they first factorise $$$m$$$ into $$$p_1^{e_1}p_2^{e_2}...p_t^{e_t}$$$ and then proceed for each $$$p_i^k$$$ in a manner similar to this method on math.stackexchange To elborate:

  1. Express as $$$(\binom{n}{k}\mod p_1^{e_1})\mod p_2^{e_2})$$$ ... $$$\mod p_t^{e_t})$$$
  2. then strip off all the $$$ p_i $$$ and its epponents from n!, k! and (n-k)!
  3. and compute $$$netexpo_i =$$$ total exponents of $$$p_i$$$ in n! — total exponents of $$$p_i$$$ in (n-k)! — total exponents of $$$p_i$$$ in k!
  4. if $$$netexpo_i \ge$$$ total exponents of $$$p_i$$$ in $$$m!$$$ then answer is $$$0$$$, if not proceed
  5. You Repeat step 2-4 for all $$$p_i$$$ in $$$m$$$.
  6. and then result is $$$ n! * ModularInverse((n — k)!) * ModularInverse(k!) * (p_1)^{netexpo_1}* (p_2)^{netexpo_2}... (p_t)^{netexpo_t}$$$
  7. Details of this method for each $$$(\binom{n}{k}\mod p_1^{e_1})$$$ have been discussed on math.stackexchange (at least for cases when each $$$p_i$$$ is 32 bit integer).

However among the solutions on yosupo none of the ones that I checked scale for large 64 bit $$$m$$$. Just changing the data type won't fix as it overshoots memory even when the largest prime factor of $$$m$$$ is 32-bit. So there can be two parts to the problem:

  • m is 64-bit but all of its prime factors are 32-bit
  • m is 64-bit and one of its prime factor is 64-bit

None of the solutions submitted on yosupo addresses either. My understanding is if they are implementing the algorithm similar to the discussion on math.stackexchange just scaling up datatype should work for the first part (ie $$$m$$$ is 64-bit but all of its prime factors are 32-bit). But to my surprise I found even in those cases it overshoots memory(or perhaps my edited code was buggy).

Any suggestions on how we can modify the code to scale to 64 bit for each of $$$n,k,m $$$ assuming that the largest prime factor of $$$m$$$ is within 32-bit(ie the fist part) I should call it out upfront that I have already gone through https://codeforces.net/blog/entry/65178 , https://codeforces.net/blog/entry/12878 , https://codeforces.net/blog/entry/55311 and https://codeforces.net/blog/entry/53039 .

To summarise all the ideas:

  • $$$O(p^2)$$$ Time per Query, $$$O(1)$$$ Memory — initial answer by Aryaman Maithani on math.stackexchange
  • $$$O(p)$$$ Time per Query, $$$O(1)$$$ Memory — proved using polynomial that in a subgroup of size $$$p$$$ product of only the constant term remains and other terms vanish . ie $$$\prod_{i=pk+1}^{p(k+1)-1}(p^2+i) \mod p^2 = (p-1)! \mod p^2 $$$. — updated answer by Aryaman Maithani on math.stackexchange
  • $$$O(p)$$$ Time preprocessing, $$$O(p^{1/2})$$$ Time per Query, $$$O(p^{1/2})$$$ Memory — idea is to break each block of $$$p$$$ into sub-blocks of size $$$p^{1/2}$$$ each and then preprocess for each blocks of $$$O(p^{1/2})$$$. This idea works because $$$\prod_{i=1}^{t}(p^2+p+i) \mod p^2 = \prod_{i=1}^{t}(i) \mod p^2 $$$. The method is useful when we can't afford $$$O(p)$$$ memory like the following two methods.
  • $$$O(p)$$$ Time preprocessing, $$$O(log_{p}{n})$$$ Time per Query, $$$O(p)$$$ Memory — Similar to Granville's idea. Basically the above 3 are also implementation of Granville's but with lesser degree of memorization that what Granville proposed.
  • $$$O(p)$$$ Time preprocessing, $$$O(log_2{n})$$$ Time per Query, $$$O(p)$$$ Memory — Similar to Min25's idea, it uses as Stirling number of the first kind and Lagrange's interpolation to implement it. Since most of the solutions submitted on yosupo use NTTT/FFT I would put them in this category.

Where $$$p$$$ is the largest prime factor of $$$m$$$ and assing it has $$$e_{i}=2$$$ or lesser

Теги binomial coefficients, modulus, implementations

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en69 Английский sibillalazzerini 2023-01-17 12:09:27 2 Tiny change: 'm$ and assing it has' -> 'm$ and assuming it has'
en68 Английский sibillalazzerini 2023-01-17 12:07:36 19 Tiny change: 'category. @SPyofcode might hel' -> 'category. [user:SPyofcode] might hel'
en67 Английский sibillalazzerini 2023-01-17 12:06:50 37 Tiny change: ' category.\n\nWhere' -> ' category. @SPyofcode might help us understand.\n\nWhere'
en66 Английский sibillalazzerini 2023-01-17 12:06:11 83
en65 Английский sibillalazzerini 2023-01-17 11:42:10 1 Tiny change: 'C) use NTTT/FFT I w' -> 'C) use NTT/FFT I w'
en64 Английский sibillalazzerini 2023-01-17 11:41:30 414
en63 Английский sibillalazzerini 2023-01-17 11:08:34 38 Tiny change: 'tor of $m$' -> 'tor of $m$ and assing it has $e_{i}=2$ or lesser'
en62 Английский sibillalazzerini 2023-01-17 11:03:59 74 Tiny change: 'prod_{i=pk}^{p(k+1)}(p^2+i)\ ' -> 'prod_{i=pk+1}^{p(k+1)-1}(p^2+i)\ '
en61 Английский sibillalazzerini 2023-01-17 10:56:30 242 Tiny change: ' $\prod_{i_1}^{t}p^2+' -> ' $\prod_{i=1}^{t}p^2+'
en60 Английский sibillalazzerini 2023-01-17 10:45:57 1016 Tiny change: ', $O(log{N,p})$ Time pe' -> ', $O(log{N}(p))$ Time pe'
en59 Английский sibillalazzerini 2023-01-12 04:36:54 29 Tiny change: 'ially for implementation and of course alternati' -> 'ially for alternati'
en58 Английский sibillalazzerini 2023-01-11 15:26:17 2 Tiny change: 'e alternate ideas. \' -> 'e alternative ideas. \'
en57 Английский sibillalazzerini 2023-01-11 13:26:30 103
en56 Английский sibillalazzerini 2023-01-11 13:17:44 130
en55 Английский sibillalazzerini 2023-01-11 13:10:29 24
en54 Английский sibillalazzerini 2023-01-11 13:08:31 40
en53 Английский sibillalazzerini 2023-01-11 13:06:15 19 Tiny change: 'hin 32-bit.\nI should' -> 'hin 32-bit(ie the fist part)\nI should'
en52 Английский sibillalazzerini 2023-01-11 12:50:33 73
en51 Английский sibillalazzerini 2023-01-11 09:31:19 7 Tiny change: 'erhaps my code was ' -> 'erhaps my edited code was '
en50 Английский sibillalazzerini 2023-01-11 09:29:48 52
en49 Английский sibillalazzerini 2023-01-11 09:27:59 38 Tiny change: 'orise $m$ and then' -> 'orise $m$ into $p_1^{e_1}p_2^{e_2}...p_t^{e_t}$ and then'
en48 Английский sibillalazzerini 2023-01-11 09:24:05 9 Tiny change: 'pe should scale for the f' -> 'pe should work for the f'
en47 Английский sibillalazzerini 2023-01-11 09:22:42 8
en46 Английский sibillalazzerini 2023-01-11 09:21:52 7 Tiny change: ' k!\n- if netexpo >= total exp' -> ' k!\n- if $netexpo \ge$ total exp'
en45 Английский sibillalazzerini 2023-01-11 09:21:21 7
en44 Английский sibillalazzerini 2023-01-11 09:19:58 19 Tiny change: ' all the $p_i$s from n!,' -> ' all the $ p_i $ s from n!,'
en43 Английский sibillalazzerini 2023-01-11 09:19:04 15 Tiny change: 'f all the factors of pi from n!, ' -> 'f all the $p_i$s from n!, '
en42 Английский sibillalazzerini 2023-01-11 09:18:25 166 Tiny change: '-2-mod-p2)\n\n\n- Ex' -> '-2-mod-p2) To elborate:\n\n\n- Ex'
en41 Английский sibillalazzerini 2023-01-11 08:59:33 4
en40 Английский sibillalazzerini 2023-01-11 08:58:08 60
en39 Английский sibillalazzerini 2023-01-11 08:54:49 1 Tiny change: 'it integer.\n\n\nHow' -> 'it integer).\n\n\nHow'
en38 Английский sibillalazzerini 2023-01-11 08:49:23 45
en37 Английский sibillalazzerini 2023-01-11 08:47:18 2 Tiny change: ' compute netexpo = total exp' -> ' compute $netexpo =$ total exp'
en36 Английский sibillalazzerini 2023-01-11 08:46:34 2 Tiny change: 'factorise m and then ' -> 'factorise $m$ and then '
en35 Английский sibillalazzerini 2023-01-11 08:46:07 2 Tiny change: 'factorise and then ' -> 'factorise m and then '
en34 Английский sibillalazzerini 2023-01-11 06:19:49 122
en33 Английский sibillalazzerini 2023-01-11 04:27:06 48
en32 Английский sibillalazzerini 2023-01-11 04:24:41 46
en31 Английский sibillalazzerini 2023-01-11 03:53:29 93
en30 Английский sibillalazzerini 2023-01-11 03:52:29 37
en29 Английский sibillalazzerini 2023-01-11 03:50:56 167
en28 Английский sibillalazzerini 2023-01-11 03:46:32 88
en27 Английский sibillalazzerini 2023-01-11 03:45:23 175
en26 Английский sibillalazzerini 2023-01-11 03:43:33 2
en25 Английский sibillalazzerini 2023-01-11 03:42:48 91
en24 Английский sibillalazzerini 2023-01-11 03:41:00 282
en23 Английский sibillalazzerini 2023-01-11 03:36:37 5 Tiny change: 'nd (n-k)! to compute ' -> 'nd (n-k)! and compute ' (published)
en22 Английский sibillalazzerini 2023-01-11 03:35:25 121 Tiny change: 'rse(k!) * p_1^(total ex' -> 'rse(k!) * (p_1)^(total ex' (saved to drafts)
en21 Английский sibillalazzerini 2023-01-11 03:26:56 426 Tiny change: 'p_{2}^e_{2+$ ... $p_t' -> 'p_{2}^e_{2}$ ... $p_t'
en20 Английский sibillalazzerini 2023-01-11 03:09:47 30 (published)
en19 Английский sibillalazzerini 2023-01-11 03:08:31 275 (saved to drafts)
en18 Английский sibillalazzerini 2023-01-11 03:02:29 12
en17 Английский sibillalazzerini 2023-01-10 23:00:16 14
en16 Английский sibillalazzerini 2023-01-10 22:50:13 2 Tiny change: 'problem \n- m is 6' -> 'problem \n\n- m is 6'
en15 Английский sibillalazzerini 2023-01-10 22:48:30 307
en14 Английский sibillalazzerini 2023-01-10 22:41:54 8 Tiny change: 'at I have gone thro' -> 'at I have already gone thro' (published)
en13 Английский sibillalazzerini 2023-01-10 22:40:52 8
en12 Английский sibillalazzerini 2023-01-10 22:40:16 137
en11 Английский sibillalazzerini 2023-01-10 22:37:49 2 Tiny change: ' k, m$ plesae do share' -> ' k, m$ please do share'
en10 Английский sibillalazzerini 2023-01-10 22:37:13 4 Tiny change: 'ge 64 bit mod. Just cha' -> 'ge 64 bit $m$. Just cha'
en9 Английский sibillalazzerini 2023-01-10 22:36:36 2 Tiny change: 'e idea of Computing B' -> 'e idea of computing B'
en8 Английский sibillalazzerini 2023-01-10 22:36:21 5 Tiny change: 'n,k,m$ ?\nIf any one ' -> 'n,k,m$ ?\nOr if any one '
en7 Английский sibillalazzerini 2023-01-10 22:36:01 113
en6 Английский sibillalazzerini 2023-01-10 22:34:14 18
en5 Английский sibillalazzerini 2023-01-10 22:33:34 232
en4 Английский sibillalazzerini 2023-01-10 22:31:56 65
en3 Английский sibillalazzerini 2023-01-10 22:31:00 147 Tiny change: 'fficients (n,k)' -> 'fficients $(n,k)$'
en2 Английский sibillalazzerini 2023-01-10 22:27:32 50 Tiny change: ' exploring' -> ' exploring the idea of Computing Binomial Coefficients (n,k)'
en1 Английский sibillalazzerini 2023-01-10 22:26:31 57 Initial revision (saved to drafts)