Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!).Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps:  Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$;  Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$;  Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$;  Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$;  Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$;  Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$;  $$$\dots$$$  Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$;  Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \in\{1, 2, \ldots, 5 \cdot 10^8\}$$$ as described above.For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ $$$\dots$$$ $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array.Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.

The first line of the input contains one integer number $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array.

Print $$$n$$$ integers — $$$b_1, b_2, \dots, b_n$$$, where $$$b_i$$$ is the final value of the $$$i$$$-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array $$$a$$$. Note that you cannot change the order of elements in the array.

NoteThe first example is described in the problem statement.

module acmd;

import std.stdio;
import std.conv;

int main () {
   version (offline_judje) {
      stdin.reopen ("input.txt", "rt");
   }
   int n;
   readf!" %d" (n);
   foreach (i; 0 .. n) {
      int num;
      readf!" %d" (num);
      if (num % 2 == 0)
         num--;
      writef!"%d " (num);
   }

   return 0;
}

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

void main()
{
    int n;
    readf !"%s\n" (n);
    auto arr = readln.splitter.map!(to!int).map!(x => x-(1 - x%2));
    writefln("%-(%d %)", arr);
}

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

void main()
{
    auto n = readln.strip.to!ulong;
    auto a = readln.strip.split();
    foreach (i, v; a)
    {
        auto va = v.to!ulong;
        if (va % 2 == 1)
            write(va, " ");
        else
         write(va - 1, " ");
    }
}

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

void main()
{
    readln;

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

    arr = arr.map!(x => x % 2 == 0 ? x - 1 : x).array;

    arr.writefln!("%(%s %)");
}

import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;
void main(){
    auto n = readln.strip.to!ulong;
    auto s = readln.strip.split();
    foreach (i, k; s){
        auto ans = k.to!ulong;
        if (ans % 2) write(ans, " ");
        else write(ans - 1, " ");
    }
}

import std.stdio;
int main()
{
	int a=0;
	readf(" %d", &a);
	int[] m = new int[a];
	for (int i=0; i<a; i++)
	{
		readf(" %d", &m[i]);
	}
	for (int i=0; i<a; i++)
	{
		if (m[i]%2==0)
		{
			m[i]=m[i]-1;
		}
	}
	for (int i=0; i<a; i++)
	{
		writeln(m[i]);
	}
	return 0;
}


Vasya goes to visit his classmate Petya. Vasya knows that Petya's apartment number is $$$n$$$. There is only one entrance in Petya's house and the distribution of apartments is the following: the first floor contains $$$2$$$ apartments, every other floor contains $$$x$$$ apartments each. Apartments are numbered starting from one, from the first floor. I.e. apartments on the first floor have numbers $$$1$$$ and $$$2$$$, apartments on the second floor have numbers from $$$3$$$ to $$$(x + 2)$$$, apartments on the third floor have numbers from $$$(x + 3)$$$ to $$$(2 \cdot x + 2)$$$, and so on.Your task is to find the number of floor on which Petya lives. Assume that the house is always high enough to fit at least $$$n$$$ apartments.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n, x \le 1000$$$) — the number of Petya's apartment and the number of apartments on each floor of the house except the first one (there are two apartments on the first floor).

For each test case, print the answer: the number of floor on which Petya lives.

NoteConsider the first test case of the example: the first floor contains apartments with numbers $$$1$$$ and $$$2$$$, the second one contains apartments with numbers $$$3$$$, $$$4$$$ and $$$5$$$, the third one contains apartments with numbers $$$6$$$, $$$7$$$ and $$$8$$$. Therefore, Petya lives on the third floor.In the second test case of the example, Petya lives in the apartment $$$1$$$ which is on the first floor.

// Vicfred
// https://codeforces.com/contest/1426/problem/A
// math
import std.stdio;

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

    int n, x;
    while(t--) {
        readf("%s %s\n", &n, &x);
        if(n <= 2) {
            "1".writeln;
            continue;
        }
        int ans = (n - 3)/x + 2;
        ans.writeln;
    }
}

// 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 tup = 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; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }
/*******************************It's a Me, Mario!*******************************************/

void play(){
    int n, x;
    n = rd!int;
    x = rd!int;
    if(n <= 2){
        writeln(1);
    }else{
        n -= 3;
        int apt = n/x;
        writeln(2 + apt);
    }
}

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


3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or aliveNEKO#ΦωΦ has just got a new maze game on her PC!The game's main puzzle is a maze, in the forms of a $$$2 \times n$$$ rectangle grid. NEKO's task is to lead a Nekomimi girl from cell $$$(1, 1)$$$ to the gate at $$$(2, n)$$$ and escape the maze. The girl can only move between cells sharing a common side.However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.After hours of streaming, NEKO finally figured out there are only $$$q$$$ such moments: the $$$i$$$-th moment toggles the state of cell $$$(r_i, c_i)$$$ (either from ground to lava or vice versa).Knowing this, NEKO wonders, after each of the $$$q$$$ moments, whether it is still possible to move from cell $$$(1, 1)$$$ to cell $$$(2, n)$$$ without going through any lava cells.Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?

The first line contains integers $$$n$$$, $$$q$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le q \le 10^5$$$). The $$$i$$$-th of $$$q$$$ following lines contains two integers $$$r_i$$$, $$$c_i$$$ ($$$1 \le r_i \le 2$$$, $$$1 \le c_i \le n$$$), denoting the coordinates of the cell to be flipped at the $$$i$$$-th moment. It is guaranteed that cells $$$(1, 1)$$$ and $$$(2, n)$$$ never appear in the query list.

For each moment, if it is possible to travel from cell $$$(1, 1)$$$ to cell $$$(2, n)$$$, print "Yes", otherwise print "No". There should be exactly $$$q$$$ answers, one after every update. You can print the words in any case (either lowercase, uppercase or mixed).

NoteWe'll crack down the example test here:  After the first query, the girl still able to reach the goal. One of the shortest path ways should be: $$$(1,1) \to (1,2) \to (1,3) \to (1,4) \to (1,5) \to (2,5)$$$.  After the second query, it's impossible to move to the goal, since the farthest cell she could reach is $$$(1, 3)$$$.  After the fourth query, the $$$(2, 3)$$$ is not blocked, but now all the $$$4$$$-th column is blocked, so she still can't reach the goal.  After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.

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

void main ()
{
	int n, q;
	while (readf !(" %s %s") (n, q) > 0)
	{
		auto wall = new bool [] [] (2, n + 2);
		int block = 0;
		foreach (r; 0..q)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			int mult = wall[u][v] ? -1 : +1;
			foreach (k; -1..+2)
			{
				block += wall[u ^ 1][v + k] * mult;
			}
			wall[u][v] ^= true;
			writeln (block == 0 ? "Yes" : "No");
		}
	}
}

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

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

void solve(){
	int n = rint, q = rint;

	bool[][] xf = new bool[][](2, n);
	int cnt;

	foreach(_; 0 .. q){
		int r = rint - 1, c = rint - 1;
		if(xf[r][c]){
			xf[r][c] = 0;
			if(c > 0 && xf[1 - r][c - 1]) cnt -= 1;
			if(xf[1 - r][c]) cnt -= 1;
			if(c < n - 1 && xf[1 - r][c + 1]) cnt -= 1;
		}
		else{
			xf[r][c] = 1;
			if(c > 0 && xf[1 - r][c - 1]) cnt += 1;
			if(xf[1 - r][c]) cnt += 1;
			if(c < n - 1 && xf[1 - r][c + 1]) cnt += 1;
		}
		if(cnt > 0) "No".writeln;
		else "Yes".writeln;
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const Q = readInt();
      auto R = new int[Q];
      auto C = new int[Q];
      foreach (q; 0 .. Q) {
        R[q] = readInt() - 1;
        C[q] = readInt() - 1;
      }

      auto can = new bool[][](2, N);
      foreach (x; 0 .. 2) {
        can[x][] = true;
      }

      auto vals = new int[N - 1];
      vals[] = 1;
      int now = N - 1;

      void check(int y) {
        if (0 <= y && y < N - 1) {
          now -= vals[y];
          vals[y] = (can[0][y] && can[0][y + 1] || can[1][y] && can[1][y + 1]) ? 1 : 0;
          now += vals[y];
        }
      }

      foreach (q; 0 .. Q) {
        can[R[q]][C[q]] = !can[R[q]][C[q]];
        check(C[q] - 1);
        check(C[q]);
        writeln((now == N - 1) ? "Yes" : "No");
      }
    }
  } catch (EOFException e) {
  }
}

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

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

    auto board = new bool[][] (2, n);

    alias t = Tuple!(int, int);
    auto vert = make!(RedBlackTree!int);
    auto cross = make!(RedBlackTree!t);

    foreach (_; 0 .. q) {
        int r, c;
        readf("%s %s", &r, &c);
        readln;

        --r, --c;
        board[r][c] = !board[r][c];

        if (board[r][c]) {
            if (board[1 - r][c]) { vert.insert(c); }

            if (c > 0 && board[1-r][c-1]) { cross.insert(tuple(1-r, c-1)); }
            if (c < n-1 && board[1-r][c+1]) { cross.insert(tuple(r, c)); }
        } else {
            if (c in vert) { vert.removeKey(c); }

            if (c > 0 && tuple(1-r, c-1) in cross) { cross.removeKey(tuple(1-r, c-1)); }
            if (c < n-1 && tuple(r, c) in cross) { cross.removeKey(tuple(r, c)); }
        }

        writeln(vert.empty() && cross.empty() ? "Yes" : "No");
    }
}

import std.container;
import std.algorithm;
import std.range;
import std.array;
import std.stdio;
import std.utf;

DList!string _words;

void read(T...)(ref T t)
{
  void singleRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  static foreach(i; 0 .. T.length)
    {
      static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))
        {
          foreach(ref element; t[i])
            read(element);
        }
      else
        {
          singleRead(t[i]);
        }
    }
}

auto next(T)()
{
  T t;
  read(t);
  return t;
}

size_t ind(T)(T t)
{
  return cast(size_t)t;
}

ref auto at(R, I)(ref R r, I i)
{
  return r[cast(size_t)i];
}

void set(R, I, V)(ref R r, I i, V v)
{
  r[cast(size_t)i] = v;
}

import std.conv;
import std.traits;
import std.meta;
int _loglevel = -1;
void _log(string functionName, argSeq...)()
{
  foreach(i; 0 .. _loglevel * 4)
    write(' ');
  write(functionName);
  static foreach(arg; argSeq)
    {
      write(" ", arg.stringof, " = ", arg);
    }
  writeln;
}
enum logCall =
  q{
    debug
    {
     {
     _loglevel++;
     alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, ".").back));
     static if (_parameterTuple.length > 0)
       mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(", ").array, `);`);
     else
       _log!(__FUNCTION__);
      }
     foreach(i; 0 .. _loglevel * 4) write(' ');
     writeln('{');
     scope(exit)
       {
         foreach(i; 0 .. _loglevel * 4) write(' ');
         writeln('}');
         _loglevel--;
       }
    }
};

void logSym(S...)(int line = __LINE__, string file = __FUNCTION__)
{
  debug
    {
      foreach(i; 0 .. _loglevel * 4) write(' ');
      write(file, "(", line, "):");
      static foreach(s; S)
        write(" ", s.stringof, " = ", s);
      writeln;

    }
}

Array!E makeArray(E, S)(S size, lazy E value = E.init)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  foreach(ref ai; a)
    ai = value();
  return a;
}
Array!E makeArray(E, S)(S size)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  return a;
}

void main()
{
  auto n = next!long, q = next!long;
  auto state = new bool[][2];
  foreach(ref row; state)
    row = new bool[n.ind];
  auto conns = long(0);
  foreach(i; 0 .. q)
    {
      auto r = next!long - 1, c = next!long - 1;
      state[r.ind][c.ind] = !state[r.ind][c.ind];
      if (state[r.ind][c.ind])
        {
          foreach(oc; c - 1 .. c + 2)
            if (oc >= 0 && oc < n)
              if (state[(r^1).ind][oc.ind])
                conns++;
        }
      else
        {
          foreach(oc; c - 1 .. c + 2)
            if (oc >= 0 && oc < n)
              if (state[(r^1).ind][oc.ind])
                conns--;
        }
      if (conns > 0 || state[0][0] || state[1][$ - 1])
        writeln("No");
      else
        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; }
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 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 main()
{
	auto n = RD!int;
	auto q = RD!int;
	auto ans = new bool[](q);
	auto cell = new bool[][](2, n);
	bool[int] set;
	foreach (i; 0..q)
	{
		auto r = RD!int-1;
		auto c = RD!int-1;
		cell[r][c] = !cell[r][c];
		debug writeln(cell[0]);
		debug writeln(cell[1]);
		if (cell[r][c])
		{
			bool ok = i == 0 ? true : ans[i-1];
			foreach (j; max(0, c-1)..min(n, c+2))
			{
				if (cell[r^1][j])
				{
					ok = false;
					set[r*n+c] = true;
				}
			}
			ans[i] = ok;
		}
		else
		{
			if (set.get(r*n+c, false))
				set.remove(r*n+c);

			foreach (j; max(0, c-1)..min(n, c+2))
			{
				if (!cell[r^1][j]) continue;
				if (set.get((r^1)*n+j, false) == false) continue;
				bool ok = true;
				foreach (k; max(0, j-1)..min(n, j+2))
				{
					if (cell[r][k])
						ok = false;
				}
				if (ok)
					set.remove((r^1)*n+j);
			}
			ans[i] = set.length == 0;
		}
	}

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


This is an interactive problem.Khanh has $$$n$$$ points on the Cartesian plane, denoted by $$$a_1, a_2, \ldots, a_n$$$. All points' coordinates are integers between $$$-10^9$$$ and $$$10^9$$$, inclusive. No three points are collinear. He says that these points are vertices of a convex polygon; in other words, there exists a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$ such that the polygon $$$a_{p_1} a_{p_2} \ldots a_{p_n}$$$ is convex and vertices are listed in counter-clockwise order.Khanh gives you the number $$$n$$$, but hides the coordinates of his points. Your task is to guess the above permutation by asking multiple queries. In each query, you give Khanh $$$4$$$ integers $$$t$$$, $$$i$$$, $$$j$$$, $$$k$$$; where either $$$t = 1$$$ or $$$t = 2$$$; and $$$i$$$, $$$j$$$, $$$k$$$ are three distinct indices from $$$1$$$ to $$$n$$$, inclusive. In response, Khanh tells you:  if $$$t = 1$$$, the area of the triangle $$$a_ia_ja_k$$$ multiplied by $$$2$$$.  if $$$t = 2$$$, the sign of the cross product of two vectors $$$\overrightarrow{a_ia_j}$$$ and $$$\overrightarrow{a_ia_k}$$$. Recall that the cross product of vector $$$\overrightarrow{a} = (x_a, y_a)$$$ and vector $$$\overrightarrow{b} = (x_b, y_b)$$$ is the integer $$$x_a \cdot y_b - x_b \cdot y_a$$$. The sign of a number is $$$1$$$ it it is positive, and $$$-1$$$ otherwise. It can be proven that the cross product obtained in the above queries can not be $$$0$$$.You can ask at most $$$3 \cdot n$$$ queries.Please note that Khanh fixes the coordinates of his points and does not change it while answering your queries. You do not need to guess the coordinates. In your permutation $$$a_{p_1}a_{p_2}\ldots a_{p_n}$$$, $$$p_1$$$ should be equal to $$$1$$$ and the indices of vertices should be listed in counter-clockwise order.

NoteThe image below shows the hidden polygon in the example:  The interaction in the example goes as below:   Contestant reads $$$n = 6$$$.  Contestant asks a query with $$$t = 1$$$, $$$i = 1$$$, $$$j = 4$$$, $$$k = 6$$$.  Jury answers $$$15$$$. The area of the triangle $$$A_1A_4A_6$$$ is $$$7.5$$$. Note that the answer is two times the area of the triangle.  Contestant asks a query with $$$t = 2$$$, $$$i = 1$$$, $$$j = 5$$$, $$$k = 6$$$.  Jury answers $$$-1$$$. The cross product of $$$\overrightarrow{A_1A_5} = (2, 2)$$$ and $$$\overrightarrow{A_1A_6} = (4, 1)$$$ is $$$-2$$$. The sign of $$$-2$$$ is $$$-1$$$.  Contestant asks a query with $$$t = 2$$$, $$$i = 2$$$, $$$j = 1$$$, $$$k = 4$$$.  Jury answers $$$1$$$. The cross product of $$$\overrightarrow{A_2A_1} = (-5, 2)$$$ and $$$\overrightarrow{A_2A_4} = (-2, -1)$$$ is $$$1$$$. The sign of $$$1$$$ is $$$1$$$.  Contestant says that the permutation is $$$(1, 3, 4, 2, 6, 5)$$$.

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

long askArea (int i, int j, int k)
{
	writeln (1, " ", i + 1, " ", j + 1, " ", k + 1);
	stdout.flush ();
	auto res = readln.strip.to !(long);
	return res;
}

int askSign (int i, int j, int k)
{
	writeln (2, " ", i + 1, " ", j + 1, " ", k + 1);
	stdout.flush ();
	auto res = readln.strip.to !(int);
	return res;
}

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

		int i = 0;
		int j = 1;
		foreach (k; 2..n)
		{
			if (askSign (i, j, k) < 0)
			{
				j = k;
			}
		}

		alias Pair = Tuple !(long, q{area}, int, q{num});
		Pair [] p;
		foreach (k; 1..n)
		{
			if (k != j)
			{
				auto cur = askArea (i, j, k);
				p ~= Pair (cur, k);
			}
		}
		sort (p);

		int [] left = [];
		int [] right = [i, j];
		foreach (const ref c; p)
		{
			auto num = c.num;
			if (askSign (right[$ - 2], right[$ - 1], num) < 0)
			{
				left ~= right[$ - 1];
				right.length -= 1;
				right.assumeSafeAppend ();
			}
			right ~= num;
		}

		auto ans = right ~ left.retro.array;
		ans[] += 1;
		writefln ("0 %(%s %)", ans);
		stdout.flush ();
	}
}

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

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

void solve(){
	int n = rint;

	X[] as, bs;
	foreach(i; 2 .. n){
		if(ask(2, 0, 1, i) < 0) as ~= X(i, ask(1, 0, 1, i));
		else bs ~= X(i, ask(1, 1, 0, i));
	}

	X az = X(-1, -1), bz = X(-1, -1);
	foreach(a; as) if(a.v > az.v) az = a;
	foreach(b; bs) if(b.v > bz.v) bz = b;

	X[] a0s, a1s, b0s, b1s;
	foreach(a; as){
		if(a.u == az.u) continue;
		if(ask(2, 0, az.u, a.u) < 0) a0s ~= a;
		else a1s ~= a;
	}
	foreach(b; bs){
		if(b.u == bz.u) continue;
		if(ask(2, 1, bz.u, b.u) < 0) b0s ~= b;
		else b1s ~= b;
	}
	a0s.sort!"a.v<b.v"();
	a1s.sort!"a.v>b.v"();
	b0s.sort!"a.v<b.v"();
	b1s.sort!"a.v>b.v"();

	X[] ans = [X(0, 0)] ~ a0s;
	if(az.u >= 0) ans ~= az;
	ans ~= a1s ~ [X(1, 0)] ~ b0s;
	if(bz.u >= 0) ans ~= bz;
	ans ~= b1s;
	writeln("0 ", ans.map!(x => x.u + 1).map!(to!string).array.join(" "));
}

struct X{
	int u;
	long v;
}

long ask(int t, int i, int j, int k){
	writeln(t, " ", i + 1, " ", j + 1, " ", k + 1);
	stdout.flush;
	return rlong;
}

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.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 {
  long[] X, Y;
}

long Ask(int t, int i, int j, int k) {
  long ret;
  debug {
    ret = (X[j] - X[i]) * (Y[k] - Y[i]) - (Y[j] - Y[i]) * (X[k] - X[i]);
    if (t == 1) {
      ret = abs(ret);
    } else {
      ret = sgn(ret);
    }
  } else {
    writeln(t, " ", i + 1, " ", j + 1, " ", k + 1);
    stdout.flush;
    ret = readLong();
  }
  return ret;
}

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

  auto as = new long[N];
  foreach (i; 2 .. N) {
    as[i] = Ask(1, 0, 1, i);
    as[i] *= Ask(2, 0, 1, i);
  }
  debug {
    writeln("as = ", as);
  }

  int[] ans;
  ans ~= 0;
  {
    int im;
    foreach (i; 2 .. N) {
      if (as[im] > as[i]) {
        im = i;
      }
    }
    if (im != 0) {
      int[] ims, ips;
      foreach (i; 2 .. N) {
        if (as[i] < 0 && i != im) {
          const res = Ask(2, 0, im, i);
          (res < 0) ? (ims ~= i) : (ips ~= i);
        }
      }
      ims.sort!((i, j) => (as[i] > as[j]));
      ips.sort!((i, j) => (as[i] < as[j]));
      foreach (i; ims) ans ~= i;
      ans ~= im;
      foreach (i; ips) ans ~= i;
    }
  }
  ans ~= 1;
  {
    int im;
    foreach (i; 2 .. N) {
      if (as[im] < as[i]) {
        im = i;
      }
    }
    if (im != 0) {
      int[] ims, ips;
      foreach (i; 2 .. N) {
        if (as[i] > 0 && i != im) {
          const res = Ask(2, 0, im, i);
          (res < 0) ? (ims ~= i) : (ips ~= i);
        }
      }
      ims.sort!((i, j) => (as[i] < as[j]));
      ips.sort!((i, j) => (as[i] > as[j]));
      foreach (i; ims) ans ~= i;
      ans ~= im;
      foreach (i; ips) ans ~= i;
    }
  }

  write(0);
  foreach (i; ans) {
    write(" ", i + 1);
  }
  writeln();
  stdout.flush;
}

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

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

void solve(){
	int n = rint;

	X[] as, bs;
	foreach(i; 2 .. n){
		if(ask(2, 0, 1, i) < 0) as ~= X(i, ask(1, 0, 1, i));
		else bs ~= X(i, ask(1, 1, 0, i));
	}

	X az = X(-1, -1), bz = X(-1, -1);
	foreach(a; as) if(a.v > az.v) az = a;
	foreach(b; bs) if(b.v > bz.v) bz = b;

	X[] a0s, a1s, b0s, b1s;
	foreach(a; as){
		if(a.u == az.u || ask(2, 0, az.u, a.u) < 0) a0s ~= a;
		else a1s ~= a;
	}
	foreach(b; bs){
		if(b.u == bz.u || ask(2, 1, bz.u, b.u) < 0) b0s ~= b;
		else b1s ~= b;
	}
	a0s.sort!"a.v<b.v"();
	a1s.sort!"a.v>b.v"();
	b0s.sort!"a.v<b.v"();
	b1s.sort!"a.v>b.v"();

	X[] ans = [X(0, 0)] ~ a0s ~ a1s ~ [X(1, 0)] ~ b0s ~ b1s;
	writeln("0 ", ans.map!(x => x.u + 1).map!(to!string).array.join(" "));
}

struct X{
	int u;
	long v;
}

long ask(int t, int i, int j, int k){
	writeln(t, " ", i + 1, " ", j + 1, " ", k + 1);
	stdout.flush;
	return rlong;
}

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

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

void solve(){
	int n = rint;
	int p = uniform(0, n);
	int q = uniform(0, n);
	if(p == q) q += 1, q %= n;

	int[] as, bs;
	foreach(i; 0 .. n){
		if(i == p || i == q) continue;
		long x = ask(2, p, q, i);
		if(x < 0) as ~= i;
		else bs ~= i;
	}
	int[] ans = [p] ~ calc(as, p, q) ~ [q] ~ calc(bs, q, p);

	int i0;
	while(ans[i0] != 0) i0 += 1;

	ans = ans[i0 .. $] ~ ans[0 .. i0];
	ans.map!(x => x + 1).map!(to!string).array.join(" ").writeln;
}

int[] calc(int[] us, int p, int q){
	if(us.length <= 1) return us;
	int[] as, bs;
	int r = 0;
	long highest = 0;
	foreach(u; us){
		long w = ask(1, p, q, u);
		if(w > highest) r = u, highest = w;
	}
	foreach(u; us){
		if(r == u) continue;
		long x = ask(2, p, r, u);
		if(x < 0) as ~= u;
		else bs ~= u;
	}
	return calc(as, p, r) ~ [r] ~ calc(bs, r, q);
}

long ask(int t, int i, int j, int k){
	writeln(t, " ", i + 1, " ", j + 1, " ", k + 1);
	stdout.flush;
	return rlong;
}

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

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

void solve(){
	int n = rint;

	X[] as, bs;
	X az = X(-1, -1), bz = X(-1, -1);
	foreach(i; 2 .. n){
		if(ask(2, 0, 1, i) < 0){
			X a = X(i, ask(1, 0, 1, i));
			as ~= a;
			if(az.v < a.v) az = a;
		}
		else{
			X b = X(i, ask(1, 1, 0, i));
			bs ~= b;
			if(bz.v < b.v) bz = b;
		}
	}
	X[] a0s, a1s, b0s, b1s;
	foreach(a; as){
		if(a.u == az.u) continue;
		if(ask(2, 0, az.u, a.u) < 0) a0s ~= a;
		else a1s ~= a;
	}
	foreach(b; bs){
		if(b.u == bz.u) continue;
		if(ask(2, 1, bz.u, b.u) < 0) b0s ~= b;
		else b1s ~= b;
	}
	a0s.sort!"a.v<b.v"();
	a1s.sort!"a.v>b.v"();
	b0s.sort!"a.v<b.v"();
	b1s.sort!"a.v>b.v"();

	X[] ans = [X(0, 0)] ~ a0s ~ az ~ a1s ~ [X(1, 0)] ~ b0s ~ bz ~ b1s;
	writeln("0 ", ans.map!(x => x.u + 1).map!(to!string).array.join(" "));
}

struct X{
	int u;
	long v;
}

long ask(int t, int i, int j, int k){
	writeln(t, " ", i + 1, " ", j + 1, " ", k + 1);
	stdout.flush;
	return rlong;
}


You are given a tree with n nodes (numbered from 1 to n) rooted at node 1. Also, each node has two values associated with it. The values for i-th node are ai and bi.You can jump from a node to any node in its subtree. The cost of one jump from node x to node y is the product of ax and by. The total cost of a path formed by one or more jumps is sum of costs of individual jumps. For every node, calculate the minimum total cost to reach any leaf from that node. Pay attention, that root can never be leaf, even if it has degree 1.Note that you cannot jump from a node to itself.

The first line of input contains an integer n (2 ≤ n ≤ 105) — the number of nodes in the tree. The second line contains n space-separated integers a1,  a2,  ...,  an( - 105  ≤  ai  ≤  105). The third line contains n space-separated integers b1,  b2,  ...,  bn( - 105  ≤  bi  ≤  105). Next n  -  1 lines contains two space-separated integers ui and vi (1 ≤ ui,  vi ≤  n) describing edge between nodes ui and vi in the tree.

Output n space-separated integers, i-th of which denotes the minimum cost of a path from node i to reach any leaf.

NoteIn the first example, node 3 is already a leaf, so the cost is 0. For node 2, jump to node 3 with cost a2 × b3 = 50. For node 1, jump directly to node 3 with cost a1 × b3 = 10.In the second example, node 3 and node 4 are leaves, so the cost is 0. For node 2, jump to node 4 with cost a2 × b4 = 100. For node 1, jump to node 2 with cost a1 × b2 =  - 400 followed by a jump from 2 to 4 with cost a2 × b4 = 100.

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

// class Func = TX -> TY
//   for any f, g: Func, x -> sgn(g(x) - f(x)) must be monotone
class LiChaoTree(Func) {
  import std.algorithm : swap;
  alias TX = Func.TX;
  alias TY = Func.TY;
  const(TX) L, R;
  class Tree {
    Tree l, r;
    Func f;
    this(Func f) {
      this.f = f;
    }
  }
  Tree root;

  // [L, R)
  this(TX L, TX R) {
    assert(L <= R, "LiChaoTree: L <= R must hold");
    this.L = L;
    this.R = R;
  }
  // Add g to the whole [L, R)
  void add(Func g) {
    root = add(root, L, R, g);
  }
  // Add g to [a, b)
  void add(TX a, TX b, Func g) {
    root = add(root, L, R, a, b, g);
  }
  // Get max at a
  TY opCall(TX a) {
    TY mx = TY.min;
    TX l = L, r = R;
    for (Tree u = root; u; ) {
      if (u.f) {
        const fX = u.f(a);
        if (mx < fX) mx = fX;
      }
      const mid = l + (r - l) / 2;
      if (a < mid) {
        u = u.l; r = mid;
      } else {
        u = u.r; l = mid;
      }
    }
    return mx;
  }

 private:
  Tree add(Tree u0, TX l, TX r, Func g) {
    if (!u0) return new Tree(g);
    TY gL = g(l), gR = g(r);
    for (Tree u = u0; ; ) {
      TY fL = u.f ? u.f(l) : TY.min, fR = u.f ? u.f(r) : TY.min;
      if (fL >= gL && fR >= gR) return u0;
      if (fL <= gL && fR <= gR) { u.f = g; return u0; }
      const mid = l + (r - l) / 2;
      TY fMid = u.f(mid), gMid = g(mid);
      if (fMid < gMid) {
        swap(u.f, g);
        if (!g) return u0;
        if (gL < fL) {
          if (!u.l) { u.l = new Tree(g); return u0; }
          u = u.l; r = mid; gL = fL; gR = fMid;
        } else {
          if (!u.r) { u.r = new Tree(g); return u0; }
          u = u.r; l = mid; gL = fMid; gR = fR;
        }
      } else {
        if (fL < gL) {
          if (!u.l) { u.l = new Tree(g); return u0; }
          u = u.l; r = mid; gR = gMid;
        } else {
          if (!u.r) { u.r = new Tree(g); return u0; }
          u = u.r; l = mid; gL = gMid;
        }
      }
    }
  }
  Tree add(Tree u, TX l, TX r, TX a, TX b, Func g) {
    if (b <= l || r <= a) return u;
    if (a <= l && r <= b) return add(u, l, r, g);
    if (u && u.f && u.f(l) >= g(l) && u.f(r) >= g(r)) return u;
    if (!u) u = new Tree(null);
    const mid = l + (r - l) / 2;
    u.l = add(u.l, l, mid, a, b, g);
    u.r = add(u.r, mid, r, a, b, g);
    return u;
  }
}

// y = p x + q
class Line {
  alias TX = long;
  alias TY = long;
  long p, q;
  this(long p, long q) {
    this.p = p;
    this.q = q;
  }
  TY opCall(TX x) {
    return p * x + q;
  }
}

enum INF = 10L^^18;
enum LIM = 10^^5 + 10;

int N;
long[] A, B;
int[] U, V;

int[][] G;
long[] dp;
Line[][] liness;
LiChaoTree!Line[] lcts;

void dfs(int u, int p) {
  liness[u] = [];
  lcts[u] = new LiChaoTree!Line(-LIM, LIM);
  foreach (i; G[u]) {
    const v = U[i] ^ V[i] ^ u;
    if (v != p) {
      dfs(v, u);
      if (liness[u].length < liness[v].length) {
        swap(liness[u], liness[v]);
        swap(lcts[u], lcts[v]);
      }
      liness[u] ~= liness[v];
      foreach (l; liness[v]) {
        lcts[u].add(l);
      }
    }
  }
  if (liness[u]) {
    dp[u] = -lcts[u](A[u]);
  } else {
    // leaf
    dp[u] = 0;
  }
  auto l = new Line(-B[u], -dp[u]);
  liness[u] ~= l;
  lcts[u].add(l);
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[U[i]] ~= i;
        G[V[i]] ~= i;
      }
      dp.length = N;
      liness.length = N;
      lcts.length = N;
      dfs(0, -1);
      foreach (u; 0 .. N) {
        if (u > 0) write(" ");
        write(dp[u]);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}


You are given two circles. Find the area of their intersection.

The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle. The second line contains three integers x2, y2, r2 ( - 109 ≤ x2, y2 ≤ 109, 1 ≤ r2 ≤ 109) — the position of the center and the radius of the second circle.

Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.

module main;

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

immutable real eps = 1e-9;

int main()
{
    real x1, y1, r1;
    real x2, y2, r2;

    readf(" %f %f %f", &x1, &y1, &r1);
    readf(" %f %f %f", &x2, &y2, &r2);
    readln();

    real d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);
    real result;

    if (d - r1 - r2 > 0)
        result = 0.0;
    else if (d - r1 + r2 < eps)
        result = PI * r2 ^^ 2;
    else if (d - r2 + r1 < eps)
        result = PI * r1 ^^ 2;
    else {
        real angle = 2 * acos((r1 ^^ 2 + d ^^ 2 - r2 ^^ 2) /
                                2 / r1 / d);
        real sectorSquare = r1 ^^ 2 * angle / 2;
        real segmentSquare = sectorSquare -
                               1.0 / 2 * r1 ^^ 2 * sin(angle);

        real angle2 = 2 * acos((r2 ^^ 2 + d ^^ 2 - r1 ^^ 2) /
                                2 / r2 / d);
        real sectorSquare2 = r2 ^^ 2 * angle2 / 2;
        real segmentSquare2 = sectorSquare2 -
                               1.0 / 2 * r2 ^^ 2 * sin(angle2);

        result = segmentSquare + segmentSquare2;
    }

    writefln("%.10f", result);

	return 0;
}

module main;

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

immutable double eps = 1e-7;

int main()
{
    double x1, y1, r1;
    double x2, y2, r2;

    readf(" %f %f %f", &x1, &y1, &r1);
    readf(" %f %f %f", &x2, &y2, &r2);
    readln();

    double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);
    double result;

    if (d - r1 - r2 > 0)
        result = 0.0;
    else if (d - r1 + r2 < eps)
        result = PI * r2 ^^ 2;
    else if (d - r2 + r1 < eps)
        result = PI * r1 ^^ 2;
    else {
        double h = (r1 + r2 - d) / 2.0;

        double angle = 2 * acos((r1 - h) / r1);

        result = 1.0 * r1 ^^ 2 * (angle - sin(angle));

        debug {writeln(d, ' ', h, ' ', angle);};
    }

    writefln("%.10f", result);

	return 0;
}

module main;

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

immutable double eps = 1e-7;

int main()
{
    double x1, y1, r1;
    double x2, y2, r2;

    readf(" %f %f %f", &x1, &y1, &r1);
    readf(" %f %f %f", &x2, &y2, &r2);
    readln();

    double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);
    double result;

    if (d - r1 - r2 > 0)
        result = 0.0;
    else if (d - r1 + r2 < eps)
        result = PI * r2 ^^ 2;
    else if (d - r2 + r1 < eps)
        result = PI * r1 ^^ 2;
    else {
        double angle = 2 * acos((r1 ^^ 2 + d ^^ 2 - r2 ^^ 2) /
                                2 / r1 / d);
        double sectorSquare = r1 ^^ 2 * angle / 2;
        double h = r1 / cos(angle / 2);
        double segmentSquare = sectorSquare - 1.0 / 2 * r1 * r1 * sin(angle);

        debug {writeln(angle, ' ',
                       sectorSquare, ' ',
                       h, ' ', segmentSquare);};

        result = segmentSquare * 2;
    }

    writefln("%.10f", result);

	return 0;
}

module main;

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

immutable double eps = 1e-7;

int main()
{
    double x1, y1, r1;
    double x2, y2, r2;

    readf(" %f %f %f", &x1, &y1, &r1);
    readf(" %f %f %f", &x2, &y2, &r2);
    readln();

    double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);
    double result;

    if (d - r1 - r2 > 0)
        result = 0.0;
    else if (d - r1 + r2 < eps)
        result = PI * r2 ^^ 2;
    else if (d - r2 + r1 < eps)
        result = PI * r1 ^^ 2;
    else {
        double angle = 2 * acos((r1 ^^ 2 + d ^^ 2 - r2 ^^ 2) /
                                2 / r1 / d);
        double sectorSquare = r1 ^^ 2 * angle / 2;
        double h = r1 / cos(angle / 2);
        double segmentSquare = sectorSquare -
                               1.0 / 2 * r1 * r1 * sin(angle);

        double angle2 = 2 * acos((r2 ^^ 2 + d ^^ 2 - r1 ^^ 2) /
                                2 / r2 / d);
        double sectorSquare2 = r2 ^^ 2 * angle2 / 2;
        double h2 = r2 / cos(angle2 / 2);
        double segmentSquare2 = sectorSquare2 -
                               1.0 / 2 * r2 ^^ 2 * sin(angle2);

        debug {writeln(angle, ' ',
                       sectorSquare, ' ',
                       h, ' ', segmentSquare);};

        result = segmentSquare + segmentSquare2;
    }

    writefln("%.10f", result);

	return 0;
}

module main;

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

immutable double eps = 1e-7;

int main()
{
    double x1, y1, r1;
    double x2, y2, r2;

    readf(" %f %f %f", &x1, &y1, &r1);
    readf(" %f %f %f", &x2, &y2, &r2);
    readln();

    double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);
    double result;

    if (d - r1 - r2 > 0)
        result = 0.0;
    else if (d - r1 + r2 < eps)
        result = PI  * r1 ^^ 2;
    else if (d - r2 + r1 < eps)
        result = PI * r2 ^^ 2;
    else {
        double h = (r1 + r2 - d) / 2.0;

        double angle = 2 * acos((r1 - h) / r1);

        result = 1.0 * r1 ^^ 2 * (angle - sin(angle));

        debug {writeln(d, ' ', h, ' ', angle);};
    }

    writefln("%.10f", result);

	return 0;
}

module main;

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

immutable double eps = 1e-7;

int main()
{
    double x1, y1, r1;
    double x2, y2, r2;

    readf(" %f %f %f", &x1, &y1, &r1);
    readf(" %f %f %f", &x2, &y2, &r2);
    readln();

    double d = sqrt((x1 - x2) ^^ 2 + (y1 - y2) ^^ 2);
    double result;

    if (d - r1 - r2 > 0)
        result = 0.0;
    else if (d - r1 + r2 < eps)
        result = 0.0;
    else if (d - r2 + r1 < eps)
        result = 0.0;
    else {
        double h = (r1 + r2 - d) / 2.0;

        double angle = 2 * acos((r1 - h) / r1);

        result = 1.0 * r1 ^^ 2 * (angle - sin(angle));

        debug {writeln(d, ' ', h, ' ', angle);};
    }

    writefln("%.10f", result);

	return 0;
}


HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≤ fi ≤ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th.It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time.

The first line contains a positive integer n (1 ≤ n ≤ 2·105) — the number of fragments. The second line contains n different integers fi (1 ≤ fi ≤ n) — the number of the fragment written in the i-th sector.

Print the only integer — the number of time units needed to read the file.

NoteIn the second example the head moves in the following way:  1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units  2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units  3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units  4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10.

import std.stdio;
import std.string;
import std.conv;
import std.regex;
import std.file;
import std.math;
void main()
{

    string[] inp = split(strip(readln()));
    int n = to!int(inp[0]);
    string[] s = split(strip(readln()));
    int[] a=new int[s.length];
    for(int i = 0 ; i < s.length ; i++)
    {
        a[to!int(s[i])-1]=i;
    }
    long res;
    for(int i = 1 ; i < a.length ; i++)
    {
        res+=abs(a[i]-a[i-1]);
    }
    writeln(res);

    //writeln("-1");

}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto p = readln.split.map !(to !(int)).array;
		auto q = new int [n];
		foreach (i, c; p)
		{
			q[c - 1] = i;
		}
		(n - 1).iota.map !(i => abs (q[i + 1] - q[i]))
		    .sum (0L).writeln;
	}
}

module main;

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

int main()
{
    auto n = readln().strip().to! (int);
    auto f = [0] ~ readln().strip().split().map! (to! (int)).array();

    auto dict = new int [n + 2];
    foreach (int i; 1..f.length)
        dict[f[i]] = i;

    long result;
    for (int i = 2; i <= n; i++)
        result += abs(dict[i] - dict[i - 1]);

    writeln(result);

	return 0;
}

module main;

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

int main()
{
    auto n = readln().strip().to! (int);
    auto f = readln().strip().split().map! (to! (int)).array();

    int[int] dict;
    foreach (int i; 0..f.length)
        dict[f[i] - 1] = i;

    int result;
    for (int i = 0; i < n - 1; i++)
        result += abs(dict[i + 1] - dict[i]);

    writeln(result);

	return 0;
}

module main;

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

int main()
{
    auto n = readln().strip().to! (int);
    auto f = [0] ~ readln().strip().split().map! (to! (int)).array();

    auto dict = new int [n + 2];
    foreach (int i; 1..f.length)
        dict[f[i]] = i;

    int result;
    for (int i = 2; i <= n; i++)
        result += abs(dict[i] - dict[i - 1]);

    writeln(result);

	return 0;
}

module main;

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

int main()
{
    auto n = readln().strip().to! (int);
    auto f = [0] ~ readln().strip().split().map! (to! (int)).array();

    int[int] dict;
    foreach (int i; 1..f.length)
        dict[f[i]] = i;

    int result;
    for (int i = 1; i <= n - 1; i++)
        result += abs(dict[i + 1] - dict[i]);

    writeln(result);

	return 0;
}


You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, and an array $$$b$$$, with length $$$n$$$. Initially $$$b_i=0$$$ for each $$$1 \leq i \leq n$$$.In one move you can choose an integer $$$i$$$ ($$$1 \leq i \leq n$$$), and add $$$a_i$$$ to $$$b_i$$$ or subtract $$$a_i$$$ from $$$b_i$$$. What is the minimum number of moves needed to make $$$b$$$ increasing (that is, every element is strictly greater than every element before it)?

The first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 5000$$$). The second line contains $$$n$$$ integers, $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array $$$a$$$.

Print a single integer, the minimum number of moves to make $$$b$$$ increasing.

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 n = RD!int;
	auto a = RDA;

	long ans = long.max;
	foreach (i; 0..n)
	{
		long cnt;
		{
			long last;
			foreach_reverse (j; 0..i)
			{
				auto c = (last+a[j]) / a[j];
				last = a[j] * c;
				cnt += c;
			}
		}
		{
			long last;
			foreach (j; i+1..n)
			{
				auto c = (last+a[j]) / a[j];
				last = a[j] * c;
				cnt += c;
			}
		}
		ans.chmin(cnt);
	}

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

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		long res = long.max;
		foreach (k; 0..n)
		{
			long cur = 0;
			long lo = 0;
			foreach_reverse (i; 0..k)
			{
				long steps = lo / a[i] - 1;
				cur -= steps;
				lo = steps * a[i];
			}
			long hi = 0;
			foreach (j; k + 1..n)
			{
				long steps = hi / a[j] + 1;
				cur += steps;
				hi = steps * a[j];
			}
			res = min (res, cur);
		}
		writeln (res);
	}
}

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

enum INF = 10L^^18;

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

      long ans = INF;
      foreach (i; 0 .. N) {
        long cost;
        {
          long b = 0;
          foreach_reverse (j; 0 .. i) {
            const k = b / A[j] + 1;
            cost += k;
            b = A[j] * k;
          }
        }
        {
          long b = 0;
          foreach (j; i + 1 .. N) {
            const k = b / A[j] + 1;
            cost += k;
            b = A[j] * k;
          }
        }
        debug {
          writeln(i, ": ", cost);
        }
        chmin(ans, cost);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


This is the easy version of the problem. The only difference between easy and hard versions is the constraint of $$$m$$$. You can make hacks only if both versions are solved.Chiori loves dolls and now she is going to decorate her bedroom! As a doll collector, Chiori has got $$$n$$$ dolls. The $$$i$$$-th doll has a non-negative integer value $$$a_i$$$ ($$$a_i < 2^m$$$, $$$m$$$ is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are $$$2^n$$$ different picking ways.Let $$$x$$$ be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls $$$x = 0$$$). The value of this picking way is equal to the number of $$$1$$$-bits in the binary representation of $$$x$$$. More formally, it is also equal to the number of indices $$$0 \leq i < m$$$, such that $$$\left\lfloor \frac{x}{2^i} \right\rfloor$$$ is odd.Tell her the number of picking ways with value $$$i$$$ for each integer $$$i$$$ from $$$0$$$ to $$$m$$$. Due to the answers can be very huge, print them by modulo $$$998\,244\,353$$$.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le m \le 35$$$)  — the number of dolls and the maximum value of the picking way. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i < 2^m$$$)  — the values of dolls.

Print $$$m+1$$$ integers $$$p_0, p_1, \ldots, p_m$$$  — $$$p_i$$$ is equal to the number of picking ways with value $$$i$$$ by modulo $$$998\,244\,353$$$.

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(int M_) {
  import std.conv : to;
  alias M = 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 if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      if (a < 0) return (this = inv()^^(-a));
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e > 0; 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 { 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");
  }
  bool opCast(T: bool)() const { return (x != 0); }
  string toString() const { return x.to!string; }
}

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

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

      long[] bases;
      foreach (i; 0 .. N) {
        long a = A[i];
        foreach (base; bases) {
          chmin(a, a ^ base);
        }
        if (a) {
          bases ~= a;
        }
      }
      bases.sort!"a > b";
      const r = cast(int)(bases.length);
      foreach (j; 0 .. r) {
        foreach (k; j + 1 .. r) {
          chmin(bases[k], bases[k] ^ bases[j]);
        }
      }
      debug {
        foreach (base; bases) {
          writefln("%0*b", M, base);
        }
      }

      long[] ps, qs;
      foreach (j; 0 .. r) {
        if (bases[j] & ~((1L << (M / 2)) - 1)) {
          ps ~= bases[j];
        } else {
          qs ~= bases[j];
        }
      }
      debug {
        writeln("ps = ", ps);
        writeln("qs = ", qs);
      }
      const psLen = cast(int)(ps.length);
      const qsLen = cast(int)(qs.length);
      auto psSum = new long[1 << psLen];
      auto qsSum = new long[1 << qsLen];
      foreach (j; 0 .. psLen) {
        foreach (h; 0 .. 1 << j) {
          psSum[h | 1 << j] = psSum[h] ^ ps[j];
        }
      }
      foreach (j; 0 .. qsLen) {
        foreach (h; 0 .. 1 << j) {
          qsSum[h | 1 << j] = qsSum[h] ^ qs[j];
        }
      }
      auto xss = new Mint[][](M - M / 2 + 1, 1 << (M / 2));
      auto ys = new Mint[1 << (M / 2)];
      foreach (h; 0 .. 1 << psLen) {
        xss[popcnt(psSum[h] >> (M / 2))][cast(int)(psSum[h] & ((1L << (M / 2)) - 1))] += 1;
      }
      foreach (h; 0 .. 1 << qsLen) {
        ys[cast(int)(qsSum[h])] += 1;
      }

      foreach (e; 0 .. M / 2) {
        foreach (f; 0 .. 1 << (M / 2)) {
          if (!(f & 1 << e)) {
            const tmp = ys[f] - ys[f | 1 << e];
            ys[f] += ys[f | 1 << e];
            ys[f | 1 << e] = tmp;
          }
        }
      }
      const invTwo = Mint(2)^^(-(M / 2));
      auto ans = new Mint[M + 1];
      foreach (s; 0 .. M - M / 2 + 1) {
        foreach (e; 0 .. M / 2) {
          foreach (f; 0 .. 1 << (M / 2)) {
            if (!(f & 1 << e)) {
              const tmp = xss[s][f] - xss[s][f | 1 << e];
              xss[s][f] += xss[s][f | 1 << e];
              xss[s][f | 1 << e] = tmp;
            }
          }
        }
        xss[s][] *= ys[];
        xss[s][] *= invTwo;
        foreach (e; 0 .. M / 2) {
          foreach (f; 0 .. 1 << (M / 2)) {
            if (!(f & 1 << e)) {
              const tmp = xss[s][f] - xss[s][f | 1 << e];
              xss[s][f] += xss[s][f | 1 << e];
              xss[s][f | 1 << e] = tmp;
            }
          }
        }
        foreach (f; 0 .. 1 << (M / 2)) {
          ans[s + popcnt(f)] += xss[s][f];
        }
      }

      ans[] *= Mint(2)^^(N - r);
      foreach (s; 0 .. M + 1) {
        if (s > 0) write(" ");
        write(ans[s]);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}


The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!There are $$$n$$$ snacks flavors, numbered with integers $$$1, 2, \ldots, n$$$. Bessie has $$$n$$$ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:  First, Bessie will line up the guests in some way.  Then in this order, guests will approach the snacks one by one.  Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.  Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.

The first line contains integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le 10^5$$$), the number of snacks and the number of guests.  The $$$i$$$-th of the following $$$k$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$, $$$x_i \ne y_i$$$), favorite snack flavors of the $$$i$$$-th guest.

Output one integer, the smallest possible number of sad guests.

NoteIn the first example, Bessie can order the guests like this: $$$3, 1, 2, 4$$$. Guest $$$3$$$ goes first and eats snacks $$$1$$$ and $$$4$$$. Then the guest $$$1$$$ goes and eats the snack $$$2$$$ only, because the snack $$$1$$$ has already been eaten. Similarly, the guest $$$2$$$ goes up and eats the snack $$$3$$$ only. All the snacks are gone, so the guest $$$4$$$ will be sad. In the second example, one optimal ordering is $$$2, 1, 3, 5, 4$$$. All the guests will be satisfied.

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

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

    auto p = new int[] (k+1);
    auto g = new int[][] (n+1);
    foreach (i; 1 .. k+1) {
        int x, y;
        readf("%s %s", &x, &y);
        readln;

        p[i] = x;
        g[x] ~= y;
        g[y] ~= x;
    }

    int ok = 0;
    auto vis = new bool[] (n+1);
    vis[] = false;
    foreach (i; 1 .. k+1) {
        if (vis[p[i]]) { continue; }

        int dfs(int v) {
            vis[v] = true;
            int subn = 0;
            foreach (u; g[v]) {
                if (vis[u]) { continue; }
                subn += dfs(u);
            }

            return 1 + subn;
        }

        ok += dfs(p[i]) - 1;
    }

    int ans = k - ok;

    ans.writeln;
}

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

	Node[] nodes;
	foreach(i; 0 .. n) nodes ~= new Node(i, 0);

	Edge[] edges;
	foreach(j; 0 .. k){
		int x = rint - 1, y = rint - 1;
		edges ~= new Edge(nodes[x], nodes[y], 0);
		nodes[x].value += 1, nodes[y].value += 1;
	}

	foreach(ed; edges){
		if(ed.node1.group.id != ed.node2.group.id){
			ed.node1.group.eat(ed.node2.group);
		}
	}

	long ans;
	foreach(nd; nodes){
		if(nd.group.id != nd.id) continue;
		long edcnt;
		foreach(nx; nd.group.nodes) edcnt += nx.value;
		ans += edcnt / 2 - (nd.group.nodes.length - 1);
	}

	ans.writeln;

}

// Union-find ※多分使いやすいやつ
class Edge{
	Node node1;
	Node node2;
	long value;
	this(Node node1, Node node2, long value){
		this.node1 = node1, this.node2 = node2;
		this.value = value;
	}
	override string toString(){
		return [node1.id, node2.id, value].to!string;
	}
}

class Node{
	long id;
	long value;
	Group group;
	this(long id, long value){
		this.id = id;
		this.value = value;
		this.group = new Group(this);
	}
	override string toString(){
		return [id, group.id, value].to!string;
	}
}

class Group{
	long value;
	long pendingEdgeCount;
	long id;
	long edgecount;
	Node[] nodes;
	this(Node node){
		this.id = node.id;
		this.nodes = [node];
		this.value = node.value;
	}
	void eat(Group gp){
		if(gp.nodes.length > this.nodes.length) gp.eat(this);
		else{
			foreach(nd; gp.nodes){
				this.nodes ~= nd;
				nd.group = this;
				this.value += nd.value;
			}
			this.edgecount += gp.edgecount;
			this.pendingEdgeCount += gp.pendingEdgeCount;
		}
	}
}

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

struct UnionFind
{
	void init(int n) { par = new int[](n); foreach (i; 0..n) par[i] = i; cnt = new int[](n); fill(cnt, 1); }
	int root(int i) { return par[i] == i ? i : (par[i] = root(par[i])); }
	bool same(int i, int j) { return root(i) == root(j); }
	void unite(int i, int j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; }
	int size(int i) { return cnt[root(i)]; }
	int[] par, cnt;
}

void main()
{
	auto n = RD!int;
	auto k = RD!int;
	//auto edges = new int[][](n);
	UnionFind uf;
	uf.init(n);
	foreach (i; 0..k)
	{
		auto a = RD!int-1;
		auto b = RD!int-1;
		//edges[a] ~= b;
		//edges[b] ~= a;
		uf.unite(a, b);
	}

	auto cnt = new int[](n);
	foreach (i; 0..n)
	{
		cnt[i] = uf.size(i);
	}

	long ans;
	auto index = cnt.MAKE_IDX!("a > b")();
	auto used = new bool[](n);
	foreach (i; index)
	{
		auto r = uf.root(cast(int)i);
		if (used[r]) continue;
		ans += cnt[i]-1;
		used[r] = true;
	}

	writeln(max(k-ans, 0));
	stdout.flush();
	debug readln();
}