D. Prime Graph
Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but couldn't think of anything inventive. Hence, he will be giving her a graph. How original, Bob! Alice will surely be thrilled!
When building the graph, he needs four conditions to be satisfied:
- It must be a simple undirected graph, i.e. without multiple (parallel) edges and self-loops.
- The number of vertices must be exactly n — a number he selected. This number is not necessarily prime.
- The total number of edges must be prime.
- The degree (i.e. the number of edges connected to the vertex) of each vertex must be prime.
Below is an example for n=4.
Note that the graph can be disconnected.
Please help Bob to find any such graph!
The input consists of a single integer 3≤n≤1000) — the number of vertices.
If there is no graph satisfying the conditions, print a single line containing the integer −1.
Otherwise, first print a line containing a prime number vi. The degree of each vertex must be prime. There must be no multiple (parallel) edges or self-loops.
If there are multiple solutions, you may print any of them.
Note that the graph can be disconnected.
4
5 1 2 1 3 2 3 2 4 3 4
8
13 1 2 1 3 2 3 1 4 2 4 1 5 2 5 1 6 2 6 1 7 1 8 5 8 7 8
The first example was described in the statement.
In the second example, the degrees of vertices are 13, is also a prime number, hence both conditions are satisfied.
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> #include <unordered_set> #include <unordered_map> #define ll long long #define PII pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) using namespace std; int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const double eps = 1e-6; const int mod =1e9+7; const int N = 1e5+5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } ll lcm(ll m, ll n) { return m * n / gcd(m, n); } bool prime(int x) { if (x < 2) return false; for (int i = 2; i * i <= x; ++i) { if (x % i == 0) return false; } return true; } ll qpow(ll m, ll k, ll mod) { ll res = 1, t = m; while (k) { if (k & 1) res = res * t % mod; t = t * t % mod; k >>= 1; } return res; } int main() { int m,n; cin >> n; m = n; while (!prime(m)) m++; cout << m << " 1 " << n << endl; rep(i, 1, n - 1) cout << i << " " << i + 1 << endl; rep(i, 1, m - n) cout << i << " " << i + n/2<< endl; return 0; }