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)

use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

type Weight = i64;
#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
struct Edge {
    index: usize,
    src: usize,
    dest: usize,
    weight: Weight,
}
#[allow(dead_code)]
impl Edge {
    fn new(index: usize, src: usize, dest: usize, weight: Weight) -> Edge {
        Edge {
            index: index,
            src: src,
            dest: dest,
            weight: weight,
        }
    }
}
impl std::cmp::Ord for Edge {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (self.weight, self.src, self.dest).cmp(&(other.weight, other.src, other.dest))
    }
}
impl std::cmp::PartialOrd for Edge {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
type Edges = Vec<Edge>;
type Graph = Vec<Edges>;
#[allow(dead_code)]
fn graph_new(n: usize) -> Graph {
    vec![vec![]; n]
}
#[allow(dead_code)]
fn add_bi_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    graph[dest].push(Edge::new(*index, dest, src, weight));
    *index += 1;
}
#[allow(dead_code)]
fn add_uni_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    *index += 1;
}

fn main() {
    let l = read::<i64>();
    let g = solve(l);
    let n = g.len();
    let m = g.iter().fold(0, |v, es| v + es.len());
    println!("{} {}", n, m);
    for i in 0..n {
        for e in g[i].iter() {
            println!("{} {} {}", e.src + 1, e.dest + 1, e.weight);
        }
    }
}

fn solve(l: i64) -> Graph {
    let n = 20;
    let mut g = graph_new(n);
    let mut index = 0;
    let mut mask = 0;
    let mut prev = false;
    for i in 0..(n - 1) {
        let bit = 1 << (n - i - 2);
        if l >= (bit << 1) {
            add_uni_edge(&mut g, &mut index, i, i + 1, 0);
            add_uni_edge(&mut g, &mut index, i, i + 1, bit);
            if prev {
                add_uni_edge(&mut g, &mut index, 0, i, l & mask);
            }
            mask |= bit << 1;
        }
        if (l & bit) > 0 {
            prev = true;
        } else {
            prev = false;
        }
    }
    if prev {
        add_uni_edge(&mut g, &mut index, 0, n - 1, l - 1);
    }
    return g;
}

#[test]
fn test() {
    fn dfs(g: &Graph, from: usize, total: i64, visit: &mut Vec<i64>) -> bool {
        if from == g.len() - 1 {
            if visit[total as usize] != 0 {
                return false;
            }
            visit[total as usize] = 1;
        }
        for e in g[from].iter() {
            if !dfs(g, e.dest, total + e.weight, visit) {
                return false;
            }
        }
        return true;
    }
    let l = 1000000i64;
    // for l in 1..1000 {
    let g = solve(l);
    let mut visit = vec![0; l as usize];
    if !dfs(&g, 0, 0, &mut visit) {
        assert!(false, format!("l={}: visit twice", l));
    }
    for i in 0..l as usize {
        assert!(visit[i] == 1, format!("l={}: not visited {}", l, i));
    }
    for i in 0..g.len() {
        for e in g[i].iter() {
            assert!(e.weight <= 1e+6 as i64, format!("l={}: too heavy", l));
        }
    }
    let n = g.len();
    let m = g.iter().fold(0, |v, es| v + es.len());
    assert!(n <= 20, format!("l={}: too many vertex", l));
    assert!(m <= 60, format!("l={}: too many edge", l));
    // }
}