For a collection of integers $$$S$$$, define $$$\operatorname{mex}(S)$$$ as the smallest non-negative integer that does not appear in $$$S$$$.NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.The universe can be represented as a 1-indexed array $$$a$$$ of length $$$n$$$. When NIT snaps his fingers, he does the following operation on the array:  He selects positive integers $$$l$$$ and $$$r$$$ such that $$$1\le l\le r\le n$$$. Let $$$w=\operatorname{mex}(\{a_l,a_{l+1},\dots,a_r\})$$$. Then, for all $$$l\le i\le r$$$, set $$$a_i$$$ to $$$w$$$. We say the universe is destroyed if and only if for all $$$1\le i\le n$$$, $$$a_i=0$$$ holds.Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to $$$0$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1\le n\le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_n$$$ ($$$0\le a_i\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

For each test case, print one integer — the answer to the problem.

NoteIn the first test case, we do $$$0$$$ operations and all elements in the array are already equal to $$$0$$$.In the second test case, one optimal way is doing the operation with $$$l=2$$$, $$$r=5$$$.In the third test case, one optimal way is doing the operation twice, respectively with $$$l=4$$$, $$$r=4$$$ and $$$l=2$$$, $$$r=6$$$.In the fourth test case, one optimal way is doing the operation with $$$l=1$$$, $$$r=1$$$.

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

void solve()
{
    readln;
    auto ans = readln.splitter
        .map!(to!int)
        .chunkBy!(x => x != 0)
        .count!(x => x[0] != 0);

    .writeln(min(2, ans));
}

void main()
{
    size_t TC = 1;
    TC = readln.strip.to!int;
    foreach (_; 0 .. TC)
        solve();
}

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 numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        auto A = new long[N];
        foreach (i; 0 .. N) {
          A[i] = readLong;
        }

        long ans;
        for (int i = 0, j; i < N; i = j) {
          for (j = i; j < N && (A[i] != 0) == (A[j] != 0); ++j) {}
          if (A[i] != 0) {
            ++ans;
          }
        }
        chmin(ans, 2);
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

import std;

void main(){
	int t=readln().strip.to!int;
	foreach(_;0..t){
		readln();
		auto a=readln.strip.split.to!(int[]);
		writeln(min(2,a.map!(x=>x==0).group.filter!(x=>!x[0]).walkLength));
	}
}

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

void solve()
{
    readln;
    readln.splitter
        .map!(to!int)
        .chunkBy!(x => x != 0)
        .count!(x=> x[0] != 0)
        .writeln();
}

void main()
{
    size_t TC = 1;
    TC = readln.strip.to!int;
    foreach (_; 0 .. TC)
        solve();
}

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 numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        auto A = new long[N];
        foreach (i; 0 .. N) {
          A[i] = readLong;
        }

        long ans;
        for (int i = 0, j; i < N; i = j) {
          for (j = i; j < N && (A[i] != 0) == (A[j] != 0); ++j) {}
          if (A[i] != 0) {
            ++ans;
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}


You have a given picture with size $$$w \times h$$$. Determine if the given picture has a single "+" shape or not. A "+" shape is described below:  A "+" shape has one center nonempty cell.  There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In other words, there should be a ray in each direction.  All other cells are empty. Find out if the given picture has single "+" shape.

The first line contains two integers $$$h$$$ and $$$w$$$ ($$$1 \le h$$$, $$$w \le 500$$$) — the height and width of the picture. The $$$i$$$-th of the next $$$h$$$ lines contains string $$$s_{i}$$$ of length $$$w$$$ consisting "." and "*" where "." denotes the empty space and "*" denotes the non-empty space.

If the given picture satisfies all conditions, print "YES". Otherwise, print "NO". You can output each letter in any case (upper or lower).

NoteIn the first example, the given picture contains one "+".In the second example, two vertical branches are located in a different column.In the third example, there is a dot outside of the shape.In the fourth example, the width of the two vertical branches is $$$2$$$.In the fifth example, there are two shapes.In the sixth example, there is an empty space inside of the shape.

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

void main() {
  auto s = readln.strip.split.map!(to!int).array;
  immutable h = s[0], w = s[1];
  auto a = new string[h];
  foreach (i; 0 .. h) {
    a[i] = readln.strip;
  }
  int c;
  foreach (i; 0 .. h) {
    foreach (j; 0 .. w) {
      if (a[i][j] == '*') ++c;
    }
  }
  bool test() {
    foreach (i; 0 .. h) {
      foreach (j; 0 .. w) {
        if (a[i][j] == '*') {
          int go (int di, int dj) {
            int x = i + di;
            int y = j + dj;
            int r;
            while (x >= 0 && x < h && y >= 0 && y < w && a[x][y] == '*') {
              ++r;
              x += di;
              y += dj;
            }
            return r;
          }
          auto e = new int[4];
          e[0] = go (0, 1);
          e[1] = go (0, -1);
          e[2] = go (1, 0);
          e[3] = go (-1, 0);
          if (e.minElement () > 0 && e.sum == c - 1) {
            return true;
          }
        }
      }
    }
    return false;
  }
  writeln (test() ? "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 read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int h = read.to!int;
	int w = read.to!int;

	bool[][] as;
	foreach(i; 0 .. h){
		as ~= readln.chomp.to!(char[]).map!(x => x == '*').array;
	}

	string ans = "YES";

	bool f = 0;
	int i0, j0;
	foreach(i; 1 .. h - 1) foreach(j; 1 .. w - 1){
		if(as[i][j] && as[i - 1][j] && as[i + 1][j] && as[i][j - 1] && as[i][j + 1]){
			if(f == 0) i0 = i, j0 = j, f = 1;
			else ans = "NO";
		}
	}
	if(f == 0) ans = "NO";

	if(ans == "NO"){
		ans.writeln;
		return;
	}

	int t, b, l, r;
	foreach_reverse(i; 0 .. i0) if(!as[i][j0]) break; else t = i;
	foreach(i; i0 + 1 .. h) if(!as[i][j0]) break; else b = i;
	foreach_reverse(j; 0 .. j0) if(!as[i0][j]) break; else l = j;
	foreach(j; j0 + 1 .. w) if(!as[i0][j]) break; else r = j;

	foreach(i; 0 .. h) foreach(j; 0 .. w){
		if(i != i0 && j != j0 && as[i][j]) ans = "NO";
		if(i == i0 && (j < l || j > r) && as[i][j]) ans = "NO";
		if(j == j0 && (i < t || i > b) && as[i][j]) ans = "NO";
	}

	ans.writeln;

}

/*
	bool ng = 0;
	int[] ls, rs, ts, bs;
	foreach(i; 0 .. h){
		int f = 0;
		int l = -1, r = -1;
		foreach(j; 0 .. w){
			if(as[i][j]){
				if(f == 0) f = 1, l = j;
				else if(f == 2) ng = 1;
			}
			else{
				if(f == 1) f = 2, r = j;
			}
		}
		ls ~= l, rs ~= r;
	}
	foreach(j; 0 .. w){
		int f = 0;
		int t = -1, b = -1;
		foreach(i; 0 .. h){
			if(as[i][j]){
				if(f == 0) f = 1, t = i;
				else if(f == 2) ng = 1;
			}
			else{
				if(f == 1) f = 2, b = i;
			}
		}
		ts ~= t, bs ~= b;
	}

*/

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string ;
import core.memory : GC;
import std.typecons : tuple;
import std.math;

alias Tree = RBT!int;

void main() {
  GC.disable();

  //

  int h, w;
  readf(" %s %s\n", h, w);

  auto m = new string[h];

  int tt = 0;
  foreach(i; 0..h) {
    m[i] = readln.strip;
    foreach(e; m[i]) if (e=='*') tt++;
  }

  foreach(i; 1..h-1) {
    foreach(j; 1..w-1) {
    if (m[i][j] == '*'  && m[i-1][j] == '*'
        && m[i+1][j] == '*' && m[i][j+1] == '*'
        && m[i][j-1] == '*') {
      int k = j+1;
      while(k < w && m[i][k] == '*') { tt--; k++;}
      k = j-1;
      while(k >= 0 && m[i][k] == '*') { tt--; k--;}
      k = i-1;
      while(k >= 0 && m[k][j] == '*') { tt--; k--;}
      k = i+1;
      while(k < h && m[k][j] == '*') { tt--; k++;}

      tt--;

      if (tt == 0) {
        writeln("YES");
        return;
      } else {
        writeln("NO");
        return;
      }

    }
  }
  }

  writeln("NO");

}

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

string FMT_F = "%.10f";

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

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

long mod = 10^^9 + 7;
//long mod = 998244353;
//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 h = RD!int;
	auto w = RD!int;
	auto s = new string[](h);
	foreach (i; 0..h)
	{
		s[i] = RD!string;
	}

	auto cnt_w = new long[](h);
	foreach (y; 0..h)
	{
		bool isStart;
		char last = '.';
		foreach (x; 0..w)
		{
			if (s[y][x] == '*')
			{
				if (last == '.')
				{
					if (!isStart)
					{
						isStart = true;
						cnt_w[y] = 1;
					}
					else
					{
						cnt_w[y] = -1;
						break;
					}
				}
				else
				{
					++cnt_w[y];
				}
			}
			last = s[y][x];
		}
	}

	auto cnt_h = new long[](w);
	foreach (x; 0..w)
	{
		bool isStart;
		char last = '.';
		foreach (y; 0..h)
		{
			if (s[y][x] == '*')
			{
				if (last == '.')
				{
					if (!isStart)
					{
						isStart = true;
						cnt_h[x] = 1;
					}
					else
					{
						cnt_h[x] = -1;
						break;
					}
				}
				else
				{
					++cnt_h[x];
				}
			}
			last = s[y][x];
		}
	}

	bool ans = true;
	auto index_w = cnt_w.MAKE_IDX!"a > b"();
	auto index_h = cnt_h.MAKE_IDX!"a > b"();
	auto pos_y   = index_w[0];
	auto pos_x   = index_h[0];
	if (cnt_w[pos_y] < 3)
		ans = false;
	foreach (y; index_w[1..$])
	{
		if (cnt_w[y] == 0) break;
		if (cnt_w[y] > 1)
			ans = false;
		if (s[y][pos_x] == '.')
			ans = false;
	}

	if (cnt_h[pos_x] < 3)
		ans = false;
	foreach (x; index_h[1..$])
	{
		if (cnt_h[x] == 0) break;
		if (cnt_h[x] > 1)
			ans = false;
		if (s[pos_y][x] == '.')
			ans = false;
	}

	if (ans)
	{
		if (pos_y == 0 || pos_y == h - 1)
			ans = false;
		if (pos_x == 0 || pos_x == w - 1)
			ans = false;

		if (ans)
		{
			if (s[pos_y][pos_x-1] != '*')
				ans = false;
			if (s[pos_y][pos_x+1] != '*')
				ans = false;
			if (s[pos_y-1][pos_x] != '*')
				ans = false;
			if (s[pos_y+1][pos_x] != '*')
				ans = false;
		}
	}

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

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

void main() {
  auto s = readln.strip.split.map!(to!int).array;
  immutable h = s[0], w = s[1];
  auto a = new string[h];
  foreach (i; 0 .. h) {
    a[i] = readln.strip;
  }
  int c;
  foreach (i; 0 .. h) {
    foreach (j; 0 .. w) {
      if (a[i][j] == '*') ++c;
    }
  }
  bool test() {
    foreach (i; 0 .. h) {
      foreach (j; 0 .. w) {
        if (a[i][j] == '*') {
          int go (int di, int dj) {
            int x = i + di;
            int y = j + dj;
            int r;
            while (x >= 0 && x < h && y >= 0 && y < h && a[x][y] == '*') {
              ++r;
              x += di;
              y += dj;
            }
            return r;
          }
          auto e = new int[4];
          e[0] = go (0, 1);
          e[1] = go (0, -1);
          e[2] = go (1, 0);
          e[3] = go (-1, 0);
          if (e.minElement () > 0 && e.sum == c - 1) {
            return true;
          }
        }
      }
    }
    return false;
  }
  writeln (test() ? "YES" : "NO");
}


Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any $$$i$$$ ($$$1 \le i \le n-1$$$), this condition must be satisfied: $$$$$$\frac{\max(a[i], a[i+1])}{\min(a[i], a[i+1])} \le 2$$$$$$For example, the arrays $$$[1, 2, 3, 4, 3]$$$, $$$[1, 1, 1]$$$ and $$$[5, 10]$$$ are dense. And the arrays $$$[5, 11]$$$, $$$[1, 4, 2]$$$, $$$[6, 6, 1]$$$ are not dense.You are given an array $$$a$$$ of $$$n$$$ integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added.For example, if $$$a=[4,2,10,1]$$$, then the answer is $$$5$$$, and the array itself after inserting elements into it may look like this: $$$a=[4,2,\underline{\textbf{3}},\underline{\textbf{5}},10,\underline{\textbf{6}},\underline{\textbf{4}},\underline{\textbf{2}},1]$$$ (there are other ways to build such $$$a$$$).

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 50$$$) — the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 50$$$).

For each test case, output one integer — the minimum number of numbers that must be added to the array to make it dense.

NoteThe first test case is explained in the statements.In the second test case, you can insert one element, $$$a=[1,\underline{\textbf{2}},3]$$$.In the third test case, you can insert two elements, $$$a=[6,\underline{\textbf{4}},\underline{\textbf{2}},1]$$$.In the fourth test case, you can insert one element, $$$a=[1,\underline{\textbf{2}},4,2]$$$.In the fifth test case, the array $$$a$$$ is already dense.

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;
		int res = 0;
		foreach (i; 0..n - 1)
		{
			auto b = a[i..i + 2].dup;
			sort (b);
			while (b[0] * 2 < b[1])
			{
				b[0] *= 2;
				res += 1;
			}
		}
		writeln (res);
	}
}

import std.algorithm;
import std.conv;
import std.math;
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 !(real)).array;
		zip (a, a.drop (1)).map !(c => max (0,
		    to !(int) (abs (log2 (c[0] / c[1])) - 1E-9))).sum.writeln;
	}
}

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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T); while (T--) {
        int N; get(N);
        int[] AA; get(AA);
        int c;
        foreach (i; 0..N-1) {
            auto a = AA[i];
            auto b = AA[i+1];
            if (a > b) swap(a, b);
            while (a * 2 < b) {
                ++c;
                a *= 2;
            }
        }
        writeln(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)); }

//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 int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		foreach (i; 0..n-1)
		{
			auto x = min(a[i], a[i+1]) * 2;
			auto y = max(a[i], a[i+1]);
			while (x < y)
			{
				++ans[ti];
				x *= 2;
			}
		}
	}

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


Alice and Bob received $$$n$$$ candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice's candies is equal to the total weight of Bob's candies.Check if they can do that.Note that candies are not allowed to be cut in half.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of candies that Alice and Bob received. The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the weights of the candies. The weight of each candy is either $$$1$$$ or $$$2$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output on a separate line:    "YES", if all candies can be divided into two sets with the same weight;  "NO" otherwise.  You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

NoteIn the first test case, Alice and Bob can each take one candy, then both will have a total weight of $$$1$$$.In the second test case, any division will be unfair.In the third test case, both Alice and Bob can take two candies, one of weight $$$1$$$ and one of weight $$$2$$$.In the fourth test case, it is impossible to divide three identical candies between two people.In the fifth test case, any division will also be unfair.

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(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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		long x, y;
		foreach (i; 0..n)
		{
			if (a[i] == 1)
				++x;
			else
				++y;
		}

		if (x % 2) continue;
		if (n % 2)
		{
			if (x < 2) continue;
		}
		ans[ti] = true;
	}

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

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;
		auto v = (a.sum % 2 == 0 && (a.sum % 4 == 0 || a.canFind (1)));
		writeln (v ? "Yes" : "No");
	}
}


Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.For each of the m ghosts Anya knows the time at which it comes: the i-th visit will happen wi seconds after midnight, all wi's are distinct. Each visit lasts exactly one second.What is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time.

The first line contains three integers m, t, r (1 ≤ m, t, r ≤ 300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.  The next line contains m space-separated numbers wi (1 ≤ i ≤ m, 1 ≤ wi ≤ 300), the i-th of them repesents at what second after the midnight the i-th ghost will come. All wi's are distinct, they follow in the strictly increasing order.

If it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that. If that is impossible, print  - 1.

NoteAnya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight.In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.In the third sample test the answer is  - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes.

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

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

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

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

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

int M, T, R;
int[] W;

int solve() {
	bool[] used = new bool[W[$ - 1] + T + 1];
	int[] cnt = new int[M];
	foreach (i; 0 .. M) {
		//	[x - T, x)
		for (int x = W[i] + T; cnt[i] < R && x >= W[i] + 1; --x) {
			if (!used[x]) {
				used[x] = true;
				foreach (j; 0 .. M) {
					if (x - T <= W[j] && W[j] < x) {
						++cnt[j];
					}
				}
			}
		}
		if (cnt[i] < R) {
			return -1;
		}
	}
	return used.count(true);
}

void main(string[] args) {
	try {
	for (; ; ) {
		M = readInt;
		T = readInt;
		R = readInt;
		W = new int[M];
		foreach (i; 0 .. M) {
			W[i] = readInt;
		}

		const res = solve;
		writeln(res);

	}
	} catch (EOFException) {}
}


Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive.Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set.Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.

The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets. Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.

If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n). If there are multiple answers, print any of them.

NoteConsider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one).Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].

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

void main() {
    auto N = readln.chomp.to!int;
    auto LR = N.iota.map!(_ => readln.split.map!(to!int).array).array;
    int[] tmp;
    foreach (lr; LR) tmp ~= lr;
    tmp = tmp.sort().uniq().array;

    int[int] comp;
    int cnt = 0;
    foreach (i; 0..tmp.length.to!int) {
        if (i > 0 && tmp[i] - tmp[i - 1] > 1)
            cnt += 1;
        comp[tmp[i]] = cnt;
        cnt += 1;
    }

    auto acm = new int[](comp.values.reduce!max + 2);
    foreach (lr; LR) {
        int l = comp[lr[0]];
        int r = comp[lr[1]];
        acm[l] += 1;
        acm[r+1] -= 1;
    }

    foreach (i; 0..acm.length.to!int - 1) acm[i+1] += acm[i];

    auto st = new SegmentTree(acm.length.to!int);
    foreach (i; 0..acm.length) st.assign(i.to!int, acm[i]);

    foreach (i; 0..N) {
        int l = comp[LR[i][0]];
        int r = comp[LR[i][1]];
        if (st.query(l, r) >= 2) {
            writeln(i + 1);
            return;
        }
    }

    writeln(-1);
    return;
}

class SegmentTree {
    int[] table;
    int size;

    this(int n) {
        assert(bsr(n) < 29);
        size = 1 << (bsr(n) + 2);
        table = new int[](size);
        fill(table, 1 << 29);
    }

    void assign(int pos, int num) {
        return assign(pos, num, 0, 0, size/2-1);
    }

    void assign(int pos, int num, int i, int left, int right) {
        if (left == right) {
            table[i] = num;
            return;
        }
        auto mid = (left + right) / 2;
        if (pos <= mid)
            assign(pos, num, i*2+1, left, mid);
        else
            assign(pos, num, i*2+2, mid+1, right);
        table[i] = min(table[i*2+1], table[i*2+2]);
    }

    int query(int pl, int pr) {
        return query(pl, pr, 0, 0, size/2-1);
    }

    int query(int pl, int pr, int i, int left, int right) {
        if (pl > right || pr < left)
            return 1 << 29;
        else if (pl <= left && right <= pr)
            return table[i];
        else
            return
                min(query(pl, pr, i*2+1, left, (left+right)/2),
                    query(pl, pr, i*2+2, (left+right)/2+1, right));
    }
}

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

void main() {
    auto N = readln.chomp.to!int;
    auto LR = N.iota.map!(_ => readln.split.map!(to!int).array).array;
    int[] tmp;
    foreach (lr; LR) tmp ~= lr;
    tmp = tmp.sort().uniq().array;

    int[int] comp;
    foreach (i; 0..tmp.length) comp[tmp[i]] = i.to!int;

    auto acm = new int[](tmp.length + 1);
    foreach (lr; LR) {
        int l = comp[lr[0]];
        int r = comp[lr[1]];
        acm[l] += 1;
        acm[r+1] -= 1;
    }

    foreach (i; 0..acm.length.to!int - 1) acm[i+1] += acm[i];

    auto st = new SegmentTree(acm.length.to!int);
    foreach (i; 0..acm.length) st.assign(i.to!int, acm[i]);

    foreach (i; 0..N) {
        int l = comp[LR[i][0]];
        int r = comp[LR[i][1]];
        if (st.query(l, r) >= 2) {
            writeln(i + 1);
            return;
        }
    }

    writeln(-1);
    return;
}

class SegmentTree {
    int[] table;
    int size;

    this(int n) {
        assert(bsr(n) < 29);
        size = 1 << (bsr(n) + 2);
        table = new int[](size);
        fill(table, 1 << 29);
    }

    void assign(int pos, int num) {
        return assign(pos, num, 0, 0, size/2-1);
    }

    void assign(int pos, int num, int i, int left, int right) {
        if (left == right) {
            table[i] = num;
            return;
        }
        auto mid = (left + right) / 2;
        if (pos <= mid)
            assign(pos, num, i*2+1, left, mid);
        else
            assign(pos, num, i*2+2, mid+1, right);
        table[i] = min(table[i*2+1], table[i*2+2]);
    }

    int query(int pl, int pr) {
        return query(pl, pr, 0, 0, size/2-1);
    }

    int query(int pl, int pr, int i, int left, int right) {
        if (pl > right || pr < left)
            return 1 << 29;
        else if (pl <= left && right <= pr)
            return table[i];
        else
            return
                min(query(pl, pr, i*2+1, left, (left+right)/2),
                    query(pl, pr, i*2+2, (left+right)/2+1, right));
    }
}


For months Maxim has been coming to work on his favorite bicycle. And quite recently he decided that he is ready to take part in a cyclists' competitions.He knows that this year n competitions will take place. During the i-th competition the participant must as quickly as possible complete a ride along a straight line from point si to point fi (si < fi).Measuring time is a complex process related to usage of a special sensor and a time counter. Think of the front wheel of a bicycle as a circle of radius r. Let's neglect the thickness of a tire, the size of the sensor, and all physical effects. The sensor is placed on the rim of the wheel, that is, on some fixed point on a circle of radius r. After that the counter moves just like the chosen point of the circle, i.e. moves forward and rotates around the center of the circle.At the beginning each participant can choose any point bi, such that his bike is fully behind the starting line, that is, bi < si - r. After that, he starts the movement, instantly accelerates to his maximum speed and at time tsi, when the coordinate of the sensor is equal to the coordinate of the start, the time counter starts. The cyclist makes a complete ride, moving with his maximum speed and at the moment the sensor's coordinate is equal to the coordinate of the finish (moment of time tfi), the time counter deactivates and records the final time. Thus, the counter records that the participant made a complete ride in time tfi - tsi.  Maxim is good at math and he suspects that the total result doesn't only depend on his maximum speed v, but also on his choice of the initial point bi. Now Maxim is asking you to calculate for each of n competitions the minimum possible time that can be measured by the time counter. The radius of the wheel of his bike is equal to r.

The first line contains three integers n, r and v (1 ≤ n ≤ 100 000, 1 ≤ r, v ≤ 109) — the number of competitions, the radius of the front wheel of Max's bike and his maximum speed, respectively.  Next n lines contain the descriptions of the contests. The i-th line contains two integers si and fi (1 ≤ si < fi ≤ 109) — the coordinate of the start and the coordinate of the finish on the i-th competition.

Print n real numbers, the i-th number should be equal to the minimum possible time measured by the time counter. Your answer will be considered correct if its absolute or relative error will not exceed 10 - 6.  Namely: let's assume that your answer equals a, and the answer of the jury is b. The checker program will consider your answer correct if .

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;
	real r, v;
	while (readf (" %s %s %s", &n, &r, &v) > 0)
	{
		foreach (q; 0..n)
		{
			real s, f;
			readf (" %s %s", &s, &f);
			real dist = f - s;
			real len = 2.0 * PI * r;
			real periods = floor (dist / len);
			real part = dist - periods * len;
			real half = part * 0.5 / r;

			// x = a (t - sin t)
			real lo = 0.0;
			real hi = PI;
			real x0 = PI - sin (PI);
			foreach (step; 0..100)
			{
				real me = (lo + hi) * 0.5;
				real x = x0 - (me - sin (me));
				if (x > half)
				{
					lo = me;
				}
				else
				{
					hi = me;
				}
			}
			real t = PI - lo;

			real res = periods * len / v + t * r / v * 2.0;
			writefln ("%.10f", res);
		}
	}
}


Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.

The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109). Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109). It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.

Output a single integer representing the maximum number of the train routes which can be closed.

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

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

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

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

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

int N, M, K;
int[] A, B;
long[] C;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		M = readInt;
		K = readInt;
		A = new int[M + K];
		B = new int[M + K];
		C = new long[M + K];
		foreach (i; 0 .. M + K) {
			A[i] = (i < M) ? (readInt - 1) : 0;
			B[i] = readInt - 1;
			C[i] = readLong;
		}

		auto G = new Pair!(long, int)[][N];
		foreach (i; 0 .. M + K) {
			G[A[i]] ~= pair(C[i], B[i]);
			G[B[i]] ~= pair(C[i], A[i]);
		}

		auto q = heapify!"a.x > b.x"(Array!(Pair!(long, int))());
		long[] d = new long[N];
		d[] = long.max;
		d[0] = 0;
		q.insert(Pair!(long, int)(0, 0));
		for (; !q.empty; ) {
			const c = q.front.x;
			const u = q.front.y;
			q.removeFront;
			if (d[u] == c) {
				foreach (edge; G[u]) {
					const cc = c + edge.x;
					const v = edge.y;
					if (d[v] > cc) {
						d[v] = cc;
						q.insert(Pair!(long, int)(cc, v));
					}
				}
			}
		}
// writeln("d = ",d);

		bool[] has = new bool[N];
		foreach (i; 0 .. M) {
			if (d[B[i]] == d[A[i]] + C[i]) {
				has[B[i]] = true;
			}
			if (d[A[i]] == d[B[i]] + C[i]) {
				has[A[i]] = true;
			}
		}

		int ans;
		foreach (i; M .. M + K) {
			if (d[B[i]] == d[A[i]] + C[i]) {
				if (has[B[i]]) {
					++ans;
				} else {
					has[B[i]] = true;
				}
			} else {
				++ans;
			}
		}
		writeln(ans);
	}
	} catch (EOFException) {}
}


One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: "Find such positive integer n, that among numbers n + 1, n + 2, ..., 2·n there are exactly m numbers which binary representation contains exactly k digits one".The girl got interested in the task and she asked you to help her solve it. Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 1018.

The first line contains two space-separated integers, m and k (0 ≤ m ≤ 1018; 1 ≤ k ≤ 64).

Print the required number n (1 ≤ n ≤ 1018). If there are multiple answers, print any of them.

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

long c[65][65];

void init()
{
    foreach (i; 0 .. 65)
    {
        c[i][0] = 1;
    }
    foreach (i; 1 .. 65)
    {
        foreach (j; 1 .. i + 1)
        {
            c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
        }
    }
}

int[] toBinary(long n)
{
    int[] ret, tmp;
    while (n)
    {
        tmp ~= (n % 2);
        n >>= 1;
    }
    for (int i = tmp.length - 1; i >= 0; -- i)
    {
        ret ~= tmp[i];
    }
    return ret;
}

long count(long n, int k)
{
    long res = 0;
    int cnt = 1;
    auto b = toBinary(n);
    foreach (i; k .. b.length)
    {
        res += c[i - 1][k - 1];
    }
    for (int i = 1; i < b.length && cnt <= k; ++ i)
    {
        if (b[i] == 1)
        {
            res += c[b.length - i - 1][k - cnt];
            ++ cnt;
        }
    }
    if (cnt == k)
    {
        ++ res;
    }
    return res;
}

void solve(long m, int k)
{
    long left = 1, right = 1000000000000000000, ans = 0;
    while (left <= right)
    {
        long mid = (left + right) >> 1;
        long cnt = count(mid << 1, k) - count(mid, k);
        if (cnt == m)
        {
            ans = mid;
            break;
        }
        else if (cnt > m)
        {
            right = mid - 1;
        }
        else
        {
            left = mid + 1;
        }
    }
    writefln("%d", ans);
}

void main(string[] args)
{
    init();
    long m;
    int k;
    while (scanf("%lld%d", &m, &k) == 2)
    {
        solve(m, k);
    }
}

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

long c[65][65];

void init()
{
    foreach (i; 0 .. 65)
    {
        c[i][0] = 1;
    }
    foreach (i; 1 .. 65)
    {
        foreach (j; 1 .. i + 1)
        {
            c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
        }
    }
}

int[] toBinary(long n)
{
    int[] ret, tmp;
    while (n)
    {
        tmp ~= (n % 2);
        n >>= 1;
    }
    for (int i = tmp.length - 1; i >= 0; -- i)
    {
        ret ~= tmp[i];
    }
    return ret;
}

long count(long n, int k)
{
    long res = 0;
    int cnt = 0;
    auto b = toBinary(n);
    for (int i = 0; i < b.length && cnt <= k; ++ i)
    {
        if (b[i] == 1)
        {
            res += c[b.length - i - 1][k - cnt];
            ++ cnt;
        }
    }
    if (cnt == k)
    {
        ++ res;
    }
    return res;
}

void solve(long m, int k)
{
    long left = 1, right = 1000000000000000000, ans = 0;
    while (left <= right)
    {
        long mid = (left + right) >> 1;
        long cnt = count(mid << 1, k) - count(mid, k);
        if (cnt == m)
        {
            ans = mid;
            break;
        }
        else if (cnt > m)
        {
            right = mid - 1;
        }
        else
        {
            left = mid + 1;
        }
    }
    writefln("%d", ans);
}

void main(string[] args)
{
    init();
    long m;
    int k;
    while (scanf("%d%d", &m, &k) == 2)
    {
        solve(m, k);
    }
}

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

long c[65][65];

void init()
{
    foreach (i; 0 .. 65)
    {
        c[i][0] = 1;
    }
    foreach (i; 1 .. 65)
    {
        foreach (j; 1 .. i + 1)
        {
            c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
        }
    }
}

int[] toBinary(long n)
{
    int[] ret, tmp;
    while (n)
    {
        tmp ~= (n % 2);
        n >>= 1;
    }
    for (int i = tmp.length - 1; i >= 0; -- i)
    {
        ret ~= tmp[i];
    }
    return ret;
}

long count(long n, int k)
{
    long res = 0;
    int cnt = 1;
    auto b = toBinary(n);
    foreach (i; k .. b.length)
    {
        res += c[i - 1][k - 1];
    }
    for (int i = 1; i < b.length && cnt <= k; ++ i)
    {
        if (b[i] == 1)
        {
            res += c[b.length - i - 1][k - cnt];
            ++ cnt;
        }
    }
    if (cnt == k)
    {
        ++ res;
    }
    return res;
}

void solve(long m, int k)
{
    long left = 1, right = 1000000000000000000, ans = 0;
    while (left <= right)
    {
        long mid = (left + right) >> 1;
        long cnt = count(mid << 1, k) - count(mid, k);
        if (cnt == m)
        {
            ans = mid;
            break;
        }
        else if (cnt > m)
        {
            right = mid - 1;
        }
        else
        {
            left = mid + 1;
        }
    }
    writefln("%d", ans);
}

void main(string[] args)
{
    init();
    long m;
    int k;
    while (scanf("%d%d", &m, &k) == 2)
    {
        solve(m, k);
    }
}


You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.

Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.

NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: "cbcabba", "ccbbaaa" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$).

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

void main() {
    auto Q = readln.chomp.to!int;

    while (Q--) {
        auto s = readln.split.map!(to!int);
        auto N = s[0];
        auto K = s[1];
        string ans = "";
        for (int i = 0, j = 0; j < N; i = (i + 1) % K, ++j) {
            ans ~= ('a' + i);
        }
        ans.writeln;
    }
}