Repository navigation
Template for modulo operations #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bjorn-martinsson
wants to merge
7
commits into
cheran-senthil:master
Choose a base branch
from
bjorn-martinsson:modmul_template
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eef3b7e
Added template for modulo calculations
bjorn-martinsson 54ea14c
Fixed erroneous inequality
bjorn-martinsson ccb5210
General improvements
bjorn-martinsson 4766516
Fixed description
bjorn-martinsson 0a990ea
Changed formatting, comments and variable names.
bjorn-martinsson 8eaee24
Merge branch 'cheran-senthil:master' into modmul_template
bjorn-martinsson 138687a
Modernized old PR
bjorn-martinsson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """ | ||
| A template for usefull stuff involving prime modulo. It contains: | ||
| 1. Calculation of factorial, inverse factorial and modular inverse | ||
| for all integers < maxN in O(maxN) time. | ||
| 2. Calculate n choose k in O(1) time using precalculated fac and inv_fac. | ||
| 3. Multiply matrices mod MOD. | ||
| """ | ||
| MOD = 10 ** 9 + 7 # needs to be prime! | ||
| maxN = 10 ** 6 # needs to be <= MOD | ||
|
|
||
| modmul = lambda a,b,c=0:(a*b + c) % MOD | ||
|
|
||
| """ Precalculate factorial, inverse factorial and modular inverse """ | ||
|
|
||
| def mod_precalc(n): | ||
| """ Calculates fac, inv_fac and (modular) inv for i < n in O(n) time """ | ||
| assert n <= MOD | ||
|
|
||
| fac = [1] * n | ||
| for i in range(2, n): | ||
| fac[i] = modmul(fac[i - 1], i) | ||
|
|
||
| inv_fac = [pow(fac[-1], MOD - 2, MOD)] * n | ||
| for i in reversed(range(1, n)): | ||
| inv_fac[i - 1] = modmul(inv_fac[i], i) | ||
|
|
||
| inv = [modmul(inv_fac[i], fac[i - 1]) for i in range(n)] | ||
|
|
||
| return fac, inv_fac, inv | ||
|
|
||
| fac, inv_fac, inv = mod_precalc(maxN) | ||
|
|
||
|
|
||
| """ Useful functions involving modulo """ | ||
|
|
||
| def choose(n, k): | ||
| """ Calculate n choose k in O(1) time """ | ||
| if k < 0 or k > n: | ||
| return 0 | ||
| return modmul(modmul(fac[n], inv_fac[k]), inv_fac[n - k]) | ||
|
|
||
| def matrix_modmul(A, B): | ||
| """ Multiplies matrices A and B mod MOD""" | ||
| assert len(A[0]) == len(B) | ||
| C = [] | ||
| for Ai in A: | ||
| tmp = [0] * len(B[0]) | ||
| for k in range(len(B)): | ||
| Aik = Ai[k] | ||
| Bk = B[k] | ||
| for j in range(len(Bk)): | ||
| tmp[j] = modmul(Aik, Bk[j], tmp[j]) | ||
| C.append(tmp) | ||
| return C | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have something for this elsewhere, so let's remove that too.