This page is still under construction.

Parts of this page are still being built. What you see may change.

Calendar

Time limit1sMemory limit512 MB

Summary
Find the minimum number of segment reversals that rotate an array of n elements cyclically by k positions, and output the reversals.
Level

Medium7 of 10

Topics
Array, Math, Implementation, Divide and conquer
Solved
No attempts yet

Problem

Handy Smurf created his newest invention: a nanobot calendar. It consists of nanobots showing the current date. Every day, to switch the current date, they have to perform a cyclic rotation by kk places, so that a nanobot that was initially at position ii is now at position (i+k) mod n(i+k) \bmod n. Nanobots are indexed from 00. However, nanobots can only understand one command: reverse ll rr, which reverses the positions of all nanobots at positions between ll and rr, so that a nanobot that was initially at position ll is now at rr, the one that was at l+1l+1 is now at r−1r-1, and so on. Help Handy write an algorithm for updating the date with the minimum number of commands issued.

Input

The first and only line of input contains two integers nn and kk (1≤n≤1091 \leq n \leq 10^9, 0≤k<n0 \leq k < n), specifying the number of nanobots and the number of places to rotate.

Output

The first line of output should contain the integer mm, the number of reverse commands used. On each of the next mm lines, output two integers aa and bb (0≤a≤b<n0 \leq a \leq b < n), which means that the next command is reverse aa bb.

Examples1

  1. Example 1

    Input
    2 1
    
    Expected output
    1
    0 1