Calendar
Time limit1sMemory limit512 MB
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 places, so that a nanobot that was initially at position is now at position . Nanobots are indexed from . However, nanobots can only understand one command: reverse , which reverses the positions of all nanobots at positions between and , so that a nanobot that was initially at position is now at , the one that was at is now at , 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 and (, ), specifying the number of nanobots and the number of places to rotate.
Output
The first line of output should contain the integer , the number of reverse commands used. On each of the next lines, output two integers and (), which means that the next command is reverse .