Last active
August 29, 2015 14:21
-
-
Save knuu/ab8d51364a7170efe4ae to your computer and use it in GitHub Desktop.
SRM509 div.2 500 LuckyRemainder
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
class LuckyRemainder: | |
def getLuckyRemainder(self, X): | |
N = str(X) | |
D = len(N) | |
pascalTri = [[0]*D for _ in range(D)] | |
for i in range(D): | |
pascalTri[i][0] = 1 | |
pascalTri[i][i] = 1 | |
for j in range(1, i): | |
pascalTri[i][j] = (pascalTri[i-1][j-1] + pascalTri[i-1][j]) % 9 | |
ans = 0 | |
for i, s in enumerate(N): | |
for j in range(D-i): | |
ans += (pascalTri[D-i-1][j] * int(s) * (10**j) * (2**i)) % 9 | |
return ans % 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment