Router 6

Construct a layered routing network of collectors, hubs, and distributors connecting N inputs to N outputs within given connection and power limits.

Medium6GraphImplementationMathNo attempts yetTime limit2sMemory limit512 MB

Problem

Henry and Hetty were hired by a networking company, and their first project is a router, the Connect Ethernet Operating Interface 2016. A router is made of:

  • NN input nodes, numbered 11 to NN,
  • NN output nodes, numbered N+1N+1 to 2N2N,
  • KK internal nodes, numbered 2N+12N+1 to 2N+K2N+K,
  • MM unidirectional direct connections, each one between two distinct nodes.

A node XX can send data to a node YY, which is the same as saying YY can receive data from XX, when X=YX = Y, or when some node ZZ exists such that XX can send data to ZZ and there is a direct connection from ZZ to YY.

When XX can send data to YY and XYX \neq Y, a data path from XX to YY is a set of direct connections {(A1,A2),(A2,A3),,(AL1,AL)}\{(A_1, A_2), (A_2, A_3), \ldots, (A_{L-1}, A_L)\} with L2L \geq 2, A1=XA_1 = X and AL=YA_L = Y.

The router works properly when all five conditions hold:

  • every input node can send data to every output node,
  • every input node can receive data only from itself,
  • every output node can send data only to itself,
  • for two nodes XYX \neq Y, if XX can send data to YY then YY cannot send data to XX,
  • for two nodes XYX \neq Y, if XX can send data to YY then the data path from XX to YY is unique. In particular, two nodes are joined by at most one direct connection.

The power needed to operate node XX is PX=INX×OUTXP_X = IN_X \times OUT_X, where INXIN_X is the number of input nodes that can send data to XX, and OUTXOUT_X is the number of output nodes that can receive data from XX. The maximum power used by the router is Pmax=max(P1,P2,,P2N+K)P_{max} = \max(P_1, P_2, \ldots, P_{2N+K}).

Build a router that works properly, has exactly NN input nodes and exactly NN output nodes, uses at most MlimM_{lim} direct connections, has PmaxPlimP_{max} \le P_{lim}, and uses at most 500000500\,000 nodes in total, so Ntot=2N+K500000N_{tot} = 2N + K \le 500\,000.

Many routers stay inside those limits, so the output section fixes one of them. Print that router.

Input

One line holds three integers NN, MlimM_{lim} and PlimP_{lim}: the number of input nodes, which equals the number of output nodes, the largest number of direct connections allowed, and the largest power allowed.

1N100001 \le N \le 10\,000, 1Mlim5000001 \le M_{lim} \le 500\,000, NPlim1000000N \le P_{lim} \le 1\,000\,000. Every input node XX has INX=1IN_X = 1 and OUTX=NOUT_X = N, so an answer exists only when PlimNP_{lim} \ge N.

In every test case the router described in the output section uses at most MlimM_{lim} direct connections.

Output

Print the router built by the rules below. It works properly and its maximum power is at most PlimP_{lim}.

Let

g=min(N,PlimN),c=Plim,b=cg.g = \min\left(N, \left\lfloor \frac{P_{lim}}{N} \right\rfloor\right), \qquad c = \left\lfloor \sqrt{P_{lim}} \right\rfloor, \qquad b = \left\lfloor \frac{c}{g} \right\rfloor.

Cut the input nodes into S=N/gS = \lceil N / g \rceil groups of gg consecutive nodes. Input group jj holds the input nodes (j1)g+1(j-1)g+1 to min(jg,N)\min(jg, N), so the last group can be smaller than the others. Cut the output nodes the same way: output group jj holds the output nodes N+(j1)g+1N + (j-1)g + 1 to N+min(jg,N)N + \min(jg, N).

Cut the group indices 11 to SS into B=S/bB = \lceil S / b \rceil blocks of bb consecutive indices. Block kk holds the group indices (k1)b+1(k-1)b+1 to min(kb,S)\min(kb, S), so group jj lies in block (j1)/b+1\lfloor (j-1)/b \rfloor + 1. The input side and the output side use the same blocks.

There are K=2S+B2K = 2S + B^2 internal nodes, numbered in this order:

  • one collector node per input group: node 2N+j2N + j belongs to input group jj,
  • one hub node per pair of blocks: node 2N+S+(k1)B+l2N + S + (k-1)B + l belongs to the pair (input block kk, output block ll),
  • one distributor node per output group: node 2N+S+B2+j2N + S + B^2 + j belongs to output group jj.

The direct connections, in the order they must be printed:

  1. for each input group jj in increasing order, and each input node xx of that group in increasing order, a connection from xx to the collector node of group jj,
  2. for each input group jj in increasing order, and each output block ll in increasing order, a connection from the collector node of group jj to the hub node of the pair (block of group jj, block ll),
  3. for each output group jj in increasing order, and each input block kk in increasing order, a connection from the hub node of the pair (block kk, block of group jj) to the distributor node of group jj,
  4. for each output group jj in increasing order, and each output node yy of that group in increasing order, a connection from the distributor node of group jj to yy.

That router has Ntot=2N+2S+B2N_{tot} = 2N + 2S + B^2 and M=2N+2SBM = 2N + 2SB.

On the first line print NtotN_{tot} and MM, separated by a space. On each of the next MM lines print two integers XX and YY, separated by a space, meaning a direct connection runs from node XX to node YY. Keep the order given above.