Yurii is sure he can do everything. Can he solve this task, though?He has an array $$$a$$$ consisting of $$$n$$$ positive integers. Let's call a subarray $$$a[l...r]$$$ good if the following conditions are simultaneously satisfied:   $$$l+1 \leq r-1$$$, i. e. the subarray has length at least $$$3$$$;  $$$(a_l \oplus a_r) = (a_{l+1}+a_{l+2}+\ldots+a_{r-2}+a_{r-1})$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. In other words, a subarray is good if the bitwise XOR of the two border elements is equal to the sum of the rest of the elements. Yurii wants to calculate the total number of good subarrays. What is it equal to?An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

The first line contains a single integer $$$n$$$ ($$$3 \leq n \leq 2\cdot 10^5$$$) — the length of $$$a$$$.  The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \lt 2^{30}$$$) — elements of $$$a$$$.

Output a single integer — the number of good subarrays.

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;

		auto s = [0L];
		foreach (ref c; a)
		{
			s ~= s.back + c;
		}

		long r (int lo, int hi)
		{
			return s[hi] - s[lo];
		}

		long res = 0;
		foreach (i; 2..n)
		{
			int pos = i - 1;
			long cur = a[pos];
			pos -= 1;
			while (pos >= 0 && cur < a[i])
			{
				if ((a[pos] ^ a[i]) == cur)
				{
					res += 1;
				}
				cur += a[pos];
				pos -= 1;
			}
			while (pos >= 0)
			{
				long ask = cur - a[i];
				int lo = 0;
				int hi = pos;
				debug {writeln (ask, " ", lo, " ", hi);}
				while (lo < hi)
				{
					int me = (lo + hi + 1) / 2;
					if (s[pos + 1] - s[me] < ask)
					{
						hi = me - 1;
					}
					else
					{
						lo = me;
					}
				}
				cur += s[pos + 1] - s[lo + 1];
				pos = lo;
				if ((a[pos] ^ a[i]) == cur)
				{
					res += 1;
				}
				cur += a[pos];
				pos -= 1;
			}
		}

		writeln (res);
	}
}


A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex.The gardener Vitaly grew a tree from $$$n$$$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree.  Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree.  The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation:  applying an operation to an empty tree (of $$$0$$$ vertices) does not change it;  applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf);  applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $$$k$$$ operations sequentially to the tree. How many vertices remain?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 4 \cdot 10^5$$$, $$$1 \le k \le 2 \cdot 10^5$$$) — the number of vertices in the tree and the number of operations, respectively. Then $$$n - 1$$$ lines follow, each of them contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \neq v$$$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $$$n$$$ from all test cases does not exceed $$$4 \cdot 10^5$$$.

For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $$$k$$$ operations.

NoteThe first test case is considered in the statement.The second test case contains a tree of two vertices. $$$200000$$$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree.In the third test case, a tree of three vertices is given. As a result of the first operation, only $$$1$$$ vertex remains in it (with the index $$$2$$$), the second operation makes the tree empty.

// Not my code! Taken from https://codeforces.com/contest/1755/submission/179873022
// and converted from Kotlin to D

import std;

private void go()
{
  readln;
  int N, K; readln.formattedRead!" %d %d "(N, K);
  auto adj = new int[][N];
  auto deg = new int[N];
  foreach (_ ; 0 .. N - 1) {
    int i, j; readln.formattedRead!" %d %d "(i, j); i--; j--;
    adj[i] ~= j;
    adj[j] ~= i;
    deg[i]++;
    deg[j]++;
  }

  int[] q;
  foreach (i ; 0 .. N) {
    if (deg[i] <= 1) {
      q ~= i;
    }
  }
  size_t idx = 0;
  foreach (_ ; 0 .. K) {
    auto en = q.length;
    foreach (i ; idx .. en) {
      auto cur = q[i];
      foreach (nxt ; adj[cur]) {
        deg[nxt]--;
        if (deg[nxt] == 1) {
          q ~= nxt;
        }
      }
    }
    idx = en;
    if (idx == N) {
        break;
    }
  }
  writeln(N - idx);
}

void main()
{
  foreach (_ ; 0 .. readln.strip.to!int) { go(); }
}


Sam lives in Awesomeburg, its downtown has a triangular shape. Also, the following is true about the triangle:  its vertices have integer coordinates,  the coordinates of vertices are non-negative, and  its vertices are not on a single line. He calls a point on the downtown's border (that is the border of the triangle) safe if he can reach this point from at least one point of the line $$$y = 0$$$ walking along some straight line, without crossing the interior of the triangle.  In the picture the downtown is marked with grey color. The first path is invalid because it does not go along a straight line. The second path is invalid because it intersects with the interior of the downtown. The third and fourth paths are correct. Find the total length of the unsafe parts of the downtown border. It can be proven that these parts are segments and their number is finite.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Description of the test cases follows. Each test case contains three lines, each of them contains two integers $$$x_i$$$, $$$y_i$$$ ($$$0 \le x_i, y_i \le 10^9$$$) — coordinates of the vertices of the downtown's border.

For each test case print a single number — the answer to the problem. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$. Formally let your answer be $$$a$$$, jury answer be $$$b$$$. Your answer will be considered correct if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}$$$.

NoteIn the picture, the downtowns of the first three test cases are illustrated. Triangles are enumerated according to the indices of test cases they belong to.  In the first two test cases, all points on the borders of the downtowns are safe, thus the answers are $$$0$$$.In the following picture unsafe points for the third test case are marked with black color:  In the fourth test case, all points on the border of the downtown are safe.

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

auto line_to_array(ref File input)
{
    import std.algorithm.sorting;
    import std.conv;
    import std.string;
    return input.readln.chomp.split;
}

auto line_to_int(ref File input)
{
    import std.string;
    import std.conv;
    return input.readln.chomp.to!int;
}

void solution(ref File input, ref File output)
{
    auto fd = input.getFP;

    double answer = 0;
    long[3] x, y;

    for (int i = 0; i < 3; i++) input.readf!"%d %d\n"(x[i], y[i]);

    for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    for (int c = 0; c < 3; c++)
        if (i != j && j != c && i != c && y[i] == y[j] && y[i] > y[c])
        {
            answer += sqrt(cast(double) (pow(y[i] - y[j], 2) + pow(x[i] - x[j], 2)));
            break;
        }
    output.writefln("%f", answer / 2);
}

void loop(ref File input, ref File output)
{
    auto t = input.readln.chomp.to!int;
    while (t-- > 0) solution(input, output);
}

int main()
{
    File input, output;
    debug(1)
    {
        input = File("input.txt", "r");
        output = File("output.txt", "w");
    } else
    {
        input = stdin;
        output = stdout;
    }
    loop(input, output);
    return 0;
}

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto P = 3.iota.map!(_ => Point(scan!long, scan!long)).array;

      auto maxY = P.map!"a.y".maxElement;
      auto mys = P.filter!(a => a.y == maxY).array;

      if (mys.length == 2) {
        return (mys[0].x - mys[1].x).abs;
      }

      return 0;
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

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

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

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

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

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

void main()
{
	auto t = RD!int;
	auto ans = new double[](t);
	foreach (ti; 0..t)
	{
		auto x = new long[](3);
		auto y = new long[](3);
		foreach (i; 0..3)
		{
			x[i] = RD;
			y[i] = RD;
		}

		auto index = y.MAKE_IDX;
		if (y[index[1]] == y[index[2]])
			ans[ti] = abs(x[index[1]] - x[index[2]]);
		else
			ans[ti] = 0.0;
	}

	foreach (e; ans)
		writefln(FMT_F, e);
	stdout.flush;
	debug readln;
}

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

auto line_to_array(ref File input)
{
    import std.algorithm.sorting;
    import std.conv;
    import std.string;
    return input.readln.chomp.split;
}

auto line_to_int(ref File input)
{
    import std.string;
    import std.conv;
    return input.readln.chomp.to!int;
}

void solution(ref File input, ref File output)
{
    auto fd = input.getFP;

    double answer = 0;
    long[3] x, y;

    for (int i = 0; i < 3; i++) input.readf!"%d %d\n"(x[i], y[i]);

    for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    for (int c = 0; c < 3; c++)
        if (i != j && j != c && i != c && y[i] == y[j] && y[i] > y[c])
        {
            answer += sqrt(cast(double) (pow(y[i] - y[j], 2) + pow(x[i] - x[j], 2)));
            break;
        }
    output.writeln(answer / 2);
}

void loop(ref File input, ref File output)
{
    auto t = input.readln.chomp.to!int;
    while (t-- > 0) solution(input, output);
}

int main()
{
    File input, output;
    debug(1)
    {
        input = File("input.txt", "r");
        output = File("output.txt", "w");
    } else
    {
        input = stdin;
        output = stdout;
    }
    loop(input, output);
    return 0;
}

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

auto line_to_array(ref File input)
{
    import std.algorithm.sorting;
    import std.conv;
    import std.string;
    return input.readln.chomp.split;
}

auto line_to_int(ref File input)
{
    import std.string;
    import std.conv;
    return input.readln.chomp.to!int;
}

void solution(ref File input, ref File output)
{
    auto fd = input.getFP;

    float answer = 0;
    int[3] x, y;

    core.stdc.stdio.fscanf(fd, "%d %d\n", &x[0], &y[0]);
    core.stdc.stdio.fscanf(fd, "%d %d\n", &x[1], &y[1]);
    core.stdc.stdio.fscanf(fd, "%d %d\n", &x[2], &y[2]);

    for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    for (int c = 0; c < 3; c++)
        if (i != j && j != c && i != c && y[i] == y[j] && y[i] > y[c])
        {
            //output.writefln("i: %d x: %d y: %d", i, x[i], y[i]);
            //output.writefln("j: %d x: %d y: %d", j, x[j], y[j]);
            //output.writefln("c: %d x: %d y: %d", c, x[c], y[c]);
            answer += sqrt(cast(float) (pow(y[i] - y[j], 2) + pow(x[i] - x[j], 2)));
            //answer += cast(float) (pow(y[i] - y[j], 2) + pow(x[i] - x[j], 2));
            break;
        }
    output.writeln(answer / 2);
}

void loop(ref File input, ref File output)
{
    auto t = input.readln.chomp.to!int;
    while (t-- > 0) solution(input, output);
}

int main()
{
    File input, output;
    debug(1)
    {
        input = File("input.txt", "r");
        output = File("output.txt", "w");
    } else
    {
        input = stdin;
        output = stdout;
    }
    loop(input, output);
    return 0;
}

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto P = 3.iota.map!(_ => Point(scan!long, scan!long)).array;

      auto maxY = P.map!"a.y".maxElement;
      auto mys = P.filter!(a => a.y == maxY).array;

      if (mys.length == 2) {
        return ((mys[0].x - mys[1].x).to!real.pow(2) + (mys[0].y - mys[1].y).to!real.pow(2)).sqrt;
      }

      return 0.0;
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

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

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

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


Circular land is an $$$2n \times 2n$$$ grid. Rows of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from top to bottom and columns of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from left to right. The cell $$$(x, y)$$$ is the cell on the intersection of row $$$x$$$ and column $$$y$$$ for $$$1 \leq x \leq 2n$$$ and $$$1 \leq y \leq 2n$$$.There are $$$n^2$$$ of your friends in the top left corner of the grid. That is, in each cell $$$(x, y)$$$ with $$$1 \leq x, y \leq n$$$ there is exactly one friend. Some of the other cells are covered with snow.Your friends want to get to the bottom right corner of the grid. For this in each cell $$$(x, y)$$$ with $$$n+1 \leq x, y \leq 2n$$$ there should be exactly one friend. It doesn't matter in what cell each of friends will be.You have decided to help your friends to get to the bottom right corner of the grid.For this, you can give instructions of the following types:   You select a row $$$x$$$. All friends in this row should move to the next cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 \leq y < 2n$$$ will move to the cell $$$(x, y + 1)$$$ and friend from the cell $$$(x, 2n)$$$ will move to the cell $$$(x, 1)$$$.  You select a row $$$x$$$. All friends in this row should move to the previous cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 < y \leq 2n$$$ will move to the cell $$$(x, y - 1)$$$ and friend from the cell $$$(x, 1)$$$ will move to the cell $$$(x, 2n)$$$.  You select a column $$$y$$$. All friends in this column should move to the next cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 \leq x < 2n$$$ will move to the cell $$$(x + 1, y)$$$ and friend from the cell $$$(2n, y)$$$ will move to the cell $$$(1, y)$$$.  You select a column $$$y$$$. All friends in this column should move to the previous cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 < x \leq 2n$$$ will move to the cell $$$(x - 1, y)$$$ and friend from the cell $$$(1, y)$$$ will move to the cell $$$(2n, y)$$$. Note how friends on the grid border behave in these instructions.  Example of applying the third operation to the second column. Here, colorful circles denote your friends and blue cells are covered with snow. You can give such instructions any number of times. You can give instructions of different types. If after any instruction one of your friends is in the cell covered with snow he becomes ill.In order to save your friends you can remove snow from some cells before giving the first instruction:   You can select the cell $$$(x, y)$$$ that is covered with snow now and remove snow from this cell for $$$c_{x, y}$$$ coins. You can do this operation any number of times.You want to spend the minimal number of coins and give some instructions to your friends. After this, all your friends should be in the bottom right corner of the grid and none of them should be ill.Please, find how many coins you will spend.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \leq n \leq 250$$$). Each of the next $$$2n$$$ lines contains $$$2n$$$ integers $$$c_{i, 1}, c_{i, 2}, \ldots, c_{i, 2n}$$$ ($$$0 \leq c_{i, j} \leq 10^9$$$) — costs of removing snow from cells. If $$$c_{i, j} = 0$$$ for some $$$i, j$$$ than there is no snow in cell $$$(i, j)$$$. Otherwise, cell $$$(i, j)$$$ is covered with snow. It is guaranteed that $$$c_{i, j} = 0$$$ for $$$1 \leq i, j \leq n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$250$$$.

For each test case output one integer — the minimal number of coins you should spend.

NoteIn the first test case you can remove snow from the cells $$$(2, 1)$$$ and $$$(2, 2)$$$ for $$$100$$$ coins. Then you can give instructions   All friends in the first collum should move to the previous cell. After this, your friend will be in the cell $$$(2, 1)$$$.  All friends in the second row should move to the next cell. After this, your friend will be in the cell $$$(2, 2)$$$. In the second test case you can remove all snow from the columns $$$3$$$ and $$$4$$$ for $$$22$$$ coins. Then you can give instructions   All friends in the first row should move to the next cell.  All friends in the first row should move to the next cell.  All friends in the second row should move to the next cell.  All friends in the second row should move to the next cell.  All friends in the third column should move to the next cell.  All friends in the third column should move to the next cell.  All friends in the fourth column should move to the next cell.  All friends in the fourth column should move to the next cell. It can be shown that none of the friends will become ill and that it is impossible to spend less coins.

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);
		int [] [] board;
		foreach (row; 0..n * 2)
		{
			board ~= readln.splitter.map !(to !(int)).array;
		}
		auto res = 0L;
		foreach (row; n..n * 2)
		{
			foreach (col; n..n * 2)
			{
				res += board[row][col];
			}
		}
		res += min (board[n][0], board[$ - 1][0],
		    board[n][n - 1], board[$ - 1][n - 1],
		    board[0][n], board[n - 1][n],
		    board[0][$ - 1], board[n - 1][$ - 1]);
		writeln (res);
	}
}


You are given a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters.A substring of string $$$s$$$ is a continuous segment of letters from $$$s$$$. For example, "defor" is a substring of "codeforces" and "fors" is not. The length of the substring is the number of letters in it.Let's call some string of length $$$n$$$ diverse if and only if there is no letter to appear strictly more than $$$\frac n 2$$$ times. For example, strings "abc" and "iltlml" are diverse and strings "aab" and "zz" are not.Your task is to find any diverse substring of string $$$s$$$ or report that there is none. Note that it is not required to maximize or minimize the length of the resulting substring.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the length of string $$$s$$$. The second line is the string $$$s$$$, consisting of exactly $$$n$$$ lowercase Latin letters.

Print "NO" if there is no diverse substring in the string $$$s$$$. Otherwise the first line should contain "YES". The second line should contain any diverse substring of string $$$s$$$.

NoteThe first example has lots of correct answers. Please, restrain yourself from asking if some specific answer is correct for some specific test or not, these questions always lead to "No comments" answer.

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 N = readln.chomp.to!int;
    auto S = readln.chomp;
    auto cnt = new int[](26);

    bool ok(int l, int r) {
        cnt[] = 0;
        foreach (i; l..r+1) cnt[S[i]-'a'] += 1;
        return cnt.reduce!max * 2 <= r - l + 1;
    }

    foreach (i; 0..N) {
        foreach (j; i..N) {
            if (ok(i, j)) {
                writeln("YES");
                writeln(S[i..j+1]);
                return;
            }
        }
    }

    writeln("NO");
}

//ahmat
import core.stdc.stdio:printf,scanf;
import std.stdio;
import std.string;
import std.container;
import std.array;
import std.range;
import std.algorithm;
import std.math;
import std.numeric;
import std.conv;
import std.bitmanip;
import core.bitop;
int sz(Range)(in Range r){return cast(int)r.length;}
alias rbt=RedBlackTree!int;

void main(){
	int n;
	scanf("%d",&n);
	readln;
	string s=readln.strip;
	bool found=false;
	int i=0;
	for(;i<n-1;i++){
		if(s[i]!=s[i+1]){
			writeln("YES");
			writeln(s[i..i+2]);
			found=true;
			break;
		}
	}
	if(!found) writeln("NO");
}

//ahmat
import core.stdc.stdio:printf,scanf;
import std.stdio;
import std.string;
import std.container;
import std.array;
import std.range;
import std.algorithm;
import std.math;
import std.numeric;
import std.conv;
import std.bitmanip;
import core.bitop;
int sz(Range)(in Range r){return cast(int)r.length;}
alias rbt=RedBlackTree!int;

void main(){
	int n;
	scanf("%d",&n);
	readln;
	string s=readln.strip;
	bool found=false;
	int i=0;
	if(s.length>1){
		for(;i<n-1;i++){
			if(s[i]!=s[i+1]){
				found=true;
				break;
			}
		}
	}
	if(found){
		writeln("YES");
		writeln(s[i..i+2]);
	}else writeln("NO");
}

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

void main() {
    int n;
    scan(n);
    auto s = readln.chomp;

    foreach (i ; 0 .. n - 1) {
        if (s[i] != s[i+1]) {
            writeln("YES");
            writeln(s[i .. i+2]);
            return;
        }
    }

    writeln("NO");
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

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

import std.stdio, std.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 N = readln.chomp.to!int;
    auto S = readln.chomp;
    auto cnt = new int[](26);

    bool ok(int l, int r) {
        cnt[] = 0;
        foreach (i; l..r+1) cnt[S[i]-1] += 1;
        return cnt.reduce!max * 2 <= r - l + 1;
    }

    foreach (i; 0..N) {
        foreach (j; i..N) {
            if (ok(i, j)) {
                writeln("YES");
                writeln(S[i..j+1]);
                return;
            }
        }
    }

    writeln("NO");
}


We get more and more news about DDoS-attacks of popular websites.Arseny is an admin and he thinks that a website is under a DDoS-attack if the total number of requests for a some period of time exceeds $$$100 \cdot t$$$, where $$$t$$$ — the number of seconds in this time segment. Arseny knows statistics on the number of requests per second since the server is booted. He knows the sequence $$$r_1, r_2, \dots, r_n$$$, where $$$r_i$$$ — the number of requests in the $$$i$$$-th second after boot. Determine the length of the longest continuous period of time, which Arseny considers to be a DDoS-attack. A seeking time period should not go beyond the boundaries of the segment $$$[1, n]$$$.

The first line contains $$$n$$$ ($$$1 \le n \le 5000$$$) — number of seconds since server has been booted. The second line contains sequence of integers $$$r_1, r_2, \dots, r_n$$$ ($$$0 \le r_i \le 5000$$$), $$$r_i$$$ — number of requests in the $$$i$$$-th second.

Print the only integer number — the length of the longest time period which is considered to be a DDoS-attack by Arseny. If it doesn't exist print 0.

import std.stdio;
import std.algorithm.comparison;
void main()
{
  int n;
  scanf("%d", &n);

  int[5000] a;
  foreach (i; 0..n) {
    scanf("%d", &a[i]);
  }

  int m = 0;
  foreach (i; 0..n) {
    int t = 0;
    foreach (j; i..n) {
      t += a[j];
      if (t > 100 * (j - i + 1)) {
        m = max(m, j - i + 1);
      }
    }
  }

  printf("%d\n", m);
}

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

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

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

    int mxlen = 0;
    foreach (le; 0 .. n) {
        int sm = 0, len = 0;
        foreach (r; le .. n) {
            sm += arr[r];
            len += 1;

            if (sm > 100 * len) { mxlen = max(mxlen, len); }
        }
    }

    mxlen.writeln;
}

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

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

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

    int mxlen = 0;
    foreach (le; 0 .. n) {
        int sm = 0, len = 0;
        foreach (r; le .. n) {
            sm += arr[r];
            len += 1;

            if (sm >= 100 * len) { mxlen = max(mxlen, len); }
        }
    }

    if (mxlen == n) { mxlen -= 1; }

    mxlen.writeln;
}


Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect.The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him!

The first line contains two integers, n and k (0 ≤ k < n ≤ 105). The second line contains a string consisting of n digits.

Print the answer to the problem modulo 109 + 7.

NoteIn the first sample the result equals (1 + 08) + (10 + 8) = 27.In the second sample the result equals 1 + 0 + 8 = 9.

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;

immutable int MOD  = 1_000_000_007;
immutable int BASE =            10;

int powmod (int a, int p, int m)
{
	int res = 1;
	while (p)
	{
		if (p & 1)
		{
			res = (cast (long) res * a) % m;
		}
		a = (cast (long) a * a) % m;
		p >>= 1;
	}
	return res;
}

int inv (int a)
{
	return powmod (a, MOD - 2, MOD);
}

void main ()
{
	int n, k;
	while (readf (" %s %s ", &n, &k) > 0)
	{
		auto a = readln ().strip ().map !(q{a - '0'}) ().array ();
		debug {writeln (a);}

		long [] p = [1];
		foreach (i; 0..n)
		{
			p ~= (p[$ - 1] * BASE) % MOD;
		}
		debug {writeln (p);}

		long [] c = [1];
		foreach (i; k..n - 1)
		{
			c ~= (((c[$ - 1] * i) % MOD) * inv (i - k + 1)) % MOD;
		}
		reverse (c);
		debug {writeln (c);}

		long s = 0;
		foreach (i; 0..c.length)
		{
			s = (s + c[i] * p[i]) % MOD;
		}
		debug {writeln (s);}

		long res = 0;
		foreach (i; 0..n)
		{
			res = (res + s * a[i]) % MOD;
			int j = n - i - 1;
			if (0 < j && j < c.length)
			{
				s = (s - c[j] * p[j]) % MOD;
				s = (s + c[j] * p[j - 1]) % MOD;
				c[j - 1] = (c[j - 1] + c[j]) % MOD;
				j--;
				s = (s + MOD) % MOD;
				debug {writeln (c, ' ', s);}
			}
		}
		writeln (res);
	}
}

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; ) { if (stdin.eof) throw new EOFException; tokens = readln.split; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

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

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

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

immutable long MO = 10^^9 + 7;
immutable LIM = 2 * 10^^5 + 5;

long[] inv, fac, facInv;

void prepare() {
	inv = new long[LIM];
	inv[1] = 1;
	foreach (i; 2 .. LIM) {
		inv[i] = MO - MO / i * inv[cast(size_t)(MO % i)] % MO;
	}
	fac = new long[LIM];
	facInv = new long[LIM];
	fac[0] = 1;
	facInv[0] = 1;
	foreach (i; 1 .. LIM) {
		fac[i] = fac[i - 1] * i % MO;
		facInv[i] = facInv[i - 1] * inv[i] % MO;
	}
}

long binom(int n, int k) {
	if (!(0 <= k && k <= n)) {
		return 0;
	}
	return fac[n] * facInv[k] % MO * facInv[n - k] % MO;
}

int N, K;
string S;

void main(string[] args) {
	prepare;

	try {
	for (; ; ) {
		N = readInt;
		K = readInt;
		S = readToken;

debug{
long brt;
foreach(h;0..1<<(N-1)){
 int cnt;
 foreach(i;0..N-1)if(h&1<<i)++cnt;
 if(cnt!=K)continue;
 long x;
 foreach(i;0..N){
  x=(x*10+S[i]-'0')%MO;
  // writeln(h," ",x);
  if((h&1<<i)||i==N-1){(brt+=x)%=MO;x=0;}
 }
}
writeln("brt = ",brt);
}

		long ans;
		long sum = 0;
		long ten = 1;
		foreach_reverse (i; 0 .. N) {
debug{
// writeln("sum = ",sum,", binom(i, K) * ten = ",binom(i, K) * ten);
}
			(ans += ((sum + binom(i, K) * ten) % MO) * (S[i] - '0')) %= MO;
			(sum += binom(i - 1, K - 1) * ten) %= MO;
			(ten *= 10) %= MO;
		}
		ans = (ans % MO + MO) % MO;
		writeln(ans);

	}
	} catch (EOFException) {}
}


You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. Friends asked you to make the greatest common divisor (GCD) of all numbers in the array equal to $$$1$$$. In one operation, you can do the following: Select an arbitrary index in the array $$$1 \leq i \leq n$$$; Make $$$a_i = \gcd(a_i, i)$$$, where $$$\gcd(x, y)$$$ denotes the GCD of integers $$$x$$$ and $$$y$$$. The cost of such an operation is $$$n - i + 1$$$.You need to find the minimum total cost of operations we need to perform so that the GCD of the all array numbers becomes equal to $$$1$$$.

Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5\,000$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 20$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array.

For each test case, output a single integer — the minimum total cost of operations that will need to be performed so that the GCD of all numbers in the array becomes equal to $$$1$$$. We can show that it's always possible to do so.

NoteIn the first test case, the GCD of the entire array is already equal to $$$1$$$, so there is no need to perform operations.In the second test case, select $$$i = 1$$$. After this operation, $$$a_1 = \gcd(2, 1) = 1$$$. The cost of this operation is $$$1$$$.In the third test case, you can select $$$i = 1$$$, after that the array $$$a$$$ will be equal to $$$[1, 4]$$$. The GCD of this array is $$$1$$$, and the total cost is $$$2$$$.In the fourth test case, you can select $$$i = 2$$$, after that the array $$$a$$$ will be equal to $$$[3, 2, 9]$$$. The GCD of this array is $$$1$$$, and the total cost is $$$2$$$.In the sixth test case, you can select $$$i = 4$$$ and $$$i = 5$$$, after that the array $$$a$$$ will be equal to $$$[120, 60, 80, 4, 5]$$$. The GCD of this array is $$$1$$$, and the total cost is $$$3$$$.

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

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 solve() {
    int n = read!int;
    auto a = readarray!long;
    auto result = a[0];
    foreach(i; 1 .. n) {
        result = gcd(result, a[i]);
    }
    if (result == 1) {
        writeln(0);
        return;
    }
    if (n == 1)
        writeln(1);
    else if (gcd(result, gcd(n,a[n-1])) == 1)
        writeln(1);
    else if (gcd(result, gcd(n-1,a[n-2])) == 1)
        writeln(2);
    else
        writeln(3);
}

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

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

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 solve() {
    int n = read!int;
    auto a = readarray!long;
    auto result = a[0];
    if (result == 1) {
        writeln(0);
        return;
    }
    foreach(i; 1 .. n) {
        if (result == 1) {
            writeln(0);
            return;
        }
        result = gcd(result, a[i]);
    }
    if (n == 1)
        writeln(1);
    else if (gcd(result, gcd(n,a[n-1])) == 1)
        writeln(1);
    else if (gcd(gcd(n-1,a[n-2]), result) == 1 )
        writeln(2);
    else
        writeln(min(n,3));
}

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


The only difference between easy and hard versions is constraints.Nauuo is a girl who loves random picture websites.One day she made a random picture website by herself which includes $$$n$$$ pictures.When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The $$$i$$$-th picture has a non-negative weight $$$w_i$$$, and the probability of the $$$i$$$-th picture being displayed is $$$\frac{w_i}{\sum_{j=1}^nw_j}$$$. That is to say, the probability of a picture to be displayed is proportional to its weight.However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add $$$1$$$ to its weight; otherwise, she would subtract $$$1$$$ from its weight.Nauuo will visit the website $$$m$$$ times. She wants to know the expected weight of each picture after all the $$$m$$$ visits modulo $$$998244353$$$. Can you help her?The expected weight of the $$$i$$$-th picture can be denoted by $$$\frac {q_i} {p_i}$$$ where $$$\gcd(p_i,q_i)=1$$$, you need to print an integer $$$r_i$$$ satisfying $$$0\le r_i<998244353$$$ and $$$r_i\cdot p_i\equiv q_i\pmod{998244353}$$$. It can be proved that such $$$r_i$$$ exists and is unique.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n\le 2\cdot 10^5$$$, $$$1\le m\le 3000$$$) — the number of pictures and the number of visits to the website. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$a_i$$$ is either $$$0$$$ or $$$1$$$) — if $$$a_i=0$$$ , Nauuo does not like the $$$i$$$-th picture; otherwise Nauuo likes the $$$i$$$-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains $$$n$$$ positive integers $$$w_1,w_2,\ldots,w_n$$$ ($$$w_i \geq 1$$$) — the initial weights of the pictures. It is guaranteed that the sum of all the initial weights does not exceed $$$998244352-m$$$.

The output contains $$$n$$$ integers $$$r_1,r_2,\ldots,r_n$$$ — the expected weights modulo $$$998244353$$$.

NoteIn the first example, if the only visit shows the first picture with a probability of $$$\frac 2 3$$$, the final weights are $$$(1,1)$$$; if the only visit shows the second picture with a probability of $$$\frac1 3$$$, the final weights are $$$(2,2)$$$.So, both expected weights are $$$\frac2 3\cdot 1+\frac 1 3\cdot 2=\frac4 3$$$ .Because $$$332748119\cdot 3\equiv 4\pmod{998244353}$$$, you need to print $$$332748119$$$ instead of $$$\frac4 3$$$ or $$$1.3333333333$$$.In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, $$$w_1$$$ will be increased by $$$1$$$.So, the expected weight is $$$1+2=3$$$.Nauuo is very naughty so she didn't give you any hint of the third example.

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, 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)); }

// a^-1 (mod m)
long modInv(long a, long m)
in {
  assert(m > 0, "modInv: m > 0 must hold");
}
do {
  long b = m, x = 1, y = 0, t;
  for (; ; ) {
    t = a / b;
    a -= t * b;
    if (a == 0) {
      assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1");
      if (b == -1) {
        y = -y;
      }
      return (y < 0) ? (y + m) : y;
    }
    x -= t * y;
    t = b / a;
    b -= t * a;
    if (b == 0) {
      assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1");
      if (a == -1) {
        x = -x;
      }
      return (x < 0) ? (x + m) : x;
    }
    y -= t * x;
  }
}

enum long MO = 998244353;

int N, M;
int[] A;
long[] W;

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

      long R, S;
      foreach (i; 0 .. N) {
        if (A[i]) {
          R += W[i];
        }
        S += W[i];
      }

      auto invs = new long[2 * M + 1];
      foreach (x; -M .. +M + 1) {
        if (S + x > 0) {
          invs[M + x] = modInv(S + x, MO);
        }
      }

      auto dp = new long[][](M + 1, M + 1);
      dp[0][0] = 1;
      foreach (j; 0 .. M) {
        foreach (x; 0 .. j + 1) {
          if (S + x + (j - x) > 0) {
            const prob = ((R + x) * invs[M + x - (j - x)]) % MO;
            (dp[j + 1][x + 0] += dp[j][x] * (1 - prob)) %= MO;
            (dp[j + 1][x + 1] += dp[j][x] * prob) %= MO;
          }
        }
      }
      debug {
        writeln("dp = ", dp);
      }

      long eP, eN;
      foreach (x; 0 .. M + 1) {
        (eP += dp[M][x] * (R + x)) %= MO;
        (eN += dp[M][x] * (S - R - (M - x))) %= MO;
      }
      foreach (i; 0 .. N) {
        long ans;
        if (A[i]) {
          ans = (((eP * W[i]) % MO) * modInv(R, MO)) % MO;
        } else {
          ans = (((eN * W[i]) % MO) * modInv(S - R, MO)) % MO;
        }
        ans = (ans % MO + MO) % MO;
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}


There is a deck of $$$n$$$ cards. The $$$i$$$-th card has a number $$$a_i$$$ on the front and a number $$$b_i$$$ on the back. Every integer between $$$1$$$ and $$$2n$$$ appears exactly once on the cards.A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if $$$a_i< a_{i+1}$$$ and $$$b_i> b_{i+1}$$$ for all $$$1\le i<n$$$.To flip a card $$$i$$$ means swapping the values of $$$a_i$$$ and $$$b_i$$$. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck?

The first line contains a single integer $$$n$$$ ($$$1\le n\le 2\cdot 10^5$$$) — the number of cards. The next $$$n$$$ lines describe the cards. The $$$i$$$-th of these lines contains two integers $$$a_i, b_i$$$ ($$$1\le a_i, b_i\le 2n$$$). Every integer between $$$1$$$ and $$$2n$$$ appears exactly once.

If it is impossible to sort the deck, output "-1". Otherwise, output the minimum number of flips required to sort the deck.

NoteIn the first test case, we flip the cards $$$(1, 9)$$$ and $$$(2, 7)$$$. The deck is then ordered $$$(3,10), (5,8), (6,4), (7,2), (9,1)$$$. It is sorted because $$$3<5<6<7<9$$$ and $$$10>8>4>2>1$$$.In the second test case, it is impossible to sort the deck.

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

alias Value = int;
int root(int[] ps, Value[] qs, int u) {
  if (ps[u] < 0) {
    return u;
  } else {
    int r = root(ps, qs, ps[u]);
    // qs[u] += qs[ps[u]];
    qs[u] ^= qs[ps[u]];
    return (ps[u] = r);
  }
}
bool connect(int[] ps, Value[] qs, int u, int v, Value c) {
  int ru = root(ps, qs, u);
  int rv = root(ps, qs, v);
  // const Value cc = c + qs[u] - qs[v];
  const Value cc = c ^ qs[u] ^ qs[v];
  if (ru == rv) return (cc == 0);
  // if (ps[ru] > ps[rv]) { swap(ru, rv); cc *= -1; }
  if (ps[ru] > ps[rv]) { swap(ru, rv); }
  ps[ru] += ps[rv]; ps[rv] = ru; qs[rv] = cc;
  return true;
}

alias Card = Tuple!(int, "a", int, "b");

int N;
Card[] C;

int brute() {
  int ans = N + 1;
  foreach (p; 0 .. 1 << N) {
    foreach (u; 0 .. N) if (p & 1 << u) swap(C[u].a, C[u].b);
    bool ok = true;
    foreach (u; 0 .. N) foreach (v; 0 .. N) {
      if (C[u].a < C[v].a) {
        ok = ok && (C[u].b > C[v].b);
      }
    }
    if (ok) {
      chmin(ans, popcnt(p));
    }
    foreach (u; 0 .. N) if (p & 1 << u) swap(C[u].a, C[u].b);
  }
  return (ans > N) ? -1 : ans;
}

int solve() {
  auto ps = new int[2 * N];
  auto qs = new int[2 * N];
  ps[] = -1;

  C.sort!((c0, c1) => (min(c0.a, c0.b) < min(c1.a, c1.b)));
  foreach (u; 0 .. N) {
    if (C[u].a < C[u].b) {
      connect(ps, qs, u, N + u, 0);
    } else {
      swap(C[u].a, C[u].b);
      connect(ps, qs, u, N + u, 1);
    }
  }

  int minB = 2 * N + 1, maxA = 0;
  foreach (u; 0 .. N) {
    chmin(minB, C[u].b);
    chmax(maxA, C[u].a);
  }
  if (minB < maxA) {
    return -1;
  }

  int[] us, vs;
  foreach (u; 0 .. N) {
    if (us.empty || C[us[$ - 1]].b > C[u].b) {
      us ~= u;
    } else if (vs.empty || C[vs[$ - 1]].b > C[u].b) {
      vs ~= u;
    } else {
      return -1;
    }
  }
  const usLen = cast(int)(us.length);
  const vsLen = cast(int)(vs.length);
  debug {
    // writeln("C = ", C);
    // writeln("us = ", us);
    // writeln("vs = ", vs);
  }

  auto vals = new int[vsLen + 1];
  {
    int f;
    foreach (e; 0 .. usLen) {
      for (; f < vsLen && vs[f] < us[e]; ++f) {}
      int lo = f - 1, hi = vsLen;
      for (; lo + 1 < hi; ) {
        const mid = (lo + hi) / 2;
        ((C[us[e]].b < C[vs[mid]].b) ? lo : hi) = mid;
      }
      // e - [f, lo]
      if (f <= lo) {
        debug {
          // writefln("%s: [%s, %s]", e, f, lo);
        }
        if (!connect(ps, qs, us[e], vs[f], 1)) {
          return false;
        }
        ++vals[f];
        --vals[lo];
      }
    }
  }
  foreach (f; 0 .. vsLen) {
    vals[f + 1] += vals[f];
  }
  debug {
    // writeln("vals = ", vals);
  }
  foreach (f; 0 .. vsLen - 1) {
    if (vals[f] > 0) {
      debug {
        // writefln("connect0 %s %s", vs[f], vs[f + 1]);
      }
      if (!connect(ps, qs, vs[f], vs[f + 1], 0)) {
        return false;
      }
    }
  }

  auto cnt = new int[][](2 * N, 2);
  foreach (u; 0 .. N) {
    const r = root(ps, qs, N + u);
    ++cnt[r][qs[N + u]];
  }
  debug {
    // writeln("cnt = ", cnt);
    // writeln("ps = ", ps);
    // writeln("qs = ", qs);
  }
  int ans;
  foreach (r; 0 .. 2 * N) {
    ans += min(cnt[r][0], cnt[r][1]);
  }
  return ans;
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      C = new Card[N];
      foreach (u; 0 .. N) {
        C[u].a = readInt();
        C[u].b = readInt();
      }

      debug {
        const CSave = C.dup;
        const brt = brute();
      }

      const ans = solve();
      writeln(ans);

      debug {
        assert(brt == ans, format("%s: %s %s", CSave, brt, ans));
      }
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

alias Value = int;
int root(int[] ps, Value[] qs, int u) {
  if (ps[u] < 0) {
    return u;
  } else {
    int r = root(ps, qs, ps[u]);
    // qs[u] += qs[ps[u]];
    qs[u] ^= qs[ps[u]];
    return (ps[u] = r);
  }
}
bool connect(int[] ps, Value[] qs, int u, int v, Value c) {
  int ru = root(ps, qs, u);
  int rv = root(ps, qs, v);
  // const Value cc = c + qs[u] - qs[v];
  const Value cc = c ^ qs[u] ^ qs[v];
  if (ru == rv) return (cc == 0);
  // if (ps[ru] > ps[rv]) { swap(ru, rv); cc *= -1; }
  if (ps[ru] > ps[rv]) { swap(ru, rv); }
  ps[ru] += ps[rv]; ps[rv] = ru; qs[rv] = cc;
  return true;
}

alias Card = Tuple!(int, "a", int, "b");

int N;
Card[] C;

int brute() {
  auto ps = new int[N];
  auto qs = new int[N];
  ps[] = -1;
  foreach (u; 0 .. N) foreach (v; u + 1 .. N) {
    int au = C[u].a, bu = C[u].b;
    int av = C[v].a, bv = C[v].b;
    if (au > av) {
      swap(au, av);
      swap(bu, bv);
    }
    if (bu < av) {
      return -1;
    }
    if (bu < bv) {
      if (!connect(ps, qs, u, v, 1)) {
        return -1;
      }
    }
  }
  auto cnt = new int[][](N, 2);
  foreach (u; 0 .. N) {
    const r = root(ps, qs, u);
    ++cnt[r][qs[u]];
  }
  int ans;
  foreach (r; 0 .. N) {
    ans += min(cnt[r][0], cnt[r][1]);
  }
  return ans;
}

int solve() {
  foreach (u; 0 .. N) {
    if (C[u].a > C[u].b) {
      swap(C[u].a, C[u].b);
    }
  }
  C.sort;
  debug {
    // writeln("C = ", C);
  }

  int minB = 2 * N + 1, maxA = 0;
  foreach (u; 0 .. N) {
    chmin(minB, C[u].b);
    chmax(maxA, C[u].a);
  }
  if (minB < maxA) {
    return -1;
  }

  int[] us, vs;
  foreach (u; 0 .. N) {
    if (us.empty || C[us[$ - 1]].b > C[u].b) {
      us ~= u;
    } else if (vs.empty || C[vs[$ - 1]].b > C[u].b) {
      vs ~= u;
    } else {
      return -1;
    }
  }
  const usLen = cast(int)(us.length);
  const vsLen = cast(int)(vs.length);
  debug {
    // writeln("us = ", us);
    // writeln("vs = ", vs);
  }

  auto ps = new int[N];
  auto qs = new int[N];
  ps[] = -1;
  auto vals = new int[vsLen + 1];
  {
    int f;
    foreach (e; 0 .. usLen) {
      for (; f < vsLen && vs[f] < us[e]; ++f) {}
      int lo = f - 1, hi = vsLen;
      for (; lo + 1 < hi; ) {
        const mid = (lo + hi) / 2;
        ((C[us[e]].b < C[vs[mid]].b) ? lo : hi) = mid;
      }
      // e - [f, lo]
      if (f <= lo) {
        debug {
          // writefln("%s: [%s, %s]", e, f, lo);
        }
        if (!connect(ps, qs, us[e], vs[f], 1)) {
          return false;
        }
        ++vals[f];
        --vals[lo];
      }
    }
  }
  foreach (f; 0 .. vsLen) {
    vals[f + 1] += vals[f];
  }
  debug {
    // writeln("vals = ", vals);
  }
  foreach (f; 0 .. vsLen - 1) {
    if (vals[f] > 0) {
      debug {
        // writefln("connect0 %s %s", vs[f], vs[f + 1]);
      }
      if (!connect(ps, qs, vs[f], vs[f + 1], 0)) {
        return false;
      }
    }
  }

  auto cnt = new int[][](N, 2);
  foreach (u; 0 .. N) {
    const r = root(ps, qs, u);
    ++cnt[r][qs[u]];
  }
  int ans;
  foreach (r; 0 .. N) {
    ans += min(cnt[r][0], cnt[r][1]);
  }
  return ans;
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      C = new Card[N];
      foreach (u; 0 .. N) {
        C[u].a = readInt();
        C[u].b = readInt();
      }

      const ans = solve();
      writeln(ans);

      debug {
        const brt = brute();
        assert(brt == ans, format("%s: %s %s", C, brt, ans));
      }
    }
  } catch (EOFException e) {
  }
}


Luntik came out for a morning stroll and found an array $$$a$$$ of length $$$n$$$. He calculated the sum $$$s$$$ of the elements of the array ($$$s= \sum_{i=1}^{n} a_i$$$). Luntik calls a subsequence of the array $$$a$$$ nearly full if the sum of the numbers in that subsequence is equal to $$$s-1$$$.Luntik really wants to know the number of nearly full subsequences of the array $$$a$$$. But he needs to come home so he asks you to solve that problem!A sequence $$$x$$$ is a subsequence of a sequence $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) elements.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2 \cdot t$$$ lines contain descriptions of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 60$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array $$$a$$$.

For each test case print the number of nearly full subsequences of the array.

NoteIn the first test case, $$$s=1+2+3+4+5=15$$$, only $$$(2,3,4,5)$$$ is a nearly full subsequence among all subsequences, the sum in it is equal to $$$2+3+4+5=14=15-1$$$.In the second test case, there are no nearly full subsequences.In the third test case, $$$s=1+0=1$$$, the nearly full subsequences are $$$(0)$$$ and $$$()$$$ (the sum of an empty subsequence is $$$0$$$).

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

string FMT_F = "%.10f";

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

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

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

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

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

		long cnt0, cnt1;
		foreach (e; a)
		{
			if (e == 0)
				++cnt0;
			else if (e == 1)
				++cnt1;
		}
		ans[ti] = cnt1 * (2L^^cnt0);
	}

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


There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a way on it that   starts in the upper left cell of the matrix;  each following cell is to the right or down from the current cell;  the way ends in the bottom right cell. Moreover, if we multiply together all the numbers along the way, the result should be the least "round". In other words, it should end in the least possible number of zeros.

The first line contains an integer number n (2 ≤ n ≤ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).

In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.

import std.stdio;
import core.stdc.stdio;
import std.conv;
import std.algorithm;

void printmat(int[][] mat)
{
	for (int i = 0; i < mat.length; i++)
	for (int j = 0; j < mat[i].length; j++)
	{
		write(to!string(mat[i][j]) ~ " ");
		if (j == mat[i].length - 1)
			writeln();
	}
}

int getpow5(int num)
{
	if (num == 0)
		return 0;
	int count = 0;
	int n = num;
	while ((n % 5) == 0)
	{
		count++;
		n /= 5;
	}
	return count;
}

int getpow2(int num)
{
	if (num == 0)
		return 0;
	int count = 0;
	int n = num;
	while ((n & 1) == 0)
	{
		count++;
		n >>= 1;
	}
	return count;
}

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int[][] mat; mat.length = n;
	bool hasZero = false;
	int zx = -1;
	int zy = -1;
	for (int i = 0; i < n; i++)
	{
		mat[i].length = n;
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &mat[i][j]);
			if (mat[i][j] == 0)
			{
				hasZero = true;
				zx = i;
				zy = j;
			}
		}
	}
	//printmat(mat);

	int[][] num5; num5.length = n;
	num5[0].length = n;
	num5[0][0] = getpow5(mat[0][0]);
	for (int j = 1; j < n; j++)
	{
		num5[0][j] = getpow5(mat[0][j]);
		num5[0][j] += num5[0][j-1];
	}

	for (int i = 1; i < n; i++)
	{
		num5[i].length = n;
		num5[i][0] = getpow5(mat[i][0]);
		num5[i][0] += num5[i-1][0];
		for (int j = 1; j < n; j++)
		{
			num5[i][j] = getpow5(mat[i][j]);
			num5[i][j] += min(num5[i-1][j], num5[i][j-1]);
		}
	}
	//printmat(num5);

	int[][] num2; num2.length = n;
	num2[0].length = n;
	num2[0][0] = getpow2(mat[0][0]);
	for (int j = 1; j < n; j++)
	{
		num2[0][j] = getpow2(mat[0][j]);
		num2[0][j] += num2[0][j-1];
	}

	for (int i = 1; i < n; i++)
	{
		num2[i].length = n;
		num2[i][0] = getpow2(mat[i][0]);
		num2[i][0] += num2[i-1][0];
		for (int j = 1; j < n; j++)
		{
			num2[i][j] = getpow2(mat[i][j]);
			num2[i][j] += min(num2[i-1][j], num2[i][j-1]);
		}
	}
	//printmat(num2);
	//writeln();

	int[][] sol = (num2[n-1][n-1] < num5[n-1][n-1] ? num2 : num5);
	if (hasZero && sol[n-1][n-1] >= 1)
	{
		writeln("1");
		for (int i = 0; i < zx; i++)
		{
			write("D");
		}
		for (int i = 0; i < zy; i++)
		{
			write("R");
		}
		for (int i = zx; i < (n-1); i++)
		{
			write("D");
		}
		for (int i = zy; i < (n-1); i++)
		{
			write("R");
		}
		writeln();
	} else {
		int cx = n - 1;
		int cy = n - 1;
		int st = 2*(n-1);
		char[] trail = new char[st];
		for (int i = 0; i < st; i++)
		{
			if (cx == 0 && cy != 0)
			{
				trail[st - i - 1] = 'R';
				cy--;
			} else
			if (cx != 0 && cy == 0)
			{
				trail[st - i - 1] = 'D';
				cx--;
			} else {
				int t = sol[cx-1][cy];
				int l = sol[cx][cy-1];
				if (t < l)
				{
					trail[st - i - 1] = 'D';
					cx--;
				} else {
					trail[st - i - 1] = 'R';
					cy--;
				}
			}
		}
		writeln(sol[n-1][n-1]);
		writeln(trail);
	}
	return 0;
}

import std.stdio;
import core.stdc.stdio;
import std.conv;
import std.algorithm;

void printmat(int[][] mat)
{
	for (int i = 0; i < mat.length; i++)
	for (int j = 0; j < mat[i].length; j++)
	{
		write(to!string(mat[i][j]) ~ " ");
		if (j == mat[i].length - 1)
			writeln();
	}
}

int getpow5(int num)
{
	if (num == 0)
		return 0;
	int count = 0;
	int n = num;
	while ((n % 5) == 0)
	{
		count++;
		n /= 5;
	}
	return count;
}

int getpow2(int num)
{
	if (num == 0)
		return 0;
	int count = 0;
	int n = num;
	while ((n & 1) == 0)
	{
		count++;
		n >>= 1;
	}
	return count;
}

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int[][] mat; mat.length = n;
	for (int i = 0; i < n; i++)
	{
		mat[i].length = n;
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &mat[i][j]);
		}
	}
	//printmat(mat);

	int[][] num5; num5.length = n;
	num5[0].length = n;
	num5[0][0] = getpow5(mat[0][0]);
	for (int j = 1; j < n; j++)
	{
		num5[0][j] = getpow5(mat[0][j]);
		num5[0][j] += num5[0][j-1];
	}

	for (int i = 1; i < n; i++)
	{
		num5[i].length = n;
		num5[i][0] = getpow5(mat[i][0]);
		num5[i][0] += num5[i-1][0];
		for (int j = 1; j < n; j++)
		{
			num5[i][j] = getpow5(mat[i][j]);
			num5[i][j] += min(num5[i-1][j], num5[i][j-1]);
		}
	}
	//printmat(num5);

	int[][] num2; num2.length = n;
	num2[0].length = n;
	num2[0][0] = getpow2(mat[0][0]);
	for (int j = 1; j < n; j++)
	{
		num2[0][j] = getpow2(mat[0][j]);
		num2[0][j] += num2[0][j-1];
	}

	for (int i = 1; i < n; i++)
	{
		num2[i].length = n;
		num2[i][0] = getpow2(mat[i][0]);
		num2[i][0] += num2[i-1][0];
		for (int j = 1; j < n; j++)
		{
			num2[i][j] = getpow2(mat[i][j]);
			num2[i][j] += min(num2[i-1][j], num2[i][j-1]);
		}
	}
	//printmat(num2);
	//writeln();

	int[][] sol = (num2[n-1][n-1] < num5[n-1][n-1] ? num2 : num5);
	int cx = n - 1;
	int cy = n - 1;
	int st = 2*(n-1);
	char[] trail = new char[st];
	for (int i = 0; i < st; i++)
	{
		if (cx == 0 && cy != 0)
		{
			trail[st - i - 1] = 'R';
			cy--;
		} else
		if (cx != 0 && cy == 0)
		{
			trail[st - i - 1] = 'D';
			cx--;
		} else {
			int t = sol[cx-1][cy];
			int l = sol[cx][cy-1];
			if (t < l)
			{
				trail[st - i - 1] = 'D';
				cx--;
			} else {
				trail[st - i - 1] = 'R';
				cy--;
			}
		}
	}
	writeln(sol[n-1][n-1]);
	writeln(trail);
	return 0;
}

import std.stdio;
import core.stdc.stdio;
import std.algorithm;
import std.string;

int main()
{
	int n;
	scanf("%d", &n);
	int[][] m; m.length = n;
	foreach (int i; 0..n)
	{
		m[i].length = n;
		foreach (int j; 0..n)
		{
			scanf("%d", &m[i][j]);
		}
	}

	int[][] ml; ml.length = n;
	foreach (int i; 0..n)
	{
		ml[i].length = n;
		foreach (int j; 0..n)
		{
			int n10 = m[i][j] % 10 == 0 ? m[i][j] / 10 : 0;
			int n5 = n10 == 0 && m[i][j] % 5 == 0 ? m[i][j] / 5 : 0;
			int n2 = n10 == 0 && m[i][j] % 2 == 0 ? m[i][j] / 2 : 0;
			ml[i][j] = n10 * 3 + n5 + n2;
		}
	}

	foreach (int i; 0..n)
	{
		foreach (int j; 0..n)
		{
			int a = i - 1 >= 0 ? ml[i-1][j] : 0;
			int b = j - 1 >= 0 ? ml[i][j-1] : 0;
			ml[i][j] += min(a, b);
		}
	}

	int i = n-1; int j = n-1;
	int total = 0;
	string sol = "";
	while (i > 0 || j > 0)
	{
		int nz = m[i][j] % 10 == 0 ? m[i][j] / 10 : 0;
		total += nz;
		int ni = i - 1 >= 0 ? m[i - 1][j] : int.max;
		int nj = j - 1 >= 0 ? m[i][j - 1] : int.max;
		if (ni < nj)
		{
			sol ~= "D";
			i--;
		} else {
			sol ~= "R";
			j--;
		}
	}
	char[] ns = sol.dup;
	ns.reverse();
	printf("%d\n%s", total, toStringz(ns));
	return 0;
}

import std.stdio;
import core.stdc.stdio;
import std.algorithm;
import std.string;

int main()
{
	int n;
	scanf("%d", &n);
	int[][] m; m.length = n;
	foreach (int i; 0..n)
	{
		m[i].length = n;
		foreach (int j; 0..n)
		{
			scanf("%d", &m[i][j]);
		}
	}

	int[][] ml; ml.length = n;
	foreach (int i; 0..n)
	{
		ml[i].length = n;
		foreach (int j; 0..n)
		{
			int n10 = m[i][j] % 10 == 0 ? m[i][j] / 10 : 0;
			int n5 = n10 == 0 && m[i][j] % 5 == 0 ? m[i][j] / 5 : 0;
			int n2 = n10 == 0 && m[i][j] % 2 == 0 ? m[i][j] / 2 : 0;
			ml[i][j] = n10 * 3 + n5 + n2;
		}
	}

	foreach (int i; 0..n)
	{
		foreach (int j; 0..n)
		{
			int a = i - 1 >= 0 ? ml[i-1][j] : 0;
			int b = j - 1 >= 0 ? ml[i][j-1] : 0;
			ml[i][j] += min(a, b);
		}
	}

	int i = n-1; int j = n-1;
	int total = 0;
	string sol = "";
	int mnwz = 1;
	while (i > 0 || j > 0)
	{
		mnwz *= m[i][j];
		int nz = 0;
		while (mnwz % 10 == 0)
		{
			nz++; mnwz /= 10;
		}
		total += nz;
		int ni = i - 1 >= 0 ? m[i - 1][j] : int.max;
		int nj = j - 1 >= 0 ? m[i][j - 1] : int.max;
		if (ni < nj)
		{
			sol ~= "D";
			i--;
		} else {
			sol ~= "R";
			j--;
		}
	}
	char[] ns = sol.dup;
	ns.reverse();
	printf("%d\n%s", total, toStringz(ns));
	return 0;
}

import std.stdio;
import core.stdc.stdio;
import std.conv;
import std.algorithm;

void printmat(int[][] mat)
{
	for (int i = 0; i < mat.length; i++)
	for (int j = 0; j < mat[i].length; j++)
	{
		write(to!string(mat[i][j]) ~ " ");
		if (j == mat[i].length - 1)
			writeln();
	}
}

int getpow5(int num)
{
	int count = 0;
	int n = num;
	while ((n % 5) == 0)
	{
		count++;
		n /= 5;
	}
	return count;
}

int getpow2(int num)
{
	int count = 0;
	int n = num;
	while ((n & 1) == 0)
	{
		count++;
		n >>= 1;
	}
	return count;
}

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int[][] mat; mat.length = n;
	for (int i = 0; i < n; i++)
	{
		mat[i].length = n;
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &mat[i][j]);
		}
	}
	//printmat(mat);

	int[][] num5; num5.length = n;
	num5[0].length = n;
	num5[0][0] = getpow5(mat[0][0]);
	for (int j = 1; j < n; j++)
	{
		num5[0][j] = getpow5(mat[0][j]);
		num5[0][j] += num5[0][j-1];
	}

	for (int i = 1; i < n; i++)
	{
		num5[i].length = n;
		num5[i][0] = getpow5(mat[i][0]);
		num5[i][0] += num5[i-1][0];
		for (int j = 1; j < n; j++)
		{
			num5[i][j] = getpow5(mat[i][j]);
			num5[i][j] += min(num5[i-1][j], num5[i][j-1]);
		}
	}
	//printmat(num5);

	int[][] num2; num2.length = n;
	num2[0].length = n;
	num2[0][0] = getpow2(mat[0][0]);
	for (int j = 1; j < n; j++)
	{
		num2[0][j] = getpow2(mat[0][j]);
		num2[0][j] += num2[0][j-1];
	}

	for (int i = 1; i < n; i++)
	{
		num2[i].length = n;
		num2[i][0] = getpow2(mat[i][0]);
		num2[i][0] += num2[i-1][0];
		for (int j = 1; j < n; j++)
		{
			num2[i][j] = getpow2(mat[i][j]);
			num2[i][j] += min(num2[i-1][j], num2[i][j-1]);
		}
	}
	//printmat(num2);
	//writeln();

	int[][] sol = (num2[n-1][n-1] < num5[n-1][n-1] ? num2 : num5);
	int cx = n - 1;
	int cy = n - 1;
	string trail = "";
	for (int i = 0; i < n; i++)
	{
		if (cx == 0 && cy != 0)
		{
			trail ~= "R";
			cy--;
		} else
		if (cx != 0 && cy == 0)
		{
			trail ~= "D";
			cx--;
		} else {
			int t = sol[cx-1][cy];
			int l = sol[cx][cy-1];
			if (t < l)
			{
				trail ~= "D";
				cx--;
			} else {
				trail ~= "R";
				cy--;
			}
		}
	}
	writeln(sol[n-1][n-1]);
	char[] mt = trail.dup;
	mt.reverse();
	writeln(mt);
	return 0;
}

import std.stdio;
import core.stdc.stdio;
import std.conv;
import std.algorithm;

void printmat(int[][] mat)
{
	for (int i = 0; i < mat.length; i++)
	for (int j = 0; j < mat[i].length; j++)
	{
		write(to!string(mat[i][j]) ~ " ");
		if (j == mat[i].length - 1)
			writeln();
	}
}

int getpow5(int num)
{
	if (num == 0)
		return 0;
	int count = 0;
	int n = num;
	while ((n % 5) == 0)
	{
		count++;
		n /= 5;
	}
	return count;
}

int getpow2(int num)
{
	if (num == 0)
		return 0;
	int count = 0;
	int n = num;
	while ((n & 1) == 0)
	{
		count++;
		n >>= 1;
	}
	return count;
}

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int[][] mat; mat.length = n;
	bool hasZero = false;
	int zx = -1;
	int zy = -1;
	for (int i = 0; i < n; i++)
	{
		mat[i].length = n;
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &mat[i][j]);
			if (mat[i][j] == 0)
			{
				hasZero = true;
				zx = i;
				zy = j;
			}
		}
	}
	//printmat(mat);

	int[][] num5; num5.length = n;
	num5[0].length = n;
	num5[0][0] = getpow5(mat[0][0]);
	for (int j = 1; j < n; j++)
	{
		num5[0][j] = getpow5(mat[0][j]);
		num5[0][j] += num5[0][j-1];
	}

	for (int i = 1; i < n; i++)
	{
		num5[i].length = n;
		num5[i][0] = getpow5(mat[i][0]);
		num5[i][0] += num5[i-1][0];
		for (int j = 1; j < n; j++)
		{
			num5[i][j] = getpow5(mat[i][j]);
			num5[i][j] += min(num5[i-1][j], num5[i][j-1]);
		}
	}
	//printmat(num5);

	int[][] num2; num2.length = n;
	num2[0].length = n;
	num2[0][0] = getpow2(mat[0][0]);
	for (int j = 1; j < n; j++)
	{
		num2[0][j] = getpow2(mat[0][j]);
		num2[0][j] += num2[0][j-1];
	}

	for (int i = 1; i < n; i++)
	{
		num2[i].length = n;
		num2[i][0] = getpow2(mat[i][0]);
		num2[i][0] += num2[i-1][0];
		for (int j = 1; j < n; j++)
		{
			num2[i][j] = getpow2(mat[i][j]);
			num2[i][j] += min(num2[i-1][j], num2[i][j-1]);
		}
	}
	//printmat(num2);
	//writeln();

	int[][] sol = (num2[n-1][n-1] < num5[n-1][n-1] ? num2 : num5);
	if (hasZero && sol[n-1][n-1] >= 1)
	{
		writeln("1");
		for (int i = 0; i < zx; i++)
		{
			write("D");
		}
		for (int i = 0; i < zy; i++)
		{
			write("R");
		}
		for (int i = zx; i < n; i++)
		{
			write("D");
		}
		for (int i = zy; i < n; i++)
		{
			write("R");
		}
		writeln();
	} else {
		int cx = n - 1;
		int cy = n - 1;
		int st = 2*(n-1);
		char[] trail = new char[st];
		for (int i = 0; i < st; i++)
		{
			if (cx == 0 && cy != 0)
			{
				trail[st - i - 1] = 'R';
				cy--;
			} else
			if (cx != 0 && cy == 0)
			{
				trail[st - i - 1] = 'D';
				cx--;
			} else {
				int t = sol[cx-1][cy];
				int l = sol[cx][cy-1];
				if (t < l)
				{
					trail[st - i - 1] = 'D';
					cx--;
				} else {
					trail[st - i - 1] = 'R';
					cy--;
				}
			}
		}
		writeln(sol[n-1][n-1]);
		writeln(trail);
	}
	return 0;
}


In this problem we consider Boolean functions of four variables A, B, C, D. Variables A, B, C and D are logical and can take values 0 or 1. We will define a function using the following grammar:<expression> ::= <variable> | (<expression>) <operator> (<expression>)<variable> ::= 'A' | 'B' | 'C' | 'D' | 'a' | 'b' | 'c' | 'd'<operator> ::= '&' | '|'Here large letters A, B, C, D represent variables, and small letters represent their negations. For example, if A = 1, then character 'A' corresponds to value 1, and value character 'a' corresponds to value 0. Here character '&' corresponds to the operation of logical AND, character '|' corresponds to the operation of logical OR.You are given expression s, defining function f, where some operations and variables are missing. Also you know the values of the function f(A, B, C, D) for some n distinct sets of variable values. Count the number of ways to restore the elements that are missing in the expression so that the resulting expression corresponded to the given information about function f in the given variable sets. As the value of the result can be rather large, print its remainder modulo 109 + 7.

The first line contains expression s (1 ≤ |s| ≤ 500), where some characters of the operators and/or variables are replaced by character '?'.  The second line contains number n (0 ≤ n ≤ 24) — the number of integers sets for which we know the value of function f(A, B, C, D). Next n lines contain the descriptions of the sets: the i-th of them contains five integers ai, bi, ci, di, ei (0 ≤ ai, bi, ci, di, ei ≤ 1), separated by spaces and meaning that f(ai, bi, ci, di) = ei.  It is guaranteed that all the tuples (ai, bi, ci, di) are distinct.

In a single line print the answer to the problem.

NoteIn the first sample the two valid expressions are 'C' and 'd'.In the second sample the expressions look as follows: '(A)&(a)', '(A)&(b)', '(A)&(C)', '(A)&(D)'.

import std.stdio, std.conv;
import std.algorithm, std.range, std.random;
import std.string, std.array, std.container, std.bigint;

immutable S = 4;
immutable N = 1<<S;
alias P = long[1<<N];
immutable MD = 10^^9+7;
P[4][2] p;
P pq;

struct Parser {
	string s;
	int co;
	this(string ss) {
		s = ss; co = 0;
	}
	char front() {
		return s[co];
	}
	char pop() {
		return s[co++];
	}
}

P val(ref Parser s) {
	char c = s.pop();
	if ('a' <= c && c <= 'd') {
		return p[0][cast(int)(c-'a')];
	} else if ('A' <= c && c <= 'D') {
		return p[1][cast(int)(c-'A')];
	}
	return pq;
}

P zeta(P d) {
	foreach (i; 0..N) {
		foreach (j; 0..(1<<N)) {
			if (j & (1<<i)) {
				d[j] += d[j ^ (1<<i)];
				d[j] %= MD;
			}
		}
	}
	return d;
}

P numor(P p1, P p2) {
	p1 = zeta(p1); p2 = zeta(p2);
	P r;
	foreach (i; 0..(1<<N)) {
		r[i] = p1[i]*p2[i] % MD;
	}

	foreach (i; 0..N) {
		foreach (j; 0..(1<<N)) {
			if (j & (1<<i)) {
				r[j] -= r[j ^ (1<<i)];
				r[j] %= MD;
			}
		}
	}
	return r;
}

P nummul(P p1, P p2) {
	foreach (i; 0..(1<<(N-1))) {
		swap(p1[i], p1[((1<<N)-1) ^ i]);
		swap(p2[i], p2[((1<<N)-1) ^ i]);
	}
	P r = numor(p1, p2);
	foreach (i; 0..(1<<(N-1))) {
		swap(r[i], r[((1<<N)-1) ^ i]);
	}
	return r;
}

P expr(ref Parser s) {
	char c = s.front();
	if (c == '(') {
		s.pop();
		P p1 = expr(s);
		s.pop();
		char op = s.pop();
		s.pop();
		P p2 = expr(s);
		s.pop();
		P or = numor(p1, p2);
		P mul = nummul(p1, p2);
		if (op == '|') return or;
		if (op == '&') return mul;
		P r;
		foreach (i; 0..(1<<N)) {
			r[i] = or[i]+mul[i];
			r[i] %= MD;
		}
		return r;
	} else {
		return val(s);
	}
}

int main() {
	string s = readln.strip;

	foreach (i; 0..2) {
		foreach (j; 0..S) {
			int m = 0;
			foreach (k; 0..(1<<S)) {
				if (((k >> j) & 1) == i) {
					m |= (1<<k);
				}
			}
			p[i][j][m] = 1;
			pq[m] += 1;
		}
	}

	int n = readln.strip.to!int;
	int[2][] l = new int[2][](n);
	foreach (i; 0..n) {
		auto inp = readln.split.map!(to!int).array;
		int m = 0;
		foreach (k; 0..4) {
			if (inp[k]) {
				m |= (1<<k);
			}
		}
		l[i] = [m, inp[4]];
	}
	Parser pr = Parser(s);
	P p = expr(pr);
	long sm = 0;
	foreach (i; 0..(1<<N)) {
		bool f = true;
		foreach (j; 0..n) {
			if (((i>>l[j][0]) & 1) != l[j][1]) {
				f = false;
				break;
			}
		}
		if (!f) continue;
		sm += p[i];
		sm %= MD;
	}
	sm = (sm + MD) % MD;
	writeln(sm);
	return 0;
}


There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.    Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).

The first line of the input contains integers n and k (1 ≤ n ≤ 1.5·105, 1 ≤ k ≤ n) — the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 100), where hi is the height of the i-th plank of the fence.

Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.

NoteIn the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.

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

void main() {
    int n, k; readf("%d %d\n", &n, &k);
    int[] xs = stdin.readln.chomp.split(" ").map!(to!int).array;
    int[] ss = new int[xs.length+1];
    ss[0] = 0;
    for (int i = 0; i < xs.length; i++) {
        ss[i+1] = ss[i] + xs[i];
    }
    int ans = int.max;
    int index = -1;
    for (int i = 0; i <= xs.length - k; i++) {
        if (ans > ss[i+k] - ss[i]) {
            ans = ss[i+k] - ss[i];
            index = i + 1;
        }
    }
    index.writeln;
}

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto H = readln.split.map!(to!int).array;

    auto acm = new int[](N+1);
    foreach (i; 0..N) {
        acm[i+1] = acm[i] + H[i];
    }

    int minv = int.max;
    int mini = -1;

    foreach (i; 0..N-K+1) {
        auto a = acm[i+K] - acm[i];
        if (a < minv) {
            minv = a;
            mini = i+1;
        }
    }
    mini.writeln;
}

import std.stdio
    , std.typecons
    , std.traits
    , std.algorithm
    , std.range
    , std.container;

void main() {
    uint n, k;
    readf("%s %s\n", &n, &k);

    auto fence = new uint[n];
    auto sum_arr = new uint[n+1];

    uint f_sum;

    foreach(i, ref f; fence) {
        //uint f;
        readf("%s ", &f);
        sum_arr[i+1] = f_sum + f;
        f_sum += f;
    }

    uint min = sum_arr[k] - sum_arr[0], min_idx = 1;

    for(uint j = k; j < n+1; ++j) {
        if(sum_arr[j] - sum_arr[j - k] < min) {
            min = sum_arr[j] - sum_arr[j - k];
            min_idx = j - k + 1;
        }
    }

    min_idx.writeln;
    //foreach()
}

alias id(alias token) = token;

alias var(alias elem) = std.traits.Unqual!(typeof(elem));

auto mut(T)(auto ref T in_val) {
    std.traits.Unqual!(T) rv = in_val;
    return rv;
}

auto imut(T)(auto ref T in_val) {
    immutable immrv = in_val;
    return immrv;
}

import std.stdio
    , std.typecons
    , std.traits
    , std.algorithm
    , std.range
    , std.container;

void main() {
    uint n, k;
    readf("%s %s\n", &n, &k);

    //auto fence = new uint[n];
    auto sum_arr = new uint[n+1];

    uint f_sum;

    foreach(i; 0..n) {
        uint f;
        readf("%s ", &f);
        sum_arr[i+1] = f_sum + f;
        f_sum += f;
    }

    uint min = sum_arr[k] - sum_arr[0], min_idx = 1;

    for(uint j = k; j < n+1; ++j) {
        if(sum_arr[j] - sum_arr[j - k] < min) {
            min = sum_arr[j] - sum_arr[j - k];
            min_idx = j - k + 1;
        }
    }

    min_idx.writeln;
    //foreach()
}

alias id(alias token) = token;

alias var(alias elem) = std.traits.Unqual!(typeof(elem));

auto mut(T)(auto ref T in_val) {
    std.traits.Unqual!(T) rv = in_val;
    return rv;
}

auto imut(T)(auto ref T in_val) {
    immutable immrv = in_val;
    return immrv;
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n,k;
    readf!"%d %d"(n,k);
    readln;
    auto a=readln.splitter
                 .map!(to!int)
                 .cumulativeFold!"a+b"
                 .array;
    int m=int.max,r;
    foreach(i;k-1..n){
        int cur=a[i];
        if(i>=k) cur-=a[i-k];
        if(cur<m){
            m=cur;
            r=i;
        }
    }
    writeln(r-k+2);
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n,k;
    readf!"%d %d"(n,k);
    readln;
    auto a=readln.splitter
                 .map!(to!int)
                 .array;
    foreach(i;1..n)
        a[i]+=a[i-1];
    int m=int.max,r;
    foreach(i;k-1..n){
        int cur=a[i];
        if(i>=k) cur-=a[i-k];
        if(cur<m){
            m=cur;
            r=i;
        }
    }
    writeln(r-k+2);
}

import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;

immutable int mod = 10^^9 + 7;

void main()
{
    int n, k;
    readVars(n, k);
    auto h = readln.split.to!(int[]);

    int j;
    int maxsum = h[0 .. k].sum;
    int v = maxsum;

    foreach(r ; k .. n){
        v += h[r] - h[r - k];

        if (maxsum > v) {
            maxsum = v;
            j = r - k + 1;
        }
    }

    writeln(j + 1);
}

void readVars(T...)(auto ref T args)
{
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        writeln(line);
        throw new Exception("args num < input num");
    }
}

import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;

immutable int mod = 10^^9 + 7;

void main()
{
    int n, k;
    readVars(n, k);
    auto h = readln.split.to!(int[]);
    auto hps = new int[](n + 1);

    foreach(i ; 0 .. n) hps[i + 1] += hps[i] + h[i];

    debug {
        writeln(hps);
    }

    int minsum = mod, j;

    foreach(i ; 0 .. n - k + 1) {
        if (minsum > hps[i + k] - hps[i]) {
            minsum = hps[i + k] - hps[i];
            j = i;
        }
    }

    writeln(j + 1);
}

void readVars(T...)(auto ref T args)
{
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        writeln(line);
        throw new Exception("args num < input num");
    }
}

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

void main() {
    int n, k; readf("%d %d\n", &n, &k);
    int[] xs = stdin.readln.chomp.split(" ").map!(to!int).array;
    int[] ss = new int[xs.length+1];
    ss[0] = 0;
    for (int i = 0; i < xs.length - 1; i++) {
        ss[i+1] = ss[i] + xs[i];
    }
    int ans = int.max;
    int index = -1;
    for (int i = 0; i < xs.length - k; i++) {
        if (ans > ss[i+k] - ss[i]) {
            ans = ss[i+k] - ss[i];
            index = i + 1;
        }
    }
    index.writeln;
}

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

void main() {
    int n, k; readf("%d %d\n", &n, &k);
    int[] xs = stdin.readln.chomp.split(" ").map!(to!int).array;
    int[] ss = new int[xs.length+1];
    ss[0] = 0;
    for (int i = 0; i < xs.length - 1; i++) {
        ss[i+1] = ss[i] + xs[i];
    }
    int ans = int.max;
    int index = 1;
    for (int i = 0; i < xs.length - k; i++) {
        if (ans > ss[i+k] - ss[i]) {
            ans = ss[i+k] - ss[i];
            index = i + 1;
        }
    }
    index.writeln;
}

import std.stdio
    , std.typecons
    , std.traits
    , std.algorithm
    , std.range
    , std.container;

void main() {
    uint n, k;
    readf("%s %s\n", &n, &k);

    //auto fence = new uint[n];
    auto sum_arr = new uint[n+2];

    uint f_sum;

    foreach(i; 0..n) {
        uint f;
        readf("%s ", &f);
        sum_arr[i+1] = f_sum + f;
        f_sum += f;
    }

    uint min = sum_arr[k] - sum_arr[0], min_idx = k+1;

    for(uint j = k; j < n+1; ++j) {
        if(sum_arr[j] - sum_arr[j - k] < min) {
            min = sum_arr[j] - sum_arr[j - k];
            min_idx = j - k + 1;
        }
    }

    min_idx.writeln;
    //foreach()
}

alias id(alias token) = token;

alias var(alias elem) = std.traits.Unqual!(typeof(elem));

auto mut(T)(auto ref T in_val) {
    std.traits.Unqual!(T) rv = in_val;
    return rv;
}

auto imut(T)(auto ref T in_val) {
    immutable immrv = in_val;
    return immrv;
}