Skip to content

Instantly share code, notes, and snippets.

@nullhack
Created November 2, 2016 12:15
Show Gist options
  • Save nullhack/29c7fb92d97d7cead2276de0a6334d54 to your computer and use it in GitHub Desktop.
Save nullhack/29c7fb92d97d7cead2276de0a6334d54 to your computer and use it in GitHub Desktop.
How many 18-digit number is there such that no digit occurs more than 3 times. All numbers start with a non-zero digit.
"""Licensed under GPLv3 <http://www.gnu.org/licenses/>.
Authors:
Santiago Alessandri
Eric Lopes
"""
helpTable = {
(1, 5) : 5,
(1, 4) : 6,
(1, 3) : 7,
(1, 2) : 8,
(1, 1) : 9,
(1, 0) : 10,
}
def s(*v):
if v in helpTable:
return helpTable[v]
if v[0]==1:
return helpTable[v[:2]]
result = 0
cant = 10 - v[1]
if v[2]:
result += v[2] * s(v[0] - 1, v[1] + 1, v[2] - 1, v[3])
cant -= v[2]
if v[3]:
result += v[3] * s(v[0] - 1, v[1], v[2] + 1, v[3] - 1)
cant -= v[3]
if cant:
result += cant * s(v[0] - 1, v[1], v[2], v[3] + 1)
helpTable[v] = result
return result
if __name__ == '__main__':
length = 17
result = 9 * s(length, 0, 0, 1)
print("The result is:", result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment