Problem:
 Score : 700 points    Problem Statement You are given an integer L . Construct a directed graph that satisfies the conditions below. The graph may contain multiple edges between the same pair of vertices. It can be proved that such a graph always exists.   The number of vertices, N , is at most 20 . The vertices are given ID numbers from 1 to N .  The number of edges, M , is at most 60 . Each edge has an integer length between 0 and 10^6 (inclusive).  Every edge is directed from the vertex with the smaller ID to the vertex with the larger ID. That is, 1,2,...,N is one possible topological order of the vertices.  There are exactly L different paths from Vertex 1 to Vertex N . The lengths of these paths are all different, and they are integers between 0 and L-1 .   Here, the length of a path is the sum of the lengths of the edges contained in that path, and two paths are considered different when the sets of the edges contained in those paths are different.      Constraints  2 \leq L \leq 10^6  L is an integer.         Input Input is given from Standard Input in the following format:  L       Output In the first line, print N and M , the number of the vertices and edges in your graph.
In the i -th of the following M lines, print three integers u_i,v_i and w_i , representing the starting vertex, the ending vertex and the length of the i -th edge.
If there are multiple solutions, any of them will be accepted.        Sample Input 1 4      Sample Output 1 8 10
1 2 0
2 3 0
3 4 0
1 5 0
2 6 0
3 7 0
4 8 0
5 6 1
6 7 1
7 8 1  In the graph represented by the sample output, there are four paths from Vertex 1 to N=8 :   1 → 2 → 3 → 4 → 8 with length 0  1 → 2 → 3 → 7 → 8 with length 1  1 → 2 → 6 → 7 → 8 with length 2  1 → 5 → 6 → 7 → 8 with length 3   There are other possible solutions.       Sample Input 2 5      Sample Output 2 5 7
1 2 0
2 3 1
3 4 0
4 5 0
2 4 0
1 3 3
3 5 1

Solution (Python):
L = input()
E = []
N = 1
while 2**(N-1) < L:
  N += 1
if 2**(N-1)>L:
  N -= 1
for i in range(N-1):
  E += [(i,i+1,0),
        (i,i+1,2**(N-i-2))]
if 2**(N-1)<L:
  E += [(0,N-1,L-1)]
  for i in range(N-1):
    if (L-1)>>i&1:
      E += [(0, N-i-1, (L-1)>>(i+1)<<(i+1))]
print N, len(E)
for e in E:
  print e[0]+1, e[1]+1, e[2]

Solution (Python):
L = int(input())
l = L - 1
N = 0
while l > 0:
	l = l // 2
	N += 1
E = [(i+1, i+2, 0) for i in range(N - 1)]
E += [(i+1, i+2, 2 ** (N - i - 2)) for i in range(N - 1)]

if L == 2 ** N:
	w = 2 ** (N-1)
	E.append((1,2,w))
	if w > 1:
		E.append((1,2,w + w // 2))
else:
	rest = L - (2 ** (N - 1))
	cnt = 0
	bit = [0 for i in range(N-1)]
	while rest > 0:
		bit[cnt] = (rest % 2)
		rest = rest // 2
		cnt += 1
	bit.reverse()

	weight = 2 ** (N-1)
	for i in range(len(bit)):
		if bit[i] == 1:
			E.append((1, i+2, weight))
			weight += 2 ** (N - i - 2)

if L == 2:
	N = 2
	E = [(1,2,0),(1,2,1)]
print(N, len(E))
for e in E:
	print(e[0], e[1], e[2])

Solution (Python):
def solve(l):
    buf = []
    lb = len(bin(l)) - 3
    for i in range(lb):
        buf.append((i + 1, i + 2, 0))
        buf.append((i + 1, i + 2, 1 << i))
    ofs = 1 << lb
    for i, c in enumerate(bin(l)[3:]):
        if c == '1':
            buf.append((lb - i, lb + 1, ofs))
            ofs += 1 << (lb - i - 1)
    return lb + 1, buf


l = int(input())
n, buf = solve(l)
print(n, len(buf))
print('\n'.join(' '.join(map(str, e)) for e in buf))

Solution (Python):
N = int(input())

for i in range(100):
    if 2 ** i > N:
        num = i
        break
n = num
ans = []
N = N - 1
n_rev_list = list(range(2,n+1))[::-1]
for num in n_rev_list:
    if  N % 2 == 0:
        ans.append("{} {} {}".format(1,num,N))
        ans.append("{} {} {}".format(num - 1,num,0))
        ans.append("{} {} {}".format(num - 1,num, N // 2 ))
        N = N // 2 - 1
    else:
        ans.append("{} {} {}".format(num - 1,num,0))
        ans.append("{} {} {}".format(num - 1,num, (N + 1 )// 2))
        N = (N - 1) // 2
K = len(ans)
print("{} {}".format(n,K))
for a in ans:
    print(a)

Solution (Python):
L = int(input())
p = []
while L:
    p.append(L % 2)
    L >>= 1
p = p[::-1]
k = 1
ed = []
for i in range(2, len(p) + 1):
    ed.append((i - 1, i, 0))
    ed.append((i - 1, i, k))
    k *= 2
    if p[i - 1]:
        ed.append((1, i, k))
        k += 1
print(len(p), len(ed))
for u, v, w in ed:
    print(u, v, w)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    l = I()
    if l < 60:
        r = ["2 {}".format(l)]
        for i in range(l):
            r.append("1 2 {}".format(i))
        return '\n'.join(r)
    n = 1
    l -= 1
    while 2**n-1 <= l:
        n += 1
    a = [2**(n-i-1) for i in range(n)] + [0]
    r = []
    for i in range(1,n):
        r.append("{} {} 0".format(i,i+1))
        r.append("{} {} {}".format(i,i+1,a[i]))
    t = sum(a) - a[0]
    l -= t
    for i in range(0,n):
        if l >= a[i]:
            r.append("1 {} {}".format(i+1,t+1))
            t += a[i]
            l -= a[i]

    return "{} {}\n".format(n,len(r)) + '\n'.join(r)


print(main())

Solution (Python):
l = int(input())

l2 = bin(l)[2:]
n = len(l2)
count = 0
for i in range(len(l2)):
    count += int(l2[i])
m = 2 * (n - 1) + count - 1
print(n, m)
for i in range(n - 1):
    print(i + 1, i + 2, 0)
    print(i + 1, i + 2, 2 ** i)
for i in range(1, n):
    if int(l2[i]) == 0:
        continue
    num = 0
    for j in range(i):
        if int(l2[j]) == 1:
            num += 2 ** (n - j - 1)
    print(n - i, n, num)

Solution (Python):
def ii():
    return int(input())
def mi():
    return map(int, input().split())
def li():
    return list(mi())

L = ii()

p = 1
n = 0
while p <= L:
    p *= 2
    n += 1
p //= 2

ed = []
q = p
for i in range(1, n):
    q //= 2
    ed.append((i, i + 1, 0))
    ed.append((i, i + 1, q))

L -= p
off = p
p //= 2
m = 2
while L:
    if p <= L:
        ed.append((1, m, off))
        L -= p
        off += p
    p //= 2
    m += 1

print(n, len(ed))
for u, v, w in ed:
    print(u, v, w)

Solution (Python):
l = int(input())

ns = [i for i in range(1, 21)]
edges = [(i, i+1, 0) for i in range(1, 20)]

t = l // 2
i = 19
length = 1
while t:
    edges.append((i, i + 1, length))
    i -= 1
    length *= 2
    t //= 2

for i in range(20):
    if l & (1 << i):
        start = l - (l & (2 ** i - 1)) - 2 ** i
        if start == 0: continue
        edges.append((1, 20 - i, start))

print(len(ns), len(edges))
for a, b, l in edges:
    print(a, b, l)

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

L = input()
rem = L / (2 ** 18)
L %= (2 ** 18)
N = 20
M = 0

ls = []
for i in range(2, 20):
    ls.append((i, i + 1, 2 ** (20 - i - 1)))
    ls.append((i, i + 1, 0))
    M += 2

L = bin(L)[2:][::-1]
S = 0
for i in range(len(L)):
    c = L[i]
    if c == '1':
        pos = 20 - i
        ls.append((1, pos, S))
        S += 2 ** i
        M += 1

for i in range(rem):
    ls.append((1, 2, S))
    M += 1
    S += (2 ** 18)

print N, M

for p in ls:
    print " ".join(map(str, p))

Solution (Python):




import sys

input=sys.stdin.readline

li=[1]
back=[0]
edges=[]

for i in range(2,14):
    b=3
    edges.append((15-i,16-i,0))
    edges.append((15-i,16-i,li[-1]))
    edges.append((15-i,16-i,li[-1]*2))
    li.append(li[-1]*b)
    back.append(b)
    




l=int(input())
s=0
for i in range(13)[::-1]:
    while s+li[i]<=l:
        edges.append((1,14-i,s))
        s+=li[i]

edges.sort()
print(14,len(edges))
for e in edges: print("%d %d %d"%e)

Solution (Python):
l = int(input())
l-=1

node = 1
ans = []
while l:
    half = l // 2
    half = l - half
    if l % 2 == 0:
        ans.append((node, -1, l))
        l -= 1
    ans.append((node, node+1, 0))
    ans.append((node, node+1, half))
    l -= half
    node += 1

print("{} {}".format(node, len(ans)))
for a, b, c in ans:
    if b == -1:b = node
    print("{} {} {}".format(a, b, c))