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):
import math

L = int(input())

N = math.floor(math.log2(L)) + 1

edge = []
for i in range(N - 1):
  edge.append((i + 1, i + 2, 0))
  edge.append((i + 1, i + 2, (1 << i)))

for i in range(N - 1, 0, -1):
  if L - (1 << (i - 1)) >= 2 ** (N - 1):
    edge.append((i, N, L - (1 << (i - 1))))
    L -= (1 << (i - 1))

print(N, len(edge))
for e in edge:
  print(str(e).lstrip('(').rstrip(')').replace(',', ''))

Solution (Python):
l=int(input())
d=l.bit_length()
g=[]
for i in range(d-1):
  g.append((i+1,i+2,0))
  g.append((i+1,i+2,2**(d-i-2)))
s=1<<(d-1)
for i in range(d-1)[::-1]:
  if (1<<i)&l:
    g.append((1,d-i,s))
    s+=1<<i
print(d,len(g))
for u,v,c in g:
  print(u,v,c)

Solution (Python):

def main():
    L = int(input())
    b = bin(L)[2:]
    bl = len(b)
    r = []
    for i in range(bl-1):
        r.append((i+1, i+2, 0))
        r.append((i+1, i+2, 2**i))
    if L == 2**(bl-1):
        pr(r)
        return
    end = bl
    for i in reversed(range(1, end)):
        if L - 2**(i-1) >= 2**(end-1):
            r.append((i, end, L-2**(i-1)))
            L -= 2**(i-1)
    pr(r)
def pr(r):
    m = 0
    for i in r:
        m = max(m, i[1])
    print(m, len(r))
    for i in r:
        print(i[0], i[1], i[2])
main()

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

Lbin = (bin(L)[2:])[::-1]
N = len(Lbin)
M = 2 * (N - 1) + Lbin.count("1") - 1
print(N, M)

for i in range(0, N - 1):
    print(i + 1, i + 2, 0)
    print(i + 1, i + 2, 2 ** i)

weight = L
for i in range(0, N - 1):
    if Lbin[i] == '1':  
        weight -= 2 ** i
        print(i + 1, N, weight)

Solution (Python):

from collections import deque
L = int(input())

Lbin = (bin(L)[2:])[::-1]
N = len(Lbin)
M = 0
E = deque()

for i in range(0, N - 1):
    E.append(((i + 1, i + 2), 0))
    E.append(((i + 1, i + 2), 2 ** i))
    M += 2

weight = L
for i in range(0, N - 1):
    if Lbin[i] == '1':  
        weight -= 2 ** i
        E.append(((i + 1, N), weight))
        M += 1

print(N, M)
for e in E:
    print(e[0][0], e[0][1], e[1])

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    L=I()
    power = 1
    N = 1
    while power<<1 <= L:
        power<<=1
        N+=1
    
    ans = []
    for i in range(N-1):
        ans.append((i+1,i+2,0))
        ans.append((i+1,i+2,1<<i))
    for i in range(N-1)[::-1]:
        X=L-(1<<i)
        if(X>=power): 
            ans.append((i+1,N,X))
            L-=1<<i
    M=len(ans)
    print(N,M)
    for a in ans:
        print(*a)
if __name__ == '__main__':
    main()

Solution (Python):
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


L = ir()
length = L.bit_length()
edges = []
for i in range(1, length):
    edges.append((i, i+1, 0))
    edges.append((i, i+1, 2**(i-1)))

for i in range(length-1):
    if L >> i & 1:
        L -= 2**i
        edges.append((i+1, length, L))

print(length, len(edges))
for e in edges:
    print(*e)

Solution (Python):
from math import log2


L = int(input())
r = int(log2(L))
N = r + 1
edge = []
for i in range(r):
    edge.append((i + 1, i + 2, 2 ** i))
    edge.append((i + 1, i + 2, 0))
for t in range(N - 1, 0, -1):
    s = L - 2 ** (t - 1)
    if s >= 2 ** r:
        edge.append((t, N, s))
        L = s

print(N, len(edge))
for e in edge:
    print(*e)

Solution (Python):










import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

L = int(input())

v = 1
edges = []  








n = 2
cost = 1
while n <= L:
    edges.append((v, v+1, 0))
    edges.append((v, v+1, cost))
    cost *= 2
    v += 1
    n *= 2


v_sink = v




n //= 2
N = n
L -= N

while L > 0:
    if L >= n:
        edges.append((v, v_sink, N))
        L -= n
        N += n
    n //= 2
    v -= 1

print(v_sink, len(edges))
for e in edges:
    print(*e)

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

ans = []
m = 0
add = -1
mask = 0xffffff
for i in range(24):
    mask >>= 1
    d = 24 - i - 1
    if ((~mask) & L) != 0:
        if add == -1:
            n = d+1
            add = 1 << d
            b = 1
            
            for i in range(1,d+1):
                ans.append(str(i) + " " + str(i+1) + " 0")
                ans.append(str(i) + " " + str(i+1) + " " + str(b))
                b *= 2
                m += 2
        else :
            ans.append(str(d+1) + " " + str(n) + " " + str(add))
            add += (1 << d)
            m += 1
        L &= mask

print(n,m)
for i in ans:
    print(i)

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

p=[1]*21
N=0
for i in range(20):
    p[i+1]=p[i]*2
    if p[i+1]>L:
        N=i+1
        break

        
ans=[]
M=0
for i in range(N-1):
    ans.append([i+1,i+2,0])
    ans.append([i+1,i+2,p[i]])
    M+=2
    
path=L-p[N-1]
N2=len(bin(path))-2    



for i in range(N):
    if path&1 ==1:
        ans.append([i+1,N,L-p[i]])
        M+=1
        L-=p[i]
    path=path>>1
    
print(' '.join(map(str, [N,M])))
for i in range(M):
    print(' '.join(map(str, ans[i])))

Solution (Python):
L = int(input())
n = L.bit_length()
ans = []
for i in range(1,n):
  ans.append((i,i+1,0))
  ans.append((i,i+1,2**(n-i-1)))
binL = bin(L)
for i in range(1,n):
  if binL[i+2] == "1":
    mask = int(binL[:i+2],2)<<(n-i)
    ans.append((1,i+1,L&mask))
print(n,len(ans))
for edge in ans:
  print(*edge)

Solution (Python):
l = int(input())
n = l.bit_length()
edge = list()
for i in range(1,n):
  edge.append((i,i+1,0))
  edge.append((i,i+1,1<<(i-1)))
t = l - (1<<(n-1))
cnt = (n-1)*2
while t:
  m = t.bit_length()
  edge.append((m,n,l-t))
  t -= (1<<m-1)
  cnt += 1
print(n,cnt)
for t in edge:
  print(*t)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    L=int(input())
    tmp=L
    tmp2=L
    l=[]
    while tmp2!=0:
        c=0
        while tmp!=1:
            tmp//=2
            c+=1
        l.append(c)
        tmp2-=2**c
        tmp=tmp2
    N=l[0]+1
    M=0
    ans=[]
    w=1
    for i in range(l[0]):
        ans.append((i+1,i+2,0))
        ans.append((i+1,i+2,w))
        M+=2
        w*=2
    for x in l[1:]:
        ans.append((x+1,N,w))
        w+=2**x
        M+=1
    print(N,M)
    for z in ans:
        print(*z)
    

if __name__ == '__main__':
    main()

Solution (Python):
L=int(input())
l=0
pp=0
tmp=L
while tmp>0:l+=1;pp+=tmp%2;tmp//=2
print(l,l*2-3+pp)
for i in range(1,l):
  print(i,i+1,0)
  a=1<<i-1
  print(i,i+1,a)
  if L&a:L-=a;print(i,l,L)

Solution (Python):
L=int(input())
l=0
pp=0
tmp=L
while tmp>0:l+=1;pp+=tmp%2;tmp//=2
print(l,l*2-3+pp)
for i in range(1,l):
  print(i,i+1,0)
  a=1<<(i-1)
  print(i,i+1,a)
  if L&a:L-=a;print(i,l,L)

Solution (Python):
import math


l = int(input())
node = int(math.log2(l)) + 1
edges = []

for _from in range(1, node):
    edges.append([_from, _from+1, 0])
    edges.append([_from, _from+1, 2**(_from-1)])

count = 2 ** (node-1)
lack = l - count
length = 2 ** (node-1)

for _from in range(node-1, 0, -1):
    if not lack:
        break
    add_line = 2 ** (_from-1)
    if add_line > lack:
        continue
    edges.append([_from, node, length])
    lack -= add_line
    length += add_line

print(node, len(edges))
for _from, to, length in edges:
    print(_from, to, length)

Solution (Python):
L = int(input())
N = len(bin(L)[2:])
R = 1 << N - 1
UVW = sum([[(i, i+1, 0), (i, i+1, 1<<i-1)] for i in range(1, N)], [])
for i, s in enumerate(bin(L)[:2:-1], 1):
    if s == "1":
        UVW.append((i, N, R))
        R += 1 << i - 1

print(N, len(UVW))
for t in UVW:
    print(*t)

Solution (Python):
L = t = int(input())
X = []
B = 4
while L:
    X.append(L%B)
    L//=B

E = []
N = len(X)*2
for i, x in enumerate(X[:-1]):
    r = B**i
    t -= r*x
    for k in range(0, x):
        E.append((2*i+1, 2*i+2, r*k))

    E.append((2*i+2, 2*i+3, 0))

    for k in range(x, B):
        E.append((2*i+1, 2*i+3, r*k))

    E.append((2*i+2, N, t))

for k in range(0, X[-1]):
    E.append((N-1, N, B**(len(X)-1)*k))

print(N, len(E))
for e in E:
    print(*e)

Solution (Python):
import sys
from math import log2, floor

L = int(sys.stdin.readline().rstrip())

def main():
    n = floor(log2(L)) + 1
    m = 2 * (n - 1)
    edges = []
    for i in range(1, n):
        edges.append((i, i+1, 0))
        edges.append((i, i+1, 2**(i-1)))
    
    cur = 2 ** (n - 1)
    q = L - cur
    for i in range(1, n):
        q, r = divmod(q, 2)
        if not r:
            continue
        else:
            edges.append((i, n, cur))
            cur += 2 ** (i - 1)
            m += 1
    
    yield (n, m)
    for e in edges:
        yield e

if __name__ == '__main__':
    ans = main()
    for i in ans:
        print(*i, sep=' ')

Solution (Python):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7




L = int(sys.stdin.buffer.readline())

V = L.bit_length()
ans = []
for v in range(V - 1):
    ans.append((v, v + 1, 0))
    ans.append((v, v + 1, 2 ** v))

pos = V - 2
w = 1 << L.bit_length() - 1
while pos >= 0:
    if L & (1 << pos):
        ans.append((pos, V - 1, w))
        w |= L & (1 << pos)
    pos -= 1
E = len(ans)
print(V, E)
for r in ans:
    v, u, w = r
    v += 1
    u += 1
    print(v, u, w)

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

N = 1
l = L
while l // 2 >= 1:
    N += 1
    l //= 2

G = [[] for _ in range(N + 1)]

for i in range(1, N):
    G[i].append([i + 1, 0])
    G[i].append([i + 1, 2 ** (N - 1 - i)])

M = 2 * (N - 1)
tmp = 2 ** (N - 1)

for i in range(N - 2, -1, -1):
    if (L >> i) & 1:
        G[1].append([N - i, tmp])
        tmp += 2 ** i
        M += 1

if N > 20 or M > 60:
    print ('error')

print (N, M)
for i in range(1, N):
    for j in G[i]:
        print (i, *j)

Solution (Python):
from math import log2
L=int(input())
ans=[]
i=1
while True:
    if 2**i>L:
        last=i
        break
    ans.append([i,i+1,2**(i-1)])
    ans.append([i,i+1,0])
    i+=1
"""for i in ans:
    print(i)
print(last)"""
num=2**(last-1)
a=L-num
while a>0:
    ans.append([int(log2(a))+1,last,num])
    num+=2**(int(log2(a)))
    a=L-num
print(last,len(ans))
for i in ans:
    print(*i)

Solution (Python):
def solve(l):
    n = 0
    while 2**n <= l:
        n += 1
    E = []
    for i in range(n-1, -1, -1):
        x = 2**i
        if i != n-1:
            E.append((i, i+1, 0))
            E.append((i, i+1, x))
        if l - x >= 2**(n-1):
            E.append((i, n-1, l-x))
            l -= x
    print(n, len(E))
    for u, v, x in E:
        print(u+1, v+1, x)

l = int(input())
solve(l)

Solution (Python):
L = int(input())
r = 0
answer = []

while 2**r <= L:
    r += 1
N = r

for i in range(1,r):
    answer.append([i,i+1,0])
    answer.append([i,i+1,2**(i-1)])


for t in range(N-1,0,-1):
    if L - 2**(t-1) >= 2**(r-1):
        answer.append([t,N,L-2**(t-1)])
        L -= 2**(t-1)

answer.insert(0,[N,len(answer)])

for ans in answer:
    print(*ans)

Solution (Python):
import math

L = int(input())
N = int(math.log2(L))+1

M = 2*(N-1)


edges = []
for i in range(N-1):
    edges.append('{0} {1} {2}'.format((i+1) , (i+2), 0))
    
for i in range(N-1):
    edges.append('{0} {1} {2}'.format((i+1) , (i+2), 2**i))

node = L-2**(N-1)
X = 2**(N-1)

while node > 0:
    M += 1
    t = int(math.log2(node))+1
    edges.append('{0} {1} {2}'.format(t, N, X))
    node -= 2**(t-1)
    X += 2**(t-1)
    
output = """{0} {1}
{2}""".format(N,M,'\n'.join(edges))
print(output)

Solution (Python):

L = int(input())

r = L.bit_length()-1
N = r+1
graph = []
append = graph.append
for i in range(1,r+1):
    append([i,i+1,0])
    append([i,i+1,2**(i-1)])

for t in range(N-1,0,-1):
    if L-2**(t-1) >= 2**r:
        append([t,N,L-2**(t-1)])
        L -= 2**(t-1)

print(N,len(graph))
for g in graph:
    print(*g)

Solution (Python):
import math
L = int(input())
r = math.floor(math.log2(L))
N = r+1
graph = []
for i in range(1,r+1):
    graph.append([i,i+1,0])
    graph.append([i,i+1,2**(i-1)])
for t in range(N-1,0,-1):
    if L-2**(t-1) >= 2**r:
        graph.append([t,N,L-2**(t-1)])
        L -= 2**(t-1)

print(N,len(graph))
for g in graph:
    print(*g)

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

N = L.bit_length()

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

s = 2 ** (N - 1)
for i in reversed(range(N - 1)):
    if (L >> i) & 1:
        E.append((i + 1, N, s))
        s += 2 ** i

print(N, len(E))
for a, b, c in E:
    print(a, b, c)

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

N = L.bit_length()

edges = [[] for _ in range(N)]
for i in range(N - 1):
    edges[i].append((i + 1, 0))
    edges[i].append((i + 1, 2**i))

now = 2**(N - 1)

for i in range(N - 1)[:: -1]:
    cnt = 2**i
    if L - now >= cnt:
        edges[i].append((N - 1, now))
        now += cnt

M = sum(len(edges[i]) for i in range(N))
print(N, M)

for i in range(N):
    for to, cost in edges[i]:
        print(i + 1, to + 1, cost)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def iterate(L,n,m,edges,mode):
    
    if(mode&1 or L==1):
        edges+=[(1,n,L)]
        return L+1,n,m+1,edges
    
    else:
        edges=[(u,v,2*w) for u,v,w in edges]
        edges+=[(n,n+1,0),(n,n+1,1)]
        return 2*L,n+1,m+2,edges
        
def resolve():
    L=int(input())
    res=[L%2]
    while(L!=1):
        if(L&1): L-=1
        else: L//=2
        res.append(L%2)
    res.reverse()

    
    L=1
    n=2
    m=1
    edges=[(1,2,0)]

    
    for mode in res[1:]:
        L,n,m,edges=iterate(L,n,m,edges,mode)

    
    print(n,m)
    for e in edges:
        print(*e)
resolve()

Solution (Python):


L = int(input())

N = 2
while 2**N-1 <= L-1:
    N += 1

edge = []
for i in range(1,N):
    edge.append([i,i+1,0])
    edge.append([i,i+1,2**(i-1)])

for i in reversed(range(1,N)):
    if L - 2**(i-1) > 2**(N-1) - 1:
        edge.append([i,N,L-2**(i-1)])
        L = L-2**(i-1)

print(N, len(edge))
for x in edge:
    print(*x)

Solution (Python):
L = int(input())
v = list(map(int,bin(L)[:2:-1]))
N = len(v)+1
edges = []
unit = 1
for i in range(N-1):
    L -= unit*v[i]
    edges.append((i+1, i+2, 0))
    edges.append((i+1, i+2, unit))
    if v[i]:
        edges.append((i+1, N, L))
    unit *= 2

print(N, len(edges))
for a, b, c in edges:
    print(a, b, c)

Solution (Python):
L = int(input())
v = list(map(int,bin(L)[:2:-1]))
N = len(v)+1
edges = []
unit = 1
for i in range(N-1):
    L -= unit*v[i]
    edges.append((i+1, i+2, 0))
    edges.append((i+1, i+2, unit))
    if v[i]:
        edges.append((i+1, N, L))
    unit *= 2

print(N, len(edges))
for a, b, c in edges:
    print(a, b, c)

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

k = L.bit_length()
ans = []
for i in range(1, k):
    ans.append([i,i+1,0])
    ans.append([i,i+1,1<<(k-i-1)])

for i in range(k-1):
    if L & 1<<i:
        mask = (1<<(k+1)) - (1<<(i+1))
        w = L & mask
        ans.append([1, k-i, w])

print(k, len(ans))
for i in ans:
    print(*i)

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

print(L.bit_length(), 2*(L.bit_length()-1) + (bin(L)[3:]).count('1'))
for i in range(L.bit_length()-1,0,-1):
    print(i,i+1,0)
    print(i,i+1,1<<(i-1))
    if (L >> i-1) & 1:
        print(i,L.bit_length(),L>>i<<i)

Solution (Python):
L = int(input())
  
ret = []
count = 0
for i in range(19, 0, -1) :
    while L - (1 << (i - 1)) >= 0 :
        ret.append((i, count))
        count += 1 << (i - 1)
        L -= 1 << (i - 1)

N = ret[0][0]
M = len(ret) + 2 * (N - 1)

print(N + 1, M)
for i in range(1, N) :
    print(i, i + 1, 0)
    print(i, i + 1, 1 << (i - 1))
for r in ret :
    print(r[0], N + 1, r[1])

Solution (Python):
l=int(input())
for i in range(1,21):
    if 2**i>l:
        n=i
        break
m=2*(n-1)
data=[]
for i in range(1,n):
    data.append([i,i+1,0])
    data.append([i,i+1,2**(n-i-1)])
add=2**(n-1)
for i in range(1,n):
    if (l>>n-1-i)&1==1:
        data.append([1,i+1,add])
        add+=2**(n-1-i)
        m+=1
print(str(n)+" "+str(m))
for u in data:
    print(*u)

Solution (Python):
import math
l=int(input())
n=int(math.log2(l)+1)
E=[]
for i in range(n-1):
    E.append([i+1,i+2,0])
    E.append([i+1,i+2,2**i])
i=n-2
while l>2**(n-1):
    t=l-2**i
    if t>2**(n-1)-1:
        E.append([i+1,n,t])
        l=t
    i-=1
print(n,len(E))
for i in range(len(E)):
    print(*E[i])

Solution (Python):
L = int(input())
for i in range(21):
    if 2**i > L:
        r = i-1
        break
        
N = r+1
ans = []
for i in range(1, N):
    ans.append([str(i), str(i+1), str(2**(i-1))])
    ans.append([str(i), str(i+1), str(0)])

t = N-1
while t >= 1:
    if L-2**(t-1) >= 2**r:
        ans.append([str(t), str(N), str(L-2**(t-1))])
        L -= 2**(t-1)
    t -= 1
        
print(N, len(ans))
for i in range(len(ans)):
    print(" ".join(ans[i]))

Solution (Python):
def in_list(l, l_t, a):
    l[a] = l_t


ans_list = [[] for _ in range(60)]
p = 0

l = int(input())
bin_str = format(l, '020b')
for i in range(20):
    c = 0
    if c == 0 and str(bin_str)[i] == '1':
        c = 1
        n = 20-i
        k = i
        break;
for j in range(1, n):
    in_list(ans_list, [j, j+1, 0], p)
    p += 1
    in_list(ans_list, [j, j+1, 2**(n-1-j)], p)
    p += 1
c = 2**(n-1)

for m in range(k+1, 20):
    if str(bin_str[m]) == '1':
        in_list(ans_list, [1, n+m-20+1, c], p)
        p += 1
        c += 2 ** (19-m)

print(n, p)
for q in range(p):
    print(ans_list[q][0], ans_list[q][1], ans_list[q][2])

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007


def A():
    n = II()
    a = n // 2
    b = n - a
    print(a*b)
    return


def B():
    x1, y1, x2, y2 = LI()
    a = x2 - x1
    b = y2 - y1
    print(x2 - b, y2 + a, x1 - b, y1 + a)
    return


def C():
    n, k = LI()
    if k % 2:
        print((n // k)** 3)
    else:
        print(int((n//k)**3 + ((n-k/2)//k+1)**3))
        
    return











def D():
    l = II()
    lBit = bin(l)[:1:-1]
    
    n = len(lBit)
    ans = []
    for i in range(1, n):
        ans += [(i, i + 1, 0)] + [(i, i + 1, 1 << i - 1)]
    b = 1 << n - 1
    for i in range(n - 1):
        if lBit[-2 - i] == "1":
            ans += [(n - i - 1, n, b)]
            b += 1 << (n - i - 2)
    print(n,len(ans))
    for a in ans:
        print(*a)
    return



if __name__ == '__main__':
    D()

Solution (Python):
l = int(input())
s = bin(l)[2:]
n = len(s)
m = 0
ans = []
for i in range(n-1):
    ans.append([i+1, i+2, 0])
    ans.append([i+1, i+2, 2**(n-2-i)])
    m += 2
imp = 2**(n-1)
for i in range(n-1, 0, -1):
    if s[i] == '1':
        ans.append([1, i+1, imp])
        m += 1
        imp += 2 ** (n-1-i)
print(n, m)
for x in ans:
    print(' '.join(list(map(str, x))))

Solution (Python):
import copy
n = int(input())
a = [2**x for x in range(0,21) ]
b = 0
for i, name in enumerate(a):
    b_copy = copy.deepcopy(b)
    b = name
    if b > n:
        break

ans = []
ans_l = []
c = n - b_copy
for j in range(i-1)[::-1]:
    if a[j] <= c:
        c -= a[j]
        ans.append(j)
        ans_l.append(b_copy)
        b_copy += a[j]

N = i
H = 0
A = []

for k in range(1, i):
    A.append([k, k+1, 0])
    A.append([k, k+1, 2**(k-1) ])
    H += 2
    if k-1 in ans:
        A.append([k, N, ans_l[ans.index(k-1)]])
        H += 1

print(" ".join([str(N), str(H)]))
for kk in A:
    print(" ".join(list(map(str, kk))))

Solution (Python):
l = int(input())
n = 1
ll = l
while ll > 1:
    n += 1
    ll = ll // 2
s = 1
e = []
while l > 1:
    h = l // 2
    e.append((s, s + 1, 0))
    e.append((s, s + 1, h))
    if l > h * 2:
        e.append((s, n, l - 1))
    l = h
    s += 1

print(n, len(e))
for x, y, w in e:
    print(x, y, w)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


l = I()
ans = []
d = len(bin(l)) - 2
ret = 2 ** (d - 1)


remain = l-(2**(d - 1))
ret = 2 ** (d - 1)
for i in range(d-1, 0, -1):
    ans += [(i, i + 1, 0)]
    ans += [(i, i + 1, 2 ** (i - 1))]
    if 1 << (i-1) & remain:
        ans += [(i, d, ret)]
        ret += 2 ** (i - 1)




print(d, len(ans))
for k in ans:
    print(*k)

Solution (Python):

l = int(input())
s = bin(l)[2:]
n = len(s)
m = 0
ans = []
for i in range(n-1):
  ans.append([i+1,i+2,0])
  ans.append([i+1,i+2,2**(n-2-i)])
  m += 2

imp = 2 ** (n-1)
for i in range(n-1,0,-1):
  if s[i] == '1':
    ans.append([1,i+1,imp])
    m += 1
    imp += 2 ** (n-1-i)
print(n,m)
for x in ans:
  print(' '.join(list(map(str,x))))

Solution (Python):
import sys
input = sys.stdin.readline

L = int(input())
ans = [[1,2,0],[1,2,1]]
S=1
T=2
def recursion(x):
    global T
    if x == 2:
        return
    
    if x%2==1:
        recursion(x-1)
        ans.append([S,T,x-1])
        
    else:
        recursion(x//2)
        for i in range(len(ans)):
            ans[i][2] = 2*ans[i][2]
        T += 1
        ans.append([T-1,T,0])
        ans.append([T-1,T,1])
    return

recursion(L)
print(T,len(ans))
for i,j,k in ans:
    print(i,j,k)

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))



L = I()

N = math.floor(math.log2(L))+1
M = 0
edge = []
for i in range(N-1):
    edge.append([i,i+1,0])
    edge.append([i,i+1,2**i])
    M += 2
for i in range(N-1)[::-1]:
    if L-2**i >= 2**(N-1):
        edge.append([i,N-1,L-2**i])
        M += 1
        L -= 2**i

print(N,M)
for e in edge:
    print(e[0]+1, e[1]+1, e[2])

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


n = math.floor(math.log2(l)) + 1
edge = []
for i in range(1, n):
    edge.append((i, i+1, 0))
for j, i in enumerate(reversed(range(1, n))):
    edge.append((i, i+1, 2**j))

l2 = l - 2**(n-1)
c = 2**(n-1)

while l2 > 0:
    l_mini = 1
    while l_mini * 2 <= l2:
        l_mini *= 2
    
    edge.append((1, n-int(math.log2(l_mini)), c))
    l2 -= l_mini
    c += l_mini

print(n, len(edge))
for i in range(len(edge)):
    print(*edge[i])

Solution (Python):
L = int(input())
L2 = bin(L)[2:]
N = len(L2)
L2d = L2[1:]
L2u = int("0b"+L2,0) - int("0b"+L2d,0)
L2u2 = L2u//2
tmp = 1
M = 0
Ans = []
if L2u != 0:
    tmpN = N
    while tmp != L2u:
        Ans.append([tmpN-1,tmpN,0])
        Ans.append([tmpN-1,tmpN,tmp])
        tmp *= 2
        tmpN -= 1
        M += 2
flag = 0
for i in range(len(L2d)):
    if L2d[i] == "1" and flag == 0:
        Ans.append([1, i+2, L2u])
        tmp = 2**(N-i-2)
        M += 1
        flag = 1
        B = L2u
    elif L2d[i] == "1":
        Ans.append([1, i+2, B+tmp])
        B = B+tmp
        tmp = 2**(N-i-2)
        M += 1
print(*[N,M])
for i in range(M):
    print(*Ans[i])

Solution (Python):
L=int(input());n=len(str(bin(L)))-3;e=[];h=2**n
for i in range(1,n+1):
  e+=[(i,i+1,0),(i,i+1,2**(n-i))]
  if 2**(n-i)&L:e+=[(1,i+1,h)];h+=2**(n-i)
print(n+1,len(e))
for v in e:print(*v)

Solution (Python):
L=int(input())
n=len(str(bin(L)))-3
e=[]
for i in range(n):
    e.append((i+1, i+2, 0))
    e.append((i+1, i+2, 2**(n-i-1)))
h=2**n
for i, c in enumerate(str(bin((L)))[2:]):
    if c=="1"and i:
        e.append((1,i+1,h))
        h+=2**(n-i)
print(n+1,len(e))
for i in e:print(*i)

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

r = 0
while 2 ** (r + 1) <= L:
    r += 1
    
N = r + 1
edges = []
for i in range(1, N):
    edges.append([i, i + 1, 0])
    edges.append([i, i + 1, 2 ** (i - 1)])
    
for t in reversed(range(1, N)):
    if L - 2 ** (t - 1) >= 2 ** r:
        edges.append([t, N, L - 2 ** (t - 1)])
        L -= 2 ** (t - 1)
    
print(N, len(edges))
for v in edges:
    print(*v)

Solution (Python):

import math


def main():
    l = int(input())
    n = math.floor(math.log2(l)) + 1
    e = []
    for v in range(n-1):
        v2 = 2 ** v
        e.append([v + 1, v + 2, 0])
        e.append([v + 1, v + 2, v2])
        if (l >> v) & 1:
            e.append([v + 1, n, l - v2])
            l -= v2
    print(n, len(e))
    for x in e:
        print(*x)


main()

Solution (Python):
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *

L = int(input())
n = int(log(L, 2))
es = []
m = min(n, 19)
for i in range(1, 20):
    es.append((i, i+1, 0))
for i in range(m):
    es.append((i+1, i+2, 1<<i))
t = L-(1<<m)
for i in range(20):
    if (t>>i) & 1:
        L -= 1<<i
        es.append((i+1, 20, L))
print(20, len(es))
for u, v, c in es:
    print(u, v, c)

Solution (Python):
l=bin(int(input()))[2:];n=len(l);print(n,2*n+l.count("1")-3)
for i in range(1,n):print(i,i+1,0);print(i,i+1,1<<(i-1))
w=1<<(n-1)
for i in range(1,n)[::-1]:
  if l[n-i]=="1":print(i,n,w);w+=1<<(i-1)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    return


def B():
    return


def C():
    return


def D():
    l = I()
    l = list(map(int,bin(l)[2:]))
    n = len(l)
    ans = []
    for i in range(n-1):
        ans.append((i+1,i+2,0))
        ans.append((i+1,i+2,1<<i))
    w = (1<<(n-1))
    for i in range(1,n):
        if l[i]:
            x = n-i
            ans.append((x,n,w))
            w += (1<<(x-1))
    print(n,len(ans))
    for x,y,w in ans:
        print(x,y,w)
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    D()

Solution (Python):

L = int(input())

digit = L.bit_length()  

if 2**digit > L :
    digit -= 1

ans = []
for i in range(1, digit+1) :
    ans.append([i, i+1, 0])
    ans.append([i, i+1, 2**(i-1)])

L = L - 2**digit  

tmp = 0
for i in range(digit, 0, -1) :
    if L - 2**(i - 1) >= 0 :
        ans.append([i, digit+1, 2**digit + tmp])
        L -= 2**(i - 1)
        tmp += 2**(i - 1)

print(digit+1, len(ans))
for a in ans :
    print(*a)

Solution (Python):
def gen_graph(i,step,L):
  
  
  if L == 1:
    return [i],[]
  v,e = gen_graph(i+1,2*step,L//2)
  v = [i] + v
  e = e + [(i,i+1,0),(i,i+1,step)]
  
  if L%2 == 0:
    return v,e
  e.append((i,v[-1],step*(L-1)))
  return v,e

L = int(input())

v,e = gen_graph(1,1,L)
print(len(v),len(e))
for a,b,c in e:
  print(a,b,c)