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 in Rust (1.15.1) (judge verdict: Accepted)

pub trait FromFragment: Sized {
    fn from_fragment(s: &str) -> Result<Self, String>;
}
impl FromFragment for String {
    fn from_fragment(s: &str) -> Result<String, String> {
        Ok(s.to_owned())
    }
}
macro_rules ! impl_from_fragment { ( $ ( $ t : ty ) * ) => { $ ( impl FromFragment for $ t { fn from_fragment ( s : & str ) -> Result <$ t , String > { use std :: str :: FromStr ; <$ t >:: from_str ( s ) . map_err ( | _ | { format ! ( "cannot parse \"{}\" as {}" , s , stringify ! ( $ t ) ) } ) } } ) * } ; }
impl_from_fragment ! ( isize usize i8 u8 i16 u16 i32 u32 i64 u64 f64 ) ;
pub trait FromLine: Sized {
    fn from_line(line: &str) -> Result<Self, String>;
}
impl<T: FromFragment> FromLine for T {
    fn from_line(line: &str) -> Result<T, String> {
        T::from_fragment(line.trim_right_matches('\n'))
    }
}
impl<T: FromFragment> FromLine for Vec<T> {
    #[doc = " Reads arbitrary number of `T`s."]
    fn from_line(line: &str) -> Result<Vec<T>, String> {
        let mut result = Vec::new();
        for fragment in line.trim_right_matches('\n').split_whitespace() {
            match T::from_fragment(fragment) {
                Ok(v) => result.push(v),
                Err(msg) => {
                    return Err(format!(
                        "fragment {} of line \"{}\": {}",
                        result.len() + 1,
                        line,
                        msg
                    ))
                }
            }
        }
        Ok(result)
    }
}
macro_rules ! impl_from_line_for_tuples { ( $ t : ident $ var : ident $ count : expr ) => ( ) ; ( $ t : ident $ var : ident $ count : expr ; $ ( $ inner_t : ident $ inner_var : ident $ inner_count : expr ) ;* ) => { impl_from_line_for_tuples ! ( $ ( $ inner_t $ inner_var $ inner_count ) ;* ) ; impl <$ t : FromFragment , $ ( $ inner_t : FromFragment ) ,*> FromLine for ( $ t , $ ( $ inner_t ) ,* ) { # [ allow ( unused_assignments ) ] fn from_line ( line : & str ) -> Result < ( $ t , $ ( $ inner_t ) ,* ) , String > { let fragments : Vec <& str > = line . trim_right_matches ( '\n' ) . split_whitespace ( ) . collect ( ) ; if fragments . len ( ) != $ count { return Err ( format ! ( "line \"{}\" has {} fragments, expected {}" , line , fragments . len ( ) , $ count ) ) ; } let mut iter = fragments . iter ( ) ; let mut n = 1 ; let $ var = <$ t >:: from_fragment ( iter . next ( ) . unwrap ( ) ) . map_err ( | msg | { format ! ( "fragment {} of line \"{}\": {}" , n , line , msg ) } ) ?; n += 1 ; $ ( let $ inner_var = <$ inner_t >:: from_fragment ( iter . next ( ) . unwrap ( ) ) . map_err ( | msg | { format ! ( "fragment {} of line \"{}\": {}" , n , line , msg ) } ) ?; n += 1 ; ) * Ok ( ( $ var , $ ( $ inner_var ) ,* ) ) } } } }
impl_from_line_for_tuples ! ( T4 x4 4 ; T3 x3 3 ; T2 x2 2 ; T1 x1 1 ) ;
#[macro_export]
macro_rules ! read { ( ) => { let mut line = String :: new ( ) ; std :: io :: stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; } ; ( $ pat : pat = $ t : ty ) => { let mut line = String :: new ( ) ; std :: io :: stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let $ pat = <$ t >:: from_line ( & line ) . unwrap ( ) ; } ; ( $ ( $ pat : pat = $ t : ty ) ,+ ) => { read ! ( ( $ ( $ pat ) ,* ) = ( $ ( $ t ) ,* ) ) ; } ; }
#[macro_export]
macro_rules ! readls { ( $ ( $ pat : pat = $ t : ty ) ,+ ) => { $ ( read ! ( $ pat = $ t ) ; ) * } }
#[macro_export]
macro_rules ! read_loop { ( $ pat : pat = $ t : ty ; $ block : block ) => { use std :: io :: BufRead ; let stdin = std :: io :: stdin ( ) ; for line in stdin . lock ( ) . lines ( ) { let line = line . expect ( "read from stdin failed" ) ; let $ pat = <$ t >:: from_line ( & line ) . unwrap ( ) ; $ block } } ; ( $ ( $ pat : pat = $ t : ty ) ,* ; $ block : block ) => { read_loop ! ( ( $ ( $ pat ) ,* ) = ( $ ( $ t ) ,* ) ; $ block ) ; } }
#[macro_export]
macro_rules ! readn_loop { ( $ n : expr ; $ pat : pat = $ t : ty ; $ block : block ) => { use std :: io :: BufRead ; let stdin = std :: io :: stdin ( ) ; { let lock = stdin . lock ( ) ; for _ in 0 ..$ n { let line = String :: new ( ) ; lock . read_line ( & mut line ) . expect ( "read from stdin failed" ) ; let $ pat = <$ t >:: from_line ( & line ) . unwrap ( ) ; $ block } } } ; ( $ n : expr ; $ ( $ pat : pat = $ t : ty ) ,* ; $ block : block ) => { read_times_loop ! ( $ n ; ( $ ( $ pat ) ,* ) = ( $ ( $ t ) ,* ) ; $ block ) ; } }

struct Bypass {
    dest_digit: u8,
    weight: u32
}

fn count_digit(n: u32) -> u8 {
    (n as f64).log2().floor() as u8
}

fn digit_to_point(digit: u8, max_digit: u8) -> u8 {
    max_digit - digit + 1
}

fn main() {
    read!(mut l = u32);
    let max_digit = count_digit(l);
    let diff = 2u32.pow(max_digit as u32);
    l -= diff;
    let mut bypasses = Vec::new();
    let mut next_weight = diff;

    while l != 0 {
        let digit = count_digit(l);
        let diff = 2u32.pow(digit as u32);
        l -= diff;
        bypasses.push(Bypass { dest_digit: digit, weight: next_weight });
        next_weight += diff;
    }

    println!("{} {}", max_digit+1, 2*max_digit + bypasses.len() as u8);
    for i in 0..max_digit {
        let d = digit_to_point(i, max_digit);
        println!("{} {} {}", d-1, d, 0);
        println!("{} {} {}", d-1, d, 2u32.pow(i as u32));
    }
    for b in bypasses {
        println!("1 {} {}", digit_to_point(b.dest_digit, max_digit), b.weight);
    }
}