Key Storage (Codeforces 1267K)
Tags: Math, combinatorics

Karl is developing a key storage service. Each user has a positive integer key.Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions. For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders $$$[1, 2, 1]$$$ and has the fingerprint multiset $$$\{1, 1, 2\}$$$.Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of $$$\{0, 0, 0, 0, 2, 3, 3, 4\}$$$. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 50\,000$$$) — the number of commonly used keys to examine. Each of the next $$$t$$$ lines contains one integer $$$k_i$$$ ($$$1 \le k_i \le 10^{18}$$$) — the key itself. 

Output: For each of the keys print one integer — the number of other keys that have the same fingerprint. 

Note: The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders $$$[1, 1, 2]$$$. So both numbers have the fingerprint multiset $$$\{1, 1, 2\}$$$.

Sample Inputs:
3
1
11
123456

Sample Outputs:
0
1
127

Solution (Python):
f = [1, 1]
for i in range(2, 21):
    f.append(f[-1] * i)

def C(n, r):
    if n < r:
        return 0
    return f[n] // (f[n - r] * f[r])

class KeyProblem:

    def __init__(self, x):
        self.multiplicity = {}
        self.calcReminders(x)
        ans = self.solve()
        if 0 in self.multiplicity:
            self.multiplicity[0] -= 1
            self.total -= 1
            ans -= self.solve()
        self.answer = ans - 1

    def solve(self):
        ans = 1
        rep = 0
        for i in sorted(self.multiplicity.keys(), reverse=True):
            if i == 0:
                ans *= C(max(0, self.total - i - rep), self.multiplicity[i])
            else:
                ans *= C(max(0, self.total + 1 - i - rep), self.multiplicity[i])
            rep += self.multiplicity[i]
        return ans

    def calcReminders(self, val):
        d = 2
        while val:
            r = val % d
            if r in self.multiplicity:
                self.multiplicity[r] += 1
            else:
                self.multiplicity[r] = 1
            val = val // d
            d += 1
        self.total = d - 2

    def __str__(self):
        return str(self.answer)
if __name__ == '__main__':
    total = int(input())
    for _ in range(total):
        print(KeyProblem(int(input())))

Solution (Python):
f = [1, 1]
for i in range(2, 21):
    f.append(f[-1] * i)

def C(n, r):
    if n < r:
        return 0
    return f[n] // (f[n - r] * f[r])

class KeyProblem:

    def __init__(self, x):
        self.multiplicity = {}
        self.calcReminders(x)
        ans = self.solve()
        if 0 in self.multiplicity:
            self.multiplicity[0] -= 1
            self.total -= 1
            ans -= self.solve()
        print(ans - 1)

    def solve(self):
        ans = 1
        rep = 0
        for i in sorted(self.multiplicity.keys(), reverse=True):
            if i == 0:
                ans *= C(max(0, self.total - i - rep), self.multiplicity[i])
            else:
                ans *= C(max(0, self.total + 1 - i - rep), self.multiplicity[i])
            rep += self.multiplicity[i]
        return ans

    def calcReminders(self, val):
        d = 2
        while val:
            r = val % d
            if r in self.multiplicity:
                self.multiplicity[r] += 1
            else:
                self.multiplicity[r] = 1
            val = val // d
            d += 1
        self.total = d - 2
if __name__ == '__main__':
    total = int(input())
    for _ in range(total):
        KeyProblem(int(input()))

Solution (Python):
fact = [1, 1]
for i in range(2, 30):
    fact.append(fact[-1] * i)
for i in range(int(input())):
    (a, r, d) = (int(input()), [], 2)
    while a:
        r.append(a % d)
        a //= d
        d += 1
    m = [0] * (len(r) + 2)
    for i in r:
        m[i] += 1
    (z, o) = (m[0], m[1])
    (p, c, d) = (z + o, 1, fact[max(1, z)] * fact[max(1, o)])
    for i in range(2, len(r) + 2):
        c *= p
        t = m[i]
        if t:
            d *= fact[t]
        p += m[i] - 1
    n = int(c / d)
    if z:
        z -= 1
        (p, c, d) = (z + o, 1, fact[max(1, z)] * fact[max(1, o)])
        for i in range(2, len(r) + 1):
            c *= p
            t = m[i]
            if t:
                d *= fact[t]
            p += m[i] - 1
        print(n - int(c / d) - 1)
    else:
        print(n - 1)