Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?

The first line contains a single integer n (1 ≤ n ≤ 105).

If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c. If there are multiple solutions, print any of them.

NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds:  ;  ;  ;  ;  . In Sample 2, you can easily notice that no lucky permutation triple exists.

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

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

    if (n % 2 == 0) {
        writeln(-1);
        return;
    }

    foreach (_; 0 .. 2) {
        n.iota.writefln!("%(%s %)");
    }
    auto lst = (2*n).iota.stride(2).map!(x => x % n);
    lst.writefln!("%(%s %)");
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		if (n % 2 == 0)
		{
			writeln (-1);
		}
		else
		{
			auto a = array (iota (n));
			auto c = a.dup;
			c[] *= 2;
			c[] %= n;
			writefln ("%(%s %)", a);
			writefln ("%(%s %)", a);
			writefln ("%(%s %)", c);
		}
	}
}


You are given an array $$$a$$$ of length $$$n$$$. The array is called 3SUM-closed if for all distinct indices $$$i$$$, $$$j$$$, $$$k$$$, the sum $$$a_i + a_j + a_k$$$ is an element of the array. More formally, $$$a$$$ is 3SUM-closed if for all integers $$$1 \leq i < j < k \leq n$$$, there exists some integer $$$1 \leq l \leq n$$$ such that $$$a_i + a_j + a_k = a_l$$$.Determine if $$$a$$$ is 3SUM-closed.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output "YES" (without quotes) if $$$a$$$ is 3SUM-closed and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).

NoteIn the first test case, there is only one triple where $$$i=1$$$, $$$j=2$$$, $$$k=3$$$. In this case, $$$a_1 + a_2 + a_3 = 0$$$, which is an element of the array ($$$a_2 = 0$$$), so the array is 3SUM-closed.In the second test case, $$$a_1 + a_4 + a_5 = -1$$$, which is not an element of the array. Therefore, the array is not 3SUM-closed.In the third test case, $$$a_i + a_j + a_k = 0$$$ for all distinct $$$i$$$, $$$j$$$, $$$k$$$, and $$$0$$$ is an element of the array, so the array is 3SUM-closed.

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

bool solve (int n, int [] a)
{
	sort (a);
	auto g = a.group;

	a = [];
	foreach (ref x; g)
	{
		foreach (i; 0..min (3, x[1]))
		{
			a ~= x[0];
		}
	}
	if (a.length > 10)
	{
		return false;
	}

	bool [long] b;
	foreach (ref c; a)
	{
		b[c] = true;
	}

	foreach (i; 0..a.length)
	{
		foreach (j; i + 1..a.length)
		{
			foreach (k; j + 1..a.length)
			{
				if (0L + a[i] + a[j] + a[k] !in b)
				{
					return false;
				}
			}
		}
	}
	return true;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (n, a) ? "YES" : "NO");
	}
}

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

void solve()
{
    readln;
    auto arr = readln.splitter.map!(to!int).array;
    auto pos = arr.count!(x => x > 0);
    auto neg = arr.count!(x => x < 0);
    auto z = arr.length - pos - neg;
    if (pos >= 3 || neg >= 3)
    {
        writeln("NO");
        return;
    }
    if (z)
    {
        arr = [0] ~ arr.filter!(x => x != 0).array;
    }
    foreach (i; 0 .. arr.length)
        foreach (j; i + 1 .. arr.length)
            foreach (k; j + 1 .. arr.length)
            {
                auto sum = arr[i] + arr[j] + arr[k];
                if (!arr.canFind(sum))
                {
                    writeln("NO");
                    return;
                }
            }
    writeln("YES");
}

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


Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha.

The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space.

In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order.

NoteThe XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal — as "xor".

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto d = new int [n];
		auto s = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s %s", &d[i], &s[i]);
		}

		int [] q;
		foreach (i; 0..n)
		{
			if (d[i] == 1)
			{
				q ~= i;
			}
		}

		int [] [] e;
		while (!q.empty)
		{
			int i = q.front;
			q.popFront ();
			if (d[i] != 1)
			{
				continue;
			}
			e ~= [i, s[i]];
			d[i]--;
			d[s[i]]--;
			if (d[s[i]] == 1)
			{
				q ~= s[i];
			}
			s[s[i]] ^= i;
			s[i] ^= s[i];
		}

		writeln (e.length);
		foreach (c; e)
		{
			writeln (c[0], ' ', c[1]);
		}
	}
}

import std.algorithm;
import std.array;
import std.stdio;
import std.typecons;
immutable int N = 2 ^^ 16 + 8;
int[N] deg, s;
int n;
int[] d1;
Tuple!(int, int)[] edges;
void main() {
	scanf("%d", &n);
	foreach (i; 0..n) {
		scanf("%d%d", &deg[i], &s[i]);
		if (deg[i] == 1) d1 ~= i;
	}
	while (!d1.empty()) {
		int i = d1.back(); // this and next are UFCS for std.array
		d1.popBack();
		if (deg[i] == 0) continue;
		assert(deg[i] == 1);
		deg[i] = 0;
		int j = s[i];
		deg[j] -= 1;
		if (deg[j] == 1) d1 ~= j;
		s[j] ^= i;
		edges ~= tuple(i, j);
	}
	writefln("%d", edges.length);
	foreach (t; edges) {
		writefln("%d %d", t[0], t[1]);
	}
}

import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

struct P {
    int v;
    int d, s;
}
struct E {
    int from, to;
}

void main() {
    int N; scanf("%d\n", &N);
    BinaryHeap!(Array!P, "a.d > b.d") PQ;
    auto ps = new P[N];
    foreach (i; 0 .. N) {
        int d, s; scanf("%d %d\n", &d, &s);
        int v = i;
        ps[i] = P(v, d, s);
        PQ.insert(P(v, d, s));
    }
    auto done = new bool[N];
    E[] ans;
    while (!PQ.empty) {
        auto p = PQ.front; PQ.removeFront;
        if (done[p.v]) continue;
        done[p.v] = true;
        if (p.d == 0) continue;
        assert(p.d == 1);
        ans ~= E(p.v, p.s);
        ps[ p.s ].d--;
        ps[ p.s ].s = ps[ p.s ].s ^ p.v;
        PQ.insert(ps[ p.s ]);
        done[p.v] = true;
    }
    writeln(ans.length);
    foreach (a; ans) {
        writefln("%d %d", a.from, a.to);
    }
}

// 1. start with a full forest
// 2. take any vertex with the current degree of 1 = V
// 3. its "s" value is the adjacent vertex number = S
// 4. add an edge (V, S)
// 5. remove V from the forest, decrease S degree, XOR s value of S with V
// 6. if vertices with non-zero degree have remained, then goto 2

import std.stdio;
import std.algorithm;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int main(string[] args)
{
    auto n = read!int;
    int[2][0x10000] desc;
    int[0x10000] queue; // vertices with "current" degree of 1
    int queue_n = 0;
    foreach (i; 0..n)
    {
        auto d = read!int;
        auto s = read!int;

        if (d == 1)
            queue[queue_n++] = i;

        desc[i][0] = d;
        desc[i][1] = s;
    }

    int m = 0;
    int[2][0x10000] ans;
    foreach (i; 0..n)
    {
        debug writeln("queue = ", queue[0..queue_n]);

        if (queue_n == 0)
            break;

        auto p = queue[--queue_n];

        if (desc[p][0] == 0)
            continue;

        auto j = desc[p][1];

        ans[m][0] = p;
        ans[m][1] = j;
        m++;
        desc[j][0]--;
        desc[j][1] ^= p;

        if (desc[j][0] == 1)
            queue[queue_n++] = j;
    }

    writeln(m);
    foreach (i; 0..m)
    {
        writefln("%s %s", ans[i][0], ans[i][1]);
    }

    return 0;
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto d = new int [n];
		auto s = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s %s", &d[i], &s[i]);
		}

		int [] q;
		foreach (i; 0..n)
		{
			if (d[i] == 1)
			{
				q ~= i;
			}
		}

		int [] [] e;
		while (!q.empty)
		{
			int i = q.front;
			q.popFront ();
			if (d[i] != 1)
			{
				continue;
			}
			e ~= [i, s[i]];
			d[i]--;
			d[s[i]]--;
			s[s[i]] ^= i;
			s[i] ^= s[i];
		}

		writeln (e.length);
		foreach (c; e)
		{
			writeln (c[0], ' ', c[1]);
		}
	}
}

import std.algorithm;
import std.array;
import std.stdio;
import std.typecons;
immutable int N = 2 ^^ 16 + 8;
int[N] deg, s;
int n;
int[] d1;
Tuple!(int, int)[] edges;
void main() {
	scanf("%d", &n);
	foreach (i; 0..n) {
		scanf("%d%d", &deg[i], &s[i]);
		if (deg[i] == 1) d1 ~= i;
	}
	while (!d1.empty()) {
		int i = d1.back(); // this and next are UFCS for std.array
		d1.popBack();
		if (deg[i] == 0) continue;
		assert(deg[i] == 1);
		deg[i] = 0;
		int j = s[i];
		deg[j] -= 1;
		s[j] ^= i;
		edges ~= tuple(i, j);
	}
	writefln("%d", edges.length);
	foreach (t; edges) {
		writefln("%d %d", t[0], t[1]);
	}
}

import std.stdio;
import std.algorithm;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int main(string[] args)
{
    auto n = read!int;
    int[3][0x10000] desc;
    foreach (i; 0..n)
    {
        desc[i][0] = read!int;
        desc[i][1] = read!int;
        desc[i][2] = i;
    }

    auto sorted = sort!"a[0] < b[0]"(desc[0..n].dup);
    debug writeln("sorted = ", sorted);

    int m = 0;
    Tuple!(int, int)[0x10000] ans;
    foreach (i; 0..n)
    {
        auto p = sorted[i][2];

        if (desc[p][0] == 0)
            continue;

        debug writeln("i = ", i);
        debug write("desc = ", desc[0..n], " -> ");
        assert(sorted[i][0] == 1);

        auto j = desc[p][1];
        ans[m][0] = p;
        ans[m][1] = j;
        m++;
        desc[j][0]--;
        desc[j][1] ^= p;

        debug writeln(desc[0..n]);
    }

    writeln(m);
    foreach (i; 0..m)
    {
        writefln("%s %s", ans[i][0], ans[i][1]);
    }

    return 0;
}


Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price — their owners are ready to pay Bob if he buys their useless apparatus. Bob can «buy» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn.

The first line contains two space-separated integers n and m (1 ≤ m ≤ n ≤ 100) — amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≤ ai ≤ 1000) — prices of the TV sets.

Output the only number — the maximum sum of money that Bob can earn, given that he can carry at most m TV sets.

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

void main() {
    int n, m; readf("%d %d\n", &n, &m);
    int[] a = stdin.readln.chop.split.map!(to!int).array;
    a.sort;
    int sum = 0;
    for (int i = 0; i < m; i++) {
        if (a[i] < 0) {
            sum += -a[i];
        }
    }
    sum.writeln;
}


You have a large electronic screen which can display up to $$$998244353$$$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $$$7$$$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $$$10$$$ decimal digits:As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $$$1$$$, you have to turn on $$$2$$$ segments of the screen, and if you want to display $$$8$$$, all $$$7$$$ segments of some place to display a digit should be turned on.You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $$$n$$$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $$$n$$$ segments.Your program should be able to process $$$t$$$ different test cases.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the input. Then the test cases follow, each of them is represented by a separate line containing one integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the maximum number of segments that can be turned on in the corresponding testcase. It is guaranteed that the sum of $$$n$$$ over all test cases in the input does not exceed $$$10^5$$$.

For each test case, print the greatest integer that can be displayed by turning on no more than $$$n$$$ segments of the screen. Note that the answer may not fit in the standard $$$32$$$-bit or $$$64$$$-bit integral data type.

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;

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

    while (T--) {
        auto N = readln.chomp.to!int;
        if (N % 2) write(7), N -= 3;
        foreach (i; 0..N/2) write(1);
        writeln;
    }
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  auto t = r.nextA!uint (nt);
  foreach (n; t) {
    int x = n;
    if (x & 1) {
      write ('7');
      x -= 3;
    }
    while (x > 0) {
      write ('1');
      x -= 2;
    }
    writeln;
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new string[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto digit = n / 2;
		//if (digit <= 9)
		{
			string str;
			if (n % 2 == 0)
				str ~= "1";
			else
				str ~= "7";

			foreach (i; 1..digit)
				str ~= "1";
			ans[ti] = str;
		}
		/*else
		{
			auto arr = [9, 9, 8, 2, 4, 4, 3, 5, 3];
			auto cnt = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6];
			auto remain = n - 18;
			bool isLimit = true;
			foreach (i; 0..9)
			{
				auto lim = isLimit ? arr[i] : 9;
				long num;
				foreach_reverse (j; 0..lim+1)
				{
					if (remain >= cnt[j] - 2)
					{
						num = j;
						remain -= cnt[j] - 2;
						break;
					}
				}
				if (num < lim)
					isLimit = false;
				ans[ti] += num * 10^^(8-i);
			}
		}*/
	}

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

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;

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

    while (T--) {
        auto N = readln.chomp.to!int;
        for (int i = N; N > 3; N -= 2) write(1);
        if (N == 2) write(1);
        else write(7);
        writeln;
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		if (n >= 47)
		{
			ans[ti] = 998244353;
			continue;
		}

		auto digit = n / 2;
		if (digit <= 9)
		{
			string str;
			if (n % 2 == 0)
				str ~= "1";
			else
				str ~= "7";

			foreach (i; 1..digit)
				str ~= "1";
			ans[ti] = str.to!long;
		}
		else
		{
			auto arr = [9, 9, 8, 2, 4, 4, 3, 5, 3];
			auto cnt = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6];
			auto remain = n - 18;
			bool isLimit = true;
			foreach (i; 0..9)
			{
				auto lim = isLimit ? arr[i] : 9;
				long num;
				foreach_reverse (j; 0..lim+1)
				{
					if (remain >= cnt[j] - 2)
					{
						num = j;
						remain -= cnt[j] - 2;
						break;
					}
				}
				if (num < lim)
					isLimit = false;
				ans[ti] += num * 10^^(8-i);
			}
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		if (n >= 47)
		{
			ans[ti] = 998244353;
			continue;
		}

		auto digit = n / 2;
		if (digit <= 9)
		{
			string str;
			if (n % 2 == 0)
				str ~= "1";
			else
				str ~= "7";

			foreach (i; 1..digit)
				str ~= "1";
			ans[ti] = str.to!long;
		}
		else
		{
			auto remain = n - 18;
			string str;
			foreach (i; 0..9)
			{
				if (remain >= 4)
				{
					str ~= "9";
					remain -= 4;
				}
				else if (remain >= 1)
				{
					str ~= "7";
					remain -= 1;
				}
				else
					str ~= "1";
			}
			ans[ti] = str.to!long;
		}
	}

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


You are given an integer $$$n$$$. You have to change the minimum number of digits in it in such a way that the resulting number does not have any leading zeroes and is divisible by $$$7$$$.If there are multiple ways to do it, print any of them. If the given number is already divisible by $$$7$$$, leave it unchanged.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 990$$$) — the number of test cases. Then the test cases follow, each test case consists of one line containing one integer $$$n$$$ ($$$10 \le n \le 999$$$).

For each test case, print one integer without any leading zeroes — the result of your changes (i. e. the integer that is divisible by $$$7$$$ and can be obtained by changing the minimum possible number of digits in $$$n$$$). If there are multiple ways to apply changes, print any resulting number. If the given number is already divisible by $$$7$$$, just print it.

NoteIn the first test case of the example, $$$42$$$ is already divisible by $$$7$$$, so there's no need to change it.In the second test case of the example, there are multiple answers — $$$28$$$, $$$21$$$ or $$$63$$$.In the third test case of the example, other possible answers are $$$357$$$, $$$371$$$ and $$$378$$$. Note that you cannot print $$$077$$$ or $$$77$$$.

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        int n;
        scanf("%d", &n);
        getchar();
        auto d = n % 7;
        if (d == 0)
            writeln(n);
        else {
            int m = n % 10;
            if (m >= d)
                writeln(n - d);
            else {
                writeln(n + 7 - (n+7) % 7);
            }
        }
    }
}

// cheese-cracker [2022-01-31]

void solve(){
    long n = scan;
    long ten = (n/10) % 10;
    long d = n % 7;
    long u = 7 - d;
    long down = n - d;
    long up = n + u;
    long tend = (down / 10) % 10;
    long tenu = (up / 10) % 10;
    if(tend == ten){
        writeln(down);
    }else{
        writeln(up);
    }
}

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

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


You are given an array $$$a$$$ of $$$n$$$ integers. Find the number of pairs $$$(i, j)$$$ ($$$1 \le i < j \le n$$$) where the sum of $$$a_i + a_j$$$ is greater than or equal to $$$l$$$ and less than or equal to $$$r$$$ (that is, $$$l \le a_i + a_j \le r$$$).For example, if $$$n = 3$$$, $$$a = [5, 1, 2]$$$, $$$l = 4$$$ and $$$r = 7$$$, then two pairs are suitable:   $$$i=1$$$ and $$$j=2$$$ ($$$4 \le 5 + 1 \le 7$$$);  $$$i=1$$$ and $$$j=3$$$ ($$$4 \le 5 + 2 \le 7$$$).

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains three integers $$$n, l, r$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le l \le r \le 10^9$$$) — the length of the array and the limits on the sum in the pair. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ overall test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output a single integer — the number of index pairs $$$(i, j)$$$ ($$$i < j$$$), such that $$$l \le a_i + a_j \le r$$$.

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

void main() {
    foreach(_; 0 .. to!int(readln.strip)) {
        int n, l, r; readf("%d %d %d\n", n, l, r);
        auto ar = readln.strip.split.map!(to!int).array.sort;
        long ans = 0;
        foreach(e; ar) {
            int up = ar.upperBound(l-e-1).length;
            int dw = ar.upperBound(r-e).length;
            ans += up - dw;
            if(l <= 2*e && 2*e <= r) ans--;
        }
        writeln(ans/2);
    }
}

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

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n, l, r;
    readf!" %d %d %d "(n, l, r);
    auto a = readln.strip.split.map!(to!int).array.sort;
    long result = 0;
    for (int i = 0; i < a.length; i++) {
      int x = a[i];
      int min_x = l - x;
      int max_x = r - x;
      auto chop_l = a.assumeSorted.lowerBound(min_x).length;
      auto chop_r = a.assumeSorted.upperBound(max_x).length;
      if (a.length > chop_l + chop_r) {
        auto count = a.length - chop_l - chop_r;
        if (x >= min_x && x <= max_x)
          count--;
        result += count;
      }
    }
    writeln(result / 2);
  }
}

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

void main() {
    foreach(_; 0 .. to!int(readln.strip)) {
        int n, l, r; readf("%d %d %d\n", n, l, r);
        auto ar = readln.strip.split.map!(to!int).array.sort;
        int ans = 0;
        foreach(e; ar) {
            int up = ar.upperBound(l-e-1).length;
            int dw = ar.upperBound(r-e).length;
            ans += up - dw;
            if(l <= 2*e && 2*e <= r) ans--;
        }
        writeln(ans/2);
    }
}


You are given $$$n$$$ non-decreasing arrays of non-negative numbers. Vasya repeats the following operation $$$k$$$ times:   Selects a non-empty array.  Puts the first element of the selected array in his pocket.  Removes the first element from the selected array. Vasya wants to maximize the sum of the elements in his pocket.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 3\,000$$$): the number of arrays and operations. Each of the next $$$n$$$ lines contain an array. The first integer in each line is $$$t_i$$$ ($$$1 \le t_i \le 10^6$$$): the size of the $$$i$$$-th array. The following $$$t_i$$$ integers $$$a_{i, j}$$$ ($$$0 \le a_{i, 1} \le \ldots \le a_{i, t_i} \le 10^8$$$) are the elements of the $$$i$$$-th array. It is guaranteed that $$$k \le \sum\limits_{i=1}^n t_i \le 10^6$$$.

Print one integer: the maximum possible sum of all elements in Vasya's pocket after $$$k$$$ operations.

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

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

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

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

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

      auto ASum = new long[][N];
      foreach (i; 0 .. N) {
        ASum[i] = new long[T[i] + 1];
        foreach (j; 0 .. T[i]) {
          ASum[i][j + 1] = ASum[i][j] + A[i][j];
        }
      }

      long ans;
      void solve(int l, int r, long[] dp) {
        if (r - l == 1) {
          foreach (x; 0 .. K + 1) {
            if (K - x <= T[l]) {
              chmax(ans, dp[x] + ASum[l][K - x]);
            }
          }
        } else {
          const mid = (l + r) / 2;
          auto dpL = dp.dup;
          foreach (i; mid .. r) {
            foreach_reverse (x; T[i] .. K + 1) {
              chmax(dpL[x], dpL[x - T[i]] + ASum[i][T[i]]);
            }
          }
          solve(l, mid, dpL);
          auto dpR = dp.dup;
          foreach (i; l .. mid) {
            foreach_reverse (x; T[i] .. K + 1) {
              chmax(dpR[x], dpR[x - T[i]] + ASum[i][T[i]]);
            }
          }
          solve(mid, r, dpR);
        }
      }

      solve(0, N, new long[K + 1]);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Let $$$f_{x} = c^{2x-6} \cdot f_{x-1} \cdot f_{x-2} \cdot f_{x-3}$$$ for $$$x \ge 4$$$.You have given integers $$$n$$$, $$$f_{1}$$$, $$$f_{2}$$$, $$$f_{3}$$$, and $$$c$$$. Find $$$f_{n} \bmod (10^{9}+7)$$$.

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

struct IntM {
  enum q = 1_000_000_007;
  int v;
  this (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
  }
  IntM opAssign (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
    return this;
  }
  IntM opUnary (string op : "-")() const pure nothrow @nogc {
    return IntM ((q - v) % q);
  }
  ref IntM opUnary (string op : "++")() pure nothrow @nogc {
    if (++v >= q) {
      v -= q;
    }
    return this;
  }
  ref IntM opUnary (string op : "--")() pure nothrow @nogc {
    if (--v < 0) {
      v += q;
    }
    return this;
  }
  ref IntM opOpAssign (string op : "+")(in IntM rhs) pure nothrow @nogc {
    v += rhs.v;
    v %= q;
    return this;
  }
  ref IntM opOpAssign (string op : "-")(in IntM rhs) pure nothrow @nogc {
    v -= rhs.v;
    v %= q;
    return this;
  }
  ref IntM opOpAssign (string op : "*")(in IntM rhs) pure nothrow @nogc {
    v = ((v.to!(long)) * rhs.v.to!(long)) % q;
    return this;
  }
  IntM opBinary (string op : "+")(in IntM rhs) const pure nothrow @nogc {
    return IntM ( (v + rhs.v) % q);
  }
  IntM opBinary (string op : "-")(in IntM rhs) const pure nothrow @nogc {
    return IntM ( (v - rhs.v) % q);
  }
  IntM opBinary (string op : "*")(in IntM rhs) const pure nothrow @nogc {
    return IntM (((v.to!(long)) * rhs.v.to!(long)) % q);
  }
  IntM opBinary (string op : "^^")(in long rhs) const pure nothrow @nogc {
    IntM a = 1, b = this;
    long p = rhs;
    while (p > 0) {
      //a * (b ^ p) == x ^ rhs
      if (p & 1) {
        a *= b;
      }
      b *= b;
      p >>>= 1;
    }
    return a;
  }
  IntM opBinary (string op)(in int v) const pure nothrow @nogc if (op == "+" || op == "-" || op == "*") {
    mixin ("return this " ~ op ~ " IntM(v);");
  }
  int opCast(T : int)() const pure nothrow @nogc { return v; }
  int opCmp (const IntM rhs) const pure nothrow @nogc {
    if (v < rhs.v) {
      return -1;
    }
    if (v > rhs.v) {
      return 1;
    }
    return 0;
  }
  bool opEquals (const IntM rhs) const pure nothrow @nogc { return v == rhs.v; }
  string toString() const pure nothrow { return ((v < 0) ? v + q : v).text; }
  //a ^ x = this (mod q)
  int discreteLogarithm (in IntM a) const {
    enum int n = ceil (sqrt (q.to!double)).to!int;
    IntM an = a ^^ n;
    int[int] h;
    IntM cur = an;
    foreach (p; 1 .. n + 1) {
      auto ptr = cur.v !in h;
      if (cur.v !in h) {
        h[cur.v] = p;
      }
      cur *= an;
    }
    cur = this;
    int res = int.max;
    foreach (q; 0 .. n + 1) {
      auto ptr = cur.v in h;
      if (ptr) {
        res = min (res, *ptr * n - q);
      }
      cur *= a;
    }
    return res != int.max ? res : -1;
  }
}

struct IntM2 {
  enum q = 1_000_000_006;
  int v;
  this (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
  }
  IntM2 opAssign (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
    return this;
  }
  IntM2 opUnary (string op : "-")() const pure nothrow @nogc {
    return IntM2 ((q - v) % q);
  }
  ref IntM2 opUnary (string op : "++")() pure nothrow @nogc {
    if (++v >= q) {
      v -= q;
    }
    return this;
  }
  ref IntM2 opUnary (string op : "--")() pure nothrow @nogc {
    if (--v < 0) {
      v += q;
    }
    return this;
  }
  ref IntM2 opOpAssign (string op : "+")(in IntM2 rhs) pure nothrow @nogc {
    v += rhs.v;
    v %= q;
    return this;
  }
  ref IntM2 opOpAssign (string op : "-")(in IntM2 rhs) pure nothrow @nogc {
    v -= rhs.v;
    v %= q;
    return this;
  }
  ref IntM2 opOpAssign (string op : "*")(in IntM2 rhs) pure nothrow @nogc {
    v = ((v.to!(long)) * rhs.v.to!(long)) % q;
    return this;
  }
  IntM2 opBinary (string op : "+")(in IntM2 rhs) const pure nothrow @nogc {
    return IntM2 ( (v + rhs.v) % q);
  }
  IntM2 opBinary (string op : "-")(in IntM2 rhs) const pure nothrow @nogc {
    return IntM2 ( (v - rhs.v) % q);
  }
  IntM2 opBinary (string op : "*")(in IntM2 rhs) const pure nothrow @nogc {
    return IntM2 (((v.to!(long)) * rhs.v.to!(long)) % q);
  }
  IntM2 opBinary (string op : "^^")(in long rhs) const pure nothrow @nogc {
    IntM2 a = 1, b = this;
    long p = rhs;
    while (p > 0) {
      //a * (b ^ p) == x ^ rhs
      if (p & 1) {
        a *= b;
      }
      b *= b;
      p >>>= 1;
    }
    return a;
  }
  IntM2 opBinary (string op)(in int v) const pure nothrow @nogc if (op == "+" || op == "-" || op == "*") {
    mixin ("return this " ~ op ~ " IntM2(v);");
  }
  int opCast(T : int)() const pure nothrow @nogc { return v; }
  int opCmp (const IntM2 rhs) const pure nothrow @nogc {
    if (v < rhs.v) {
      return -1;
    }
    if (v > rhs.v) {
      return 1;
    }
    return 0;
  }
  bool opEquals (const IntM2 rhs) const pure nothrow @nogc { return v == rhs.v; }
  string toString() const pure nothrow { return ((v < 0) ? v + q : v).text; }
}

alias Matrix = IntM2[3][3];
Matrix mul (in Matrix a, in Matrix b) {
  Matrix c;
  foreach (i; 0 .. 3) foreach (j; 0 .. 3) foreach (k; 0 .. 3) {
    c[i][j] += a[i][k] * b[k][j];
  }
  return c;
}

Matrix power (Matrix x, long rhs) {
  Matrix a;
  foreach (i; 0 .. 3) foreach (j; 0 .. 3) a[i][j] = IntM2 (i == j ? 1 : 0);
  Matrix b;
  foreach (i; 0 .. 3) b[i] = x[i].dup;
  long p = rhs;
  while (p > 0) {
    //a * (b ^ p) == x ^ rhs
    if (p & 1) {
      a = mul (a, b);
    }
    b = mul (b, b);
    p >>>= 1;
  }
  return a;
}

void main() {
  long n;
  int[4] f;
  int[4] h;
  int c;
  readf! " %d %d %d %d %d" (n, f[1], f[2], f[3], c);
  IntM[4] g;
  foreach (i; 1 .. 4) {
    g[i] = IntM (f[i]);
    foreach (j; 0 .. i) {
      g[i] *= IntM (c);
    }
    h[i] = g[i].to!int;
  }
  foreach (p; [2, 3, 5, 7, 11, 13, 17, 23, 27, 31]) {
    bool ok = true;
    foreach (i; 1 .. 4) {
      f[i] = IntM(h[i]).discreteLogarithm (IntM(p));
      if (f[i] < 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      debug stderr.writeln ("p = ", p);
      Matrix q;
      foreach (i; 0 .. 3) foreach (j; 0 .. 3) q[i][j] = IntM2 (0);
      q[0][0] = q[0][1] = q[0][2] = q[1][0] = q[2][1] = IntM2 (1);
      Matrix w = power (q, n - 3);
      debug stderr.writeln (w);
      IntM2 res2 = 0;
      foreach (i; 0 .. 3) {
        res2 += IntM2 (f[3 - i]) * w[0][i];
      }
      debug stderr.writeln (res2);
      IntM res = IntM (p) ^^ res2.v;
      res *= (IntM (c) ^^ n) ^^ (IntM.q - 2);
      writeln (res);
      return;
    }
  }
}

unittest {
  enum DL_TESTS = 10;
  foreach (p; [2, 3, 5, 7, 11, 17]) {
    stderr.writeln (p);
    IntM a = p;
    IntM x = 1;
    foreach (j; 0 .. DL_TESTS) {
      assert (x.discreteLogarithm (a) == j);
      x *= a;
    }
    foreach (j; iota (100, 10007 * DL_TESTS, 10007)) {
      IntM y = IntM (p) ^^ j;
      assert (y.discreteLogarithm (a) == j);
    }
  }
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

T gcdext(T) (T a, T b, ref T x, ref T y) pure nothrow @nogc {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  T res = gcdext (b, a % b, y, x);
  y -= x * (a / b);
  return res;
}

struct IntM(int q = 1_000_000_007) {
  alias N = IntM!q;
  int v;
  private void fromInt (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
  }
  this (int m) pure nothrow @nogc {
    fromInt (m);
  }
  N opAssign (int m) pure nothrow @nogc {
    fromInt (m);
    return this;
  }
  N opUnary (string op : "-")() const pure nothrow @nogc {
    return N ((q - v) % q);
  }
  ref N opUnary (string op : "++")() pure nothrow @nogc {
    if (++v >= q) {
      v -= q;
    }
    return this;
  }
  ref N opUnary (string op : "--")() pure nothrow @nogc {
    if (--v < 0) {
      v += q;
    }
    return this;
  }
  ref N opOpAssign (string op : "+")(in N rhs) pure nothrow @nogc {
    v += rhs.v;
    v %= q;
    return this;
  }
  ref N opOpAssign (string op : "-")(in N rhs) pure nothrow @nogc {
    v -= rhs.v;
    v %= q;
    return this;
  }
  ref N opOpAssign (string op : "*")(in N rhs) pure nothrow @nogc {
    v = ((v.to!(long)) * rhs.v.to!(long)) % q;
    return this;
  }
  N opBinary (string op : "+")(in N rhs) const pure nothrow @nogc {
    return N ( (v + rhs.v) % q);
  }
  N opBinary (string op : "-")(in N rhs) const pure nothrow @nogc {
    return N ( (v - rhs.v) % q);
  }
  N opBinary (string op : "*")(in N rhs) const pure nothrow @nogc {
    return N (((v.to!(long)) * rhs.v.to!(long)) % q);
  }
  N opBinary (string op : "/")(in N rhs) const pure nothrow @nogc {
    return this * N.inversion ();
  }
  N inversion () const pure @nogc {
    int x, y;
    immutable g = gcdext!int (v, q, x, y);
    assert (g == 1);
    return N (x);
  }
  N opBinary (string op : "^^")(in long rhs) const pure nothrow @nogc {
    N a = 1, b = this;
    long p = rhs;
    while (p > 0) {
      //a * (b ^ p) == x ^ rhs
      if (p & 1) {
        a *= b;
      }
      b *= b;
      p >>>= 1;
    }
    return a;
  }
  N opBinary (string op)(in int v) const pure nothrow @nogc if (op == "+" || op == "-" || op == "*") {
    mixin ("return this " ~ op ~ " N(v);");
  }
  int opCast(T : int)() const pure nothrow @nogc { return v; }
  int opCmp (const N rhs) const pure nothrow @nogc {
    if (v < rhs.v) {
      return -1;
    }
    if (v > rhs.v) {
      return 1;
    }
    return 0;
  }
  bool opEquals (const N rhs) const pure nothrow @nogc { return v == rhs.v; }
  string toString() const pure nothrow { return ((v < 0) ? v + q : v).text; }
  //a ^ x = this (mod q)
  int discreteLogarithm (in N a) const {
    enum int n = ceil (sqrt (q.to!double)).to!int;
    N an = a ^^ n;
    int[int] h;
    N cur = an;
    foreach (p; 1 .. n + 1) {
      auto ptr = cur.v !in h;
      if (cur.v !in h) {
        h[cur.v] = p;
      }
      cur *= an;
    }
    cur = this;
    int res = int.max;
    foreach (q; 0 .. n + 1) {
      auto ptr = cur.v in h;
      if (ptr) {
        res = min (res, *ptr * n - q);
      }
      cur *= a;
    }
    return res != int.max ? res : -1;
  }
}

alias Matrix = IntM2[3][3];
Matrix mul (in Matrix a, in Matrix b) {
  Matrix c;
  foreach (i; 0 .. 3) foreach (j; 0 .. 3) foreach (k; 0 .. 3) {
    c[i][j] += a[i][k] * b[k][j];
  }
  return c;
}

Matrix power (Matrix x, long rhs) {
  Matrix a;
  foreach (i; 0 .. 3) foreach (j; 0 .. 3) a[i][j] = IntM2 (i == j ? 1 : 0);
  Matrix b;
  foreach (i; 0 .. 3) b[i] = x[i].dup;
  long p = rhs;
  while (p > 0) {
    //a * (b ^ p) == x ^ rhs
    if (p & 1) {
      a = mul (a, b);
    }
    b = mul (b, b);
    p >>>= 1;
  }
  return a;
}

alias IntM1 = IntM!();
alias IntM2 = IntM!(1_000_000_006);

void main() {
  long n;
  int[4] f;
  int[4] h;
  int c;
  readf! " %d %d %d %d %d" (n, f[1], f[2], f[3], c);
  IntM1[4] g;
  foreach (i; 1 .. 4) {
    g[i] = IntM1 (f[i]);
    foreach (j; 0 .. i) {
      g[i] *= IntM1 (c);
    }
    h[i] = g[i].to!int;
  }
  foreach (p; [2, 3, 5, 7, 11, 13, 17, 23, 27, 31]) {
    bool ok = true;
    foreach (i; 1 .. 4) {
      f[i] = IntM1(h[i]).discreteLogarithm (IntM1(p));
      if (f[i] < 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      debug stderr.writeln ("p = ", p);
      Matrix q;
      foreach (i; 0 .. 3) foreach (j; 0 .. 3) q[i][j] = IntM2 (0);
      q[0][0] = q[0][1] = q[0][2] = q[1][0] = q[2][1] = IntM2 (1);
      Matrix w = power (q, n - 3);
      debug stderr.writeln (w);
      IntM2 res2 = 0;
      foreach (i; 0 .. 3) {
        res2 += IntM2 (f[3 - i]) * w[0][i];
      }
      debug stderr.writeln (res2);
      IntM1 res = IntM1 (p) ^^ res2.v;
      res *= (IntM1 (c) ^^ n).inversion ();
      writeln (res);
      return;
    }
  }
}


Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.

The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.

Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.

module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string[] tk;
        string read_string() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int read_int() {
                return read_string.to!int;
        }
        double read_double() {
                return read_string.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.read_int;
                }
                for (int i = 0; i < n; i++) {
                        for (int j = i + 1; j < n; j++) {
                                if (arr.canFind(arr[i] + arr[j])) {
                                        write(arr.length - arr.find(arr[i] + arr[j]).length + 1, " ");
                                        writeln(i + 1, " ", j + 1);
                                        return;
                                }
                        }
                }
                writeln(-1);
        }
}

module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string[] tk;
        string read_string() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int read_int() {
                return read_string.to!int;
        }
        double read_double() {
                return read_string.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.read_int;
                }
                for (int i = 0; i < n; i++) {
                        for (int j = i + 1; j < n; j++) {
                                if (arr.canFind(arr[i] + arr[j])) {
                                        write(countUntil(arr, arr.find(arr[i] + arr[j])) + 1, " ");
                                        writeln(i + 1, " ", j + 1);
                                        return;
                                }
                        }
                }
                writeln(-1);
        }
}

module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string[] tk;
        string read_string() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int read_int() {
                return read_string.to!int;
        }
        double read_double() {
                return read_string.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.read_int;
                }
                for (int i = 0; i < n; i++) {
                        for (int j = i + 1; j < n; j++) {
                                if (arr.canFind(arr[i] + arr[j])) {
                                        writeln(arr[i] + arr[j], " ", arr[i],  " ", arr[j]);
                                        return;
                                }
                        }
                }
                writeln(-1);
        }
}

module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string[] tk;
        string read_string() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int read_int() {
                return read_string.to!int;
        }
        double read_double() {
                return read_string.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.read_int;
                }
                for (int i = 0; i < n; i++) {
                        for (int j = i + 1; j < n; j++) {
                                if (arr.canFind(arr[i] + arr[j])) {
                                        writeln(arr[i], " ", arr[j],  " ", arr[i] + arr[j]);
                                        return;
                                }
                        }
                }
                writeln(-1);
        }
}


This is the easy version of the problem. The difference in the constraints between both versions is colored below in red. You can make hacks only if all versions of the problem are solved.Marin and Gojou are playing hide-and-seek with an array.Gojou initially performs the following steps:   First, Gojou chooses $$$2$$$ integers $$$l$$$ and $$$r$$$ such that $$$l \leq r$$$.  Then, Gojou makes an array $$$a$$$ of length $$$r-l+1$$$ which is a permutation of the array $$$[l,l+1,\ldots,r]$$$.  Finally, Gojou chooses a secret integer $$$x$$$ and sets $$$a_i$$$ to $$$a_i \oplus x$$$ for all $$$i$$$ (where $$$\oplus$$$ denotes the bitwise XOR operation). Marin is then given the values of $$$l,r$$$ and the final array $$$a$$$. She needs to find the secret integer $$$x$$$ to win. Can you help her?Note that there may be multiple possible $$$x$$$ that Gojou could have chosen. Marin can find any possible $$$x$$$ that could have resulted in the final value of $$$a$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. In the first line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$ \color{red}{\boldsymbol{0} \boldsymbol{=} \boldsymbol{l}} \le r < 2^{17}$$$). The second line contains $$$r - l + 1$$$ integers of $$$a_1,a_2,\ldots,a_{r-l+1}$$$ ($$$0 \le a_i < 2^{17}$$$). It is guaranteed that $$$a$$$ can be generated using the steps performed by Gojou. It is guaranteed that the sum of $$$r - l + 1$$$ over all test cases does not exceed $$$2^{17}$$$.

For each test case print an integer $$$x$$$. If there are multiple answers, print any.

NoteIn the first test case, the original array is $$$[3, 2, 1, 0]$$$. In the second test case, the original array is $$$[0, 3, 2, 1]$$$.In the third test case, the original array is $$$[2, 1, 0]$$$.

import std.stdio;
import core.bitop;
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;

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

void solve() {
    int L, R; readf("%d %d\n", &L, &R);
    auto A = readarray!int;
    if ((R - L + 1) % 2 == 1) {
        int a = 0;
        for (int i = L; i <= R; i++) {
            a ^= A[i];
        }
        for (int i = L; i <= R; i++) {
            a ^= i;
        }
        writeln(a);
    } else {
        int ans = 0;
        for (int i = 0; i < 17; i++) {
            int[2] c;
            foreach (a; A) {
                if (a & (1<<i)) {
                    c[1]++;
                } else {
                    c[0]++;
                }
            }
            if (c[1] > c[0]) {
                ans |= 1<<i;
            }
        }
        writeln(ans);
    }
}

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

import std.stdio;
import core.bitop;
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;

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

void solve() {
    int L, R; readf("%d %d\n", &L, &R);
    auto A = readarray!int;
    if ((R - L + 1) % 2 == 1) {
        int a = 0;
        for (int i = L; i <= R; i++) {
            a ^= A[i];
        }
        for (int i = L; i <= R; i++) {
            a ^= i;
        }
        writeln(a);
    } else {
        bool[int] s;
        int m = A[0];
        foreach (a; A) {
            m &= a;
        }
        foreach (a; A) {
            s[a ^ m] = true;
        }
        int b = R.bsr;
        int nx = ~0;
        for (int z = 0; z < (1<<b); z++) {
            if (z !in s) {
                nx &= z;
            }
        }
        int ans = 0;
        for (int i = 0; i < 17; i++) {
            if (i <= b) {
                ans |= (~nx) & (1<<i);
            } else {
                ans |= m & (1<<i);
            }
        }
        writeln(ans);
    }
}

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

import std.stdio;
import core.bitop;
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;

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

void solve() {
    int L, R; readf("%d %d\n", &L, &R);
    auto A = readarray!int;
    if ((R - L + 1) % 2 == 1) {
        int a = 0;
        for (int i = L; i <= R; i++) {
            a ^= A[i];
        }
        for (int i = L; i <= R; i++) {
            a ^= i;
        }
        writeln(a);
    } else {
        bool[int] s;
        int m = A[0];
        foreach (a; A) {
            m &= a;
        }
        foreach (a; A) {
            s[a ^ m] = true;
        }
        int nx = ~0;
        for (int z = 0; z <= R; z++) {
            if (z !in s) {
                nx &= z;
            }
        }
        int b = R.bsr;
        int ans = 0;
        for (int i = 0; i < 17; i++) {
            if (i <= b) {
                ans |= (~nx) & (1<<i);
            } else {
                ans |= m & (1<<i);
            }
        }
        writeln(ans);
    }
}

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


Petya has an array of integers $$$a_1, a_2, \ldots, a_n$$$. He only likes sorted arrays. Unfortunately, the given array could be arbitrary, so Petya wants to sort it.Petya likes to challenge himself, so he wants to sort array using only $$$3$$$-cycles. More formally, in one operation he can pick $$$3$$$ pairwise distinct indices $$$i$$$, $$$j$$$, and $$$k$$$ ($$$1 \leq i, j, k \leq n$$$) and apply $$$i \to j \to k \to i$$$ cycle to the array $$$a$$$. It simultaneously places $$$a_i$$$ on position $$$j$$$, $$$a_j$$$ on position $$$k$$$, and $$$a_k$$$ on position $$$i$$$, without changing any other element.For example, if $$$a$$$ is $$$[10, 50, 20, 30, 40, 60]$$$ and he chooses $$$i = 2$$$, $$$j = 1$$$, $$$k = 5$$$, then the array becomes $$$[\underline{50}, \underline{40}, 20, 30, \underline{10}, 60]$$$.Petya can apply arbitrary number of $$$3$$$-cycles (possibly, zero). You are to determine if Petya can sort his array $$$a$$$, i. e. make it non-decreasing.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 5 \cdot 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.

For each test case, print "YES" (without quotes) if Petya can sort the array $$$a$$$ using $$$3$$$-cycles, and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower).

NoteIn the $$$6$$$-th test case Petya can use the $$$3$$$-cycle $$$1 \to 3 \to 2 \to 1$$$ to sort the array.In the $$$7$$$-th test case Petya can apply $$$1 \to 3 \to 2 \to 1$$$ and make $$$a = [1, 4, 2, 3]$$$. Then he can apply $$$2 \to 4 \to 3 \to 2$$$ and finally sort the array.

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

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

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

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

    bool f() {
        if (N == 1) return true;
        if (N == 2) return (A[0] <= A[1]);

        auto B = A.dup; B.sort;
        for (int i = 0; i+1 < N; i++) {
            if (B[i] == B[i+1]) return true;
        }

        long c = count_inversion(A);
        return c % 2 == 0;
    }
    writeln(f() ? "YES" : "NO");
}

long count_inversion(T)(in T[] xs_) {
    long inversion = 0;
    auto xs = xs_.dup;
    auto buf = new T[xs.length];
    void f(int s, int t) {
        if (s + 1 == t) return;
        int mid = (s + t) / 2;
        int i = s;
        f(s, mid);
        f(mid, t);
        int li = s, ri = mid;
        while (li < mid || ri < t) {
            if (li == mid) { buf[i++] = xs[ri++]; continue; }
            if (ri == t) { buf[i++] = xs[li++]; continue; }
            if (xs[li] < xs[ri]) {
                buf[i++] = xs[li++];
            } else if (xs[li] > xs[ri]) {
                buf[i++] = xs[ri++];
                inversion += mid - li;
            } else {
                writefln("s = %d, t = %d", s, t);
                writefln("li = %d, ri = %d, i = %d", li, ri, i);
                assert(false);
            }
        }
        for (int j = s; j < t; j++) xs[j] = buf[j];
        assert(i == t);
    }
    f(0, xs.length);
    return inversion;
}

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto a = ma(n, readInt!int - 1);
  auto sa = a.dup; sort(sa);
  auto sPosS = new int[][](n);
  foreach(i, ai; sa) sPosS[ai] ~= cast(int)i;
  bool excedent = false;
  foreach(i; 0 .. n) if (sPosS[i].length > 1) excedent = true;
  debug writeln("sPosS ", sPosS);
  auto adj = new int[](n);
  foreach(i; 0 .. n)
    {
      assert(sPosS[a[i]].length > 0);
      adj[i] = sPosS[a[i]].front;
      sPosS[a[i]].popFront;
    }
  debug writeln("adj ", adj);
  auto visited = new bool[](n);
  int getPar(int i)
  {
    assert(!visited[i]);
    visited[i] = true;
    int ans = 1;
    if (!visited[adj[i]]) ans ^= getPar(adj[i]);
    return ans;
  }
  auto par = int(0);
  foreach(i; 0 .. n) if (!visited[i]) par ^= 1^getPar(i);
  if (excedent) return writeln("YES");
  if (par) return writeln("NO");
  return writeln("YES");
}

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

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

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

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

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

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

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

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

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

    bool f() {
        if (N == 1) return true;
        if (N == 2) return (A[0] <= A[1]);
        long c = count_inversion(A);
        return c % 2 == 0;
    }
    writeln(f() ? "YES" : "NO");
}

long count_inversion(T)(in T[] xs_) {
    long inversion = 0;
    auto xs = xs_.dup;
    auto buf = new T[xs.length];
    void f(int s, int t) {
        if (s + 1 == t) return;
        int mid = (s + t) / 2;
        int i = s;
        f(s, mid);
        f(mid, t);
        int li = s, ri = mid;
        while (li < mid || ri < t) {
            if (li == mid) { buf[i++] = xs[ri++]; continue; }
            if (ri == t) { buf[i++] = xs[li++]; continue; }
            if (xs[li] <= xs[ri]) {
                buf[i++] = xs[li++];
            } else if (xs[li] > xs[ri]) {
                buf[i++] = xs[ri++];
                inversion += mid - li;
            } else {
                writefln("s = %d, t = %d", s, t);
                writefln("li = %d, ri = %d, i = %d", li, ri, i);
                assert(false);
            }
        }
        for (int j = s; j < t; j++) xs[j] = buf[j];
        assert(i == t);
    }
    f(0, xs.length);
    return inversion;
}

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto a = ma(n, readInt!int - 1);
  auto sa = a.dup; sort(sa);
  auto sPosS = new int[][](n);
  foreach(i, ai; sa) sPosS[ai] ~= cast(int)i;
  debug writeln("sPosS ", sPosS);
  auto adj = new int[](n);
  foreach(i; 0 .. n)
    {
      assert(sPosS[a[i]].length > 0);
      adj[i] = sPosS[a[i]].front;
      sPosS[a[i]].popFront;
    }
  auto visited = new bool[](n);
  int getPar(int i)
  {
    assert(!visited[i]);
    visited[i] = true;
    int ans = 1;
    if (!visited[adj[i]]) ans ^= getPar(adj[i]);
    return ans;
  }
  auto par = int(0);
  foreach(i; 0 .. n) if (!visited[i]) par ^= 1^getPar(i);
  if (par) return writeln("NO");
  return writeln("YES");
}

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

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

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

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

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


Xenia is an amateur programmer. Today on the IT lesson she learned about the Hamming distance.The Hamming distance between two strings s = s1s2... sn and t = t1t2... tn of equal length n is value . Record [si ≠ ti] is the Iverson notation and represents the following: if si ≠ ti, it is one, otherwise — zero.Now Xenia wants to calculate the Hamming distance between two long strings a and b. The first string a is the concatenation of n copies of string x, that is, . The second string b is the concatenation of m copies of string y. Help Xenia, calculate the required Hamming distance, given n, x, m, y.

The first line contains two integers n and m (1 ≤ n, m ≤ 1012). The second line contains a non-empty string x. The third line contains a non-empty string y. Both strings consist of at most 106 lowercase English letters. It is guaranteed that strings a and b that you obtain from the input have the same length.

Print a single integer — the required Hamming distance. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

NoteIn the first test case string a is the same as string b and equals 100 letters a. As both strings are equal, the Hamming distance between them is zero.In the second test case strings a and b differ in their 3-rd, 5-th, 6-th and 7-th characters. Thus, the Hamming distance equals 4.In the third test case string a is rzrrzr and string b is azazaz. The strings differ in all characters apart for the second one, the Hamming distance between them equals 5.

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.math;
import std.numeric;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!long;
  auto m = next!long;
  auto x = next!string;
  auto y = next!string;
  auto g = gcd(cast(long)x.length, cast(long)y.length);
  auto lx = x.length / g;
  auto ly = y.length / g;
  debug writeln(g, " ", ly);
  long matches = 0;
  foreach(cl; 0 .. g)
    {
      long[char] chrCnt;
      foreach(inst; 0 .. ly)
	chrCnt.require(y[cast(int)(inst * g + cl)], 0)++;
      foreach(inst; 0 .. lx)
	matches += chrCnt.require(x[cast(int)(inst * g + cl)], 0);
    }
  assert(n * x.length == m * y.length);
  auto l = lx * y.length;
  auto lres = l - matches;
  auto res = (n * x.length) / l * lres;
  res.writeln;
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

int id(char c) {
    return cast(int)(c - 'a');
}

int diff(int mod, char c, in int[][] hist) {
    int ret = 0;
    auto N = hist.length;
    foreach (j; 0 .. 26) {
        if (j == c.id) continue;
        ret += hist[mod][j].abs;
    }
    return ret;
}

void main() {
    long N, M; scanf("%lld %lld\n", &N, &M);
    auto x = readln.chomp,
         y = readln.chomp;
    int  X = cast(int)x.length,
         Y = cast(int)y.length;
    int  gcd = gcd(X, Y);
    long lcm = cast(long)(X) / gcd * Y;
    auto histogram = new int[][](gcd, 26);
    foreach (int i, c; y) {
        histogram[i % gcd][c.id]++;
    }
    long ans = 0;
    foreach (int i, c; x) {
        ans += diff(i % gcd, c, histogram);
    }
    writeln(ans * (N * X / lcm));
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.math;
import std.numeric;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!long;
  auto m = next!long;
  auto x = next!string;
  auto y = next!string;
  auto g = gcd(x.length, y.length);
  auto lx = x.length / g;
  auto ly = y.length / g;
  debug writeln(g, " ", ly);
  long matches = 0;
  foreach(cl; 0 .. g)
    {
      long[char] chrCnt;
      foreach(inst; 0 .. ly)
	chrCnt.require(y[inst * g + cl], 0)++;
      foreach(inst; 0 .. lx)
	matches += chrCnt.require(x[inst * g + cl], 0);
    }
  assert(n * x.length == m * y.length);
  auto l = lx * y.length;
  auto lres = l - matches;
  auto res = (n * x.length) / l * lres;
  res.writeln;
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

int id(char c) {
    return cast(int)(c - 'a');
}

int diff(int mod, char c, in int[][] hist) {
    int ret = 0;
    auto N = hist.length;
    foreach (j; 0 .. 26) {
        if (j == c.id) continue;
        ret += hist[mod][j].abs;
    }
    return ret;
}

void main() {
    long N, M; scanf("%lld %lld\n", &N, &M);
    auto x = readln.chomp,
         y = readln.chomp;
    int  X = cast(int)x.length,
         Y = cast(int)y.length;
    int  gcd = gcd(X, Y);
    long lcm = X / gcd * Y;
    auto histogram = new int[][](gcd, 26);
    foreach (int i, c; y) {
        histogram[i % gcd][c.id]++;
    }
    long ans = 0;
    foreach (int i, c; x) {
        ans += diff(i % gcd, c, histogram);
    }
    writeln(ans * (N * X / lcm));
}


zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ab, aba, zscoder are simple whereas aa, add are not simple.zscoder is given a string s. He wants to change a minimum number of characters so that the string s becomes simple. Help him with this task!

The only line contains the string s (1 ≤ |s| ≤ 2·105) — the string given to zscoder. The string s consists of only lowercase English letters.

Print the simple string s' — the string s after the minimal number of changes. If there are multiple solutions, you may output any of them. Note that the string s' should also consist of only lowercase English letters.

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

string solve(string s)
{
    auto n = to!int(s.length);
    auto res = new char[n];
    res[0] = s[0];
    foreach (i; 1 .. n)
    {
        if (s[i] != res[i - 1])
        {
            res[i] = s[i];
        }
        else
        {
            foreach (j; 0 .. 26)
            {
                if (j != res[i - 1] - 'a' && (i == n - 1 || j != s[i + 1] - 'a'))
                {
                    res[i] = to!char(j + 97);
                    break;
                }
            }
        }
    }
    return to!string(res);
}

int main(string[] args)
{
    auto s = readln.strip;
    auto ret = solve(s);
    writeln(ret);
    return 0;
}


Let's call the roundness of the number the number of zeros to which it ends.You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible.

The first line contains two integer numbers n and k (1 ≤ n ≤ 200, 1 ≤ k ≤ n). The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 1018).

Print maximal roundness of product of the chosen subset of length k.

NoteIn the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2, [4, 20] — product 80, roundness 1, [50, 20] — product 1000, roundness 3.In the second example subset [15, 16, 25] has product 6000, roundness 3.In the third example all subsets has product with roundness 0.

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 s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto A = readln.split.map!(to!long).array;

    auto two = new int[](N);
    auto five = new int[](N);
    foreach (i, a; A) {
        while (a % 2 == 0) a /= 2, two[i] += 1;
        while (a % 5 == 0) a /= 5, five[i] += 1;
    }

    auto dp = new int[][][](2, M+1, 25*N+1);
    auto ok = new bool[][][](2, M+1, 25*N+1);
    ok[0][0][0] = true;
    int cur = 0, tar = 1;

    foreach (i; 0..N) {
        foreach (j; 0..M+1) {
            dp[tar][j][] = 0;
            ok[tar][j][] = 0;
        }
        foreach (j; 0..M+1) foreach (k; 0..25*i+1) if (ok[cur][j][k]) {
            dp[tar][j][k] = max(dp[tar][j][k], dp[cur][j][k]);
            ok[tar][j][k] = true;
            if (j < M) {
                dp[tar][j+1][k+five[i]] = max(dp[tar][j+1][k+five[i]], dp[cur][j][k] + two[i]);
                ok[tar][j+1][k+five[i]] = true;
            }
        }
        swap(cur, tar);
    }

    int ans = 0;
    foreach (j; 0..M+1) foreach (k; 0..25*N+1) {
        ans = max(ans, min(k, dp[cur][j][k]));
    }
    ans.writeln;
}

// Codeforces My Practice
// author: Leonardone @ NEETSDKASU
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.range;

// no my think

void main() {
    auto nk = to!(int[])(split(chomp(readln())));
    int n = nk[0], k = nk[1];
    auto xs = to!(long[])(split(chomp(readln())));
    int[5300][201] tb;
    for (int i = 0; i <= k; i++) {
        for (int j = 0; j < 5300; j++) {
            tb[i][j] = -1;
        }
    }
    tb[0][0] = 0;
    foreach (x ; xs) {
        int c2, c5;
        while (x % 2 == 0) {
            x /= 2;
            c2++;
        }
        while (x % 5 == 0) {
            x /= 5;
            c5++;
        }
        for (int i = k - 1; i > 0; i--) {
            for (int j = 5299; j >= 0; j--) {
                if (tb[i][j] < 0) { continue; }
                tb[i+1][j+c5] = max(tb[i+1][j+c5], tb[i][j]+c2);
            }
        }
        tb[1][c5] = max(tb[1][c5], c2);
    }
    int ans = 0;
    for (int i = 1; i < 5300; i++) {
        if (tb[k][i] < 0) { continue; }
        ans = max(ans, min(tb[k][i], i));
    }
    writeln(ans);
}

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

int n, k;
long[] a;

void main() {
    scan(n, k);
    a = readln.split.to!(long[]);

    auto two = new int[](n);
    auto five = new int[](n);

    foreach (i ; 0 .. n) {
        while (a[i] % 2 == 0) {
            a[i] /= 2;
            two[i]++;
        }

        while (a[i] % 5 == 0) {
            a[i] /= 5;
            five[i]++;
        }
    }

    int lim = n * 25;

    auto dp = new int[][][](2, k + 1, lim + 1);
    fillAll(dp, -1);
    dp[0][0][0] = 0;

    foreach (i ; 0 .. n) {
        foreach (j ; 0 .. k + 1) {
            foreach (f ; 0 .. lim + 1) {
                if (dp[i & 1][j][f] == -1) continue;
                dp[(i & 1) ^ 1][j][f] = max(dp[(i & 1) ^ 1][j][f], dp[i & 1][j][f]);

                if (j + 1 <= k) {
                    dp[(i & 1) ^ 1][j + 1][f + five[i]] = max(dp[(i & 1) ^ 1][j + 1][f + five[i]], dp[i & 1][j][f] + two[i]);
                }
            }
        }
    }

    int ans;

    foreach (f ; 0 .. lim + 1) {
        ans = max(ans, min(f, dp[n & 1][k][f]));
    }

    writeln(ans);
}

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

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

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!long).array;
    auto TF = new Tuple!(int, int)[](N);

    foreach (i; 0..N) {
        while (A[i] % 5 == 0) {
            TF[i][1] += 1;
            A[i] /= 5;
        }
        while (A[i] % 2 == 0) {
            TF[i][0] += 1;
            A[i] /= 2;
        }
    }

    auto dp = new int[][](N + 1, 4000);
    auto dp2 = new bool[][](N + 1, 4000);
    dp2[0][0] = true;

    foreach (i; 0..N) {
        for (int j = i + 1; j >= 1; j--) {
            foreach (k; 0..3800) {
                if (k - TF[i][1] >= 0 && dp2[j - 1][k - TF[i][1]]) {
                    dp[j][k] = max(dp[j][k], dp[j - 1][k - TF[i][1]] + TF[i][0]);
                    dp2[j][k] = true;
                }
            }
        }
        dp[1][TF[i][1]] = max(dp[1][TF[i][1]], TF[i][0]);
        dp2[1][TF[i][1]] = true;
    }

    int ans = 0;
    foreach (i; 0..3800) {
        if (dp2[K][i]) ans = max(ans, min(i, dp[K][i]));
    }
    ans.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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!long).array;
    auto TF = new Tuple!(int, int)[](N);

    foreach (i; 0..N) {
        while (A[i] % 5 == 0) {
            TF[i][1] += 1;
            A[i] /= 5;
        }
        while (A[i] % 2 == 0) {
            TF[i][0] += 1;
            A[i] /= 2;
        }
    }

    auto dp = new int[][](N + 1, 2000);
    auto dp2 = new bool[][](N + 1, 2000);
    dp2[0][0] = true;

    foreach (i; 0..N) {
        for (int j = i + 1; j >= 1; j--) {
            foreach (k; 0..1900) {
                if (k - TF[i][1] >= 0 && dp2[j - 1][k - TF[i][1]]) {
                    dp[j][k] = max(dp[j][k], dp[j - 1][k - TF[i][1]] + TF[i][0]);
                    dp2[j][k] = true;
                }
            }
        }
        dp[1][TF[i][1]] = max(dp[1][TF[i][1]], TF[i][0]);
        dp2[1][TF[i][1]] = true;
    }

    int ans = 0;
    foreach (i; 0..1900) {
        if (dp2[K][i]) ans = max(ans, min(i, dp[K][i]));
    }
    ans.writeln;
}


There are three horses living in a horse land: one gray, one white and one gray-and-white. The horses are really amusing animals, which is why they adore special cards. Each of those cards must contain two integers, the first one on top, the second one in the bottom of the card. Let's denote a card with a on the top and b in the bottom as (a, b).Each of the three horses can paint the special cards. If you show an (a, b) card to the gray horse, then the horse can paint a new (a + 1, b + 1) card. If you show an (a, b) card, such that a and b are even integers, to the white horse, then the horse can paint a new  card. If you show two cards (a, b) and (b, c) to the gray-and-white horse, then he can paint a new (a, c) card.Polycarpus really wants to get n special cards (1, a1), (1, a2), ..., (1, an). For that he is going to the horse land. He can take exactly one (x, y) card to the horse land, such that 1 ≤ x < y ≤ m. How many ways are there to choose the card so that he can perform some actions in the horse land and get the required cards?Polycarpus can get cards from the horses only as a result of the actions that are described above. Polycarpus is allowed to get additional cards besides the cards that he requires.

The first line contains two integers n, m (1 ≤ n ≤ 105, 2 ≤ m ≤ 109). The second line contains the sequence of integers a1, a2, ..., an (2 ≤ ai ≤ 109). Note, that the numbers in the sequence can coincide. The numbers in the lines are separated by single spaces.

Print a single integer — the answer to the problem.  Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

import std.numeric;
import std.stdio;

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m))
	{
		auto a = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s", &a[i]);
			a[i]--;
		}

		int d;
		foreach (i; 0..n)
			d = gcd (d, a[i]);
		while (d % 2 == 0)
			d /= 2;

		long res;

		void add (int x)
		{
			while (x < m)
				res += m - x,
				x *= 2;
		}

		for (int w = 1; w * w <= d; w += 2)
			if (d % w == 0)
			{
				add (w);
				if (w * w < d)
					add (d / w);
			}
		writeln (res);
	}
}

import std.numeric;
import std.stdio;

int main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m))
	{
		m--;
		auto a = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s", &a[i]);
			a[i]--;
		}

		int d = 0;
		foreach (i; 0..n)
			d = gcd (d, a[i]);
		while (d % 2 == 0)
			d /= 2;

		long res = 0;

		void add (int x)
		{
			while (x <= m)
			{
				res += m - x + 1;
				x *= 2;
			}
		}

		for (int w = 1; w * w <= d; w += 2)
			if (d % w == 0)
			{
				add (w);
				if (w * w < d)
					add (d / w);
			}
		writefln ("%s", res);
	}
	return 0;
}

import std.algorithm;
import std.container;
import std.exception;
import std.math;
import std.numeric;
import std.stdio;
import std.string;
import std.typecons;

int main ()
{
	int n;
	long m;
	while ((readf (" %s %s", &n, &m) >= 0) && !stdin.eof ())
	{
		m--;
		auto a = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s", &a[i]);
			a[i]--;
		}

		int d = 0;
		foreach (i; 0..n)
		{
			d = gcd (d, a[i]);
		}
		while (d % 2 == 0)
		{
			d /= 2;
		}

		long res = 0;

		void add (int x)
		{
			while (x <= m)
			{
				res += m - x + 1;
				x *= 2;
			}
		}

		for (int w = 1; w * w <= d; w += 2)
		{
			if (d % w == 0)
			{
				add (w);
				if (w * w < d)
				{
					add (d / w);
				}
			}
		}
		writefln ("%s", res);
	}
	return 0;
}


Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task.Recently, out of blue Captain Flint has been interested in math and even defined a new class of integers. Let's define a positive integer $$$x$$$ as nearly prime if it can be represented as $$$p \cdot q$$$, where $$$1 < p < q$$$ and $$$p$$$ and $$$q$$$ are prime numbers. For example, integers $$$6$$$ and $$$10$$$ are nearly primes (since $$$2 \cdot 3 = 6$$$ and $$$2 \cdot 5 = 10$$$), but integers $$$1$$$, $$$3$$$, $$$4$$$, $$$16$$$, $$$17$$$ or $$$44$$$ are not.Captain Flint guessed an integer $$$n$$$ and asked you: can you represent it as the sum of $$$4$$$ different positive integers where at least $$$3$$$ of them should be nearly prime.Uncle Bogdan easily solved the task and joined the crew. Can you do the same?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per line. The first and only line of each test case contains the single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$ — the number Flint guessed.

For each test case print:    YES and $$$4$$$ different positive integers such that at least $$$3$$$ of them are nearly prime and their sum is equal to $$$n$$$ (if there are multiple answers print any of them);  NO if there is no way to represent $$$n$$$ as the sum of $$$4$$$ different positive integers where at least $$$3$$$ of them are nearly prime.  YES NO

NoteIn the first and second test cases, it can be proven that there are no four different positive integers such that at least three of them are nearly prime.In the third test case, $$$n=31=2 \cdot 7 + 2 \cdot 5 + 2 \cdot 3 + 1$$$: integers $$$14$$$, $$$10$$$, $$$6$$$ are nearly prime.In the fourth test case, $$$n=36=5 + 2 \cdot 3 + 2 \cdot 5 + 3 \cdot 5$$$: integers $$$6$$$, $$$10$$$, $$$15$$$ are nearly prime.In the fifth test case, $$$n=44=2 \cdot 3 + 7 + 2 \cdot 5 + 3 \cdot 7$$$: integers $$$6$$$, $$$10$$$, $$$21$$$ are nearly prime.In the sixth test case, $$$n=100=2 + 2 \cdot 5 + 3 \cdot 11 + 5 \cdot 11$$$: integers $$$10$$$, $$$33$$$, $$$55$$$ are nearly prime.In the seventh test case, $$$n=258=2 \cdot 5 + 3 \cdot 7 + 13 \cdot 17 + 2 \cdot 3$$$: integers $$$10$$$, $$$21$$$, $$$221$$$, $$$6$$$ are nearly prime.

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

void main ()
{
	auto tests = readln.strip.to !(int);
multitest_loop:
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		foreach (a; [7 * 2, 5 * 3])
		{
			auto p = [2 * 3, 2 * 5, a];
			p ~= n - p.sum;
			sort (p);
			if (p[0] > 0 && isStrictlyMonotonic (p))
			{
				writeln ("YES");
				writefln !("%(%s %)") (p);
				continue multitest_loop;
			}
		}
		writeln ("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)); }

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

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		if (n <= 30) continue;
		if (n-30 == 6 || n-30 == 10 || n-30 == 14)
			ans[ti] = [6, 10, 15, n-31];
		else
			ans[ti] = [6, 10, 14, n-30];
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		if (n <= 30) continue;
		ans[ti] = [6, 10, 14, n-30];
	}

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