Monocarp is playing a game "Assimilation IV". In this game he manages a great empire: builds cities and conquers new lands.Monocarp's empire has $$$n$$$ cities. In order to conquer new lands he plans to build one Monument in each city. The game is turn-based and, since Monocarp is still amateur, he builds exactly one Monument per turn.Monocarp has $$$m$$$ points on the map he'd like to control using the constructed Monuments. For each point he knows the distance between it and each city. Monuments work in the following way: when built in some city, a Monument controls all points at distance at most $$$1$$$ to this city. Next turn, the Monument controls all points at distance at most $$$2$$$, the turn after — at distance at most $$$3$$$, and so on. Monocarp will build $$$n$$$ Monuments in $$$n$$$ turns and his empire will conquer all points that are controlled by at least one Monument.Monocarp can't figure out any strategy, so during each turn he will choose a city for a Monument randomly among all remaining cities (cities without Monuments). Monocarp wants to know how many points (among $$$m$$$ of them) he will conquer at the end of turn number $$$n$$$. Help him to calculate the expected number of conquered points!

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 20$$$; $$$1 \le m \le 5 \cdot 10^4$$$) — the number of cities and the number of points. Next $$$n$$$ lines contains $$$m$$$ integers each: the $$$j$$$-th integer of the $$$i$$$-th line $$$d_{i, j}$$$ ($$$1 \le d_{i, j} \le n + 1$$$) is the distance between the $$$i$$$-th city and the $$$j$$$-th point.

It can be shown that the expected number of points Monocarp conquers at the end of the $$$n$$$-th turn can be represented as an irreducible fraction $$$\frac{x}{y}$$$. Print this fraction modulo $$$998\,244\,353$$$, i. e. value $$$x \cdot y^{-1} \bmod 998244353$$$ where $$$y^{-1}$$$ is such number that $$$y \cdot y^{-1} \bmod 998244353 = 1$$$.

NoteLet's look at all possible orders of cities Monuments will be build in:   $$$[1, 2, 3]$$$:   the first city controls all points at distance at most $$$3$$$, in other words, points $$$1$$$ and $$$4$$$;  the second city controls all points at distance at most $$$2$$$, or points $$$1$$$, $$$3$$$ and $$$5$$$;  the third city controls all points at distance at most $$$1$$$, or point $$$1$$$.  In total, $$$4$$$ points are controlled.  $$$[1, 3, 2]$$$: the first city controls points $$$1$$$ and $$$4$$$; the second city — points $$$1$$$ and $$$3$$$; the third city — point $$$1$$$. In total, $$$3$$$ points.  $$$[2, 1, 3]$$$: the first city controls point $$$1$$$; the second city — points $$$1$$$, $$$3$$$ and $$$5$$$; the third city — point $$$1$$$. In total, $$$3$$$ points.  $$$[2, 3, 1]$$$: the first city controls point $$$1$$$; the second city — points $$$1$$$, $$$3$$$ and $$$5$$$; the third city — point $$$1$$$. In total, $$$3$$$ points.  $$$[3, 1, 2]$$$: the first city controls point $$$1$$$; the second city — points $$$1$$$ and $$$3$$$; the third city — points $$$1$$$ and $$$5$$$. In total, $$$3$$$ points.  $$$[3, 2, 1]$$$: the first city controls point $$$1$$$; the second city — points $$$1$$$, $$$3$$$ and $$$5$$$; the third city — points $$$1$$$ and $$$5$$$. In total, $$$3$$$ points.  The expected number of controlled points is $$$\frac{4 + 3 + 3 + 3 + 3 + 3}{6}$$$ $$$=$$$ $$$\frac{19}{6}$$$ or $$$19 \cdot 6^{-1}$$$ $$$\equiv$$$ $$$19 \cdot 166374059$$$ $$$\equiv$$$ $$$166374062$$$ $$$\pmod{998244353}$$$

import std.stdio;
import std.ascii;
import core.stdc.stdio;
import std.functional;
import std.algorithm;

int n;
const int maxN = 20;
int m;
const int maxM = 5_0000;
int[][] d;
const long mod = 998244353;
auto mpow(long base, long exp)
{
    base %= mod;
    long res = 1;
    while (exp)
    {
        if (exp & 1)
        {
            res *= base;
            res %= mod;
        }
        base *= base;
        base %= mod;
        exp >>= 1;
    }
    return res;
}
auto inv(long n)
{
    return mpow(n, mod - 2);
}

int main()
{
    n = readInt!int; m = readInt!int;
    long factN = 1;
    foreach(i; 1 .. n + 1) { factN *= i; factN %= mod; }
    d = new int[][](n, m);
    foreach(ref row; d)
    foreach(ref elm; row)
        elm = readInt!int;
    long sum = 0;
    foreach(i; 0 .. m)
    {
        long totalPerms = factN;
        auto badPositions = new long[](n);
        foreach(j; 0 .. n)
        {
            badPositions[j] = d[j][i] - 1;
        }
        sort(badPositions);
        long badPerms = 1;
        foreach(j; 0 .. n)
        {
            badPerms *= (badPositions[j] - j);
            badPerms %= mod;
        }
        debug writeln(badPerms);
        long goodPerms = totalPerms - badPerms;
        goodPerms %= mod;
        if (goodPerms < 0) goodPerms += mod;
        sum += goodPerms;
        sum %= mod;
    }
    writeln((sum * inv(factN)) % mod);
    return 0;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
    currChar = getchar();
}
char topChar()
{
    return cast(char) currChar;
}
void popChar()
{
    currChar = getchar();
}
auto readInt(T)()
{
    T num = 0;
    int sgn = 0;
    while (topChar.isWhite) popChar;
    while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }
    while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }
    if (sgn) return -num; return num;
}
string readString()
{
    string res = "";
    while (topChar.isWhite) popChar;
    while (!topChar.isWhite) { res ~= topChar; popChar; }
    return res;
}


Consider a conveyor belt represented using a grid consisting of $$$n$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row from the top and the $$$j$$$-th column from the left is labelled $$$(i,j)$$$. Every cell, except $$$(n,m)$$$, has a direction R (Right) or D (Down) assigned to it. If the cell $$$(i,j)$$$ is assigned direction R, any luggage kept on that will move to the cell $$$(i,j+1)$$$. Similarly, if the cell $$$(i,j)$$$ is assigned direction D, any luggage kept on that will move to the cell $$$(i+1,j)$$$. If at any moment, the luggage moves out of the grid, it is considered to be lost. There is a counter at the cell $$$(n,m)$$$ from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell $$$(i,j)$$$, any luggage placed in this cell should eventually end up in the cell $$$(n,m)$$$. This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 100$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th line, $$$a_{i,j}$$$ is the initial direction of the cell $$$(i, j)$$$. Please note that $$$a_{n,m}=$$$ C.

For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.

4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C

NoteIn the first case, just changing the direction of $$$(2,3)$$$ to D is enough.You can verify that the resulting belt is functional. For example, if we place any luggage at $$$(2,2)$$$, it first moves to $$$(3,2)$$$ and then to $$$(3,3)$$$. In the second case, we have no option but to change the first $$$3$$$ cells from D to R making the grid equal to RRRC.

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int rows, cols;
		readf !(" %s %s") (rows, cols);
		readln;
		auto board = rows.iota.map !(_ => readln.strip).array;
		int res = 0;
		foreach (row; 0..rows - 1)
		{
			res += (board[row][cols - 1] != 'D');
		}
		foreach (col; 0..cols - 1)
		{
			res += (board[rows - 1][col] != 'R');
		}
		writeln (res);
	}
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto a = new string[](n);
		foreach (i; 0..n)
		{
			a[i] = RD!string;
		}

		foreach (i; 0..n)
		{
			if (a[i][$-1] == 'R')
				++ans[ti];
		}
		foreach (i; 0..m)
		{
			if (a[$-1][i] == 'D')
				++ans[ti];
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}


You are given a connected undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. The weight of the $$$i$$$-th edge is $$$i$$$.Here is a wrong algorithm of finding a minimum spanning tree (MST) of a graph:vis := an array of length ns := a set of edgesfunction dfs(u):    vis[u] := true    iterate through each edge (u, v) in the order from smallest to largest edge weight        if vis[v] = false            add edge (u, v) into the set (s)            dfs(v)function findMST(u):    reset all elements of (vis) to false    reset the edge set (s) to empty    dfs(u)    return the edge set (s)Each of the calls findMST(1), findMST(2), ..., findMST(n) gives you a spanning tree of the graph. Determine which of these trees are minimum spanning trees.

The first line of the input contains two integers $$$n$$$, $$$m$$$ ($$$2\le n\le 10^5$$$, $$$n-1\le m\le 2\cdot 10^5$$$) — the number of vertices and the number of edges in the graph. Each of the following $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1\le u_i, v_i\le n$$$, $$$u_i\ne v_i$$$), describing an undirected edge $$$(u_i,v_i)$$$ in the graph. The $$$i$$$-th edge in the input has weight $$$i$$$. It is guaranteed that the graph is connected and there is at most one edge between any pair of vertices.

You need to output a binary string $$$s$$$, where $$$s_i=1$$$ if findMST(i) creates an MST, and $$$s_i = 0$$$ otherwise.

NoteHere is the graph given in the first example.  There is only one minimum spanning tree in this graph. A minimum spanning tree is $$$(1,2),(3,5),(1,3),(2,4)$$$ which has weight $$$1+2+3+5=11$$$.Here is a part of the process of calling findMST(1):  reset the array vis and the edge set s;  calling dfs(1);  vis[1] := true;  iterate through each edge $$$(1,2),(1,3)$$$;  add edge $$$(1,2)$$$ into the edge set s, calling dfs(2):   vis[2] := true  iterate through each edge $$$(2,1),(2,3),(2,4)$$$;  because vis[1] = true, ignore the edge $$$(2,1)$$$;  add edge $$$(2,3)$$$ into the edge set s, calling dfs(3):   ...   In the end, it will select edges $$$(1,2),(2,3),(3,5),(2,4)$$$ with total weight $$$1+4+2+5=12>11$$$, so findMST(1) does not find a minimum spanning tree.It can be shown that the other trees are all MSTs, so the answer is 01111.

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

int N, M;
int[] A, B;

int[][] G;
int[] dep;
enum E = 17;
int[][] par;
int[] fs;

void dfs(int u, int p) {
  dep[u] = (~p) ? (dep[p] + 1) : -1;
  par[0][u] = p;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
}
void DFS(int u, int p) {
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      fs[v] += fs[u];
      DFS(v, u);
    }
  }
}

int lca(int u, int v) {
  foreach_reverse (e; 0 .. E) {
    if (dep[u] - (1 << e) >= dep[v]) {
      u = par[e][u];
    }
    if (dep[v] - (1 << e) >= dep[u]) {
      v = par[e][v];
    }
  }
  foreach_reverse (e; 0 .. E) {
    if (par[e][u] != par[e][v]) {
      u = par[e][u];
      v = par[e][v];
    }
  }
  if (u != v) {
    u = par[0][u];
    v = par[0][v];
  }
  return u;
}

void main() {
  try {
    for (; ; ) {
      N = readInt;
      M = readInt;
      A = new int[M];
      B = new int[M];
      foreach (i; 0 .. M) {
        A[i] = readInt - 1;
        B[i] = readInt - 1;
      }

      auto uf = new int[N];
      uf[] = -1;
      auto on = new bool[M];
      foreach (i; 0 .. M) {
        on[i] = uf.connect(A[i], B[i]);
      }

      G = new int[][N];
      foreach (i; 0 .. M) if (on[i]) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      dep = new int[N];
      par = new int[][](E, N);
      dfs(0, -1);
      foreach (e; 0 .. E - 1) {
        foreach (u; 0 .. N) {
          const p = par[e][u];
          par[e + 1][u] = (~p) ? par[e][p] : -1;
        }
      }

      fs = new int[N];
      foreach (i; 0 .. M) if (!on[i]) {
        const l = lca(A[i], B[i]);
        if (l == A[i]) {
          int u = B[i];
          foreach_reverse (e; 0 .. E) {
            if (dep[u] - (1 << e) > dep[l]) {
              u = par[e][u];
            }
          }
          fs[u] += 1;
          fs[B[i]] -= 1;
        } else if (l == B[i]) {
          int u = A[i];
          foreach_reverse (e; 0 .. E) {
            if (dep[u] - (1 << e) > dep[l]) {
              u = par[e][u];
            }
          }
          fs[u] += 1;
          fs[A[i]] -= 1;
        } else {
          fs[0] += 1;
          fs[A[i]] -= 1;
          fs[B[i]] -= 1;
        }
      }
      DFS(0, -1);

      auto ans = new char[N];
      foreach (u; 0 .. N) {
        ans[u] = (fs[u] == 0) ? '1' : '0';
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray strictly greater than previous.

The first line contains single positive integer n (1 ≤ n ≤ 105) — the number of integers. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Print the maximum length of an increasing subarray of the given array.

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;

    int ans, tmp, prev;
    foreach (a; A) {
        tmp = (a > prev) ? tmp + 1 : 1;
        prev = a;
        ans = max(ans, tmp);
    }
    ans.writeln;
}


DZY owns 2m islands near his home, numbered from 1 to 2m. He loves building bridges to connect the islands. Every bridge he builds takes one day's time to walk across.DZY has a strange rule of building the bridges. For every pair of islands u, v (u ≠ v), he has built 2k different bridges connecting them, where  (a|b means b is divisible by a). These bridges are bidirectional.Also, DZY has built some bridges connecting his home with the islands. Specifically, there are ai different bridges from his home to the i-th island. These are one-way bridges, so after he leaves his home he will never come back.DZY decides to go to the islands for sightseeing. At first he is at home. He chooses and walks across one of the bridges connecting with his home, and arrives at some island. After that, he will spend t day(s) on the islands. Each day, he can choose to stay and rest, or to walk to another island across the bridge. It is allowed to stay at an island for more than one day. It's also allowed to cross one bridge more than once.Suppose that right after the t-th day DZY stands at the i-th island. Let ans[i] be the number of ways for DZY to reach the i-th island after t-th day. Your task is to calculate ans[i] for each i modulo 1051131.

To avoid huge input, we use the following way to generate the array a. You are given the first s elements of array: a1, a2, ..., as. All the other elements should be calculated by formula: ai = (101·ai - s + 10007) mod 1051131 (s < i ≤ 2m). The first line contains three integers m, t, s (1 ≤ m ≤ 25; 1 ≤ t ≤ 1018; 1 ≤ s ≤ min(2m, 105)). The second line contains s integers a1, a2, ..., as (1 ≤ ai ≤ 106).

To avoid huge output, you only need to output xor-sum of all the answers for all i modulo 1051131 (1 ≤ i ≤ 2m), i.e. (ans[1] mod 1051131) xor (ans[2] mod 1051131) xor... xor (ans[n] mod 1051131).

NoteIn the first sample, ans = [6, 7, 6, 6].If he wants to be at island 1 after one day, he has 6 different ways:  home —> 1 -(stay)-> 1  home —> 2 —> 1  home —> 3 —> 1  home —> 3 —> 1 (note that there are two different bridges between 1 and 3)  home —> 4 —> 1  home —> 4 —> 1 (note that there are two different bridges from home to 4) In the second sample, (a1, a2, a3, a4, a5, a6, a7, a8) = (389094, 705719, 547193, 653800, 947499, 17024, 416654, 861849), ans = [235771, 712729, 433182, 745954, 139255, 935785, 620229, 644335].

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

//	Input
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, const T f) { if (t > f) t = f; }
void chmax(T)(ref T t, const T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(S x, T y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(T[] as, bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

/*
	A[0] = [ 0 ]
	A[1] = [ 0 1 ]
	       [ 0 1 ]
	A[2] = [ 0 1 2 1 ]
	       [ 1 0 1 2 ]
	       [ 2 1 0 1 ]
	       [ 1 2 1 0 ]
	A[m] = [ A[m-1]              A[m-1] + 2^(m-1) I ]
	       [ A[m-1] + 2^(m-1) I  A[m-1]             ]

	[ A + p I  A + q I ]^e [ X ]
	[ A + q I  A + p I ]   [ Y ]

	= (1/2) [  1   1 ] [ 2 A + (p + q) I            ]^e [  1   1 ] [ X ]
	        [  1  -1 ] [                  (p - q) I ]   [  1  -1 ] [ Y ]

	A[m] + r I = [ A[m-1] + r       I  A[m-1] + 2^(m-1) I ]
	             [ A[m-1] + 2^(m-1) I  A[m-1] + r       I ]
*/

immutable MO = 1051131;
immutable long INV2 = (1 + MO) / 2;

long power(long a, long e) {
	long x = a, y = 1;
	for (; e; e >>= 1, (x *= x) %= MO) if (e & 1) (y *= x) %= MO;
	return y;
}

int[] x;
long[] y;
void solve(int m, long p, long q, long e) {
// writeln(m," ",p," ",q," ",e);
	foreach (i; 0 .. 1 << m) {
		const t = x[i + (1 << m)];
		x[i + (1 << m)] = (x[i] - t) % MO;
		(x[i] += t) %= MO;
	}
	const r = ((p + q) * INV2) % MO;
	if (m == 0) {
		y[0] = (power(r, e) * x[0]) % MO;
	} else {
		solve(m - 1, r, 1 << (m - 1), e);
	}
	y[0 .. 1 << m] *= power(2, e);
	y[0 .. 1 << m] %= MO;
	// y[1 << m .. 1 << (m + 1)] = (x[1 << m .. 1 << (m + 1)] * power(p - q, e)) % MO;
	const c = power(p - q, e);
	foreach (i; 1 << m .. 1 << (m + 1)) {
		y[i] = (x[i] * c) % MO;
	}
	foreach (i; 0 .. 1 << m) {
		const t = y[i + (1 << m)];
		y[i + (1 << m)] = (y[i] - t) % MO;
		(y[i] += t) %= MO;
	}
	y[0 .. 1 << (m + 1)] *= INV2;
	y[0 .. 1 << (m + 1)] %= MO;
}

int M;
long T;
int S;

void main(string[] args) {
	try {
	for (; ; ) {
		M = readInt;
		T = readLong;
		S = readInt;

		x = new int[1 << M];
		foreach (i; 0 .. S) {
			x[i] = readInt;
		}
		foreach (i; S .. 1 << M) {
			x[i] = (101 * x[i - S] + 10007) % 1051131;
		}

		y = new long[1 << M];
		solve(M - 1, 1, 1 << (M - 1), T);

		foreach (i; 0 .. 1 << M) {
			y[i] = (y[i] % MO + MO) % MO;
		}
// if(M<=3)writeln(y);
		long ans;
		foreach (i; 0 .. 1 << M) {
			ans ^= y[i];
		}
		writeln(ans);

	}
	} catch (EOFException) {}
}


You are given an array of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. After you watched the amazing film "Everything Everywhere All At Once", you came up with the following operation.In one operation, you choose $$$n-1$$$ elements of the array and replace each of them with their arithmetic mean (which doesn't have to be an integer). For example, from the array $$$[1, 2, 3, 1]$$$ we can get the array $$$[2, 2, 2, 1]$$$, if we choose the first three elements, or we can get the array $$$[\frac{4}{3}, \frac{4}{3}, 3, \frac{4}{3}]$$$, if we choose all elements except the third.Is it possible to make all elements of the array equal by performing a finite number of such operations?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 200$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$)  — the number of integers. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 100$$$).

For each test case, if it is possible to make all elements equal after some number of operations, output $$$\texttt{YES}$$$. Otherwise, output $$$\texttt{NO}$$$. You can output $$$\texttt{YES}$$$ and $$$\texttt{NO}$$$ in any case (for example, strings $$$\texttt{yEs}$$$, $$$\texttt{yes}$$$, $$$\texttt{Yes}$$$ will be recognized as a positive response).

NoteIn the first test case, all elements are already equal.In the second test case, you can choose all elements except the third, their average is $$$\frac{1 + 2 + 4 + 5}{4} = 3$$$, so the array will become $$$[3, 3, 3, 3, 3]$$$.It's possible to show that it's impossible to make all elements equal in the third and fourth test cases.

import std.stdio;
import std.conv;
import std.string;
import std.algorithm;

string foo(){

		int n;
		readf("%d\n", n);
		int[] arr = to!(int[])(strip(readln).split(" "));
		int total = sum(arr);
		for (int i = 0; i < n; i++) {
			double arithMean = total - arr[i];
			if (arr[i]  == arithMean/(n-1)) {
				return "YES";
				break;
			}
		}
	return "NO";
}

void main() {
	int t;
	readf("%d\n", t);
	while (t--) {
		writeln(foo);
	}
}


This is an interactive problem.Little Dormi was faced with an awkward problem at the carnival: he has to guess the edges of an unweighted tree of $$$n$$$ nodes! The nodes of the tree are numbered from $$$1$$$ to $$$n$$$.The game master only allows him to ask one type of question: Little Dormi picks a node $$$r$$$ ($$$1 \le r \le n$$$), and the game master will reply with an array $$$d_1, d_2, \ldots, d_n$$$, where $$$d_i$$$ is the length of the shortest path from node $$$r$$$ to $$$i$$$, for all $$$1 \le i \le n$$$.Additionally, to make the game unfair challenge Little Dormi the game master will allow at most $$$\lceil\frac{n}{2}\rceil$$$ questions, where $$$\lceil x \rceil$$$ denotes the smallest integer greater than or equal to $$$x$$$.Faced with the stomach-churning possibility of not being able to guess the tree, Little Dormi needs your help to devise a winning strategy!Note that the game master creates the tree before the game starts, and does not change it during the game.

The first line of input contains the integer $$$n$$$ ($$$2 \le n \le 2\,000$$$), the number of nodes in the tree. You will then begin interaction.

When your program has found the tree, first output a line consisting of a single "!" followed by $$$n-1$$$ lines each with two space separated integers $$$a$$$ and $$$b$$$, denoting an edge connecting nodes $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le n$$$). Once you are done, terminate your program normally immediately after flushing the output stream. You may output the edges in any order and an edge $$$(a,b)$$$ is considered the same as an edge $$$(b,a)$$$. Answering is not considered as a query.

NoteHere is the tree from the first example.  Notice that the edges can be output in any order.Additionally, here are the answers for querying every single node in example $$$1$$$: $$$1$$$: $$$[0,1,2,2]$$$  $$$2$$$: $$$[1,0,1,1]$$$  $$$3$$$: $$$[2,1,0,2]$$$  $$$4$$$: $$$[2,1,2,0]$$$Below is the tree from the second example interaction.  Lastly, here are the answers for querying every single node in example $$$2$$$: $$$1$$$: $$$[0,4,1,3,2]$$$  $$$2$$$: $$$[4,0,3,1,2]$$$  $$$3$$$: $$$[1,3,0,2,1]$$$  $$$4$$$: $$$[3,1,2,0,1]$$$  $$$5$$$: $$$[2,2,1,1,0]$$$

import std.stdio;
import std.typecons;
import std.ascii;
import std.algorithm;
import std.range;
import std.container;

void main()
{
	int n = readInt!int;
	auto edges = new RedBlackTree!(Tuple!(int, int))();
	int[] query(int r)
	{
		debug writeln("asking for ", r);
		r += 1;
		writeln("? ", r);
		stdout.flush;
		int[] ans = new int[](n);
		foreach(ref ansi; ans) ansi = readInt!int;
		return ans;
	}
	void addEdges(int r, int[] dist)
	{
		foreach(i, disti; dist)
			if (disti == 1)
			{
				debug writeln("dist ", r, " -> ", i);
				auto mn = min(cast(int)i, r);
				auto mx = max(cast(int)i, r);
				edges.insert(tuple(mn, mx));

			}
	}
	auto dist0 = query(0);
	addEdges(0, dist0);
	int[2] cnt;
	foreach(di; dist0)
	{
		if (di >= 1)
		{
			cnt[di%2]++;
		}
	}
	int s = 0;
	if (cnt[1] < cnt[0]) s = 1;
	foreach(i, di; dist0)
	{
		if (di >= 1 && di % 2 == s)
		{
			addEdges(cast(int)i, query(cast(int)i));
		}
	}
	writeln("!");
	foreach(edge; edges)
	{
		writeln(edge[0] + 1, " ", edge[1] + 1);
	}
	writeln;
	stdout.flush;
}

/* INPUT ROUTINES */
static import core.stdc.stdio;
int currChar;
static this()
{
	currChar = ' ';
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = core.stdc.stdio.getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

// code by cutx64

module main;

import std.string, std.array, std.container, std.typecons;
import std.stdio, std.format, std.file;
import std.algorithm, std.conv, std.functional, std.range, core.bitop;
// import std.bigint, std.complex, std.bitmanip;
import std.math, std.mathspecial, std.numeric;

void main() {

  int n;
  while(readf!"%s\n"(n)> 0) {
    auto adj = new bool[int][n];
    int[] ask(int v) {
      writefln!"? %s"(v + 1);
      stdout.flush();
      auto input = readln.splitter.map!(to!int).array;
      foreach(i; 0 .. n) {
        if (input[i] == 1) {
          adj[v][i] = true;
          adj[i][v] = true;
        }
      }
      return input;
    }
    auto d = ask(0);
    auto odds = iota(1, n).filter!(i => (d[i] & 1) == 1).array;
    auto evens = iota(1, n).filter!(i => (d[i] & 1) == 0).array;
    auto list = (odds.length < evens.length) ? odds : evens;
    foreach (ref v; list) {
      ask(v);
    }
    writeln("!");
    foreach(i; 0 .. n) {
      foreach(j, _; adj[i]) {
        if(j > i) {
          writefln!"%s %s\n"(i + 1, j + 1);
        }
      }
    }
    stdout.flush();
  }

} // main

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;

		auto adj = new bool [int] [n];

		int [] ask (int v)
		{
			writeln ("? ", v + 1);
			stdout.flush ();

			auto res = readln.splitter.map !(to !(int)).array;
			foreach (i; 0..n)
			{
				if (res[i] == 1)
				{
					adj[v][i] = true;
					adj[i][v] = true;
				}
			}
			return res;
		}

		auto d = ask (0);
		auto odds = iota (1, n).filter !(i => (d[i] & 1) == 1).array;
		auto evens = iota (1, n).filter !(i => (d[i] & 1) == 0).array;
		auto list = (odds.length < evens.length) ? odds : evens;
		foreach (ref v; list)
		{
			ask (v);
		}

		writeln ("!");
		foreach (i; 0..n)
		{
			foreach (j, _; adj[i])
			{
				if (j > i)
				{
					writeln (i + 1, " ", j + 1);
				}
			}
		}
		stdout.flush ();
	}
}


You are given a positive integer $$$n$$$. Let's call some positive integer $$$a$$$ without leading zeroes palindromic if it remains the same after reversing the order of its digits. Find the number of distinct ways to express $$$n$$$ as a sum of positive palindromic integers. Two ways are considered different if the frequency of at least one palindromic integer is different in them. For example, $$$5=4+1$$$ and $$$5=3+1+1$$$ are considered different but $$$5=3+1+1$$$ and $$$5=1+3+1$$$ are considered the same. Formally, you need to find the number of distinct multisets of positive palindromic integers the sum of which is equal to $$$n$$$.Since the answer can be quite large, print it modulo $$$10^9+7$$$.

The first line of input contains a single integer $$$t$$$ ($$$1\leq t\leq 10^4$$$) denoting the number of testcases. Each testcase contains a single line of input containing a single integer $$$n$$$ ($$$1\leq n\leq 4\cdot 10^4$$$) — the required sum of palindromic integers.

For each testcase, print a single integer denoting the required answer modulo $$$10^9+7$$$.

NoteFor the first testcase, there are $$$7$$$ ways to partition $$$5$$$ as a sum of positive palindromic integers:   $$$5=1+1+1+1+1$$$  $$$5=1+1+1+2$$$  $$$5=1+2+2$$$  $$$5=1+1+3$$$  $$$5=2+3$$$  $$$5=1+4$$$  $$$5=5$$$ For the second testcase, there are total $$$77$$$ ways to partition $$$12$$$ as a sum of positive integers but among them, the partitions $$$12=2+10$$$, $$$12=1+1+10$$$ and $$$12=12$$$ are not valid partitions of $$$12$$$ as a sum of positive palindromic integers because $$$10$$$ and $$$12$$$ are not palindromic. So, there are $$$74$$$ ways to partition $$$12$$$ as a sum of positive palindromic integers.

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;
  enum MAX = 4 * 10^^4 + 10;

  auto solve() {
    int[] palindromicNumbers(int limit) {
      int [] ret;
      foreach(i; 1..limit) {
        auto s = i.to!string;
        auto r = (cast(char[])s.dup).reverse.array.to!string;
        if (s == r) ret ~= i;
      }

      return ret;
    }

    auto dp = new MInt1[](MAX + 1);
    dp[0] = 1;
    foreach(p; palindromicNumbers(MAX)) {
      foreach(from; 0..MAX - p + 1) {
        dp[from + p] += dp[from];
      }
    }

    foreach(i; 0..QN) {
      auto N = scan!int;
      dp[N].writeln;
    }
  }

  outputForAtCoder(&solve);
}

// ----------------------------------------------

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

// -----------------------------------------------

// cheese-cracker [2022-05-03]

int[] pals;
long[] dp;
const long MOD = 1_000_000_007;

bool check_pal(int m){
    int[] wval;
    while(m > 0){
        wval ~= (m % 10);
        m /= 10;
    }
    int[] rev = wval.dup.reverse;
    return (wval == rev);
}

void preprocess(){
    int N = 50_000;
    for(int m = 1; m < N; ++m){
        if(check_pal(m)){
            pals ~= m;
        }
    }
    dp = new long[](N);
    dp[0] = 1;
    foreach(k; pals){
        for(int m = k; m < N; ++m){
            dp[m] += dp[m - k];
            dp[m] %= MOD;
        }
    }
}

void solve(){
    writeln(dp[scan!int]);
}

void main(){
    preprocess;
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }


Dreamoon likes to play with sets, integers and .  is defined as the largest positive integer that divides both a and b.Let S be a set of exactly four distinct integers greater than 0. Define S to be of rank k if and only if for all pairs of distinct elements si, sj from S, .Given k and n, Dreamoon wants to make up n sets of rank k using integers from 1 to m such that no integer is used in two different sets (of course you can leave some integers without use). Calculate the minimum m that makes it possible and print one possible solution.

The single line of the input contains two space separated integers n, k (1 ≤ n ≤ 10 000, 1 ≤ k ≤ 100).

On the first line print a single integer — the minimal possible m.  On each of the next n lines print four space separated integers representing the i-th set. Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal m, print any one of them.

NoteFor the first example it's easy to see that set {1, 2, 3, 4} isn't a valid set of rank 1 since .

import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

int fun (int i, int x, int k) {return (i * 6 + x) * k;}

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		writeln ((n * 6 - 1) * k);
		foreach (i; 0..n)
		{
			writeln ((i * 6 + 1) * k, ' ',
			(i * 6 + 2) * k, ' ',
			(i * 6 + 3) * k, ' ',
			(i * 6 + 5) * k);
		}
	}
}

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

//	Input
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

immutable LIM = 2_000_000;

int N;
long K;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		K = readInt;

		writeln(K * (6 * N - 1));
		foreach (i; 0 .. N) {
			writeln(
				K * (6 * i + 1), " ",
				K * (6 * i + 2), " ",
				K * (6 * i + 3), " ",
				K * (6 * i + 5)
			);
		}
	}
	} catch (EOFException) {}
}


Guy-Manuel and Thomas are going to build a polygon spaceship. You're given a strictly convex (i. e. no three points are collinear) polygon $$$P$$$ which is defined by coordinates of its vertices. Define $$$P(x,y)$$$ as a polygon obtained by translating $$$P$$$ by vector $$$\overrightarrow {(x,y)}$$$. The picture below depicts an example of the translation:Define $$$T$$$ as a set of points which is the union of all $$$P(x,y)$$$ such that the origin $$$(0,0)$$$ lies in $$$P(x,y)$$$ (both strictly inside and on the boundary). There is also an equivalent definition: a point $$$(x,y)$$$ lies in $$$T$$$ only if there are two points $$$A,B$$$ in $$$P$$$ such that $$$\overrightarrow {AB} = \overrightarrow {(x,y)}$$$. One can prove $$$T$$$ is a polygon too. For example, if $$$P$$$ is a regular triangle then $$$T$$$ is a regular hexagon. At the picture below $$$P$$$ is drawn in black and some $$$P(x,y)$$$ which contain the origin are drawn in colored: The spaceship has the best aerodynamic performance if $$$P$$$ and $$$T$$$ are similar. Your task is to check whether the polygons $$$P$$$ and $$$T$$$ are similar.

The first line of input will contain a single integer $$$n$$$ ($$$3 \le n \le 10^5$$$) — the number of points. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i, y_i$$$ ($$$|x_i|, |y_i| \le 10^9$$$), denoting the coordinates of the $$$i$$$-th vertex. It is guaranteed that these points are listed in counterclockwise order and these points form a strictly convex polygon.

Output "YES" in a separate line, if $$$P$$$ and $$$T$$$ are similar. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower).

NoteThe following image shows the first sample: both $$$P$$$ and $$$T$$$ are squares. The second sample was shown in the statements.

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

alias Point = Tuple !(int, q{x}, int, q{y});

auto vp (const ref Point a, const ref Point b,
    const ref Point c, const ref Point d)
{
	return (b.x - a.x) * 1L * (d.y - c.y) - (b.y - a.y) * 1L * (d.x - c.x);
}

auto rho2 (const ref Point a, const ref Point b)
{
	return (b.x - a.x) * 1L * (b.x - a.x) + (b.y - a.y) * 1L * (b.y - a.y);
}

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		auto p = new Point [n];
		foreach (ref c; p)
		{
			readf !(" %s %s") (c.x, c.y);
		}
		p ~= p;

		bool ok = true;
		int j = 0;
		foreach (i; 0..n)
		{
			j = max (j, i + 1);
			while (j < i + n &&
			    vp (p[i], p[i + 1], p[j], p[j + 1]) > 0)
			{
				j += 1;
			}
			if (!(j < i + n &&
			    vp (p[i], p[i + 1], p[j], p[j + 1]) == 0 &&
			    rho2 (p[i], p[i + 1]) == rho2 (p[j], p[j + 1])))
			{
				ok = false;
				break;
			}
		}
		writeln (ok ? "YES" : "NO");
	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint;
	P[] ps;
	foreach(i; 0 .. n) ps ~= P(rlong, rlong);

	if(n % 2 != 0){
		"NO".writeln;
		return;
	}

	P[] vs;
	foreach(i; 0 .. n - 1) vs ~= P(ps[i].x - ps[i + 1].x, ps[i].y - ps[i + 1].y);
	vs ~= P(ps[n - 1].x - ps[0].x, ps[n - 1].y - ps[0].y);

	foreach(i; 0 .. n / 2){
		P u = vs[i], v = vs[i + n / 2];
		if(u.x + v.x != 0 || u.y + v.y != 0){
			"NO".writeln;
			return;
		}
	}

	"YES".writeln;
	return;

}
struct P{
	long x, y;
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto X = new long[N];
      auto Y = new long[N];
      foreach (i; 0 .. N) {
        X[i] = readLong();
        Y[i] = readLong();
      }

      bool ans;
      if (N % 2 == 0) {
        auto dx = new long[N];
        auto dy = new long[N];
        foreach (i; 0 .. N) {
          dx[i] = X[(i + 1) % N] - X[i];
          dy[i] = Y[(i + 1) % N] - Y[i];
        }
        ans = true;
        foreach (i; 0 .. N / 2) {
          ans = ans && (-dx[i] == dx[i + N / 2]);
          ans = ans && (-dy[i] == dy[i + N / 2]);
        }
      }
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }

void main()
{
	auto n = RD!int;
	auto xy = new int[][](n);
	foreach (i; 0..n)
	{
		xy[i] = [RD!int, RD!int];
	}

	if (n % 2 == 1)
	{
		writeln("NO");
	}
	else
	{
		int[][] vec;
		foreach (i; 1..n/2+1)
		{
			int[] t = xy[i].dup;
			t[] -= xy[i-1][];
			vec ~= t;
		}
		bool ok = true;
		foreach (i; n/2..n)
		{
			int[] t = xy[(i+1)%n].dup;
			t[] -= xy[i][];
			t = [-t[0], -t[1]];
			if ((vec[i-n/2][0] != t[0]) || (vec[i-n/2][1] != t[1]))
			{
				ok = false;
				break;
			}
		}
		writeln(ok ? "YES" : "NO");
	}

	stdout.flush;
	debug readln;
}


The city where Mocha lives in is called Zhijiang. There are $$$n+1$$$ villages and $$$2n-1$$$ directed roads in this city. There are two kinds of roads:  $$$n-1$$$ roads are from village $$$i$$$ to village $$$i+1$$$, for all $$$1\leq i \leq n-1$$$.  $$$n$$$ roads can be described by a sequence $$$a_1,\ldots,a_n$$$. If $$$a_i=0$$$, the $$$i$$$-th of these roads goes from village $$$i$$$ to village $$$n+1$$$, otherwise it goes from village $$$n+1$$$ to village $$$i$$$, for all $$$1\leq i\leq n$$$. Mocha plans to go hiking with Taki this weekend. To avoid the trip being boring, they plan to go through every village exactly once. They can start and finish at any villages. Can you help them to draw up a plan?

Each test contains multiple test cases.  The first line contains a single integer $$$t$$$ ($$$1 \le t \le 20$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^4$$$) — indicates that the number of villages is $$$n+1$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 1$$$). If $$$a_i=0$$$, it means that there is a road from village $$$i$$$ to village $$$n+1$$$. If $$$a_i=1$$$, it means that there is a road from village $$$n+1$$$ to village $$$i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.

For each test case, print a line with $$$n+1$$$ integers, where the $$$i$$$-th number is the $$$i$$$-th village they will go through. If the answer doesn't exist, print $$$-1$$$. If there are multiple correct answers, you can print any one of them.

NoteIn the first test case, the city looks like the following graph:So all possible answers are $$$(1 \to 4 \to 2 \to 3)$$$, $$$(1 \to 2 \to 3 \to 4)$$$.In the second test case, the city looks like the following graph:So all possible answers are $$$(4 \to 1 \to 2 \to 3)$$$, $$$(1 \to 2 \to 3 \to 4)$$$, $$$(3 \to 4 \to 1 \to 2)$$$, $$$(2 \to 3 \to 4 \to 1)$$$.

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		if (a[n-1] == 0)
		{
			foreach (i; 0..n+1)
				ans[ti] ~= i+1;
			continue;
		}
		if (a[0] == 1)
		{
			ans[ti] ~= n+1;
			foreach (i; 0..n)
				ans[ti] ~= i+1;
			continue;
		}

		int pos = -1;
		foreach (i; 0..n-1)
		{
			if (a[i] == 0 && a[i+1] == 1)
			{
				pos = i;
				break;
			}
		}
		if (pos == -1)
			continue;
		else
		{
			foreach (i; 0..pos+1)
				ans[ti] ~= i+1;
			ans[ti] ~= n+1;
			foreach (i; pos+1..n)
				ans[ti] ~= i+1;
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
			e.map!(to!string).join(" ").writeln;
	}
	stdout.flush;
	debug readln;
}

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto a = ma(n, readInt!int);
	if (a[0] == 1)
	{
		write(n + 1, " ");
		foreach(i; 1 .. n + 1) write(i, " ");
		writeln;
		return;
	}
	foreach(i; 0 .. n)
	{
		if (a[i] == 0)
		{
			if (i+1 >= n || a[i+1] == 1)
			{
				foreach(j; 1 .. i + 2)
				{
					write(j, " ");
				}
				write(n + 1, " ");
				foreach(j; i + 2 .. n + 1)
				{
					write(j, " ");
				}
				writeln;
				return;
			}
		}
	}
	writeln(-1);
	return;
}

// main {{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
// }}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		if (a[n-1] == 0)
		{
			foreach (i; 0..n+1)
				ans[ti] ~= i+1;
			continue;
		}

		int pos = -1;
		foreach (i; 0..n-1)
		{
			if (a[i] == 0 && a[i+1] == 1)
			{
				pos = i;
				break;
			}
		}
		if (pos == -1)
			continue;
		else
		{
			foreach (i; 0..pos+1)
				ans[ti] ~= i+1;
			ans[ti] ~= n+1;
			foreach (i; pos+1..n)
				ans[ti] ~= i+1;
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
			e.map!(to!string).join(" ").writeln;
	}
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		if (a[n-1] == 0)
		{
			foreach (i; 0..n+1)
				ans[ti] ~= i+1;
			continue;
		}

		int pos = -1;
		foreach (i; 0..n-1)
		{
			if (a[i] == 0 && a[i+1] == 1)
			{
				pos = i;
				break;
			}
		}
		if (pos == -1)
			continue;
		else
		{
			foreach (i; 0..pos+1)
				ans[ti] ~= i+1;
			ans[ti] ~= n+1;
			foreach (i; pos+1..n)
				ans[ti] ~= i+1;
		}
	}

	foreach (e; ans)
		e.map!(to!string).join(" ").writeln;
	stdout.flush;
	debug readln;
}


After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use. The second line contains n digits — Sherlock's credit card number. The third line contains n digits — Moriarty's credit card number.

First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.

NoteFirst sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.

import std.stdio, std.string, std.conv, std.algorithm;

void main(){
	readln();
	auto a=cast(ubyte[])readln().strip.to!(char[]);
	auto b=cast(ubyte[])readln().strip.to!(char[]);
	sort(a), sort(b);
	static int solve(alias better)(ubyte[] a,ubyte[] b){
		int r=0;
		while(a.length){
			assert(a.length==b.length);
			if(better(b[0],a[0])){
				r++;
				a=a[1..$];
				b=b[1..$];
			}else{
				a=a[0..$-1];
				b=b[1..$];
			}
		}
		return r;
	}
	writeln(a.length-solve!((x,y)=>x>=y)(a,b));
	writeln(solve!((x,y)=>x>y)(a,b));
}


You are given a rooted tree with $$$n$$$ nodes, labeled from $$$1$$$ to $$$n$$$. The tree is rooted at node $$$1$$$. The parent of the $$$i$$$-th node is $$$p_i$$$. A leaf is node with no children. For a given set of leaves $$$L$$$, let $$$f(L)$$$ denote the smallest connected subgraph that contains all leaves $$$L$$$.You would like to partition the leaves such that for any two different sets $$$x, y$$$ of the partition, $$$f(x)$$$ and $$$f(y)$$$ are disjoint. Count the number of ways to partition the leaves, modulo $$$998244353$$$. Two ways are different if there are two leaves such that they are in the same set in one way but in different sets in the other.

The first line contains an integer $$$n$$$ ($$$2 \leq n \leq 200\,000$$$) — the number of nodes in the tree. The next line contains $$$n-1$$$ integers $$$p_2, p_3, \ldots, p_n$$$ ($$$1 \leq p_i < i$$$).

Print a single integer, the number of ways to partition the leaves, modulo $$$998244353$$$.

NoteIn the first example, the leaf nodes are $$$2,3,4,5$$$. The ways to partition the leaves are in the following image In the second example, the only leaf is node $$$10$$$ so there is only one partition. Note that node $$$1$$$ is not a leaf.

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

enum long MO = 998244353;

int N;
int[] P;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      P = new int[N];
      P[0] = -1;
      foreach (u; 1 .. N) {
        P[u] = readInt() - 1;
      }

      auto graph = new int[][N];
      foreach (u; 1 .. N) {
        graph[P[u]] ~= u;
      }

      auto dp = new long[][](N, 3);
      foreach_reverse (u; 0 .. N) {
        if (graph[u].empty) {
          dp[u] = [0, 0, 1];
        } else {
          auto crt = new long[3];
          crt[0] = 1;
          foreach (v; graph[u]) {
            auto nxt = new long[3];
            foreach (x; 0 .. 3) foreach (y; 0 .. 3) {
              if (y != 1) {
                nxt[x] += crt[x] * dp[v][y];
                nxt[x] %= MO;
              }
              if (y != 0) {
                nxt[min(x + 1, 2)] += crt[x] * dp[v][y];
                nxt[min(x + 1, 2)] %= MO;
              }
            }
            crt = nxt;
          }
          dp[u] = crt;
        }
      }
      debug {
        writeln("dp = ", dp);
      }
      long ans = (dp[0][0] + dp[0][2]) % MO;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


On one quiet day all of sudden Mister B decided to draw angle a on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex n-gon (regular convex polygon with n sides).That's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices v1, v2, v3 such that the angle  (where v2 is the vertex of the angle, and v1 and v3 lie on its sides) is as close as possible to a. In other words, the value  should be minimum possible.If there are many optimal solutions, Mister B should be satisfied with any of them.

First and only line contains two space-separated integers n and a (3 ≤ n ≤ 105, 1 ≤ a ≤ 180) — the number of vertices in the polygon and the needed angle, in degrees.

Print three space-separated integers: the vertices v1, v2, v3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to n in clockwise order.

NoteIn first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct.Vertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since |45 - 67| < |90 - 67|. Other correct answers are: "3 1 2", "3 2 4", "4 2 3", "4 3 1", "1 3 4", "1 4 2", "2 4 1", "4 1 3", "3 1 4", "3 4 2", "2 4 3", "2 3 1", "1 3 2", "1 2 4", "4 2 1".In third sample test, on the contrary, the angle of 90 degrees was chosen, since |90 - 68| < |45 - 68|. Other correct answers are: "2 1 4", "3 2 1", "1 2 3", "4 3 2", "2 3 4", "1 4 3", "3 4 1".

import std.stdio, std.math, std.algorithm;

void main()
{
    int n, a;
    readf("%d %d", &n, &a);

    writeln(2, " ", 1, " ", min(n, max(3, 2+round(a*n/180.))));
}

// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable inf = 10^^9 + 7;

int n, a;

void main() {
    scan(n, a);

    int dif = inf;
    int mk;

    foreach (k ; 1 .. n - 1) {
        if (dif > abs(a*n - 180*k)) {
            dif = abs(a*n - 180*k);
            mk = k;
        }
    }

    writefln("%s %s %s", 1, 2, n + 1 - mk);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

import std.stdio, std.math, std.algorithm;

void main()
{
    int n, a;
    readf("%d %d", &n, &a);

    writeln(2, " ", 1, " ", 2+max(1, round(a*n/180.)));
}


This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.The jury guessed some array $$$a$$$ consisting of $$$6$$$ integers. There are $$$6$$$ special numbers — $$$4$$$, $$$8$$$, $$$15$$$, $$$16$$$, $$$23$$$, $$$42$$$ — and each of these numbers occurs in $$$a$$$ exactly once (so, $$$a$$$ is some permutation of these numbers).You don't know anything about their order, but you are allowed to ask up to $$$4$$$ queries. In each query, you may choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le 6$$$, $$$i$$$ and $$$j$$$ are not necessarily distinct), and you will get the value of $$$a_i \cdot a_j$$$ in return.Can you guess the array $$$a$$$?The array $$$a$$$ is fixed beforehand in each test, the interaction program doesn't try to adapt to your queries.

NoteIf you want to submit a hack for this problem, your test should contain exactly six space-separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_6$$$. Each of $$$6$$$ special numbers should occur exactly once in the test. The test should be ended with a line break character.

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

void main() {
    auto X = new int[](6);
    foreach (i; 0..4) {
        writeln("? ", i+1, " ", i+2);
        stdout.flush;
        X[i] = readln.chomp.to!int;
    }

    int[] A = [4, 8, 15, 16, 23, 42];
    do {
        bool ok = true;
        foreach (i; 0..4) {
            if (A[i] * A[i+1] != X[i]) {
                ok = false;
                break;
            }
        }
        if (ok) {
            writeln("! ", A.map!(to!string).join(" "));
            return;
        }
    } while (nextPermutation(A));
}

import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }

void main(string[] args){
	if(args.length > 1 && args[1] == "-debug"){
		if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
		else DEBUG_LEVEL = 1;
	}

	writeln("? 1 2");
	stdout.flush;
	long a12 = read.to!long;

	writeln("? 1 3");
	stdout.flush;
	long a13 = read.to!long;

	writeln("? 1 4");
	stdout.flush;
	long a14 = read.to!long;

	writeln("? 1 5");
	stdout.flush;
	long a15 = read.to!long;

	long a1 = -1, a2 = -1, a3 = -1, a4 = -1, a5 = -1, a6 = -1;

	if(a12 % 23 == 0){
		if(a13 % 23 == 0) a1 = 23;
		else a2 = 23;
	}
	else if(a13 % 23 == 0) a3 = 23;
	else if(a14 % 23 == 0) a4 = 23;
	else if(a15 % 23 == 0) a5 = 23;
	else a6 = 23;

	if(a12 % 5 == 0){
		if(a13 % 5 == 0) a1 = 15;
		else a2 = 15;
	}
	else if(a13 % 5 == 0) a3 = 15;
	else if(a14 % 5 == 0) a4 = 15;
	else if(a15 % 5 == 0) a5 = 15;
	else a6 = 15;

	if(a2 > 0) a1 = a12 / a2;
	if(a3 > 0) a1 = a13 / a3;
	if(a4 > 0) a1 = a14 / a4;
	if(a5 > 0) a1 = a15 / a5;

	if(a2 < 0) a2 = a12 / a1;
	if(a3 < 0) a3 = a13 / a1;
	if(a4 < 0) a4 = a14 / a1;
	if(a5 < 0) a5 = a15 / a1;
	a6 = (4 + 8 + 15 + 16 + 23 + 42) - (a1 + a2 + a3 + a4 + a5);

	writeln("! ", a1, " ", a2, " ", a3, " ", a4, " ", a5, " ", a6);
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

//long mod = 10^^9 + 7;
long mod = 998244353;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	bool[long] numbers = [4:true, 8:true, 15:true, 16:true, 23:true, 42:true];
	long[] ans;
	foreach (i; 0..2)
	{
		writeln("? ", i*3+1, " ", i*3+2);
		stdout.flush();
		auto x1 = RD;
		writeln("? ", i*3+2, " ", i*3+3);
		stdout.flush();
		auto x2 = RD;

		foreach (num; numbers.keys)
		{
			if (x1 % num == 0 && x2 % num == 0)
			{
				auto y1 = x1 / num;
				auto y2 = x2 / num;
				bool ok1, ok2;
				foreach (num2; numbers.keys)
				{
					if (num2 == num) continue;
					if (num2 == y1)
						ok1 = true;
					if (num2 == y2)
						ok2 = true;
				}
				if (ok1 && ok2)
				{
					ans ~= [y1, num, y2];
				}
			}
		}
	}

	write("! ");
	ans.map!(to!string).join(" ").writeln;
	stdout.flush();
	debug readln();
}

module _;
	import std.stdio;
auto gfilter(R)(int a, int b, int exp, R i) {
	debug writeln(i, i[a]*i[b]," ",exp);
	return i[a] * i[b] == exp;
}

void main() {
	import std.algorithm : permutations, map, fold, maxElement, filter;
	import std.array : array;
	auto cand = [4, 8, 16, 15, 23, 42].permutations.map!"a.array".array;
	foreach(it; 0..4) {
		int best = int.max, ba, bb;
		foreach(a; 0..6) {
			foreach(b; a..6) {
				auto res = cand.map!(i => i[a] * i[b]);
				int[int] cnt;
				foreach(r; res){
					cnt[r]++;
				}
				int m = cnt.byValue.maxElement;
				if (m < best) {
					best = m;
					ba = a;
					bb = b;
				}
			}
		}
		writefln("? %s %s", ba+1, bb+1);
		stdout.flush();
		int exp;
		readf(" %s", &exp);
		if (!exp) {
			return;
		}
		cand = cand.filter!(a => gfilter(ba, bb, exp, a)).array;
		debug writeln(cand);
	}
	if (cand.length != 1) {
		assert(false);
	}
	writefln("! %(%s %)", cand[0]);
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

//long mod = 10^^9 + 7;
long mod = 998244353;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	bool[long] numbers = [4:true, 8:true, 15:true, 16:true, 23:true, 42:true];
	long[] ans;
	foreach (i; 0..2)
	{
		writeln("? ", i*3+1, " ", i*3+2);
		stdout.flush();
		auto x1 = RD;
		writeln("? ", i*3+2, " ", i*3+3);
		stdout.flush();
		auto x2 = RD;
		long[2] pair;
		(){
		foreach (num; numbers.keys)
		{
			if (x1 % num == 0)
			{
				auto y = x1 / num;
				foreach (num2; numbers.keys)
				{
					if (num2 == y)
					{
						pair = [num, num2];
						return;
					}
				}
			}
		}}();

		if (x2 % pair[0] == 0 && numbers.get(x2/pair[0], false))
		{
			ans ~= [pair[1], pair[0], x2/pair[0]];
		}
		else
		{
			ans ~= [pair[0], pair[1], x2/pair[1]];
		}
	}

	write("! ");
	ans.map!(to!string).join(" ").writeln;
	stdout.flush();
	debug readln();
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

//long mod = 10^^9 + 7;
long mod = 998244353;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	bool[long] numbers = [4:true, 8:true, 15:true, 16:true, 23:true, 42:true];
	long[] ans;
	foreach (i; 0..2)
	{
		writeln("? ", i*3+1, " ", i*3+2);
		stdout.flush();
		auto x1 = RD;
		writeln("? ", i*3+2, " ", i*3+3);
		stdout.flush();
		auto x2 = RD;

		foreach (num; numbers.keys)
		{
			if (x1 % num == 0 && x2 % num == 0)
			{
				auto y1 = x1 / num;
				auto y2 = x2 / num;
				bool ok1, ok2;
				foreach (num2; numbers.keys)
				{
					if (num2 == y1)
						ok1 = true;
					if (num2 == y2)
						ok2 = true;
				}
				if (ok1 && ok2)
				{
					ans ~= [y1, num, y2];
				}
			}
		}
	}

	write("! ");
	ans.map!(to!string).join(" ").writeln;
	stdout.flush();
	debug readln();
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

//long mod = 10^^9 + 7;
long mod = 998244353;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	long[] numbers = [4, 8, 15, 16, 23, 42];
	long[] ans;
	foreach (i; 0..2)
	{
		writeln("? ", i*3+1, " ", i*3+2);
		stdout.flush();
		auto x1 = RD;
		writeln("? ", i*3+2, " ", i*3+3);
		stdout.flush();
		auto x2 = RD;
		long[2] pair;
		(){
		foreach (num; numbers)
		{
			if (x1 % num == 0)
			{
				auto y = x1 / num;
				foreach (num2; numbers)
				{
					if (num2 == y)
					{
						pair = [num, num2];
						return;
					}
				}
			}
		}}();

		if (x2 % pair[0] == 0)
		{
			ans ~= [pair[1], pair[0], x2/pair[0]];
		}
		else
		{
			ans ~= [pair[0], pair[1], x2/pair[1]];
		}
	}

	write("! ");
	ans.map!(to!string).join(" ").writeln;
	stdout.flush();
	debug readln();
}


Today Johnny wants to increase his contribution. His plan assumes writing $$$n$$$ blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.There are $$$n$$$ different topics, numbered from $$$1$$$ to $$$n$$$ sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most $$$n - 1$$$ neighbors.For example, if already written neighbors of the current blog have topics number $$$1$$$, $$$3$$$, $$$1$$$, $$$5$$$, and $$$2$$$, Johnny will choose the topic number $$$4$$$ for the current blog, because topics number $$$1$$$, $$$2$$$ and $$$3$$$ are already covered by neighbors and topic number $$$4$$$ isn't covered.As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?

The first line contains two integers $$$n$$$ $$$(1 \leq n \leq 5 \cdot 10^5)$$$ and $$$m$$$ $$$(0 \leq m \leq 5 \cdot 10^5)$$$ — the number of blogs and references, respectively. Each of the following $$$m$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$a \neq b$$$; $$$1 \leq a, b \leq n$$$), which mean that there is a reference between blogs $$$a$$$ and $$$b$$$. It's guaranteed that the graph doesn't contain multiple edges. The last line contains $$$n$$$ integers $$$t_1, t_2, \ldots, t_n$$$, $$$i$$$-th of them denotes desired topic number of the $$$i$$$-th blog ($$$1 \le t_i \le n$$$).

If the solution does not exist, then write $$$-1$$$. Otherwise, output $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ $$$(1 \leq p_i \leq n)$$$, which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.

NoteIn the first example, Johnny starts with writing blog number $$$2$$$, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number $$$1$$$, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number $$$3$$$, it has references to blogs number $$$1$$$ and $$$2$$$ so it receives the third topic.Second example: There does not exist any permutation fulfilling given conditions.Third example: First Johnny writes blog $$$2$$$, it receives the topic $$$1$$$. Then he writes blog $$$5$$$, it receives the topic $$$1$$$ too because it doesn't have reference to single already written blog $$$2$$$. Then he writes blog number $$$1$$$, it has reference to blog number $$$2$$$ with topic $$$1$$$, so it receives the topic $$$2$$$. Then he writes blog number $$$3$$$ which has reference to blog $$$2$$$, so it receives the topic $$$2$$$. Then he ends with writing blog number $$$4$$$ which has reference to blog $$$5$$$ and receives the topic $$$2$$$.

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto n = RD!int;
	auto m = RD!int;
	auto edges = new int[][](n);
	foreach (i; 0..m)
	{
		auto a = RD!int-1;
		auto b = RD!int-1;
		edges[a] ~= b;
		edges[b] ~= a;
	}
	auto p = RDA!int;

	auto ans = new int[](n);
	auto index = p.MAKE_IDX();
	foreach (ii, i; index)
	{
		auto num = p[i];
		auto used = new bool[](num);
		foreach (to; edges[i])
		{
			auto num2 = p[to];
			if (num2 > num) continue;
			used[num2-1] = true;
		}
		bool ok = used[$-1] == false;
		if (ok)
		{
			foreach (j; 0..num-1)
			{
				if (!used[j])
				{
					ok = false;
					break;
				}
			}
		}
		if (ok)
		{
			ans[ii] = cast(int)(i+1);
		}
		else
		{
			ans.length = 0;
			break;
		}
	}

	if (ans.empty)
		writeln(-1);
	else
		ans.map!(to!string).join(" ").writeln;
	stdout.flush;
	debug readln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

    auto g = new int[][] (n+1);

    foreach (_; m.iota) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto arr = readln.chomp.split.map!(to!int).array;

    auto groups = new int[][] (n+1);

    foreach (i, e; arr) { groups[e] ~= i.to!int; }

    int[] ans;
    int[] need;
    foreach (gridx, gr; groups.dropOne.enumerate(1)) {
        foreach (e; gr) {
            auto connected = g[e]
                .map!(v => arr[v]).filter!(v => v <= gridx).array
                .sort.uniq.array;

            bool isOk = connected == need;

            if (!isOk) {
                debug { writeln(gridx, ' ', e, ' ', ' ', g[e], ' ', connected); }
                writeln(-1);
                return;
            }

            ans ~= e;
        }

        need ~= gridx;
    }

    ans.map!(x => x+1).map!(to!string).join(" ").writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

    auto g = new int[][] (n+1);

    foreach (_; m.iota) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto arr = readln.chomp.split.map!(to!int).array;

    auto groups = new int[][] (n+1);

    foreach (i, e; arr) { groups[e] ~= i.to!int; }

    int[] ans;
    foreach (gridx, gr; groups.dropOne.enumerate(1)) {
        foreach (e; gr) {
            bool isOk = true;

            auto vals = g[e].map!(v => arr[v]).array;
            auto connected = make!(RedBlackTree!int)(vals);

            isOk &= (gridx.to!int !in connected);

            foreach (i; 1 .. gridx) { isOk &= i.to!int in connected; }

            if (!isOk) {
                debug { writeln(gridx, ' ', e, ' ', ' ', g[e], ' ', connected); }
                writeln(-1);
                return;
            }

            ans ~= e;
        }
    }

    ans.map!(x => x+1).map!(to!string).join(" ").writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

    auto g = new int[][] (n+1);

    foreach (_; m.iota) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto arr = readln.chomp.split.map!(to!int).array;

    auto groups = new int[][] (n+1);

    foreach (i, e; arr) { groups[e] ~= i.to!int; }

    int[] ans;
    foreach (gridx, gr; groups.dropOne.enumerate(1)) {
        foreach (e; gr) {
            auto vals = g[e].map!(v => arr[v]).filter!(v => v <= gridx).array;
            auto connected = make!(RedBlackTree!int)(vals);

            bool isOk = connected[].array == gridx.iota.dropOne.array;

            if (!isOk) {
                debug { writeln(gridx, ' ', e, ' ', ' ', g[e], ' ', connected); }
                writeln(-1);
                return;
            }

            ans ~= e;
        }
    }

    ans.map!(x => x+1).map!(to!string).join(" ").writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

    auto g = new int[][] (n+1);

    foreach (_; m.iota) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto arr = readln.chomp.split.map!(to!int).array;

    auto groups = new int[][] (n+1);

    foreach (i, e; arr) { groups[e] ~= i.to!int; }

    int[] ans;
    foreach (gridx, gr; groups.dropOne.enumerate(1)) {
        foreach (e; gr) {
            auto connected = g[e]
                .map!(v => arr[v]).filter!(v => v <= gridx).array
                .sort.uniq.array;

            bool isOk = connected == gridx.iota.dropOne.array;

            if (!isOk) {
                debug { writeln(gridx, ' ', e, ' ', ' ', g[e], ' ', connected); }
                writeln(-1);
                return;
            }

            ans ~= e;
        }
    }

    ans.map!(x => x+1).map!(to!string).join(" ").writeln;
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n, m;
	while (readf !(" %s %s") (n, m) > 0)
	{
		auto adj = new int [] [n];
		foreach (j; 0..m)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			adj[u] ~= v;
			adj[v] ~= u;
		}
		readln;
		auto t = readln.splitter.map !(to !(int)).array;

		auto p = n.iota.array;
		p.schwartzSort !(x => t[x]);

		bool ok = true;
		foreach (i, v; p)
		{
			bool [int] used;
			foreach (u; adj[v])
			{
				if (t[u] == t[v])
				{
					ok = false;
				}
				else if (t[u] < t[v])
				{
					used[t[u]] = true;
				}
			}
			ok &= (used.length == t[v] - 1);
		}

		if (ok)
		{
			writefln !("%(%s %)") (p.map !(q{a + 1}));
		}
		else
		{
			writeln (-1);
		}
	}
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

    auto g = new int[][] (n+1);

    foreach (_; m.iota) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto arr = readln.chomp.split.map!(to!int).array;

    auto groups = new int[][] (n+1);

    foreach (i, e; arr) { groups[e] ~= i.to!int; }

    int[] ans;
    foreach (gridx, gr; groups.dropOne.enumerate(1)) {
        foreach (e; gr) {
            bool isOk = true;

            auto vals = g[e].map!(v => arr[v]).array;
            auto connected = make!(RedBlackTree!int)(vals);

            foreach (i, c; connected[].enumerate(1)) {
                if (c == gridx + 1) { break; }

                if (i != c) { isOk = false; }
                if (c == gridx) { isOk = false; }
            }

            if (!isOk) {
                debug { writeln(gridx, ' ', e, ' ', ' ', g[e], ' ', connected); }
                writeln(-1);
                return;
            }

            ans ~= e;
        }
    }

    ans.map!(x => x+1).map!(to!string).join(" ").writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

    auto g = new int[][] (n+1);

    foreach (_; m.iota) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        --u, --v;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto arr = readln.chomp.split.map!(to!int).array;

    auto groups = new int[][] (n+1);

    foreach (i, e; arr) { groups[e] ~= i.to!int; }

    int[] ans;
    foreach (gridx, gr; groups.dropOne.enumerate(1)) {
        foreach (e; gr) {
            bool isOk = true;

            auto vals = g[e].map!(v => arr[v]).filter!(v => v <= gridx).array;
            auto connected = make!(RedBlackTree!int)(vals);

            foreach (i, c; connected[].enumerate(1)) {
                if (i != c) { isOk = false; }
                if (c == gridx) { isOk = false; }
            }

            if (!isOk) {
                debug { writeln(gridx, ' ', e, ' ', ' ', g[e], ' ', connected); }
                writeln(-1);
                return;
            }

            ans ~= e;
        }
    }

    ans.map!(x => x+1).map!(to!string).join(" ").writeln;
}