Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.

Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.

Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.

Input

The first line contains two integers n and k (3 ≤ n ≤ 2·105, 2 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes.

Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.

Output

In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids.

If there are multiple answers, print any of them.

Examples
input
3 2
output
2
1 2
2 3
input
5 3
output
3
1 2
2 3
3 4
3 5
Note

In the first example the only network is shown on the left picture.

In the second example one of optimal networks is shown on the right picture.

Exit-nodes are highlighted.

 

  直接贪心,目标是形成一个"菊花图",中间一个点,然后其它点周围一圈一圈地"围"着它。

  比如当n = 7, k = 3时,形成下面的一棵树

  计算的时候稍微判断一下n模k的余数(当被整除、余1和余数大于2的结果是不同的)然后特判一下就好了(详情可以看代码)。

Code

 /**
* Codeforces
* Problem#828D
* Accepted
* Time:78ms
* Memory:2052k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const signed long long llf = (signed long long)((1ull << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} int n, k; inline void init() {
readInteger(n);
readInteger(k);
} inline void solve() {
int temp = (n - );
int res = temp / k;
if(temp % k == ) res = res * + ;
else if(temp % k >= ) res = res * + ;
else res = res * ;
printf("%d\n", res);
for(int i = ; i <= k + ; i++)
printf("1 %d\n", i);
for(int i = k + ; i <= n; i++) {
printf("%d %d\n", i - k, i);
}
} int main() {
init();
solve();
return ;
}

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心的更多相关文章

  1. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块

    Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...

  2. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集

    Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...

  3. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

    Pronlem A In a small restaurant there are a tables for one person and b tables for two persons. It i ...

  4. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法

    Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...

  5. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力

    题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...

  6. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  7. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

  8. Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)

    Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A.String Reconstruction B. High Load C ...

  9. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组

    E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...

随机推荐

  1. mybatis核心文件详解

    MyBatis配置文件详解 configuration  这是配置文件的根元素标签,所有的其他元素都要在这个标签下使用. environments   用于管理所有环境,并可以指定默认使用哪个环境,通 ...

  2. DataGridView控件用法二:常用属性

    通常会设置的DataGridView的属性如下: AllowUserToAddRows - False指示是否向用户显示用于添加行的选项,列标题下面的一行空行将消失.一般让其消失.AllowUserT ...

  3. PLSQL oracle32位 oracle64 安装区别及注意问题

    一.先明确几个概念: 1.PLSQL 只有32位的. 2.oracle 客户端 分别有32.64位,一般使用32位. 3.oracle 服务端 分别有32.64位,一般老的服务器使用32位,新的服务器 ...

  4. html2pdf后逐页固定位置盖公章

    需要通过NuGet添加Html2Pdf引用 #region 通过Html来生成pdf,测试Html样式 /// <summary> /// 通过Html来生成pdf,测试Html样式 // ...

  5. python ---多进程 Multiprocessing

    和 threading 的比较 多进程 Multiprocessing 和多线程 threading 类似, 他们都是在 python 中用来并行运算的. 不过既然有了 threading, 为什么 ...

  6. python爬虫 ----文章爬虫(合理处理字符串中的\n\t\r........)

    import urllib.request import re import time num=input("输入日期(20150101000):") def openpage(u ...

  7. 说说html 的<!DOCTYPE>声明&标准模式与兼容模式

    我们都知道<!DOCTYPE>声明位于文档的最前面,处于<html>标签之前. <!DOCTYPE>声明不是html标签,它的作用:告知web浏览界面应该使用哪个h ...

  8. GDB && QString

    [1]GDB && QString GDB的print命令仅能打印基本数据类型,而像QString这样的复杂类型就无能为力了! 如果调试时不能看QString的值,很让人抓狂!!!幸好 ...

  9. Presto上使用SQL遇到的一些坑

    本文转载自:https://segmentfault.com/a/1190000013120454?utm_source=tag-newest 最近换了新工作,在数据处理方面,公司是用Presto连接 ...

  10. 【转】Tomcat 快速入门

    本文转载自:https://www.cnblogs.com/jingmoxukong/p/8258837.html?utm_source=gold_browser_extension 目录 Tomca ...