Skip to content

Instantly share code, notes, and snippets.

@knuu
Last active August 29, 2015 14:21
Show Gist options
  • Save knuu/ab8d51364a7170efe4ae to your computer and use it in GitHub Desktop.
Save knuu/ab8d51364a7170efe4ae to your computer and use it in GitHub Desktop.
SRM509 div.2 500 LuckyRemainder
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