Problem:
Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System; 
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Linq; 
using E = System.Linq.Enumerable; 


class Program {
   static long mod = 1000000007;
   static long end = 100000;
   static long st = 100000;
   
    static void Main(string[] args) {
        var NK = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
        var N = NK[0];
        var K = NK[1];
        
        var ans = new List<long>[N+1];
        ans[1] = new List<long>();
        for(int i = 2;i <= N;i++){
            ans[1].Add(i);
        }
        
        var count = (N-1)*(N-2)/2;
        var count2 = 0;
        for(int i = 2;i <= N;i++){
            ans[i] = new List<long>();
            for(int j = i+1;count > K && j <=N;j++){
                ans[i].Add(j);
                count--;
                count2++;
            }
        }
        
        if((N-1)*(N-2)/2 >= K){
            
        Console.WriteLine(count2+N-1);
        for(int i = 1;i <= N;i++){
            
            foreach(var item in ans[i]){
                Console.WriteLine(i+" "+item);
            }
        }
        }else{
            Console.WriteLine(-1);
        }
        
     }
     
     
     public static long UpperBound<T>(T[] a, T v) { return UpperBound(a, v, Comparer<T>.Default);
     }
     
     public static long UpperBound<T>(T[] a, T v, Comparer<T> cmp) {
         var l = st;
         var r = end;
         while (l <= r) { var mid = l + (r - l) / 2; var res = cmp.Compare(a[mid], v); if (res <= 0) l = mid + 1; else r = mid - 1; } 
         return l;
      }
     
     
     public static long RepPow(long x, long p, long m = 1000000007){ 
     if (p == 0) return 1;
     if (p == 1) return x % m; long b = RepPow(x, p / 2, m) % m; if (p % 2 == 0) { return (b * b) % m; } else { return (x * ((b * b) % m)) % m; }}
}
     
class UnionFind

 

{

 

// 親要素のインデックスを保持する

 

// 親要素が存在しない(自身がルートである)とき、マイナスでグループの要素数を持つ

 

public long[] Parents { get; set; }

 

public UnionFind(long n)

 

{

 

this.Parents = new long[n];

 

for (int i = 0; i < n; i++)

 

{

 

// 初期状態ではそれぞれが別のグループ(ルートは自分自身)

 

// ルートなのでマイナスで要素数(1個)を保持する

 

this.Parents[i] = -1;

 

}

 

}

 

 

 

// 要素xのルート要素はどれか

 

public long Find(long x)

 

{

 

// 親がマイナスの場合は自分自身がルート

 

if (this.Parents[x] < 0) return x;

 

// ルートが見つかるまで再帰的に探す

 

// 見つかったルートにつなぎかえる

 

this.Parents[x] = Find(this.Parents[x]);

 

return this.Parents[x];

 

}

 

 

 

// 要素xの属するグループの要素数を取得する

 

public long Size(long x)

 

{

 

// ルート要素を取得して、サイズを取得して返す

 

return -this.Parents[this.Find(x)];

 

}

 

 

 

// 要素x, yが同じグループかどうか判定する

 

public bool Same(long x, long y)

 

{

 

return this.Find(x) == this.Find(y);

 

}

 

 

 

// 要素x, yが属するグループを同じグループにまとめる

 

public bool Union(long x, long y)

 

{

 

// x, y のルート

 

x = this.Find(x);

 

y = this.Find(y);

 

// すでに同じグループの場合処理しない

 

if (x == y) return false;

 

 

 

// 要素数が少ないグループを多いほうに書き換えたい

 

if (this.Size(x) < this.Size(y))

 

{

 

var tmp = x;

 

x = y;

 

y = tmp;

 

}

 

// まとめる先のグループの要素数を更新

 

this.Parents[x] += this.Parents[y];

 

// まとめられるグループのルートの親を書き換え

 

this.Parents[y] = x;

 

return true;

 

}

 

}





     
                   
public class PriorityQueue<T> : IEnumerable<T> { private readonly List<T> _data = new List<T>(); private readonly IComparer<T> _comparer; private readonly bool _isDescending; public PriorityQueue(IComparer<T> comparer, bool isDescending = true) { _comparer = comparer; _isDescending = isDescending; } public PriorityQueue(Comparison<T> comparison, bool isDescending = true) : this(Comparer<T>.Create(comparison), isDescending) { } public PriorityQueue(bool isDescending = true) : this(Comparer<T>.Default, isDescending) { } public void Enqueue(T item) { _data.Add(item); var childIndex = _data.Count - 1; while (childIndex > 0) { var parentIndex = (childIndex - 1) / 2; if (Compare(_data[childIndex], _data[parentIndex]) >= 0) break; Swap(childIndex, parentIndex); childIndex = parentIndex; } } public T Dequeue() { var lastIndex = _data.Count - 1; var firstItem = _data[0]; _data[0] = _data[lastIndex]; _data.RemoveAt(lastIndex--); var parentIndex = 0; while (true) { var childIndex = parentIndex * 2 + 1; if (childIndex > lastIndex) break; var rightChild = childIndex + 1; if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0) childIndex = rightChild; if (Compare(_data[parentIndex], _data[childIndex]) <= 0) break; Swap(parentIndex, childIndex); parentIndex = childIndex; } return firstItem; } public T Peek() { return _data[0]; } private void Swap(int a, int b) { var tmp = _data[a]; _data[a] = _data[b]; _data[b] = tmp; } private int Compare(T a, T b) { return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b); } public int Count => _data.Count; public IEnumerator<T> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); }