-
-
Save nishio/d1c19bf60b0a978a1a7958776c83b6e0 to your computer and use it in GitHub Desktop.
This file contains 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
import numpy as np | |
MOD = 10 ** 9 + 7 | |
assert ((MOD - 1) ** 2).bit_length() == 60 | |
N = 20 | |
assert (N * (MOD - 1) ** 2).bit_length() == 65 | |
x = np.array([MOD - 1] * N, dtype=np.int64) | |
assert x.dot(x) % MOD == 417656019 # incorrect answer | |
upper = x // (2 ** 15) | |
lower = x % (2 ** 15) | |
r30 = (2 ** 30) % MOD | |
r15 = (2 ** 15) % MOD | |
ret = ( | |
lower.dot(lower) % MOD + # max: N * 2 ** 30 | |
lower.dot(upper) * 2 * r15 % MOD + # max: N * 2 ** 46 | |
upper.dot(upper) * r30 % MOD # max: N * 2 ** 57, safe?? | |
) % MOD | |
assert ret == N # correct answer | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment