C. Jamie and Interesting Graph
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Jamie has recently found undirected weighted graphs with the following properties veryinteresting:

  • The graph is connected and contains exactly n vertices andm edges.
  • All edge weights are integers and are in range [1, 109] inclusive.
  • The length of shortest path from 1 to n is a prime number.
  • The sum of edges' weights in the minimum spanning tree (MST) of the graph is a prime number.
  • The graph contains no loops or multi-edges.

If you are not familiar with some terms from the statement you can find definitions of them in notes section.

Help Jamie construct any graph with given number of vertices and edges that isinteresting!

Input

First line of input contains 2 integers n,m  — the required number of vertices and edges.

Output

In the first line output 2 integers sp,mstw (1 ≤ sp, mstw ≤ 1014) — the length of the shortest path and the sum of edges' weights in the minimum spanning tree.

In the next m lines output the edges of the graph. In each line output 3 integersu, v,w (1 ≤ u, v ≤ n, 1 ≤ w ≤ 109) describing the edge connectingu and v and having weightw.

Examples
Input
4 4
Output
7 7
1 2 3
2 3 2
3 4 2
2 4 4
Input
5 4
Output
7 13
1 2 2
1 3 4
1 4 3
4 5 4


思路:n-2条边用1,n-1~n的边用一个质数-(n-2)*1替代。 随意取,可以找一个比n-1大的质数x

其实n-2条边的权值可以是任意值,为了方便都取1

剩下的m-(n-1)边用很大的数去替代

最小生成树的值就是质数x,最短路的值也是x

主要是构造的时候找到规律

#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

bool is(int x){
    for(int j=2;j<=x-1;j++){
        if(x%j==0)  return false;
    }
    return true;
}

int p(int x){
    x++;
    for(int i=x;;i++){
        if(is(i))   return i;
    }
}

int main(void){
    int n,m;
    cin>>n>>m;
    m-=n-1;
    //cout <<"m="<<m<<endl;
    int mmin=p(n);
    cout << mmin <<" "<<mmin<< endl;
    for(int i=1;i<=n-2;i++) printf("%d %d %d\n",i,i+1,1);
    printf("%d %d %d\n",n-1,n,mmin-(n-2));
    if(m==0)    return 0;
    for(int i=1;i<=n-1;i++){
      for(int j=i+2;j<=n;j++){
        printf("%d %d %d\n",i,j,mmin+10086);
        m--;
        if(m==0)    return 0;
      }
    }
}