Defender of Childhood Dreams (Codeforces 1586F)
Tags: Math, divide and conquer

Even if you just leave them be, they will fall to pieces all by themselves. So, someone has to protect them, right?You find yourself playing with Teucer again in the city of Liyue. As you take the eccentric little kid around, you notice something interesting about the structure of the city.Liyue can be represented as a directed graph containing $$$n$$$ nodes. Nodes are labeled from $$$1$$$ to $$$n$$$. There is a directed edge from node $$$a$$$ to node $$$b$$$ if and only if $$$a < b$$$.A path between nodes $$$a$$$ and $$$b$$$ is defined as a sequence of edges such that you can start at $$$a$$$, travel along all of these edges in the corresponding direction, and end at $$$b$$$. The length of a path is defined by the number of edges. A rainbow path of length $$$x$$$ is defined as a path in the graph such that there exists at least 2 distinct colors among the set of $$$x$$$ edges.Teucer's favorite number is $$$k$$$. You are curious about the following scenario: If you were to label each edge with a color, what is the minimum number of colors needed to ensure that all paths of length $$$k$$$ or longer are rainbow paths?Teucer wants to surprise his older brother with a map of Liyue. He also wants to know a valid coloring of edges that uses the minimum number of colors. Please help him with this task!

Input: The only line of input contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq k < n \leq 1000$$$). 

Output: On the first line, output $$$c$$$, the minimum colors you need to satisfy the above requirements.On the second line, print a valid edge coloring as an array of $$$\frac{n(n-1)}{2}$$$ integers ranging from $$$1$$$ to $$$c$$$. Exactly $$$c$$$ distinct colors should exist in the construction. Print the edges in increasing order by the start node first, then by the second node.For example, if $$$n=4$$$, the edge colors will correspond to this order of edges: ($$$1$$$, $$$2$$$), ($$$1$$$, $$$3$$$), ($$$1$$$, $$$4$$$), ($$$2$$$, $$$3$$$), ($$$2$$$, $$$4$$$), ($$$3$$$, $$$4$$$)

Note: The corresponding construction for the first test case looks like this:    It is impossible to satisfy the constraints with less than $$$2$$$ colors.The corresponding construction for the second test case looks like this:    One can show there exists no construction using less than $$$3$$$ colors.

Sample Inputs:
5 3

5 2

8 7

3 2

Sample Outputs:
2
1 2 2 2 2 2 2 1 1 1

3
3 2 2 1 2 2 1 3 1 1

2
2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2
1 2 2

Solution (Python):
import math
(n, k) = map(int, input().split(' '))
m = int(math.ceil(math.log(n, k)))
print(m)
f = [0 for i in range(n * (n - 1) // 2)]
s = 1
while True:
    s *= k
    for l in range(1, n + 1, s):
        for i in range(l, min(l + s, n + 1)):
            for j in range(i + 1, min(l + s, n + 1)):
                g = lambda i, j: (n - 1 + (n - i + 1)) * (i - 1) // 2 + (j - i - 1)
                x = g(i, j)
                f[x] = max(f[x], m)
    m -= 1
    if m == 0:
        break
for x in f:
    print(x, end=' ')

Solution (Python):
(n, k) = map(int, input().split())
a = []
for i in range(n):
    for j in range(i + 1, n):
        (u, v, r) = (i, j, 0)
        while u ^ v:
            u //= k
            v //= k
            r += 1
        a.append(r)
print(max(a), *a)

Solution (Python):
import sys
import math
inpu = sys.stdin.readline
prin = sys.stdout.write

def numberToBase(n, b):
    if n == 0:
        return [0]
    digits = []
    while n:
        digits.append(int(n % b))
        n //= b
    return digits[::-1]
(n, k) = map(int, inpu().split())
digits = math.ceil(math.log(n) / math.log(k))
prin(str(digits))
prin('\n')
for i in range(n):
    j = i + 1
    diff = 1
    while j < n:
        if i // k ** diff != j // k ** diff:
            diff += 1
            continue
        prin(str(diff))
        prin(' ')
        j += 1