Alice gave Bob two integers $$$a$$$ and $$$b$$$ ($$$a > 0$$$ and $$$b \ge 0$$$). Being a curious boy, Bob wrote down an array of non-negative integers with $$$\operatorname{MEX}$$$ value of all elements equal to $$$a$$$ and $$$\operatorname{XOR}$$$ value of all elements equal to $$$b$$$.What is the shortest possible length of the array Bob wrote?Recall that the $$$\operatorname{MEX}$$$ (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the $$$\operatorname{XOR}$$$ of an array is the bitwise XOR of all the elements of the array.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a \leq 3 \cdot 10^5$$$; $$$0 \leq b \leq 3 \cdot 10^5$$$) — the $$$\operatorname{MEX}$$$ and $$$\operatorname{XOR}$$$ of the array, respectively.

For each test case, output one (positive) integer — the length of the shortest array with $$$\operatorname{MEX}$$$ $$$a$$$ and $$$\operatorname{XOR}$$$ $$$b$$$. We can show that such an array always exists.

NoteIn the first test case, one of the shortest arrays with $$$\operatorname{MEX}$$$ $$$1$$$ and $$$\operatorname{XOR}$$$ $$$1$$$ is $$$[0, 2020, 2021]$$$.In the second test case, one of the shortest arrays with $$$\operatorname{MEX}$$$ $$$2$$$ and $$$\operatorname{XOR}$$$ $$$1$$$ is $$$[0, 1]$$$.It can be shown that these arrays are the shortest arrays possible.

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

long mex(long[] a)
{
  foreach (i ; 0L .. 10L) {
    bool good = true;
    foreach (x ; a) {
      if (x == i)
        good = false;
    }
    if (good)
      return i;
  }
  return -1L;
}

void main()
{
  long t;
  readf!" %d "(t);

  long[] cache;
  long acc = 0;
  foreach (x ; 0L .. 3 * 100000 + 5) {
    acc ^= x;
    cache ~= acc;
  }

  assert(cache[4 - 1] == 0);
  assert(cache[5 - 1] == 4);

  while (t--) {
    long a, b;
    readf!" %d %d "(a, b);
//    writefln("a=%d b=%d", a, b);
/*    if (mex([0L, b]) == a)
      writeln(2);
    else */ {
      long x = cache[cast(int)a - 1];
      if (x == b)
        writefln("%d", a);
      else if ((x ^ b) != a)
        writefln("%d", a + 1);
      else
        writefln("%d", a + 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)); }
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);
	auto arr = new long[]((10^^5) * 3);
	foreach (i; 1..(10^^5) * 3)
	{
		arr[i] = arr[i-1] ^ i;
	}
	foreach (ti; 0..t)
	{
		auto a = RD!int;
		auto b = RD!int;

		auto x = arr[a-1] ^ b;

		if (x == 0)
			ans[ti] = a;
		else if (x == a)
			ans[ti] = a + 2;
		else
			ans[ti] = a + 1;
	}

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

immutable multi = true;

int[3_00_000 + 3] cumulativeXor;
static this()
{
	cumulativeXor[0] = 0;
	foreach(i; 1 .. cumulativeXor.length)
		cumulativeXor[i] = cast(int)i ^ cumulativeXor[i-1];
}

void solve(int tc)
{
	auto a = readInt!int;
	auto b = readInt!int;
	if (cumulativeXor[a-1] == b)
	{
		writeln(a);
	}
	else
	{
		auto req = cumulativeXor[a-1] ^ b;
		if (req < a)
		{
			writeln(a+1);
		}
		else if (req > a)
		{
			writeln(a+1);
		}
		else
		{
			writeln(a+2);
		}
	}
}

//{{{
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;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
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.algorithm, std.array, std.container, std.conv, std.format, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

long mex(long[] a)
{
  foreach (i ; 0L .. 10L) {
    bool good = true;
    foreach (x ; a) {
      if (x == i)
        good = false;
    }
    if (good)
      return i;
  }
  return -1L;
}

void main()
{
  long t;
  readf!" %d "(t);

  long[] cache;
  long acc = 0;
  foreach (x ; 0L .. 3 * 100000 + 5) {
    acc ^= x;
    cache ~= acc;
  }

  assert(cache[4 - 1] == 0);
  assert(cache[5 - 1] == 4);

  while (t--) {
    long a, b;
    readf!" %d %d "(a, b);
//    writefln("a=%d b=%d", a, b);
    if (mex([0L, b]) == a)
      writeln(2);
    else {
      long x = cache[cast(int)a - 1];
      if (x == b)
        writefln("%d", a);
      else if ((x ^ b) != a)
        writefln("%d", a + 1);
      else
        writefln("%d", a + 2);
    }
  }
}

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

long mex(long[] a)
{
  foreach (i ; 0L .. 10L) {
    bool good = true;
    foreach (x ; a) {
      if (x == i)
        good = false;
    }
    if (good)
      return i;
  }
  return -1L;
}

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long a, b;
    readf!" %d %d "(a, b);
    if (mex([0L, b]) != a)
      writeln(3);
    else
      writeln(2);
  }
}


You are given an array of $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$, and a set $$$b$$$ of $$$k$$$ distinct integers from $$$1$$$ to $$$n$$$.In one operation, you may choose two integers $$$i$$$ and $$$x$$$ ($$$1 \le i \le n$$$, $$$x$$$ can be any integer) and assign $$$a_i := x$$$. This operation can be done only if $$$i$$$ does not belong to the set $$$b$$$.Calculate the minimum number of operations you should perform so the array $$$a$$$ is increasing (that is, $$$a_1 < a_2 < a_3 < \dots < a_n$$$), or report that it is impossible.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 5 \cdot 10^5$$$, $$$0 \le k \le n$$$) — the size of the array $$$a$$$ and the set $$$b$$$, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 10^9$$$). Then, if $$$k \ne 0$$$, the third line follows, containing $$$k$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_k$$$ ($$$1 \le b_1 < b_2 < \dots < b_k \le n$$$). If $$$k = 0$$$, this line is skipped.

If it is impossible to make the array $$$a$$$ increasing using the given operations, print $$$-1$$$. Otherwise, print one integer — the minimum number of operations you have to perform.

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

immutable int infinity = int.max / 2;

int solve (int [] a)
{
	auto lo = a.front;
	a.popFront ();
	auto hi = a.back;
	a.popBack ();
	a = a.filter !(x => lo <= x && x <= hi).array;
	auto n = a.length.to !(int);
	auto b = new int [n + 1];
	b[] = +infinity;
	auto c = assumeSorted (b);
	foreach (ref x; a)
	{
		c.upperBound (x).front = x;
	}
	return c.lowerBound (+infinity).length.to !(int);
}

void main ()
{
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = -infinity ~ readln.splitter.map !(to !(int)).array ~
		    +infinity;
		foreach (i; 0..n + 2)
		{
			a[i] -= i;
		}

		auto b = (k > 0) ? readln.splitter.map !(to !(int)).array :
		    null;
		b = 0 ~ b ~ (n + 1);
		auto m = new bool [n + 2];
		foreach (ref c; b)
		{
			m[c] = true;
		}

		int prev = 0;
		int res = 0;
		foreach (i; 1..n + 2)
		{
			if (m[i])
			{
				if (a[i] < a[prev])
				{
					res = -1;
				}
				if (res >= 0)
				{
					res += i - prev - 1 -
					    solve (a[prev..i + 1]);
				}
				prev = i;
			}
		}
		writeln (res);
	}
}


There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.

Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.

NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.

// Vicfred
// https://codeforces.com/contest/1541/problem/A
// greedy
import std.stdio;

void main() {
    int t;
    readf("%s\n", &t);

while(t--) {
    int n;
    readf("%s\n", &n);
    if(n == 1) {
        "1".writeln;
        continue;
    }
    if(n % 2 == 0) {
        for(int i = 1; i <= n; ++i) {
            if(i % 2 == 1) {
                writef("%s ", i + 1);
            } else {
                writef("%s ", i - 1);
            }
        }
        "".writeln;
        continue;
    }
    "2 3 1 ".write;
    for(int i = 4; i <= n; ++i) {
        if(i % 2 == 0) {
            writef("%s ", i + 1);
        } else {
            writef("%s ", i - 1);
        }
    }
    "".writeln;
}
}

import std.stdio;

void print(int start, int n) {
  if (start > n) return;
  write(start + 1, ' ', start,' ');
  print(start + 2, n);
}

void main() {
  int t, n;

  readf!"%d"(t);

  while (t--) {
    readf!" %d"(n);

    int start = 0;

    if (n >= 3 && n % 2 == 1) {
      write("3 1 2 ");
      print(4, n);
    } else {
      print(1, n);
    }
    writeln;
  }
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    int[] a;
    for (int i = 0; i + 1 < n; i += 2) {
      a ~= (i + 1) + 1;
      a ~= (i + 0) + 1;
    }
    if (n % 2 == 1) {
      a ~= cast(int)a.length - 1;
      a[$ - 2] = cast(int)a.length;
    }
    writeln(a.map!text.joiner(" "));
  }
}

// Vicfred
// https://codeforces.com/contest/1541/problem/A
// greedy
import std.stdio;

void main() {
    int t;
    readf("%s\n", &t);

while(t--) {
    int n;
    readf("%s\n", &n);
    writef("%s ", n);
    for(int i = 1; i < n; ++i) {
        writef("%s ", i);
    } "".writeln;
}
}


Allen is playing Number Clicker on his phone.He starts with an integer $$$u$$$ on the screen. Every second, he can press one of 3 buttons.  Turn $$$u \to u+1 \pmod{p}$$$.  Turn $$$u \to u+p-1 \pmod{p}$$$.  Turn $$$u \to u^{p-2} \pmod{p}$$$. Allen wants to press at most 200 buttons and end up with $$$v$$$ on the screen. Help him!

The first line of the input contains 3 positive integers: $$$u, v, p$$$ ($$$0 \le u, v \le p-1$$$, $$$3 \le p \le 10^9 + 9$$$). $$$p$$$ is guaranteed to be prime.

On the first line, print a single integer $$$\ell$$$, the number of button presses. On the second line, print integers $$$c_1, \dots, c_\ell$$$, the button presses. For $$$1 \le i \le \ell$$$, $$$1 \le c_i \le 3$$$. We can show that the answer always exists.

NoteIn the first example the integer on the screen changes as $$$1 \to 2 \to 3$$$.In the second example the integer on the screen changes as $$$3 \to 2$$$.

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)); }

struct ModInt {
  static int M;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op)() const if (op == "-") { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op)(long a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  string toString() const { return x.to!string; }
}

enum L = 200;
enum N = 19;

void main() {
  try {
    for (; ; ) {
      const U = readLong();
      const V = readLong();
      const P = readLong();
      ModInt.M = cast(int)(P);

      alias Entry = Tuple!(long, "val", int, "key");
      Entry[] eus, evs;
      void dfs(ref Entry[] results, int pos, int key, ModInt x) {
        if (pos == N) {
          results ~= Entry(x.x, key);
          return;
        }
        const invU = (x.x == 0) ? ModInt(0) : x.inv;
        dfs(results, pos + 1, key, invU + 1);
        dfs(results, pos + 1, key | 1 << pos, invU - 1);
      }
      dfs(eus, 0, 0, ModInt(U));
      dfs(evs, 0, 0, ModInt(V));
      eus.sort;
      evs.sort;
      int[] ans;
      for (int i = 0, j = 0; i < eus.length && j < evs.length; ) {
        const diff = ModInt(evs[j].val - eus[i].val);
        if (0 <= diff.x && diff.x <= L - 4 * N) {
          foreach (k; 0 .. N) {
            ans ~= 3;
            ans ~= ((eus[i].key & 1 << k) ? 2 : 1);
          }
          foreach (_; 0 .. diff.x) {
            ans ~= 1;
          }
          foreach_reverse (k; 0 .. N) {
            ans ~= ((evs[j].key & 1 << k) ? 1 : 2);
            ans ~= 3;
          }
          goto found;
        }
        if (0 <= (-diff).x && (-diff).x <= L - 4 * N) {
          foreach (k; 0 .. N) {
            ans ~= 3;
            ans ~= ((eus[i].key & 1 << k) ? 2 : 1);
          }
          foreach (_; 0 .. (-diff).x) {
            ans ~= 2;
          }
          foreach_reverse (k; 0 .. N) {
            ans ~= ((evs[j].key & 1 << k) ? 1 : 2);
            ans ~= 3;
          }
          goto found;
        }
        (eus[i].val <= evs[j].val) ? ++i : ++j;
      }
      assert(false);
     found:

      writeln(ans.length);
      foreach (h; 0 .. ans.length) {
        if (h > 0) write(" ");
        write(ans[h]);
      }
      writeln();

      debug {
        ModInt x = U;
        foreach (h; 0 .. ans.length) {
          switch (ans[h]) {
            case 1: x += 1; break;
            case 2: x -= 1; break;
            case 3: x = (x.x == 0) ? ModInt(0) : x.inv; break;
            default: assert(false);
          }
        }
        assert(x.x == V);
      }
    }
  } catch (EOFException e) {
  }
}


Sayaka Saeki is a member of the student council, which has $$$n$$$ other members (excluding Sayaka). The $$$i$$$-th member has a height of $$$a_i$$$ millimeters.It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.A pair of two consecutive members $$$u$$$ and $$$v$$$ on a line is considered photogenic if their average height is an integer, i.e. $$$\frac{a_u + a_v}{2}$$$ is an integer.Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 500$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$)  — the number of other council members. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$)  — the heights of each of the other members in millimeters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.

For each test case, output on one line $$$n$$$ integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.

NoteIn the first test case, there is one photogenic pair: $$$(1, 1)$$$ is photogenic, as $$$\frac{1+1}{2}=1$$$ is integer, while $$$(1, 2)$$$ isn't, as $$$\frac{1+2}{2}=1.5$$$ isn't integer.In the second test case, both pairs are photogenic.

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

void main() {
    foreach(t; 0 .. to!int(readln.strip)) {
        int n = to!int(readln.strip);
        auto ar = readln.split.map!(to!int).array;
        ar.partition!"a%2";
        writeln(ar.map!(to!string).join(" "));
    }
}

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)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		a.schwartzSort !(q{a & 1});
		writefln !("%(%s %)") (a);
	}
}


This is an interactive problem.Kochiya Sanae is playing with magnets. Realizing that some of those magnets are demagnetized, she is curious to find them out.There are $$$n$$$ magnets, which can be of the following $$$3$$$ types: N  S  - — these magnets are demagnetized. Note that you don't know the types of these magnets beforehand.You have a machine which can measure the force between the magnets, and you can use it at most $$$n+\lfloor \log_2n\rfloor$$$ times.You can put some magnets to the left part of the machine and some to the right part of the machine, and launch the machine. Obviously, you can put one magnet to at most one side (you don't have to put all magnets). You can put the same magnet in different queries.Then the machine will tell the force these magnets produce. Formally, let $$$n_1,s_1$$$ be the number of N and S magnets correspondently on the left and $$$n_2,s_2$$$ — on the right. Then the force between them would be $$$n_1n_2+s_1s_2-n_1s_2-n_2s_1$$$. Please note that the force is a signed value.However, when the absolute value of the force is strictly larger than $$$n$$$, the machine will crash into pieces.You need to find all magnets of type - (all demagnetized ones), without breaking the machine.Note that the interactor is not adaptive. The types of the magnets are fixed before the start of the interaction and do not change with queries.It is guaranteed that there are at least $$$2$$$ magnets whose type is not -, and at least $$$1$$$ magnet of type -.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.

NoteThe empty lines in the sample are just for you to better understand the interaction process. You're not required to print them.In the sample, the types of the magnets are NN--.At first, you put the third magnet on the left and the fourth one on the right. Both of them have type -, thus no force is produced.Then you put the first magnet on the left and the second and third one on the right. The third magnet has type -, while the other two magnets are of type N, so the force produced is $$$1$$$.In the following two queries, the force is $$$0$$$ since there is only a magnet with property - on the right.Then we can determine that the magnets of type - are the third and the fourth one, so we should print ! 2 3 4 and exit.

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 N;
debug {
  string A;
  int numAsks;
}

int Ask(int[] xs, int[] ys) {
  writefln("? %s %s", xs.length, ys.length);
  foreach (index, x; xs) {
    if (index > 0) write(" ");
    write(x + 1);
  }
  writeln;
  foreach (index, y; ys) {
    if (index > 0) write(" ");
    write(y + 1);
  }
  writeln;
  stdout.flush;
  int ret;
  debug {
    ++numAsks;
    assert(numAsks <= N + bsr(N));
    int l, r;
    foreach (x; xs) {
      if (A[x] == 'N') ++l;
      if (A[x] == 'S') --l;
    }
    foreach (y; ys) {
      if (A[y] == 'N') ++r;
      if (A[y] == 'S') --r;
    }
    ret = l * r;
    writefln("%s %s: %s * %s = %s", xs, ys, l, r, ret);
    assert(abs(ret) <= N);
  } else {
    ret = readInt();
  }
  return ret;
}

void Answer(int[] zs) {
  zs.sort;
  writef("! %s", zs.length);
  foreach (z; zs) {
    write(" ", z + 1);
  }
  writeln;
  stdout.flush;
  debug {
    assert(A.count('-') == zs.length);
    foreach (z; zs) {
      assert(A[z] == '-');
    }
  }
}

void solve() {
  int[] ans;
  foreach (k; 1 .. N) {
    if (Ask(iota(k).array, [k]) != 0) {
      int lo = 0, hi = k;
      for (; lo + 1 < hi; ) {
        const mid = (lo + hi) / 2;
        if (Ask(iota(lo, mid).array, [k]) != 0) {
          hi = mid;
        } else {
          lo = mid;
        }
      }
      foreach (i; 0 .. k) {
        if (i != lo) {
          ans ~= i;
        }
      }
      foreach (i; k + 1 .. N) {
        if (Ask([k], [i]) == 0) {
          ans ~= i;
        }
      }
      break;
    }
  }
  Answer(ans);
}

void main() {
  const numCases = readInt();
  foreach (caseId; 0 .. numCases) {
    N = readInt();
    debug {
      A = readToken();
    }
    solve();
  }
}


Tokitsukaze has a permutation $$$p$$$. She performed the following operation to $$$p$$$ exactly $$$k$$$ times: in one operation, for each $$$i$$$ from $$$1$$$ to $$$n - 1$$$ in order, if $$$p_i$$$ > $$$p_{i+1}$$$, swap $$$p_i$$$, $$$p_{i+1}$$$. After exactly $$$k$$$ times of operations, Tokitsukaze got a new sequence $$$a$$$, obviously the sequence $$$a$$$ is also a permutation.After that, Tokitsukaze wrote down the value sequence $$$v$$$ of $$$a$$$ on paper. Denote the value sequence $$$v$$$ of the permutation $$$a$$$ of length $$$n$$$ as $$$v_i=\sum_{j=1}^{i-1}[a_i < a_j]$$$, where the value of $$$[a_i < a_j]$$$ define as if $$$a_i < a_j$$$, the value is $$$1$$$, otherwise is $$$0$$$ (in other words, $$$v_i$$$ is equal to the number of elements greater than $$$a_i$$$ that are to the left of position $$$i$$$). Then Tokitsukaze went out to work.There are three naughty cats in Tokitsukaze's house. When she came home, she found the paper with the value sequence $$$v$$$ to be bitten out by the cats, leaving several holes, so that the value of some positions could not be seen clearly. She forgot what the original permutation $$$p$$$ was. She wants to know how many different permutations $$$p$$$ there are, so that the value sequence $$$v$$$ of the new permutation $$$a$$$ after exactly $$$k$$$ operations is the same as the $$$v$$$ written on the paper (not taking into account the unclear positions).Since the answer may be too large, print it modulo $$$998\,244\,353$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^6$$$; $$$0 \leq k \leq n-1$$$) — the length of the permutation and the exactly number of operations. The second line contains $$$n$$$ integers $$$v_1, v_2, \dots, v_n$$$ ($$$-1 \leq v_i \leq i-1$$$) — the value sequence $$$v$$$. $$$v_i = -1$$$ means the $$$i$$$-th position of $$$v$$$ can't be seen clearly. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.

For each test case, print a single integer — the number of different permutations modulo $$$998\,244\,353$$$.

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

immutable int mod = 998_244_353;

int solve (int [] a, int n, int k)
{
	foreach (i; 0..n - k)
	{
		if (a[i] > i)
		{
			return 0;
		}
	}

	foreach (i; n - k..n)
	{
		if (a[i] > 0)
		{
			return 0;
		}
	}

	int res = 1;
	foreach (i; 0..k)
	{
		res = (res * 1L * (i + 1)) % mod;
	}
	foreach (i; 0..n - k)
	{
		if (a[i] == -1)
		{
			res = (res * 1L * (i + k + 1)) % mod;
		}
		else if (a[i] == 0)
		{
			res = (res * 1L * (k + 1)) % mod;
		}
	}
	return res;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (a, n, k));
	}
}

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)); }

struct ModInt(uint M_) {
  import std.conv : to;
  alias M = M_;
  uint x;
  this(ModInt a) { x = a.x; }
  this(uint x_) { x = x_ % M; }
  this(ulong x_) { x = cast(uint)(x_ % M); }
  this(int x_) { x = ((x_ %= cast(int)(M)) < 0) ? (x_ + cast(int)(M)) : x_; }
  this(long x_) { x = cast(uint)(((x_ %= cast(long)(M)) < 0) ? (x_ + cast(long)(M)) : x_); }
  ref ModInt opAssign(T)(inout(T) a) if (is(T == uint) || is(T == ulong) || is(T == int) || is(T == long)) { return this = ModInt(a); }
  ref ModInt opOpAssign(string op, T)(T a) {
    static if (is(T == ModInt)) {
      static if (op == "+") { x = ((x += a.x) >= M) ? (x - M) : x; }
      else static if (op == "-") { x = ((x -= a.x) >= M) ? (x + M) : x; }
      else static if (op == "*") { x = cast(uint)((cast(ulong)(x) * a.x) % M); }
      else static if (op == "/") { this *= a.inv(); }
      else static assert(false);
      return this;
    } else static if (op == "^^") {
      if (a < 0) return this = inv()^^(-a);
      ModInt b = this, c = 1U;
      for (long e = a; e; e >>= 1) { if (e & 1) c *= b; b *= b; }
      return this = c;
    } else {
      return mixin("this " ~ op ~ "= ModInt(a)");
    }
  }
  ModInt inv() const {
    uint a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const q = a / b; const c = a - q * b; a = b; b = c; const w = y - cast(int)(q) * z; y = z; z = w; }
    assert(a == 1); return ModInt(y);
  }
  ModInt opUnary(string op)() const {
    static if (op == "+") { return this; }
    else static if (op == "-") { ModInt a; a.x = x ? (M - x) : 0U; return a; }
    else static assert(false);
  }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op, T)(T a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  bool opCast(T: bool)() const { return (x != 0U); }
  string toString() const { return x.to!string; }
}

enum MO = 998244353;
alias Mint = ModInt!MO;

enum LIM = 2 * 10^^6 + 10;
Mint[] inv, fac, invFac;
void prepare() {
  inv = new Mint[LIM];
  fac = new Mint[LIM];
  invFac = new Mint[LIM];
  inv[1] = 1;
  foreach (i; 2 .. LIM) {
    inv[i] = -((Mint.M / i) * inv[cast(size_t)(Mint.M % i)]);
  }
  fac[0] = invFac[0] = 1;
  foreach (i; 1 .. LIM) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(long n, long k) {
  if (n < 0) {
    if (k >= 0) {
      return (-1)^^(k & 1) * binom(-n + k - 1, k);
    } else if (n - k >= 0) {
      return (-1)^^((n - k) & 1) * binom(-k - 1, n - k);
    } else {
      return Mint(0);
    }
  } else {
    if (0 <= k && k <= n) {
      assert(n < LIM);
      return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];
    } else {
      return Mint(0);
    }
  }
}

void main() {
  prepare;

  /*
  debug {
    foreach (n; 1 .. 5 + 1) {
      auto freq = new int[string][n];
      auto ps = iota(n).array;
      do {
        int[] qs = ps.dup;
        int[][] qss = [qs.dup];
        foreach (k; 0 .. n - 1) {
          foreach (i; 0 .. n - 1) {
            if (qs[i] > qs[i + 1]) {
              swap(qs[i], qs[i + 1]);
            }
          }
          qss ~= qs.dup;
        }
        auto rss = new int[][](n, n);
        foreach (k; 0 .. n) {
          foreach (i; 0 .. n) {
            foreach (j; 0 .. i) if (qss[k][j] > qss[k][i]) {
              ++rss[k][i];
            }
          }
        }
        writeln(qss, " ", rss);
        foreach (k; 0 .. n - 1) {
          auto fs = rss[k].dup;
          auto gs = rss[k + 1].dup;
          fs.sort;
          gs.sort;
          foreach (i; 0 .. n) {
            assert(max(fs[i] - 1, 0) == gs[i]);
          }
        }
        foreach (k; 0 .. n) {
          ++freq[k][rss[k].to!string];
        }
      } while (ps.nextPermutation);
      foreach (k; 0 .. n) {
        foreach (rs, val; freq[k]) {
          writeln(k, " ", rs, ": ", val);
        }
      }
    }
    // return;
  }
  */

  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        const K = readInt;
        auto V = new int[N];
        foreach (i; 0 .. N) {
          V[i] = readInt;
        }

        Mint ans = 1;
        foreach (i; 0 .. N - K) {
          if (~V[i]) {
            if (V[i] == 0) {
              ans *= (K + 1);
            }
          } else {
            ans *= ((K + 1) + i);
          }
        }
        foreach (i; N - K .. N) {
          if (~V[i]) {
            if (V[i] != 0) {
              ans = 0;
            }
          }
          ans *= (N - i);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}


You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$).

For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise.

NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(int[]);
        auto xs = new int[](5001);
        auto ls = new int[](5001);
        foreach (i; 1..N+1) {
            auto a = as[i-1];
            if (xs[a]) {
                if (xs[a] > 1) goto ok;
                if (ls[a] != i-1) goto ok;
            }
            ++xs[a];
            ls[a] = i;
        }
        writeln("NO");
        continue;
        ok:
        writeln("YES");
    }
}

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 modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA(-1);
		auto cnt = new int[][](n);
		foreach (i; 0..n)
		{
			cnt[a[i]] ~= i;
		}
		foreach (i; 0..n)
		{
			if (cnt[i].length <= 1) continue;
			if (cnt[i].length >= 3)
			{
				ans[ti] = true;
				break;
			}
			else
			{
				if (cnt[i][0]+1 != cnt[i][1])
				{
					ans[ti] = true;
					break;
				}
			}
		}
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	stdout.flush;
	debug readln;
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(int[]);
        foreach (i; 0..N) {
            if (i+2 < N && as[i] == as[i+2]) goto ok;
            if (i+3 < N && as[i] == as[i+3] && as[i+1] == as[i+2]) goto ok;
        }
        writeln("NO");
        continue;
        ok:
        writeln("YES");
    }
}


Recently, Tokitsukaze found an interesting game. Tokitsukaze had $$$n$$$ items at the beginning of this game. However, she thought there were too many items, so now she wants to discard $$$m$$$ ($$$1 \le m \le n$$$) special items of them.These $$$n$$$ items are marked with indices from $$$1$$$ to $$$n$$$. In the beginning, the item with index $$$i$$$ is placed on the $$$i$$$-th position. Items are divided into several pages orderly, such that each page contains exactly $$$k$$$ positions and the last positions on the last page may be left empty.Tokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded.    Consider the first example from the statement: $$$n=10$$$, $$$m=4$$$, $$$k=5$$$, $$$p=[3, 5, 7, 10]$$$. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices $$$3$$$ and $$$5$$$. After, the first page remains to be special. It contains $$$[1, 2, 4, 6, 7]$$$, Tokitsukaze discards the special item with index $$$7$$$. After, the second page is special (since it is the first page containing a special item). It contains $$$[9, 10]$$$, Tokitsukaze discards the special item with index $$$10$$$. Tokitsukaze wants to know the number of operations she would do in total.

The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le n \le 10^{18}$$$, $$$1 \le m \le 10^5$$$, $$$1 \le m, k \le n$$$) — the number of items, the number of special items to be discarded and the number of positions in each page. The second line contains $$$m$$$ distinct integers $$$p_1, p_2, \ldots, p_m$$$ ($$$1 \le p_1 < p_2 < \ldots < p_m \le n$$$) — the indices of special items which should be discarded.

Print a single integer — the number of operations that Tokitsukaze would do in total.

NoteFor the first example:  In the first operation, Tokitsukaze would focus on the first page $$$[1, 2, 3, 4, 5]$$$ and discard items with indices $$$3$$$ and $$$5$$$;  In the second operation, Tokitsukaze would focus on the first page $$$[1, 2, 4, 6, 7]$$$ and discard item with index $$$7$$$;  In the third operation, Tokitsukaze would focus on the second page $$$[9, 10]$$$ and discard item with index $$$10$$$. For the second example, Tokitsukaze would focus on the second page $$$[6, 7, 8, 9, 10]$$$ and discard all special items at once.

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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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;
//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 main()
{
	auto n = RD;
	auto m = RD!int;
	auto k = RD;
	auto p = RDA;

	long ans;
	int pos;
	auto last = k;
	while (true)
	{
		long cnt;
		bool end = true;
		foreach (j; pos..m)
		{
			if (p[j] > last)
			{
				if (cnt == 0)
				{
					last += (((p[j] - last) - 1) / k + 1) * k;
					break;
				}
				else
				{
					last += cnt;
					pos = j;
					end = false;
					break;
				}
			}
			++cnt;
		}
		if (cnt == 0) continue;
		debug writeln(pos, " ", cnt, " ", last);
		++ans;
		if (end) break;
	}

	writeln(ans);
	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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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;
//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 main()
{
	auto n = RD!int;
	auto m = RD!int;
	auto k = RD!int;
	auto p = RDA;

	long ans;
	int pos;
	auto last = k;
	while (true)
	{
		long cnt;
		bool end = true;
		foreach (j; pos..m)
		{
			if (p[j] > last)
			{
				if (cnt == 0)
				{
					pos = m;
					break;
				}
				else
				{
					last += cnt;
					pos = j;
					end = false;
					break;
				}
			}
			++cnt;
		}
		debug writeln(pos, " ", cnt, " ", last);
		++ans;
		if (end) break;
	}

	writeln(ans);
	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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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;
//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 main()
{
	auto n = RD;
	auto m = RD!int;
	auto k = RD;
	auto p = RDA;

	long ans;
	int pos;
	auto last = k;
	while (true)
	{
		long cnt;
		bool end = true;
		foreach (j; pos..m)
		{
			if (p[j] > last)
			{
				if (cnt == 0)
				{
					last += ((p[j] - last) / k + 1) * k;
					break;
				}
				else
				{
					last += cnt;
					pos = j;
					end = false;
					break;
				}
			}
			++cnt;
		}
		if (cnt == 0) continue;
		debug writeln(pos, " ", cnt, " ", last);
		++ans;
		if (end) break;
	}

	writeln(ans);
	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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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;
//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 main()
{
	auto n = RD;
	auto m = RD!int;
	auto k = RD;
	auto p = RDA;

	long ans;
	int pos;
	auto last = k;
	while (true)
	{
		long cnt;
		bool end = true;
		foreach (j; pos..m)
		{
			if (p[j] > last)
			{
				if (cnt == 0)
				{
					pos = m;
					break;
				}
				else
				{
					last += cnt;
					pos = j;
					end = false;
					break;
				}
			}
			++cnt;
		}
		debug writeln(pos, " ", cnt, " ", last);
		++ans;
		if (end) break;
	}

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


Given a positive integer $$$n$$$. Find three distinct positive integers $$$a$$$, $$$b$$$, $$$c$$$ such that $$$a + b + c = n$$$ and $$$\operatorname{gcd}(a, b) = c$$$, where $$$\operatorname{gcd}(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Description of the test cases follows. The first and only line of each test case contains a single integer $$$n$$$ ($$$10 \le n \le 10^9$$$).

For each test case, output three distinct positive integers $$$a$$$, $$$b$$$, $$$c$$$ satisfying the requirements. If there are multiple solutions, you can print any. We can show that an answer always exists.

NoteIn the first test case, $$$6 + 9 + 3 = 18$$$ and $$$\operatorname{gcd}(6, 9) = 3$$$.In the second test case, $$$21 + 39 + 3 = 63$$$ and $$$\operatorname{gcd}(21, 39) = 3$$$.In the third test case, $$$29 + 43 + 1 = 73$$$ and $$$\operatorname{gcd}(29, 43) = 1$$$.

import std;

T read (T, string ending = "", string beginning = "") () @trusted {
    scope T x;
    readf!(beginning ~ "%s" ~ ending)(x);
    return x;
}

void main () {
    immutable tests = readln.chomp.to!uint;
    TEST_LOOP: foreach (test_index; 0 .. tests) {
        immutable n =
            readln().chomp()
            .to!uint;

        if (n % 2 == 0) {
            writeln(n - 3, " 2 1");
        } else {
            immutable half = n / 2;

            immutable first = half - (half % 2 == 0 ? 1 : 2);
            immutable second = half + (half % 2 == 0 ? 1 : 2);

            writeln(first, ' ', second, ' ', 1);
        }
    }
}

// "" `` '' () {} []

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!long;
  auto isPrime(long m)
  {
    for (long d = 2; d * d <= m; d++)
      if (m % d == 0) return false;
    return true;
  }
  n--;
  for (long d = 2;; d++)
    {
      if (isPrime(d) && n % d != 0)
	{
	  writeln(n - d, " ", d, " ", 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;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
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.algorithm;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		int c = 1;
		int a = n - n / 2;
		int b = n - a - c;
		if (gcd (a, b) != c)
		{
			a += 1;
			b -= 1;
		}
		if (gcd (a, b) != c)
		{
			assert (false);
		}
		writeln (a, " ", b, " ", c);
	}
}

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 euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

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;

		if (n % 2)
		{
			--n;
			auto x = n / 2;
			auto d = x % 2 ? 2 : 1;
			ans[ti] = [x-d, x+d, 1];
		}
		else
		{
			ans[ti] = [n/2, n/2-1, 1];
		}
	}

	foreach (e; ans)
		writeln(e[0], " ", e[1], " ", e[2]);
	stdout.flush;
	debug readln;
}


On the way home, Karen decided to stop by the supermarket to buy some groceries.  She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars.The supermarket sells n goods. The i-th good can be bought for ci dollars. Of course, each good can only be bought once.Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given n coupons. If Karen purchases the i-th good, she can use the i-th coupon to decrease its price by di. Of course, a coupon cannot be used without buying the corresponding good.There is, however, a constraint with the coupons. For all i ≥ 2, in order to use the i-th coupon, Karen must also use the xi-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget b?

The first line of input contains two integers n and b (1 ≤ n ≤ 5000, 1 ≤ b ≤ 109), the number of goods in the store and the amount of money Karen has, respectively. The next n lines describe the items. Specifically:   The i-th line among these starts with two integers, ci and di (1 ≤ di < ci ≤ 109), the price of the i-th good and the discount when using the coupon for the i-th good, respectively.  If i ≥ 2, this is followed by another integer, xi (1 ≤ xi < i), denoting that the xi-th coupon must also be used before this coupon can be used.

Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.

NoteIn the first test case, Karen can purchase the following 4 items:  Use the first coupon to buy the first item for 10 - 9 = 1 dollar.  Use the third coupon to buy the third item for 12 - 2 = 10 dollars.  Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars.  Buy the sixth item for 2 dollars. The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.In the second test case, Karen has enough money to use all the coupons and purchase everything.

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 N;
int B;
int[] C, D;
int[] P;

int[][] child;
int[] sz;
int[][][] dp;

void dfs(int u, int p) {
  auto crt = [[0], [0]];
  foreach (v; child[u]) {
    dfs(v, u);
    auto nxt = new int[][](2, sz[u] + sz[v] + 1);
    nxt[0][] = B + 1;
    nxt[1][] = B + 1;
    foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
      foreach (a; 0 .. sz[u] + 1) foreach (b; 0 .. sz[v] + 1) {
        chmin(nxt[s | t][a + b], crt[s][a] + dp[v][t][b]);
      }
    }
    crt = nxt;
    sz[u] += sz[v];
  }
  dp[u] = new int[][](2, sz[u] + 1 + 1);
  dp[u][0][] = B + 1;
  dp[u][1][] = B + 1;
  foreach (a; 0 .. sz[u] + 1) {
    chmin(dp[u][0][a], crt[0][a]);
    chmin(dp[u][0][a + 1], crt[0][a] + C[u]);
    chmin(dp[u][1][a], crt[0][a]);
    chmin(dp[u][1][a + 1], crt[0][a] + C[u]);
    chmin(dp[u][1][a + 1], crt[0][a] + (C[u] - D[u]));
    chmin(dp[u][1][a + 1], crt[1][a] + (C[u] - D[u]));
  }
  sz[u] += 1;
}

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

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

      sz = new int[N];
      dp = new int[][][N];
      dfs(0, -1);
      debug {
        foreach (u; 0 .. N) {
          writeln(u, ": ", dp[u]);
        }
      }

      int ans;
      foreach (s; 0 .. 2) {
        foreach (a; 0 .. N + 1) {
          if (dp[0][s][a] <= B) {
            chmax(ans, a);
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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 N;
int B;
int[] C, D;
int[] P;

int[][] child;
int[] sz;
int[][][] dp;

void dfs(int u, int p) {
  auto crt = [[0], [0]];
  foreach (v; child[u]) {
    dfs(v, u);
    auto nxt = new int[][](2, sz[u] + sz[v] + 1);
    nxt[0][] = B + 1;
    nxt[1][] = B + 1;
    foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
      foreach (a; 0 .. sz[u] + 1) foreach (b; 0 .. sz[v] + 1) {
        chmin(nxt[s | t][a + b], crt[s][a] + dp[v][t][b]);
      }
    }
    crt = nxt;
    sz[u] += sz[v];
  }
  dp[u] = new int[][](2, sz[u] + 1 + 1);
  dp[u][0][] = B + 1;
  dp[u][1][] = B + 1;
  foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
    foreach (a; 0 .. sz[u] + 1) {
      chmin(dp[u][s][a], crt[s][a] + [0, B + 1][t]);
      chmin(dp[u][s | t][a + 1], crt[s][a] + [C[u], C[u] - D[u]][t]);
    }
  }
  sz[u] += 1;
}

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

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

      sz = new int[N];
      dp = new int[][][N];
      dfs(0, -1);
      debug {
        foreach (u; 0 .. N) {
          writeln(u, ": ", dp[u]);
        }
      }

      int ans;
      foreach (s; 0 .. 2) {
        foreach (a; 0 .. N + 1) {
          if (dp[0][s][a] <= B) {
            chmax(ans, a);
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Suppose you have two points $$$p = (x_p, y_p)$$$ and $$$q = (x_q, y_q)$$$. Let's denote the Manhattan distance between them as $$$d(p, q) = |x_p - x_q| + |y_p - y_q|$$$.Let's say that three points $$$p$$$, $$$q$$$, $$$r$$$ form a bad triple if $$$d(p, r) = d(p, q) + d(q, r)$$$.Let's say that an array $$$b_1, b_2, \dots, b_m$$$ is good if it is impossible to choose three distinct indices $$$i$$$, $$$j$$$, $$$k$$$ such that the points $$$(b_i, i)$$$, $$$(b_j, j)$$$ and $$$(b_k, k)$$$ form a bad triple.You are given an array $$$a_1, a_2, \dots, a_n$$$. Calculate the number of good subarrays of $$$a$$$. A subarray of the array $$$a$$$ is the array $$$a_l, a_{l + 1}, \dots, a_r$$$ for some $$$1 \le l \le r \le n$$$.Note that, according to the definition, subarrays of length $$$1$$$ and $$$2$$$ are good.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print the number of good subarrays of array $$$a$$$.

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 (n == 1)
		{
			ans[ti] = 1;
			continue;
		}

		ans[ti] = 3;
		bool f(int i, int j, int k)
		{
			return inside(a[j], min(a[i], a[k]), max(a[i], a[k])+1);
		}
		foreach (i; 0..n-2)
		{
			if (f(i, i+1, i+2))
				ans[ti] += 2;
			else
			{
				ans[ti] += 3;
				if (i + 3 < n)
				{
					bool ok = true;
					(){
					foreach (l; i..i+2)
					{
						foreach (m; l+1..i+4)
						{
							foreach (r; m+1..i+4)
							{
								if (f(l, m, r))
								{
									ok = false;
									return;
								}
							}
						}
					}}();
					if (ok)
						++ans[ti];
				}
			}
		}
	}

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

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

void main() {
    int T = read!int;
    foreach (t; 0 .. T) solve();
}

void solve() {
    int N = read!int;
    auto A = readarray!int;

    bool check(int[] xs) {
        if (xs.length <= 2) return true;
        auto n = xs.length;
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                for (int k = j+1; k < n; k++) {
                    if (xs[i] <= xs[j] && xs[j] <= xs[k]) return false;
                    if (xs[i] >= xs[j] && xs[j] >= xs[k]) return false;
                }
            }
        }
        return true;
    }

    int ans = 0;
    for (int l = 0; l < N; l++) {
        for (int r = l + 1; r <= min(l + 4, N); r++) {
            if (check(A[l..r])) {
                //writeln([l, r]);
                ans++;
            }
        }
    }
    writeln(ans);
}

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

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!int).array;
    long result = 0;
    foreach (i ; 0 .. n) {
      if (i + 2 < n) {
        int x1 = a[i];
        int x2 = a[i + 1];
        int x3 = a[i + 2];
        if ((x2 > x1 && x2 > x3) || (x2 < x1 && x2 < x3))
          result++;
      }
      if (i + 3 < n) {
        int x1 = a[i];
        int x2 = a[i + 1];
        int x3 = a[i + 2];
        int x4 = a[i + 3];
        if ((x2 > x1 && x2 > x4) && (x3 < x1 && x3 < x4))
          result++;
        else if ((x2 < x1 && x2 < x4) && (x3 > x1 && x3 > x4))
          result++;
      }
    }
    writeln(result + n + n - 1);
  }
}


YouKn0wWho has an integer sequence $$$a_1, a_2, \ldots a_n$$$. Now he will split the sequence $$$a$$$ into one or more consecutive subarrays so that each element of $$$a$$$ belongs to exactly one subarray. Let $$$k$$$ be the number of resulting subarrays, and $$$h_1, h_2, \ldots, h_k$$$ be the lengths of the longest increasing subsequences of corresponding subarrays.For example, if we split $$$[2, 5, 3, 1, 4, 3, 2, 2, 5, 1]$$$ into $$$[2, 5, 3, 1, 4]$$$, $$$[3, 2, 2, 5]$$$, $$$[1]$$$, then $$$h = [3, 2, 1]$$$.YouKn0wWho wonders if it is possible to split the sequence $$$a$$$ in such a way that the bitwise XOR of $$$h_1, h_2, \ldots, h_k$$$ is equal to $$$0$$$. You have to tell whether it is possible.The longest increasing subsequence (LIS) of a sequence $$$b_1, b_2, \ldots, b_m$$$ is the longest sequence of valid indices $$$i_1, i_2, \ldots, i_k$$$ such that $$$i_1 \lt i_2 \lt \ldots \lt i_k$$$ and $$$b_{i_1} \lt b_{i_2} \lt \ldots \lt b_{i_k}$$$. For example, the LIS of $$$[2, 5, 3, 3, 5]$$$ is $$$[2, 3, 5]$$$, which has length $$$3$$$.An array $$$c$$$ is a subarray of an array $$$b$$$ if $$$c$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10\,000$$$)  — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 \cdot 10^5$$$.

For each test case, print "YES" (without quotes) if it is possible to split into subarrays in the desired way, print "NO" (without quotes) otherwise. You can print each letter in any register (upper or lower).

NoteIn the first test case, YouKn0wWho can split the sequence in the following way: $$$[1, 3, 4]$$$, $$$[2, 2]$$$, $$$[1, 5]$$$. This way, the LIS lengths are $$$h = [3, 1, 2]$$$, and the bitwise XOR of the LIS lengths is $$$3 \oplus 1 \oplus 2 = 0$$$.In the second test case, it can be shown that it is impossible to split the sequence into subarrays that will satisfy the condition.

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

void main() {
    int cases = to!int(strip(readln()));

    // per testcase
    for (int t = 0; t < cases; t++) {
        int n;
        readf!"%d\n"(n);

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            if (i == n-1) readf!"%d\n"(a[i]);
                else readf!"%d "(a[i]);
        }

        bool ascending = true;
        bool equal = false;
        int prev = a[0];
        for (int i = 1; i < n; i++) {
            if (prev > a[i]) ascending = false;
            if (prev == a[i]) equal = true;
            prev = a[i];
        }

        if (!ascending || (ascending && n % 2 == 0) || (ascending && equal)) writeln("YES");
        else writeln("NO");
    }
}

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

void main() {
    int cases = to!int(strip(readln()));

    // per testcase
    for (int t = 0; t < cases; t++) {
        int n;
        readf!"%d\n"(n);

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            if (i == n-1) readf!"%d\n"(a[i]);
                else readf!"%d "(a[i]);
        }

        bool ascending = true;
        int prev = a[0];
        for (int i = 1; i < n; i++) {
            if (prev > a[i]) ascending = false;
            prev = a[i];
        }

        if (!ascending || (ascending && n % 2 == 0)) writeln("YES");
        else writeln("NO");
    }
}


A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from $$$1$$$ to $$$9$$$ (inclusive) are round.For example, the following numbers are round: $$$4000$$$, $$$1$$$, $$$9$$$, $$$800$$$, $$$90$$$. The following numbers are not round: $$$110$$$, $$$707$$$, $$$222$$$, $$$1001$$$.You are given a positive integer $$$n$$$ ($$$1 \le n \le 10^4$$$). Represent the number $$$n$$$ as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number $$$n$$$ as a sum of the least number of terms, each of which is a round number.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is a line containing an integer $$$n$$$ ($$$1 \le n \le 10^4$$$).

Print $$$t$$$ answers to the test cases. Each answer must begin with an integer $$$k$$$ — the minimum number of summands. Next, $$$k$$$ terms must follow, each of which is a round number, and their sum is $$$n$$$. The terms can be printed in any order. If there are several answers, print any of them.

void main() {
	auto t = ri;
	foreach(i; 0..t) {
		auto S = rs;
		ulong cnt;
		ulong[] l;
		foreach(j, c; S) {
			if(c != '0') {
				cnt++;
				l ~= 10 ^^ (S.length - j - 1) * (c - '0');
			}
		}
		cnt.writeln;
		writefln("%(%d %)", l);
	}
}

// ===================================

import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;

T readAs(T)() if (isBasicType!T) {
	return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
	return readln.split.to!T;
}

T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		res[i] = readAs!(T[]);
	}
	return res;
}

T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		auto s = rs;
		foreach(j; 0..width) res[i][j] = s[j].to!T;
	}
	return res;
}

int ri() {
	return readAs!int;
}

long rl() {
	return readAs!long;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}

import std.stdio;

int main() {
    int t;
    readf("%d\n", &t);
    while(t--) {
    	int n;
    	readf("%d\n", &n);
    	int[] result;
    	int d = 1;
    	while(n > 0) {
    		if(n % 10 != 0)
    			result ~= (n % 10) * d;
    		d *= 10;
    		n /= 10;
    	}

    	writeln(result.length);
		foreach(x; result)
			write(x, " ");
    	writeln();
    }

    return 0;
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        int[] rs;
        int k = 0;
        while (N) {
            if (N%10 != 0) {
                rs ~= N%10 * 10^^k;
            }
            N /= 10;
            ++k;
        }
        writeln(rs.length);
        writeln(rs.to!(string[]).join(" "));
    }
}

// Author: Ivan Kazmenko (gassa@mail.ru)
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)
	{
		auto s = readln.strip;
		auto d = s.length;
		string [] ans;
		foreach (i, c; s)
		{
			if (c > '0')
			{
				ans ~= c ~ "0".repeat (d - i - 1).joiner.text;
			}
		}
		writeln (ans.length);
		writefln !("%-(%s %)") (ans);
	}
}

// Author: Ivan Kazmenko (gassa@mail.ru)
// dynamic programming with large constant, run once at the start
import std.algorithm;
import std.conv;
import std.random;
import std.range;
import std.stdio;
import std.string;

auto prepare (int limit)
{
	auto moves = iota (1, limit)
	    .filter !(j => j.text.count !(q{a > '0'}) == 1).array;
	auto f = new int [] [limit];
	foreach (i; moves)
	{
		f[i] ~= i;
	}
	foreach (i; 1..limit)
	{
		foreach (j; moves)
		{
			if (j < i)
			{
				if (f[i] is null || f[i].length >
				    f[i - j].length + uniform (0, 2))
				{
					f[i] = f[i - j] ~ j;
				}
			}
		}
	}
	return f;
}

void main ()
{
	rndGen.seed (25256);
	auto f = prepare (10001);
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		writeln (f[n].length);
		writefln !("%(%s %)") (f[n]);
	}
}

import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void calc(int n) {
    int[] ans;
    int x = 1000000;
    while (x > 0) {
        int t = n / x;
        if (t > 0) ans ~= t * x;
        n %= x;
        x /= 10;
    }
    writeln(ans.length);
    writeln(ans.map!text.join(' '));
}

void main() {
    int t; scan(t);
    foreach (_; 0..t) {
        int n; scan(n);
        calc(n);
    }
}

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;

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

T readNum(T)(){
  return readStr.to!T;
}

T[] readNums(T)(){
  return readStr.split.to!(T[]);
}

string readStr(){
  return readln.chomp;
}

void main(){
  auto t = readNum!int;

  foreach(i; 0 .. t){
    auto s = readStr;
    auto n = s.length - count(s, "0");
    writeln(n);

    foreach_reverse(j, e; s){
      if(e != '0'){
        write(e);
        foreach(k; 0 .. s.length - j - 1){
          write("0");
        }
        write(" ");
      }
    }

    writeln();
  }
}

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) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!string;
		foreach (i; 0..n.length)
		{
			if (n[i] != '0')
			{
				ans[ti] ~= (n[i]-'0') * 10^^(n.length-i-1);
			}
		}
	}

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


Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.

The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105).

Print n integers forming the permutation. If there are multiple answers, print any of them.

NoteBy |x| we denote the absolute value of number x.

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;

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
	        int [] a;
	        int lo = 1;
	        int hi = k + 1;
	        while (true)
	        {
	        	if (lo > hi)
	        	{
	        		break;
	        	}
	        	a ~= lo;
	        	lo++;
	        	if (lo > hi)
	        	{
	        		break;
	        	}
	        	a ~= hi;
	        	hi--;
	        }
	        foreach (num; k + 2..n + 1)
	        {
	        	a ~= num;
	        }
		writefln ("%(%s %)", a);
	}
}


Little boy Gerald studies at school which is quite far from his house. That's why he has to go there by bus every day. The way from home to school is represented by a segment of a straight line; the segment contains exactly n + 1 bus stops. All of them are numbered with integers from 0 to n in the order in which they follow from Gerald's home. The bus stop by Gerald's home has number 0 and the bus stop by the school has number n.There are m buses running between the house and the school: the i-th bus goes from stop si to ti (si < ti), visiting all the intermediate stops in the order in which they follow on the segment. Besides, Gerald's no idiot and he wouldn't get off the bus until it is still possible to ride on it closer to the school (obviously, getting off would be completely pointless). In other words, Gerald can get on the i-th bus on any stop numbered from si to ti - 1 inclusive, but he can get off the i-th bus only on the bus stop ti.Gerald can't walk between the bus stops and he also can't move in the direction from the school to the house.Gerald wants to know how many ways he has to get from home to school. Tell him this number. Two ways are considered different if Gerald crosses some segment between the stops on different buses. As the number of ways can be too much, find the remainder of a division of this number by 1000000007 (109 + 7).

The first line contains two space-separated integers: n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 105). Then follow m lines each containing two integers si, ti. They are the numbers of starting stops and end stops of the buses (0 ≤ si < ti ≤ n).

Print the only number — the number of ways to get to the school modulo 1000000007 (109 + 7).

NoteThe first test has the only variant to get to school: first on bus number one to the bus stop number one; then on bus number two to the bus stop number two.In the second test no bus goes to the third bus stop, where the school is positioned. Thus, the correct answer is 0.In the third test Gerald can either get or not on any of the first four buses to get closer to the school. Thus, the correct answer is 24 = 16.

// Cheese-Cracker: cheese-cracker.github.io
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;

alias ll = long;
alias rbt = redBlackTree;
alias pair = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
/*******************************It's a Me, Mario!*******************************************/

const ll PRIME = 10^^9 + 7;

void play(){
    ll n, m;
    n = rd; m = rd;
    // (pos, oper, inx);
    pair[] points;
    /* ll[] st, end; */
    ll s, t;
    foreach(i; 0..m.to!int){
        s = rd; t = rd;
        /* st ~= s; end ~= t; */
        points ~= pair(t, s);
    }
    points.sort;

    ll res = 0;
    auto pref = rbt!pair([pair(-1, 0)]);
    foreach(p; points){
        // Endpoint
        ll takesofar = pref.back[1];
        if(pref.back[0] == p[0]){ pref.removeBack; }

        ll f = pref.back[1];
        ll b = pref.lowerBound(pair(p[1], 0L)).back[1];
        ll take = 0;
        take += (f - b) + PRIME;
        if(p[1] == 0) ++take;
        take %= PRIME;
        if(p[0] == n){
            res += take;
            res %= PRIME;
        }

        takesofar += take;
        takesofar %= PRIME;
        pref.insert(pair(p[0], takesofar));
        debug writeln(pref);
    }
    writeln(res);
}

int main(){
    long t = 1;
    /* t = rd;        // Toggle! */
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}


Nezzar loves the game osu!.osu! is played on beatmaps, which can be seen as an array consisting of distinct points on a plane. A beatmap is called nice if for any three consecutive points $$$A,B,C$$$ listed in order, the angle between these three points, centered at $$$B$$$, is strictly less than $$$90$$$ degrees.  Points $$$A,B,C$$$ on the left have angle less than $$$90$$$ degrees, so they can be three consecutive points of a nice beatmap; Points $$$A',B',C'$$$ on the right have angle greater or equal to $$$90$$$ degrees, so they cannot be three consecutive points of a nice beatmap. Now Nezzar has a beatmap of $$$n$$$ distinct points $$$A_1,A_2,\ldots,A_n$$$. Nezzar would like to reorder these $$$n$$$ points so that the resulting beatmap is nice.Formally, you are required to find a permutation $$$p_1,p_2,\ldots,p_n$$$ of integers from $$$1$$$ to $$$n$$$, such that beatmap $$$A_{p_1},A_{p_2},\ldots,A_{p_n}$$$ is nice. If it is impossible, you should determine it.

The first line contains a single integer $$$n$$$ ($$$3 \le n \le 5000$$$). Then $$$n$$$ lines follow, $$$i$$$-th of them contains two integers $$$x_i$$$, $$$y_i$$$ ($$$-10^9 \le x_i, y_i \le 10^9$$$) — coordinates of point $$$A_i$$$. It is guaranteed that all points are distinct.

If there is no solution, print $$$-1$$$. Otherwise, print $$$n$$$ integers, representing a valid permutation $$$p$$$. If there are multiple possible answers, you can print any.

NoteHere is the illustration for the first test:  Please note that the angle between $$$A_1$$$, $$$A_2$$$ and $$$A_5$$$, centered at $$$A_2$$$, is treated as $$$0$$$ degrees. However, angle between $$$A_1$$$, $$$A_5$$$ and $$$A_2$$$, centered at $$$A_5$$$, is treated as $$$180$$$ degrees.

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});

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

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

		auto p = n.iota.array;
		foreach (i; 1..n)
		{
			int k = i;
			foreach (j; i + 1..n)
			{
				if (dist (a[p[k]], a[p[i - 1]]) <
				    dist (a[p[j]], a[p[i - 1]]))
				{
					k = j;
				}
			}
			p.swapAt (i, k);
		}

		writefln !("%(%s %)") (p.map !(q{a + 1}));
	}
}

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

struct Point
{
	int x;
	int y;

	Point opBinary (string op) (const auto ref Point that) const
	{
		Point res;
		res.x = mixin (q{this.x } ~ op ~ q{ that.x});
		res.y = mixin (q{this.y } ~ op ~ q{ that.y});
		return res;
	}
}

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

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

		auto p = n.iota.array;
		foreach (i; 1..n)
		{
			int k = i;
			foreach (j; i + 1..n)
			{
				if (sp (a[p[k]], a[p[i - 1]]) <
				    sp (a[p[j]], a[p[i - 1]]))
				{
					k = j;
				}
			}
			p.swapAt (i, k);
		}

		writefln !("%(%s %)") (p.map !(q{a + 1}));
	}
}

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

struct Point
{
	int x;
	int y;

	Point opBinary (string op) (const auto ref Point that) const
	{
		Point res;
		res.x = mixin (q{this.x } ~ op ~ q{ that.x});
		res.y = mixin (q{this.y } ~ op ~ q{ that.y});
		return res;
	}
}

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

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

		auto p = n.iota.array;
		foreach (i; 1..n)
		{
			int k = i;
			foreach (j; i + 1..n)
			{
				if (sp (a[p[k]], a[p[i - 1]]) >
				    sp (a[p[j]], a[p[i - 1]]))
				{
					k = j;
				}
			}
			p.swapAt (i, k);
		}

		writefln !("%(%s %)") (p.map !(q{a + 1}));
	}
}

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

struct Point
{
	int x;
	int y;

	Point opBinary (string op) (const auto ref Point that) const
	{
		Point res;
		res.x = mixin (q{this.x } ~ op ~ q{ that.x});
		res.y = mixin (q{this.y } ~ op ~ q{ that.y});
		return res;
	}
}

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

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

		auto p = n.iota.array;
		foreach (i; 1..n)
		{
			int k = i;
			foreach (j; i + 1..n)
			{
				if (sp (a[p[k]] - a[p[i - 1]],
				    a[p[j]] - a[p[k]]) >= 0)
				{
					k = j;
				}
			}
			p.swapAt (i, k);
		}

		writefln !("%(%s %)") (p.map !(q{a + 1}));
	}
}