Salazar Slytherin's Locket (Codeforces 855E)
Tags: DP, bitmasks

Harry came to know from Dumbledore that Salazar Slytherin's locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black's mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry's former Defense Against the Dark Arts teacher. Harry, Ron and Hermione are infiltrating the Ministry. Upon reaching Umbridge's office, they observed a code lock with a puzzle asking them to calculate count of magic numbers between two integers l and r (both inclusive). Harry remembered from his detention time with Umbridge that she defined a magic number as a number which when converted to a given base b, all the digits from 0 to b - 1 appear even number of times in its representation without any leading zeros.You have to answer q queries to unlock the office. Each query has three integers bi, li and ri, the base and the range for which you have to find the count of magic numbers.

Input: First line of input contains q (1 ≤ q ≤ 105) — number of queries.Each of the next q lines contain three space separated integers bi, li, ri (2 ≤ bi ≤ 10, 1 ≤ li ≤ ri ≤ 1018).

Output: You have to output q lines, each containing a single integer, the answer to the corresponding query.

Note: In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get:   4 = 1002,  5 = 1012,  6 = 1102,  7 = 1112,  8 = 10002,  9 = 10012. Out of these, only base 2 representation of 9 has even number of 1 and 0. Thus, the answer is 1.

Sample Inputs:
2
2 4 9
3 1 10

2
2 1 100
5 1 100

Sample Outputs:
1
2

21
4

Solution (Python):
(standard_input, packages, output_together) = (1, 0, 0)
(dfs, hashing, read_from_file) = (0, 0, 0)
de = 1
if 1:
    if standard_input:
        import io, os, sys
        input = lambda : sys.stdin.readline().strip()
        import math
        inf = math.inf

        def I():
            return input()

        def II():
            return int(input())

        def MII():
            return map(int, input().split())

        def LI():
            return list(input().split())

        def LII():
            return list(map(int, input().split()))

        def LFI():
            return list(map(float, input().split()))

        def GMI():
            return map(lambda x: int(x) - 1, input().split())

        def LGMI():
            return list(map(lambda x: int(x) - 1, input().split()))
    if packages:
        from io import BytesIO, IOBase
        import random
        import os
        import bisect
        import typing
        from collections import Counter, defaultdict, deque
        from copy import deepcopy
        from functools import cmp_to_key, lru_cache, reduce
        from heapq import merge, heapify, heappop, heappush, heappushpop, nlargest, nsmallest
        from itertools import accumulate, combinations, permutations, count, product
        from operator import add, iand, ior, itemgetter, mul, xor
        from string import ascii_lowercase, ascii_uppercase, ascii_letters
        from typing import *
        BUFSIZE = 4096
    if output_together:

        class FastIO(IOBase):
            newlines = 0

            def __init__(self, file):
                self._fd = file.fileno()
                self.buffer = BytesIO()
                self.writable = 'x' in file.mode or 'r' not in file.mode
                self.write = self.buffer.write if self.writable else None

            def read(self):
                while True:
                    b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                    if not b:
                        break
                    ptr = self.buffer.tell()
                    (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
                self.newlines = 0
                return self.buffer.read()

            def readline(self):
                while self.newlines == 0:
                    b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                    self.newlines = b.count(b'\n') + (not b)
                    ptr = self.buffer.tell()
                    (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
                self.newlines -= 1
                return self.buffer.readline()

            def flush(self):
                if self.writable:
                    os.write(self._fd, self.buffer.getvalue())
                    (self.buffer.truncate(0), self.buffer.seek(0))

        class IOWrapper(IOBase):

            def __init__(self, file):
                self.buffer = FastIO(file)
                self.flush = self.buffer.flush
                self.writable = self.buffer.writable
                self.write = lambda s: self.buffer.write(s.encode('ascii'))
                self.read = lambda : self.buffer.read().decode('ascii')
                self.readline = lambda : self.buffer.readline().decode('ascii')
        sys.stdout = IOWrapper(sys.stdout)
    if dfs:
        from types import GeneratorType

        def bootstrap(f, stack=[]):

            def wrappedfunc(*args, **kwargs):
                if stack:
                    return f(*args, **kwargs)
                else:
                    to = f(*args, **kwargs)
                    while True:
                        if type(to) is GeneratorType:
                            stack.append(to)
                            to = next(to)
                        else:
                            stack.pop()
                            if not stack:
                                break
                            to = stack[-1].send(to)
                    return to
            return wrappedfunc
    if hashing:
        RANDOM = random.getrandbits(20)

        class Wrapper(int):

            def __init__(self, x):
                int.__init__(x)

            def __hash__(self):
                return super(Wrapper, self).__hash__() ^ RANDOM
    if read_from_file:
        file = open('input.txt', 'r').readline().strip()[1:-1]
        fin = open(file, 'r')
        input = lambda : fin.readline().strip()
        output_file = open('output.txt', 'w')

        def fprint(*args, **kwargs):
            print(*args, **kwargs, file=output_file)
    if de:

        def debug(*args, **kwargs):
            print('\x1b[92m', end='')
            print(*args, **kwargs)
            print('\x1b[0m', end='')
pre_process = [None] * 11
for b in range(2, 11):
    length = 1
    cur = 1
    while cur <= 10 ** 18:
        length += 1
        cur *= b
    length -= 1
    pre_process[b] = [[0] * (1 << b) for _ in range(length)]
    for i in range(b):
        pre_process[b][0][1 << i] = 1
    for i in range(length - 1):
        for msk in range(1 << b):
            for new_bit in range(b):
                pre_process[b][i + 1][msk ^ 1 << new_bit] += pre_process[b][i][msk]
ans = []

def main():
    (b, l, r) = MII()

    def f(x):
        vals = []
        while x:
            vals.append(x % b)
            x //= b
        k = len(vals)
        ans = 0
        for i in range(1, k - 1, 2):
            ans += pre_process[b][i][0] - pre_process[b][i - 1][1]
        if k % 2 == 0:
            cur_msk = 0
            for i in range(k - 1, -1, -1):
                for j in range(1 if i == k - 1 else 0, vals[i]):
                    ans += pre_process[b][i - 1][cur_msk ^ 1 << j] if i else 1 if cur_msk == 1 << j else 0
                cur_msk ^= 1 << vals[i]
        return ans
    ans.append(f(r + 1) - f(l))
    return
t = II()
for _ in range(t):
    main()
print('\n'.join(map(str, ans)))

Solution (Python):
(standard_input, packages, output_together) = (1, 0, 0)
(dfs, hashing, read_from_file) = (0, 0, 0)
de = 1
if 1:
    if standard_input:
        import io, os, sys
        input = lambda : sys.stdin.readline().strip()
        import math
        inf = math.inf

        def I():
            return input()

        def II():
            return int(input())

        def MII():
            return map(int, input().split())

        def LI():
            return list(input().split())

        def LII():
            return list(map(int, input().split()))

        def LFI():
            return list(map(float, input().split()))

        def GMI():
            return map(lambda x: int(x) - 1, input().split())

        def LGMI():
            return list(map(lambda x: int(x) - 1, input().split()))
    if packages:
        from io import BytesIO, IOBase
        import random
        import os
        import bisect
        import typing
        from collections import Counter, defaultdict, deque
        from copy import deepcopy
        from functools import cmp_to_key, lru_cache, reduce
        from heapq import merge, heapify, heappop, heappush, heappushpop, nlargest, nsmallest
        from itertools import accumulate, combinations, permutations, count, product
        from operator import add, iand, ior, itemgetter, mul, xor
        from string import ascii_lowercase, ascii_uppercase, ascii_letters
        from typing import *
        BUFSIZE = 4096
    if output_together:

        class FastIO(IOBase):
            newlines = 0

            def __init__(self, file):
                self._fd = file.fileno()
                self.buffer = BytesIO()
                self.writable = 'x' in file.mode or 'r' not in file.mode
                self.write = self.buffer.write if self.writable else None

            def read(self):
                while True:
                    b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                    if not b:
                        break
                    ptr = self.buffer.tell()
                    (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
                self.newlines = 0
                return self.buffer.read()

            def readline(self):
                while self.newlines == 0:
                    b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                    self.newlines = b.count(b'\n') + (not b)
                    ptr = self.buffer.tell()
                    (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
                self.newlines -= 1
                return self.buffer.readline()

            def flush(self):
                if self.writable:
                    os.write(self._fd, self.buffer.getvalue())
                    (self.buffer.truncate(0), self.buffer.seek(0))

        class IOWrapper(IOBase):

            def __init__(self, file):
                self.buffer = FastIO(file)
                self.flush = self.buffer.flush
                self.writable = self.buffer.writable
                self.write = lambda s: self.buffer.write(s.encode('ascii'))
                self.read = lambda : self.buffer.read().decode('ascii')
                self.readline = lambda : self.buffer.readline().decode('ascii')
        sys.stdout = IOWrapper(sys.stdout)
    if dfs:
        from types import GeneratorType

        def bootstrap(f, stack=[]):

            def wrappedfunc(*args, **kwargs):
                if stack:
                    return f(*args, **kwargs)
                else:
                    to = f(*args, **kwargs)
                    while True:
                        if type(to) is GeneratorType:
                            stack.append(to)
                            to = next(to)
                        else:
                            stack.pop()
                            if not stack:
                                break
                            to = stack[-1].send(to)
                    return to
            return wrappedfunc
    if hashing:
        RANDOM = random.getrandbits(20)

        class Wrapper(int):

            def __init__(self, x):
                int.__init__(x)

            def __hash__(self):
                return super(Wrapper, self).__hash__() ^ RANDOM
    if read_from_file:
        file = open('input.txt', 'r').readline().strip()[1:-1]
        fin = open(file, 'r')
        input = lambda : fin.readline().strip()
        output_file = open('output.txt', 'w')

        def fprint(*args, **kwargs):
            print(*args, **kwargs, file=output_file)
    if de:

        def debug(*args, **kwargs):
            print('\x1b[92m', end='')
            print(*args, **kwargs)
            print('\x1b[0m', end='')
pre_process = [None] * 11
for b in range(2, 11):
    length = 1
    cur = 1
    while cur <= 10 ** 18:
        length += 1
        cur *= b
    length -= 1
    pre_process[b] = [[0] * (1 << b) for _ in range(length)]
    for i in range(b):
        pre_process[b][0][1 << i] = 1
    for i in range(length - 1):
        for msk in range(1 << b):
            for new_bit in range(b):
                pre_process[b][i + 1][msk ^ 1 << new_bit] += pre_process[b][i][msk]
ans = []

def main():
    (b, l, r) = MII()

    def f(x):
        vals = []
        while x:
            vals.append(x % b)
            x //= b
        k = len(vals)
        ans = 0
        for i in range(k - 1):
            ans += pre_process[b][i][0]
            if i:
                ans -= pre_process[b][i - 1][1]
        cur_msk = 0
        for i in range(k - 1, -1, -1):
            for j in range(1 if i == k - 1 else 0, vals[i]):
                ans += pre_process[b][i - 1][cur_msk ^ 1 << j] if i else 1 if cur_msk == 1 << j else 0
            cur_msk ^= 1 << vals[i]
        return ans
    ans.append(f(r + 1) - f(l))
    return
t = II()
for _ in range(t):
    main()
print('\n'.join(map(str, ans)))

Solution (Python):
import sys
input = lambda : sys.stdin.readline().strip()
pre_process = [None] * 11
for b in range(2, 11):
    length = 1
    cur = 1
    while cur <= 10 ** 18:
        length += 1
        cur *= b
    length -= 1
    pre_process[b] = [[0] * (1 << b) for _ in range(length)]
    for i in range(b):
        pre_process[b][0][1 << i] = 1
    for i in range(length - 1):
        for msk in range(1 << b):
            for new_bit in range(b):
                pre_process[b][i + 1][msk ^ 1 << new_bit] += pre_process[b][i][msk]
ans = []
for _ in range(int(input())):
    (b, l, r) = map(int, input().split())

    def f(x):
        vals = []
        while x:
            vals.append(x % b)
            x //= b
        k = len(vals)
        ans = 0
        for i in range(k - 1):
            ans += pre_process[b][i][0]
            if i:
                ans -= pre_process[b][i - 1][1]
        cur_msk = 0
        for i in range(k - 1, -1, -1):
            for j in range(1 if i == k - 1 else 0, vals[i]):
                ans += pre_process[b][i - 1][cur_msk ^ 1 << j] if i else 1 if cur_msk == 1 << j else 0
            cur_msk ^= 1 << vals[i]
        return ans
    ans.append(f(r + 1) - f(l))
print('\n'.join(map(str, ans)))