Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem.  You are given a complete directed graph with $$$n$$$ vertices without self-loops. In other words, you have $$$n$$$ vertices and each pair of vertices $$$u$$$ and $$$v$$$ ($$$u \neq v$$$) has both directed edges $$$(u, v)$$$ and $$$(v, u)$$$.Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $$$(u, v)$$$ and $$$(v, u)$$$ may have different labels).You are also given an integer $$$m > 0$$$. You should find a path of length $$$m$$$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it.You can visit the same vertex and the same directed edge any number of times.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 1000$$$; $$$1 \leq m \leq 10^{5}$$$) — the number of vertices in the graph and desirable length of the palindrome. Each of the next $$$n$$$ lines contains $$$n$$$ characters. The $$$j$$$-th character of the $$$i$$$-th line describes the character on the edge that is going from node $$$i$$$ to node $$$j$$$. Every character is either 'a' or 'b' if $$$i \neq j$$$, or '*' if $$$i = j$$$, since the graph doesn't contain self-loops. It's guaranteed that the sum of $$$n$$$ over test cases doesn't exceed $$$1000$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$.

For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $$$m + 1$$$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them. Otherwise, (if there is no answer) print "NO".

5
3 1
*ba
b*b
ab*
3 3
*ba
b*b
ab*
3 4
*ba
b*b
ab*
4 6
*aaa
b*ba
ab*a
bba*
2 6
*a
b*

NoteThe graph from the first three test cases is shown below:  In the first test case, the answer sequence is $$$[1,2]$$$ which means that the path is:$$$$$$1 \xrightarrow{\text{b}} 2$$$$$$So the string that is obtained by the given path is b.In the second test case, the answer sequence is $$$[2,1,3,2]$$$ which means that the path is:$$$$$$2 \xrightarrow{\text{b}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{b}} 2$$$$$$So the string that is obtained by the given path is bab.In the third test case, the answer sequence is $$$[1,3,1,3,1]$$$ which means that the path is:$$$$$$1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1 \xrightarrow{\text{a}} 3 \xrightarrow{\text{a}} 1$$$$$$So the string that is obtained by the given path is aaaa.The string obtained in the fourth test case is abaaba.

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        int N, M; get(N, M);
        char[][] G; get_lines(N, G);
        int[] res;

        if (M % 2 == 1) {
            foreach (i; 0..M+1) res ~= i % 2 + 1;
            goto ok;
        }

        if (N == 2 && (G == ["*a", "b*"] || G == ["*b", "a*"])) {
            writeln("NO");
            continue;
        }

        foreach (i; 0..N-1) foreach (j; i+1..N) if (G[i][j] == G[j][i]) {
            auto pp = [i+1, j+1];
            foreach (k; 0..M+1) res ~= pp[k%2];
            goto ok;
        }

        foreach (k; 0..N) {
            int i = -1, j = -1;
            foreach (l; 0..N) {
                if (G[k][l] == 'a') {
                    i = l;
                } else if (G[k][l] == 'b') {
                    j = l;
                }

                if (i != -1 && j != -1) {
                    if (M / 2 % 2 == 0) {
                        res ~= k + 1;
                        foreach (c; 0..M/2) res ~= (c % 2 == 0 ? i : k) + 1;
                    } else {
                        res ~= i + 1;
                        foreach (c; 0..M/2) res ~= (c % 2 == 0 ? k : i) + 1;
                    }
                    foreach (c; 0..M/2) res ~= (c % 2 == 0 ? j : k) + 1;
                    goto ok;
                }
            }
        }

        ok:
        writefln!"YES\n%(%d %)"(res);
        continue;
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto s = new string[](n);
		foreach (i; 0..n)
			s[i] = RD!string;

		if (m % 2)
		{
			foreach (i; 0..m+1)
				ans[ti] ~= (i % 2) + 1;
		}
		else
		{
			int[] a;
			(){
			foreach (i; 0..n)
			{
				foreach (j; i+1..n)
				{
					if (s[i][j] == s[j][i])
					{
						a = [i, j];
						return;
					}
				}
			}}();
			if (!a.empty)
			{
				foreach (i; 0..m+1)
					ans[ti] ~= a[i % 2] + 1;
			}
			else
			{
				(){
				foreach (i; 0..n)
				{
					int x = -1, y = -1;
					foreach (j; 0..n)
					{
						if (s[i][j] == 'a')
						{
							x = j;
							break;
						}
					}
					foreach (j; 0..n)
					{
						if (s[j][i] == 'a')
						{
							y = j;
							break;
						}
					}
					if (x != -1 && y != -1)
					{
						int z = -1;
						foreach (j; 0..n)
						{
							if (s[j][y] == 'b' && s[x][j] == 'b')
							{
								z = j;
								break;
							}
						}
						a = [y, i, x, z];
						return;
					}
				}}();
				if (!a.empty)
				{
					if ((m / 2) % 2)
					{
						foreach (i; 0..m+1)
						{
							ans[ti] ~= a[i%4] + 1;
						}
					}
					else
					{
						foreach (i; 0..m+1)
						{
							ans[ti] ~= a[(i+1)%4] + 1;
						}
					}
				}
			}
		}
	}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
multitest_loop:
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		string [] a;
		foreach (i; 0..n)
		{
			a ~= readln.strip;
		}
		if (m & 1)
		{
			writeln ("YES");
			writefln !("%(%s %)") ([1, 2].cycle.take (m + 1));
			continue multitest_loop;
		}
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				if (a[i][j] == a[j][i])
				{
					writeln ("YES");
					writefln !("%(%s %)") ([i + 1, j + 1]
					    .cycle.take (m + 1));
					continue multitest_loop;
				}
			}
		}
		foreach (i; 0..n)
		{
			auto p = a[i].indexOf ('a');
			auto q = a[i].indexOf ('b');
			if (p >= 0 && q >= 0)
			{
				auto hi = [p, i].cycle.take (m / 2).array;
				auto lo = [q, i].cycle.take (m / 2).array;
				writeln ("YES");
				writefln !("%(%s %)") (chain (lo.retro,
				    only (i), hi).map !(q{a + 1}));
				continue multitest_loop;
			}
		}
		writeln ("NO");
	}
}

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        int N, M; get(N, M);
        char[][] G; get_lines(N, G);
        int[] res;

        if (M == 1) {
            writeln("YES\n1 2");
            continue;
        }

        if (N == 2 && (G == ["*a", "b*"] || G == ["*b", "a*"])) {
            writeln("NO");
            continue;
        }

        if (M % 2 == 1) {
            foreach (i; 0..M+1) res ~= i % 2 + 1;
            goto ok;
        }

        foreach (i; 0..N-1) foreach (j; i+1..N) if (G[i][j] == G[j][i]) {
            auto pp = [i+1, j+1];
            foreach (k; 0..M+1) res ~= pp[k%2];
            goto ok;
        }

        foreach (k; 0..N) {
            int i = -1, j = -1;
            foreach (l; 0..N) {
                if (G[k][l] == 'a') {
                    i = l;
                } else if (G[k][l] == 'b') {
                    j = l;
                }

                if (i != -1 && j != -1) {
                    if (M / 2 % 2 == 0) {
                        res ~= k + 1;
                        foreach (c; 0..M/2) res ~= (c % 2 == 0 ? i : k) + 1;
                    } else {
                        res ~= i + 1;
                        foreach (c; 0..M/2) res ~= (c % 2 == 0 ? k : i) + 1;
                    }
                    foreach (c; 0..M/2) res ~= (c % 2 == 0 ? j : k) + 1;
                    goto ok;
                }
            }
        }

        ok:
        writefln!"YES\n%(%d %)"(res);
        continue;
    }
}

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        int N, M; get(N, M);
        char[][] G; get_lines(N, G);
        int[] res;

        if (N == 2 && (G == ["*a", "b*"] || G == ["*b", "a*"])) {
            writeln("NO");
            continue;
        }

        if (M % 2 == 1) {
            foreach (i; 0..M+1) res ~= i % 2 + 1;
            goto ok;
        }

        foreach (i; 0..N-1) foreach (j; i+1..N) if (G[i][j] == G[j][i]) {
            auto pp = [i+1, j+1];
            foreach (k; 0..M+1) res ~= pp[k%2];
            goto ok;
        }

        foreach (k; 0..N) {
            int i = -1, j = -1;
            foreach (l; 0..N) {
                if (G[k][l] == 'a') {
                    i = l;
                } else if (G[k][l] == 'b') {
                    j = l;
                }
            }
            if (i != -1 && j != -1) {
                if (M / 2 % 2 == 0) {
                    res ~= k + 1;
                    foreach (c; 0..M/2) res ~= (c % 2 == 0 ? i : k) + 1;
                } else {
                    res ~= i + 1;
                    foreach (c; 0..M/2) res ~= (c % 2 == 0 ? k : i) + 1;
                }
                foreach (c; 0..M/2) res ~= (c % 2 == 0 ? j : k) + 1;
                goto ok;
            }
        }

        ok:
        writefln!"YES\n%(%d %)"(res);
        continue;
    }
}

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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        int N, M; get(N, M);
        char[][] G; get_lines(N, G);
        int[] res;

        if (N == 2 && (G == ["*a", "b*"] || G == ["*b", "a*"])) {
            writeln("NO");
            continue;
        }

        if (M % 2 == 1) {
            foreach (i; 0..M+1) res ~= i % 2 + 1;
            goto ok;
        }

        foreach (i; 0..N-1) foreach (j; i+1..N) if (G[i][j] == G[j][i]) {
            auto pp = [i+1, j+1];
            foreach (k; 0..M+1) res ~= pp[k%2];
            goto ok;
        }

        foreach (k; 0..N) {
            int i = -1, j = -1;
            foreach (l; 0..N) {
                if (G[k][l] == 'a') {
                    i = l;
                } else if (G[k][l] == 'b') {
                    j = l;
                }
            }
            if (i != -1 && j != -1) {
                res ~= k + 1;
                foreach (c; 0..M/2) res ~= (c % 2 == 0 ? i : k) + 1;
                foreach (c; 0..M/2) res ~= (c % 2 == 0 ? j : k) + 1;
                goto ok;
            }
        }

        ok:
        writefln!"YES\n%(%d %)"(res);
        continue;
    }
}

/*

a a

a b a b a

*/


A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).Let $$$p$$$ be any permutation of length $$$n$$$. We define the fingerprint $$$F(p)$$$ of $$$p$$$ as the sorted array of sums of adjacent elements in $$$p$$$. More formally,$$$$$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$$$$$For example, if $$$n=4$$$ and $$$p=[1,4,2,3],$$$ then the fingerprint is given by $$$F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$$$.You are given a permutation $$$p$$$ of length $$$n$$$. Your task is to find a different permutation $$$p'$$$ with the same fingerprint. Two permutations $$$p$$$ and $$$p'$$$ are considered different if there is some index $$$i$$$ such that $$$p_i \ne p'_i$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 668$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2\le n\le 100$$$)  — the length of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1,\ldots,p_n$$$ ($$$1\le p_i\le n$$$). It is guaranteed that $$$p$$$ is a permutation.

For each test case, output $$$n$$$ integers $$$p'_1,\ldots, p'_n$$$ — a permutation such that $$$p'\ne p$$$ and $$$F(p')=F(p)$$$. We can prove that for every permutation satisfying the input constraints, a solution exists. If there are multiple solutions, you may output any.

// Vicfred
// https://codeforces.com/contest/1405/problem/A
// math
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

    while(t--) {
        int n = readln.chomp.to!int;
        int[] p = readln.split.map!(to!int).array;
        p.reverse;
        foreach(x; p)
            writef("%s ", x);
        "".writeln;
    }
}

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

alias ll = long;
int inz(long t){ return to!int(t); }
static string[] tempstr;
T rd(T = long)() { while(!tempstr.length) tempstr = readln.chomp.split; string res = tempstr[0]; tempstr.popFront; return res.to!T; }
T[] rdarr(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }

void play(){
    int n;
    n = to!int(rd);
    ll[] arr;
    arr = rdarr;
    arr.reverse();
    foreach(el; arr){
        write(el, " ");
    }
    writeln;
}

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

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

string FMT_F = "%.10f";

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

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

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

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

		ans[ti] = p;
	}

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


Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.

NoteIn the first test case, you can add roads   from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$,  from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$,  from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$,  from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$,  from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$.  The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		long cur = 0;
		long res = 0;
		foreach_reverse (i; 0..n - 1)
		{
			cur += (a[i + 1] - a[i]) * (n - i - 1L);
			res += cur;
		}
		writeln (a.back - res);
	}
}

// Vicfred
// https://codeforces.com/contest/1541/problem/C
//
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

while(t--) {
    int n = readln.chomp.to!int;
    long[] d = readln.split.map!(to!long).array;
    d.sort;
    long ans = d[n - 1];
    long node = 0L;
    for(int i = 0; i < n; ++i) {
        ans -= d[i] * i - node;
        node += d[i];
    }
    ans.writeln;
}
}

import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,
       std.container, std.typecons, std.conv, std.random, std.bigint;

void read(S...)(ref S args) {
  auto input = readln.split;
  enforce(input.length == args.length);
  foreach (i, ref arg; args) {
    arg = input[i].to!(S[i]);
  }
}

auto list(T)() { return readln.split.map!(e => e.to!T).array; }

void main() {
  int t;
  read(t);

  while (t--) {
    int n;
    read(n);
    auto arr = list!long;

    long answer = 0;
    long current = 0;

    sort(arr);
    foreach (i; 1 .. n) {
      auto w = arr[i] - arr[i - 1];
      answer += w;
      answer -= i * w + current;
      current += i * w;
    }

    writeln(answer);
  }
}

// Cheese-Cracker: cheese-cracker.github.io

void theCode(){
    int n = scan!int;
    auto arr = scanArray;
    arr.sort;
    ll[] pref = [0];
    for(int i = 1; i < n; ++i){
        pref ~= pref.back + arr[i];
    }
    ll cost = 0;
    for(int i = 1; i < n; ++i){
        cost += (i - 1) * 1L * arr[i];
        if(i > 1) cost -= pref[i-2];
    }
    writeln(- cost);
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; 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; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		long cur = 0;
		long res = 0;
		foreach_reverse (i; 0..n - 1)
		{
			cur += (a[i + 1] - a[i]) * (n - i - 1);
			res += cur;
		}
		writeln (a.back - res);
	}
}


Alex decided to go on a touristic trip over the country.For simplicity let's assume that the country has $$$n$$$ cities and $$$m$$$ bidirectional roads connecting them. Alex lives in city $$$s$$$ and initially located in it. To compare different cities Alex assigned each city a score $$$w_i$$$ which is as high as interesting city seems to Alex.Alex believes that his trip will be interesting only if he will not use any road twice in a row. That is if Alex came to city $$$v$$$ from city $$$u$$$, he may choose as the next city in the trip any city connected with $$$v$$$ by the road, except for the city $$$u$$$.Your task is to help Alex plan his city in a way that maximizes total score over all cities he visited. Note that for each city its score is counted at most once, even if Alex been there several times during his trip.

First line of input contains two integers $$$n$$$ and $$$m$$$, ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le m \le 2 \cdot 10^5$$$) which are numbers of cities and roads in the country. Second line contains $$$n$$$ integers $$$w_1, w_2, \ldots, w_n$$$ ($$$0 \le w_i \le 10^9$$$) which are scores of all cities. The following $$$m$$$ lines contain description of the roads. Each of these $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$) which are cities connected by this road. It is guaranteed that there is at most one direct road between any two cities, no city is connected to itself by the road and, finally, it is possible to go from any city to any other one using only roads. The last line contains single integer $$$s$$$ ($$$1 \le s \le n$$$), which is the number of the initial city.

Output single integer which is the maximum possible sum of scores of visited cities.

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

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

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

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

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

int N, M;
long[] W;
int[] U, V;
int S;

int[][] G;
int zeit;
int[] par, dis, low;

void dfs0(int u, int p) {
  par[u] = p;
  dis[u] = low[u] = zeit++;
  foreach (v; G[u]) {
    if (v != p) {
      if (dis[v] == -1) {
        dfs0(v, u);
        chmin(low[u], low[v]);
      } else {
        chmin(low[u], dis[v]);
      }
    }
  }
}

bool isBridge(int u, int v) {
  if (dis[u] > dis[v]) swap(u, v);
  return (u == par[v] && dis[v] <= low[v]);
}

int[][] H;
int[] sz;
long[] scores;
bool[] free;
long[] dp;

void dfs1(int x, int p) {
  foreach (y; H[x]) {
    if (y != p) {
      dfs1(y, x);
      free[x] = free[x] || free[y];
      chmax(dp[x], dp[y]);
    }
  }
  free[x] = free[x] || (sz[x] >= 2);
  dp[x] += scores[x];
}

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

      G = new int[][N];
      foreach (i; 0 .. M) {
        G[U[i]] ~= V[i];
        G[V[i]] ~= U[i];
      }
      par = new int[N];
      dis = new int[N];
      low = new int[N];
      dis[] = -1;
      dfs0(S, -1);
      debug {
        writeln("par = ", par);
        writeln("dis = ", dis);
        writeln("low = ", low);
        foreach (i; 0 .. M) {
          write(isBridge(U[i], V[i]), " ");
        }
        writeln();
      }

      auto uf = new int[N];
      uf[] = -1;
      foreach (i; 0 .. M) {
        if (!isBridge(U[i], V[i])) {
          uf.connect(U[i], V[i]);
        }
      }
      H = new int[][N];
      foreach (i; 0 .. M) {
        if (isBridge(U[i], V[i])) {
          const x = uf.root(U[i]);
          const y = uf.root(V[i]);
          assert(x != y);
          H[x] ~= y;
          H[y] ~= x;
        }
      }
      sz = new int[N];
      scores = new long[N];
      foreach (u; 0 .. N) {
        const x = uf.root(u);
        ++sz[x];
        scores[x] += W[u];
      }
      free = new bool[N];
      dp = new long[N];
      dfs1(uf.root(S), -1);
      debug {
        writeln("uf = ", uf);
        writeln("free = ", free);
        writeln("dp = ", dp);
      }

      long ans;
      foreach (x; 0 .. N) {
        if (uf[x] < 0) {
          if (!free[x]) {
            chmax(ans, dp[x]);
          }
        }
      }
      foreach (x; 0 .. N) {
        if (uf[x] < 0) {
          if (free[x]) {
            ans += scores[x];
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


You are given an integer $$$n$$$.You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times:   Replace $$$n$$$ with $$$\frac{n}{2}$$$ if $$$n$$$ is divisible by $$$2$$$;  Replace $$$n$$$ with $$$\frac{2n}{3}$$$ if $$$n$$$ is divisible by $$$3$$$;  Replace $$$n$$$ with $$$\frac{4n}{5}$$$ if $$$n$$$ is divisible by $$$5$$$. For example, you can replace $$$30$$$ with $$$15$$$ using the first operation, with $$$20$$$ using the second operation or with $$$24$$$ using the third operation.Your task is to find the minimum number of moves required to obtain $$$1$$$ from $$$n$$$ or say that it is impossible to do it.You have to answer $$$q$$$ independent queries.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 1000$$$) — the number of queries. The next $$$q$$$ lines contain the queries. For each query you are given the integer number $$$n$$$ ($$$1 \le n \le 10^{18}$$$).

Print the answer for each query on a new line. If it is impossible to obtain $$$1$$$ from $$$n$$$, print -1. Otherwise, print the minimum number of moves required to do it.

import std.stdio;

void main() {
  int nt;
  readf!" %d" (nt);
  while (--nt >= 0) {
    long x;
    int res;
    readf!" %d" (x);
    while (x > 1) {
      if (!(x % 2)) {
        x /= 2;
        ++res;
      } else if (!(x % 3)) {
        x /= 3;
        res += 2;
      } else if (!(x % 5)) {
        x /= 5;
        res += 3;
      } else {
        res = -1;
        break;
      }
    }
    writeln (res);
  }
}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;
import std.math;

alias Tree = RBT!int;
alias ll = long;

ll[1000] nn;

void main() {
  GC.disable();

  int q;
  readf(" %s", q);

  foreach(i;0 ..q) {
    ll n;
    readf(" %s", n);
    ll c5=0;
    ll c3=0;
    ll c2=0;
    while(n % 5==0) {c5++; n /=5;}
    while(n % 3==0) {c3++; n /=3;}
    while(n % 2==0) {c2++; n /=2;}

    if (n != 1) writeln(-1);
    else writeln(c5 + c3 + 2*c5 + c3 + c2);
  }

}

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

string FMT_F = "%.10f";

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

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

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto q = RD!int;
	auto ans = new long[](q);
	foreach (i; 0..q)
	{
		auto n = RD;
		long cnt;
		while (n != 1)
		{
			if (n % 2 == 0)
			{
				n /= 2;
			}
			else if ((n * 2) % 3 == 0)
			{
				n = (n * 2) / 3;
			}
			else if ((n * 4) % 5 == 0)
			{
				n = (n * 4) /5;
			}
			else
			{
				cnt = -1;
				break;
			}
			++cnt;
		}
		ans[i] = cnt;
	}

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


There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has n characters. The characters are labeled from 1 to n. Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state).You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A).You are given a list of m known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count modulo 1 000 000 007.

The first line of input will contain two integers n, m (3 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000). The next m lines will contain the description of the known relationships. The i-th line will contain three integers ai, bi, ci. If ci is 1, then ai and bi are in love, otherwise, they hate each other (1 ≤ ai, bi ≤ n, ai ≠ bi, ). Each pair of people will be described no more than once.

Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo 1 000 000 007.

NoteIn the first sample, the four ways are to:   Make everyone love each other  Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this). In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other.

import std.algorithm;
import std.range;
import std.stdio;

immutable int MOD = 1_000_000_007;
immutable int NA  =            -1;

void main ()
{
	int n;
	int m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto p = n.iota.array;
		auto s = n.iota.map !(x => [x]).array;

		void unite (int u, int v)
		{
			u = p[u];
			v = p[v];
			if (u == v)
			{
				return;
			}
			if (s[u].length > s[v].length)
			{
				swap (u, v);
			}
			foreach (c; s[u])
			{
				p[c] = v;
				s[v] ~= c;
			}
		}

		auto a = new int [] [n];
		foreach (j; 0..m)
		{
			int u, v, w;
			readf (" %s %s %s", &u, &v, &w);
			u--;
			v--;
			if (w == 0)
			{
				a[u] ~= v;
				a[v] ~= u;
			}
			else if (w == 1)
			{
				unite (u, v);
			}
			else
			{
				assert (false);
			}
		}

		auto g = new int [] [n];
		foreach (i; 0..n)
		{
			if (p[i] == i)
			{
				foreach (c; s[i])
				{
					foreach (d; a[c])
					{
						g[i] ~= p[d];
					}
				}
			}
		}
		debug {writeln (g);}

		auto b = new bool [n];
		auto c = new int [n];
		c[] = NA;
		long res = 1;

		void recur (int v, int z)
		{
			assert (v == p[v]);
			if (c[v] != NA && c[v] != z)
			{
				res = 0;
				return;
			}
			if (b[v])
			{
				return;
			}
			b[v] = true;
			c[v] = z;
			foreach (u; g[v])
			{
				recur (u, !z);
			}
		}

		foreach (i; 0..n)
		{
			if (p[i] == i && !b[i])
			{
				recur (i, 0);
				res = (res * 2) % MOD;
			}
		}

		res = (res * (MOD / 2 + 1)) % MOD;
		writeln (res);
	}
}


Alice and Bob play a game. Initially they have a string $$$s_1, s_2, \dots, s_n$$$, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length $$$a$$$, and Bob must select a substring of length $$$b$$$. It is guaranteed that $$$a > b$$$.For example, if $$$s =$$$ ...X.. and $$$a = 3$$$, $$$b = 2$$$, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string $$$s =$$$ ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.You have to answer $$$q$$$ independent queries.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 3 \cdot 10^5$$$) — the number of queries. The first line of each query contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le b < a \le 3 \cdot 10^5$$$). The second line of each query contains the string $$$s$$$ ($$$1 \le |s| \le 3 \cdot 10^5$$$), consisting of only characters . and X. It is guaranteed that sum of all $$$|s|$$$ over all queries not exceed $$$3 \cdot 10^5$$$.

For each test case print YES if Alice can win and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

NoteIn the first query Alice can select substring $$$s_3 \dots s_5$$$. After that $$$s$$$ turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.In the second query Alice can not win because she cannot even make one move.In the third query Alice can choose substring $$$s_2 \dots s_6$$$. After that $$$s$$$ turns into .XXXXX.X..X, and Bob can't make a move after that.

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

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

void solve(){
	int q = rint;
	foreach(_; 0 .. q){
		int a = rint, b = rint;
		string s = read;

		string[] us = s.split("X");
		int cntb, cnt2b, cnta;
		int thelen;
		foreach(u; us){
			int x = u.length.to!int;
			if(x >= b && x < a) cntb += 1;
			if(x >= a){
				if(x >= b * 2){
					cnt2b += 1;
					thelen = x;
				}
				else cnta += 1;
			}
		}
		log("cntb:", cntb, "cnt2b:", cnt2b, "cnta:", cnta, "thelen:", thelen);
		if(cntb >= 1) "NO".writeln;
		else if(cnt2b >= 2) "NO".writeln;
		else if(cnt2b == 1){
			string ans = "NO";
			if(thelen >= a && thelen <= a + b * 2 - 2){
				// Alice can choose {(x <= b - 1), a, (x <= b - 1)}
				if(cnta %2 == 0) ans = "YES";
			}
			if(thelen >= a * 2 && thelen <= a + b * 3 - 2){
				//  Alice can choose {(x <= b - 1), a, (a <= x <= 2b - 1)}
				if(cnta %2 == 1) ans = "YES";
			}
			if(thelen >= a * 3 && thelen <= a + b * 4 - 2){
				//  Alice can choose {(a <= x <= 2b - 1), a, (a <= x <= 2b - 1)}
				if(cnta %2 == 0) ans = "YES";
			}
			ans.writeln;
		}
		else if(cnta % 2 == 1) "YES".writeln;
		else "NO".writeln;
	}
}

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

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

void solve(){
	int q = rint;
	foreach(_; 0 .. q){
		int a = rint, b = rint;
		string s = read;

		string[] us = s.split("X");
		int cntb, cnt2b, cnta;
		int thelen;
		foreach(u; us){
			int x = u.length.to!int;
			if(x >= b && x < a) cntb += 1;
			if(x >= a){
				if(x >= b * 2){
					cnt2b += 1;
					thelen = x;
				}
				else cnta += 1;
			}
		}
		log("cntb:", cntb, "cnt2b:", cnt2b, "cnta:", cnta, "thelen:", thelen);
		if(cntb >= 1) "NO".writeln;
		else if(cnt2b >= 2) "NO".writeln;
		else if(cnt2b == 1){
			string ans = "NO";
			if(thelen >= a && thelen <= a + b * 2 - 2){
				// Alice can choose {(x <= b - 1), a, (x <= b - 1)}
				if(cnta %2 == 0) ans = "YES";
			}
			if(thelen >= a * 2 && thelen <= a + b * 3 - 2){
				//  Alice can choose {(x <= b - 1), a, (a <= x <= 2b - 1)}
				if(cnta %2 == 1) ans = "YES";
			}
			ans.writeln;
		}
		else if(cnta % 2 == 1) "YES".writeln;
		else "NO".writeln;
	}
}

/*

One or more length b <= x < a
	-> Bob.
Two or more length x >= 2b
	-> Bob.
Just one length x >= 2b
	It is length x >= a + 2b - 1
		-> Bob.
	Otherwise
		-> cnt += 1
How many length a <= x < 2b
	-> cnt += it

cnt is odd
	-> Alice
cnt is even
	-> Bob

*/


There are two rival donut shops.The first shop sells donuts at retail: each donut costs $$$a$$$ dollars.The second shop sells donuts only in bulk: box of $$$b$$$ donuts costs $$$c$$$ dollars. So if you want to buy $$$x$$$ donuts from this shop, then you have to buy the smallest number of boxes such that the total number of donuts in them is greater or equal to $$$x$$$.You want to determine two positive integer values:   how many donuts can you buy so that they are strictly cheaper in the first shop than in the second shop?  how many donuts can you buy so that they are strictly cheaper in the second shop than in the first shop? If any of these values doesn't exist then that value should be equal to $$$-1$$$. If there are multiple possible answers, then print any of them.The printed values should be less or equal to $$$10^9$$$. It can be shown that under the given constraints such values always exist if any values exist at all.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Each of the next $$$t$$$ lines contains three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \le a \le 10^9$$$, $$$2 \le b \le 10^9$$$, $$$1 \le c \le 10^9$$$).

For each testcase print two positive integers. For both shops print such $$$x$$$ that buying $$$x$$$ donuts in this shop is strictly cheaper than buying $$$x$$$ donuts in the other shop. $$$x$$$ should be greater than $$$0$$$ and less or equal to $$$10^9$$$. If there is no such $$$x$$$, then print $$$-1$$$. If there are multiple answers, then print any of them.

NoteIn the first testcase buying any number of donuts will be cheaper in the second shop. For example, for $$$3$$$ or $$$5$$$ donuts you'll have to buy a box of $$$10$$$ donuts for $$$4$$$ dollars. $$$3$$$ or $$$5$$$ donuts in the first shop would cost you $$$15$$$ or $$$25$$$ dollars, respectively, however. For $$$20$$$ donuts you'll have to buy two boxes for $$$8$$$ dollars total. Note that $$$3$$$ and $$$5$$$ are also valid answers for the second shop, along with many other answers.In the second testcase buying any number of donuts will be either cheaper in the first shop or the same price. $$$8$$$ donuts cost $$$32$$$ dollars in the first shop and $$$40$$$ dollars in the second shop (because you have to buy two boxes). $$$10$$$ donuts will cost $$$40$$$ dollars in both shops, so $$$10$$$ is not a valid answer for any of the shops.In the third testcase $$$1$$$ donut costs $$$2$$$ and $$$3$$$ dollars, respectively. $$$2$$$ donuts cost $$$4$$$ and $$$3$$$ dollars. Thus, $$$1$$$ is a valid answer for the first shop and $$$2$$$ is a valid answer for the second shop.In the fourth testcase $$$10^9$$$ donuts cost $$$10^{18}$$$ dollars in the first shop and $$$10^9$$$ dollars in the second shop.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		long a, b, c;
		readf !(" %s %s %s") (a, b, c);
		auto res1 = (a < c) ? 1 : -1;
		auto res2 = (c < a * b) ? b : -1;
		writeln (res1, " ", res2);
	}
}

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 long[][](t, 2);
	foreach (ti; 0..t)
	{
		auto a = RD;
		auto b = RD;
		auto c = RD;

		if (a < c)
			ans[ti][0] = 1;
		else
			ans[ti][0] = -1;

		if (a*b > c)
			ans[ti][1] = b;
		else
			ans[ti][1] = -1;
	}

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


Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence .Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].

The only input line contains an integer n (1 ≤ n ≤ 105).

In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists. If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n. If there are multiple solutions, you are allowed to print any of them.

NoteFor the second sample, there are no valid sequences.

/* submitted, so what? */
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;

long powmod (int a, int b, int c)
{
	long cur = a % c;
	long res = 1 % c;
	while (b)
	{
		if (b & 1)
		{
			res = (res * cur) % c;
		}
		cur = (cur * cur) % c;
		b >>= 1;
	}
	return res;
}

void main ()
{
	int n;
main_loop:
	while (readf (" %s", &n) > 0)
	{
		if (n == 4)
		{
			write ("YES\n1\n3\n2\n4\n");
			continue main_loop;
		}

		for (int i = 2; i * i <= n; i++)
		{
			if (n % i == 0)
			{
				writeln ("NO");
				continue main_loop;
			}
		}

		writeln ("YES");
		if (n > 1)
		{
			writeln (1);
		}
		foreach (i; 2..n)
		{
			writeln ((i * powmod (i - 1, n - 2, n)) % n);
		}
		writeln (n);
	}
}

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

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

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

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

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

bool isPrime(long n) {
	if (n < 2) {
		return false;
	}
	for (long d = 2; d * d <= n; ++d) {
		if (n % d == 0) {
			return false;
		}
	}
	return true;
}

long[] gs;

bool isPrimitive(long p, long g) {
	gs = new long[0];
	gs ~= 1;
	foreach (i; 1 .. p - 1) {
		gs ~= (gs[$ - 1] * g) % p;
	}
	foreach (i; 1 .. p - 1) {
		if (gs[cast(size_t)(i)] == 1) {
			return false;
		}
	}
	return true;
}

long N;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readLong;

		if (N == 1) {
			writeln("YES");
			writeln(1);
		} else if (N == 2) {
			writeln("YES");
			writeln(1);
			writeln(2);
		} else if (N == 4) {
			writeln("YES");
			writeln(1);
			writeln(3);
			writeln(2);
			writeln(4);
		} else if (isPrime(N)) {
			for (long g = 2; ; ++g) {
				if (isPrimitive(N, g)) {
debug{
writeln("gs = ",gs);
}
					writeln("YES");
					writeln(1);
					foreach (i; 0 .. (N - 3) / 2) {
debug{
// writeln("g^",1+2*i);
// writeln("g^",N-3-2*i);
}
						writeln(gs[cast(size_t)(1 + 2 * i)]);
						writeln(gs[cast(size_t)(N - 3 - 2 * i)]);
					}
debug{
// writeln("g^",N-2);
}
					writeln(gs[cast(size_t)(N - 2)]);
					writeln(N);
					break;
				}
			}
		} else {
			writeln("NO");
		}
	}
	} catch (EOFException) {}
}


A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly $$$n$$$ minutes. After a round ends, the next round starts immediately. This is repeated over and over again.Each round has the same scenario. It is described by a sequence of $$$n$$$ numbers: $$$d_1, d_2, \dots, d_n$$$ ($$$-10^6 \le d_i \le 10^6$$$). The $$$i$$$-th element means that monster's hp (hit points) changes by the value $$$d_i$$$ during the $$$i$$$-th minute of each round. Formally, if before the $$$i$$$-th minute of a round the monster's hp is $$$h$$$, then after the $$$i$$$-th minute it changes to $$$h := h + d_i$$$.The monster's initial hp is $$$H$$$. It means that before the battle the monster has $$$H$$$ hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to $$$0$$$. Print -1 if the battle continues infinitely.

The first line contains two integers $$$H$$$ and $$$n$$$ ($$$1 \le H \le 10^{12}$$$, $$$1 \le n \le 2\cdot10^5$$$). The second line contains the sequence of integers $$$d_1, d_2, \dots, d_n$$$ ($$$-10^6 \le d_i \le 10^6$$$), where $$$d_i$$$ is the value to change monster's hp in the $$$i$$$-th minute of a round.

Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer $$$k$$$ such that $$$k$$$ is the first minute after which the monster is dead.

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

void main()
{
    auto v = readln.chomp.split;
    auto H = v[0].to!long;
    auto n = v[1].to!int;

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

    long cur = 0;
    long reach = 0;
    foreach (i, e; d.enumerate(1)) {
        cur += e;
        reach = min(reach, cur);
        if (H + cur <= 0) {
            writeln(i);
            return;
        }
    }

    long sm = d.sum(0L);

    debug { sm.writeln; }

    if (sm >= 0) {
        writeln(-1);
        return;
    }

    auto reps = (H - abs(reach)) / abs(sm);

    debug { writeln(H, ' ', reps); }

    cur = H + reps * sm;
    foreach (i, e; (d ~ d).enumerate(1)) {
        cur += e;
        if (cur <= 0) {
            writeln(reps * n + i);
            return;
        }
    }

    assert(false);
}


Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)·p ≤ n; q ≥ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements.Sereja needs to rush to the gym, so he asked to find all the described positions of q.

The first line contains three integers n, m and p (1 ≤ n, m ≤ 2·105, 1 ≤ p ≤ 2·105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The next line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 109).

In the first line print the number of valid qs. In the second line, print the valid values in the increasing order.

import std.stdio, std.string;
import std.algorithm;

void solve(int[] a, int[] b, int n, int m, int p)
{
    int[int] cntb;
    foreach (v; b)
    {
        if (v !in cntb) cntb[v] = 1;
        else ++ cntb[v];
    }
    int[] res;
    foreach (i; 0 .. min(n, p))
    {
        auto d = m;
        int[int] cnta;
        int j;
        for (j = i; j < n && (j - i) / p < m; j += p)
        {
            if (a[j] !in cnta) cnta[a[j]] = 1;
            else ++ cnta[a[j]];
            if (a[j] !in cntb)
            {
                ++ d;
            }
            else
            {
                if (cnta[a[j]] <= cntb[a[j]]) -- d;
                else ++ d;
            }
        }
        if ((j - i) / p < m) continue;
        if (d == 0) res ~= i;
        for (int left = i, right = j; right < n; left += p, right += p)
        {
            if (a[left] != a[right])
            {
                -- cnta[a[left]];
                if (a[left] !in cntb)
                {
                    -- d;
                }
                else
                {
                    if (cnta[a[left]] < cntb[a[left]]) ++ d;
                    else -- d;
                }
                ++ cnta[a[right]];
                if (a[right] !in cntb)
                {
                    ++ d;
                }
                else
                {
                    if (cnta[a[right]] <= cntb[a[right]]) -- d;
                    else ++ d;
                }
            }
            if (d == 0) res ~= left + p;
        }
    }
    writeln(res.length);
    if (res.length > 0)
    {
        res.sort();
        foreach (v; res) write(v + 1, " ");
        writeln();
    }
}

int main(string[] args)
{
    int n, m, p;
    while (readf("%d %d %d\n", &n, &m, &p) == 3)
    {
        auto a = new int[n];
        auto b = new int[m];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        foreach (i; 0 .. m)
        {
            readf(" %d", &b[i]);
        }
        readln;
        solve(a, b, n, m, p);
    }
    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, m, p;
	while (readf (" %s %s %s", &n, &m, &p) > 0)
	{
		auto a = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s", &a[i]);
		}
		auto b = new int [m];
		foreach (j; 0..m)
		{
			readf (" %s", &b[j]);
		}

		int [] ans;
		if ((m - 1) * cast (long) p < n)
		{
			foreach (k; 0..p)
			{
				int [int] e;
				int d = m;
				foreach (j; 0..m)
				{
					e[b[j]]++;
				}

				for (int i = k, j = i - p * m; i < n;
				     i += p, j += p)
				{
					e[a[i]]--;
					int cur = e[a[i]];
					if (cur >= 0)
					{
						d--;
					}
					else
					{
						d++;
					}

					if (j >= 0)
					{
						e[a[j]]++;
						int prev = e[a[j]];
						if (prev <= 0)
						{
							d--;
						}
						else
						{
							d++;
						}
					}

					debug {writeln (k, " ", i, " ",
					                j, " ", d);}
					if (d == 0)
					{
						ans ~= j + p + 1;
					}
				}
			}
		}

		ans.sort !("a < b", SwapStrategy.stable);
		writefln ("%s\n%(%s %)", ans.length, ans);
	}
}

import std.stdio, std.string;
import std.algorithm;

void solve(int[] a, int[] b, int n, int m, int p)
{
    int[int] cntb;
    foreach (v; b)
    {
        if (v !in cntb) cntb[v] = 1;
        else ++ cntb[v];
    }
    int[] res;
    foreach (i; 0 .. min(n, p))
    {
        auto d = m;
        int[int] cnta;
        int j;
        for (j = i; j < n && (j - i) / p < m; j += p)
        {
            if (a[j] !in cnta) cnta[a[j]] = 1;
            else ++ cnta[a[j]];
            if (a[j] !in cntb)
            {
                ++ d;
            }
            else
            {
                if (cnta[a[j]] <= cntb[a[j]]) -- d;
                else ++ d;
            }
        }
        if ((j - i) / p < m) continue;
        if (d == 0) res ~= i;
        for (int left = i, right = j; right < n; left += p, right += p)
        {
            if (a[left] != a[right])
            {
                -- cnta[a[left]];
                if (a[left] !in cntb)
                {
                    -- d;
                }
                else
                {
                    if (cnta[a[left]] < cntb[a[left]]) ++ d;
                    else -- d;
                }
                ++ cnta[a[right]];
                if (a[right] !in cntb)
                {
                    ++ d;
                }
                else
                {
                    if (cnta[a[right]] <= cntb[a[right]]) -- d;
                    else ++ d;
                }
            }
            if (d == 0) res ~= left + p;
        }
    }
    writeln(res.length);
    if (res.length > 0)
    {
        foreach (v; res) write(v + 1, " ");
        writeln();
    }
}

int main(string[] args)
{
    int n, m, p;
    while (readf("%d %d %d\n", &n, &m, &p) == 3)
    {
        auto a = new int[n];
        auto b = new int[m];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        foreach (i; 0 .. m)
        {
            readf(" %d", &b[i]);
        }
        readln;
        solve(a, b, n, m, p);
    }
    return 0;
}


Let's call the string beautiful if it does not contain a substring of length at least $$$2$$$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $$$3$$$ letters of the Latin alphabet (in lowercase).You are given a string $$$s$$$ of length $$$n$$$, each character of the string is one of the first $$$3$$$ letters of the Latin alphabet (in lowercase).You have to answer $$$m$$$ queries — calculate the cost of the substring of the string $$$s$$$ from $$$l_i$$$-th to $$$r_i$$$-th position, inclusive.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$ and the number of queries. The second line contains the string $$$s$$$, it consists of $$$n$$$ characters, each character one of the first $$$3$$$ Latin letters. The following $$$m$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — parameters of the $$$i$$$-th query.

For each query, print a single integer — the cost of the substring of the string $$$s$$$ from $$$l_i$$$-th to $$$r_i$$$-th position, inclusive.

NoteConsider the queries of the example test.  in the first query, the substring is baa, which can be changed to bac in one operation;  in the second query, the substring is baacb, which can be changed to cbacb in two operations;  in the third query, the substring is cb, which can be left unchanged;  in the fourth query, the substring is aa, which can be changed to ba in one operation.

import std;

void main () {
    immutable q = readln.chomp.splitter(' ').dropOne.front.to!uint;
    immutable s = readln.chomp;

    auto costs = (){
        scope result = appender!(uint[]);
        result.reserve(6 * (s.length + 1));

        [
            "abc",
            "acb",
            "bac",
            "bca",
            "cab",
            "cba"
        ].map!(patt => patt.makePrefix(s))
            .each!(v => v.each!(a => result.put(a)));

        assert(result[].length == 6 * (s.length + 1));
        return result[].assumeUnique.chunks(s.length + 1);
    }();

    stdin
        .byLine
        .map!((char[] line) {
                uint[2] query = line
                    .splitter(' ')
                    .map!(to!uint)
                    .staticArray!2;

                return tuple(query[0], query[1]);
        })
        .map!(query =>
              costs
                .map!(v => v[query[1]] - v[query[0] - 1])
                .minElement
        )
        .each!writeln;
}

auto makePrefix (in string patt, in string s) {
    return chain(
        [0u],
        zip(s.byCodeUnit, patt.byCodeUnit.cycle)
            .cumulativeFold!"a + (b[0] != b[1])"(0u)
    );
}
// ""

import std;

void main () {
    immutable q = readln.chomp.splitter(' ').dropOne.front.to!uint;
    immutable s = readln.chomp;

    auto costs = (){
        scope result = appender!(uint[]);
        [
            "abc",
            "acb",
            "bac",
            "bca",
            "cab",
            "cba"
        ].map!(patt => patt.makePrefix(s))
            .each!(v => v.each!(a => result.put(a)));

        assert(result[].length == 6 * (s.length + 1));
        return result[].assumeUnique.chunks(s.length + 1);
    }();

    stdin
        .byLine
        .map!((char[] line) {
                uint[2] query = line
                    .splitter(' ')
                    .map!(to!uint)
                    .staticArray!2;

                return tuple(query[0], query[1]);
        })
        .map!(query =>
              costs
                .map!(v => v[query[1]] - v[query[0] - 1])
                .minElement
        )
        .each!writeln;
}

auto makePrefix (in string patt, in string s) {
    return chain(
        [0u],
        zip(s.byCodeUnit, patt.byCodeUnit.cycle)
            .cumulativeFold!"a + (b[0] != b[1])"(0u)
    );
}
// ""

import std.stdio;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

auto sum(R)(R r, size_t i, size_t j)
{
	if (i > j) return 0;
	if (i == 0) return r[j];
	return r[j] - r[i-1];
}

void main()
{
	int n = readInt!int;
	int m = readInt!int;
	auto s = readString.map!(c => cast(int)c - 'a').array;
	int[][] perm = new int[][](6, 3);
	int[] currp = [0, 1, 2];
	{
	size_t i = 0;
	do
	{
		perm[i++][] = currp;
	} while (nextPermutation(currp));
	}
	int[][] dist = new int[][](6, n);
	foreach(p; 0 .. 6)
	{
		foreach(i; 0 .. n)
		{
			dist[p][i] = s[i] != perm[p][i%3];
		}
	}
	auto prefSum = new int[][](6);
	foreach(p; 0 .. 6)
	{
		prefSum[p] = dist[p].cumulativeFold!((a, b)=>a + b).array;
	}
	foreach(i; 0 .. m)
	{
		auto l = readInt!int - 1;
		auto r = readInt!int - 1;
		iota(0, 6).map!(p => sum(prefSum[p], l, r)).fold!min.writeln;
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

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

void popChar()
{
	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;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

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

int[3][6] perm =
[
  [0, 1, 2],
  [0, 2, 1],
  [1, 0, 2],
  [1, 2, 0],
  [2, 0, 1],
  [2, 1, 0],
];

void main()
{
    int n, m;
    readf!" %d %d "(n, m);
    auto s = readln.strip;
    int[][6] pf = [[0], [0], [0], [0], [0], [0]];
    int[6] sum;
    foreach (i ; 0 .. n) {
      int x = s[i] - 'a';
      foreach (j ; 0 .. 6) {
        sum[j] += (x != perm[j][i % 3]);
        pf[j] ~= sum[j];
      }
    }
    while (m--) {
      int l, r;
      readf!" %d %d "(l, r);
      int ans = int.max;
      foreach (j ; 0 .. 6) {
        ans = min(ans, pf[j][r] - pf[j][l - 1]);
      }
      writeln(ans);
    }
}


A tree is an undirected connected graph without cycles.Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is considered its own parent).    For this rooted tree the array p is [2, 3, 3, 2]. Given a sequence p1, p2, ..., pn, one is able to restore a tree:  There must be exactly one index r that pr = r. A vertex r is a root of the tree.  For all other n - 1 vertices i, there is an edge between vertex i and vertex pi. A sequence p1, p2, ..., pn is called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences (1,2,2), (2,3,1) and (2,1,3) are not valid.You are given a sequence a1, a2, ..., an, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them.

The first line of the input contains an integer n (2 ≤ n ≤ 200 000) — the number of vertices in the tree. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).

In the first line print the minimum number of elements to change, in order to get a valid sequence. In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in the minimum number of changes. If there are many such sequences, any of them will be accepted.

NoteIn the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (because p4 = 4), which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right drawing below). On both drawings, roots are painted red.  In the second sample, the given sequence is already valid.

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

immutable int NA = -1;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(int)).array;
		a[] -= 1;
		auto p = new int [n];
		p[] = NA;
		auto b = new bool [n];
		b[] = false;

		int recur (int v)
		{
			if (p[v] != NA)
			{
				return p[v];
			}
			else if (b[v])
			{
				p[v] = v;
				return p[v];
			}
			else
			{
				b[v] = true;
				p[v] = recur (a[v]);
				return p[v];
			}
		}

		foreach (i; 0..n)
		{
			if (p[i] == NA)
			{
				p[i] = recur (i);
			}
		}

		int res = 0;
		int start = NA;
		int good = NA;
		foreach (i; 0..n)
		{
			if (p[i] == i)
			{
				res += 1;
				start = i;
				if (a[i] == i)
				{
					good = i;
				}
			}
		}

		if (good == NA)
		{
			good = start;
		}
		else
		{
			res -= 1;
		}

		foreach (i; 0..n)
		{
			if (p[i] == i)
			{
				a[i] = good;
			}
		}

		a[] += 1;
		writeln (res);
		writefln ("%(%s %)", a);
	}
}


Jury picked a polynomial $$$f(x) = a_0 + a_1 \cdot x + a_2 \cdot x^2 + \dots + a_k \cdot x^k$$$. $$$k \le 10$$$ and all $$$a_i$$$ are integer numbers and $$$0 \le a_i < 10^6 + 3$$$. It's guaranteed that there is at least one $$$i$$$ such that $$$a_i > 0$$$.Now jury wants you to find such an integer $$$x_0$$$ that $$$f(x_0) \equiv 0 \mod (10^6 + 3)$$$ or report that there is not such $$$x_0$$$.You can ask no more than $$$50$$$ queries: you ask value $$$x_q$$$ and jury tells you value $$$f(x_q) \mod (10^6 + 3)$$$.Note that printing the answer doesn't count as a query.

NoteThe polynomial in the first sample is $$$1000002 + x^2$$$.The polynomial in the second sample is $$$1 + x^2$$$.

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;

immutable long MOD = 10^^6 + 3;

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}

int gaussianElimination(ref long[][] org_mat, ref long[] ret) {
    int N = org_mat.length.to!int;

    auto mat = new long[][](N, N+1);
    foreach (i; 0..N)
        foreach (j; 0..N+1)
            mat[i][j] = org_mat[i][j];

    foreach (i; 0..N) {
        long maxval = 0;
        int maxrow = -1;
        foreach (j; i..N)
            if (abs(mat[j][i]) > maxval)
                maxval = abs(mat[j][i]), maxrow = j;
        if (maxval == 0)
            return -1;
        swap(mat[i], mat[maxrow]);

        foreach (j; i+1..N+1)
            mat[i][j] = mat[i][j] * powmod(mat[i][i], MOD-2, MOD) % MOD;
        mat[i][i] = 1;

        foreach (j; 0..N) {
            if (j == i) continue;
            long mul = mat[j][i];
            foreach (k; i..N+1) {
                (mat[j][k] -= mul * mat[i][k] % MOD) %= MOD;
                mat[j][k] = (mat[j][k] + MOD) % MOD;
            }
        }
    }

    foreach (i; 0..N)
        ret[i] = mat[i][N];

    return 0;
}

long[] B;

long ask(long x) {
    writeln("? ", x);
    debug {
        long res = 0;
        foreach (i; 0..11) (res += B[i] * powmod(x, i, MOD) % MOD) %= MOD;
        return res;
    }
    stdout.flush;
    return readln.chomp.to!long;
}

void ans(long x) {
    writeln("! ", x);
    return;
}

void main() {
    debug {
        B = new long[](11);
        B[0] = 1000002;
        B[2] = 1;
    }

    int N = 11;
    auto mat = new long[][](N, N+1);

    foreach (i; 0..N) {
        foreach (j; 0..N) mat[i][j] = powmod(i, j, MOD);
        mat[i][N] = ask(i);
    }

    auto A = new long[](N);
    gaussianElimination(mat, A);

    foreach (i; 0..MOD) {
        long res = 0;
        foreach (j; 0..N) (res += A[j] * powmod(i, j, MOD) % MOD) %= MOD;
        if (res == 0) {
            ans(i);
            return;
        }
    }

    ans(-1);
}

//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;
import std.exception;

T gcd(T) (T a, T b) pure nothrow @nogc {
  if (a < b) {
    swap (a, b);
  }
  while (b) {
    T c = a % b; a = b; b = c;
  }
  return a;
}

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 {
  enum q = 1_000_003;
  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 int rhs) const pure nothrow @nogc {
    IntM a = 1, b = this;
    int 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; }
}

void main() {
  IntM[][] a = new IntM[][] (11, 11);
  IntM[11] b;
  foreach (i; 0 .. 11) {
    IntM ii = i;
    writeln ("? ", i); stdout.flush ();
    a[i][0] = 1;
    foreach (j; 1 .. 11) {
      a[i][j] = a[i][j-1] * ii;
    }
    int bi = readln.strip.to!int;
    b[i] = bi;
  }
  immutable IntM z = 0;
  foreach (i; 0 .. 11) {
    enforce (a[i][i] != z);
    foreach (j; i + 1 .. 11) {
      IntM aji = a[j][i];
      foreach (k; i .. 11) {
        a[j][k] *= a[i][i];
        a[j][k] -= a[i][k] * aji;
      }
      b[j] *= a[i][i];
      b[j] -= b[i] * aji;
    }
  }
  IntM[11] x;
  foreach_reverse (i; 0 .. 11) {
    x[i] = b[i];
    foreach (j; i + 1 .. 11) {
      x[i] -= a[i][j] * x[j];
    }
    x[i] *= a[i][i] ^^ (IntM.q - 2);
  }
  debug stderr.writeln (a);
  debug stderr.writeln (b);
  debug stderr.writeln (x);
  foreach (i; 0 .. IntM.q) {
    IntM r;
    IntM ii = i;
    foreach_reverse (j; 0 .. 11) {
      r *= ii;
      r += x[j];
    }
    if (r == z) {
      writeln ("! ", i);
      return;
    }
  }
  writeln ("! ", -1);
}

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;

immutable long MOD = 10^^6 + 3;

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}

int gaussianElimination(ref long[][] org_mat, ref long[] ret) {
    int N = org_mat.length.to!int;

    auto mat = new long[][](N, N+1);
    foreach (i; 0..N)
        foreach (j; 0..N+1)
            mat[i][j] = org_mat[i][j];

    foreach (i; 0..N) {
        long maxval = 0;
        int maxrow = -1;
        foreach (j; i..N)
            if (abs(mat[j][i]) > maxval)
                maxval = abs(mat[j][i]), maxrow = j;
        if (maxval == 0)
            return -1;
        swap(mat[i], mat[maxrow]);

        foreach (j; i+1..N+1)
            mat[i][j] = mat[i][j] * powmod(mat[i][i], MOD-2, MOD) % MOD;
        mat[i][i] = 1;

        foreach (j; 0..N) {
            if (j == i) continue;
            long mul = mat[j][i];
            foreach (k; i..N+1) {
                (mat[j][k] -= mul * mat[i][k] % MOD) %= MOD;
                mat[j][k] = (mat[j][k] + MOD) % MOD;
            }
        }
    }

    foreach (i; 0..N)
        ret[i] = mat[i][N];

    return 0;
}

long[] B;

long ask(long x) {
    writeln("? ", x);
    debug {
        long res = 0;
        foreach (i; 0..10) (res += B[i] * powmod(x, i, MOD) % MOD) %= MOD;
        return res;
    }
    stdout.flush;
    return readln.chomp.to!long;
}

void ans(long x) {
    writeln("! ", x);
    return;
}

void main() {
    debug {
        B = new long[](10);
        B[0] = 1000002;
        B[2] = 1;
    }

    int N = 10;
    auto mat = new long[][](N, N+1);

    foreach (i; 0..10) {
        foreach (j; 0..N) mat[i][j] = powmod(i, j, MOD);
        mat[i][N] = ask(i);
    }

    auto A = new long[](N);
    gaussianElimination(mat, A);

    foreach (i; 0..MOD) {
        long res = 0;
        foreach (j; 0..10) (res += A[j] * powmod(i, j, MOD) % MOD) %= MOD;
        if (res == 0) {
            ans(i);
            return;
        }
    }

    ans(-1);
}

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;

immutable long MOD = 10^^6 + 3;

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}

int gaussianElimination(ref long[][] org_mat, ref long[] ret) {
    int N = org_mat.length.to!int;

    auto mat = new long[][](N, N+1);
    foreach (i; 0..N)
        foreach (j; 0..N+1)
            mat[i][j] = org_mat[i][j];

    foreach (i; 0..N) {
        long maxval = 0;
        int maxrow = -1;
        foreach (j; i..N)
            if (abs(mat[j][i]) > maxval)
                maxval = abs(mat[j][i]), maxrow = j;
        if (maxval == 0)
            return -1;
        swap(mat[i], mat[maxrow]);

        foreach (j; i+1..N+1)
            mat[i][j] = mat[i][j] * powmod(mat[i][i], MOD-2, MOD);
        mat[i][i] = 1;

        foreach (j; 0..N) {
            if (j == i) continue;
            long mul = mat[j][i];
            foreach (k; i..N+1) {
                (mat[j][k] -= mul * mat[i][k] % MOD) %= MOD;
                mat[j][k] = (mat[j][k] + MOD) % MOD;
            }
        }
    }

    foreach (i; 0..N)
        ret[i] = mat[i][N];

    return 0;
}

long[] B;

long ask(long x) {
    writeln("? ", x);
    debug {
        long res = 0;
        foreach (i; 0..10) (res += B[i] * powmod(x, i, MOD) % MOD) %= MOD;
        return res;
    }
    stdout.flush;
    return readln.chomp.to!long;
}

void ans(long x) {
    writeln("! ", x);
    return;
}

void main() {
    debug {
        B = new long[](10);
        B[0] = 1000002;
        B[2] = 1;
    }

    int N = 10;
    auto mat = new long[][](N, N+1);

    foreach (i; 0..10) {
        foreach (j; 0..N) mat[i][j] = powmod(i, j, MOD);
        mat[i][N] = ask(i);
    }

    auto A = new long[](N);
    gaussianElimination(mat, A);

    foreach (i; 0..MOD) {
        long res = 0;
        foreach (j; 0..10) (res += A[j] * powmod(i, j, MOD) % MOD) %= MOD;
        if (res == 0) {
            ans(i);
            return;
        }
    }

    ans(-1);
}

//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;
import std.exception;

T gcd(T) (T a, T b) pure nothrow @nogc {
  if (a < b) {
    swap (a, b);
  }
  while (b) {
    T c = a % b; a = b; b = c;
  }
  return a;
}

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 {
  enum q = 1_000_003;
  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 int rhs) const pure nothrow @nogc {
    IntM a = 1, b = this;
    int 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; }
}

void main() {
  IntM[][] a = new IntM[][] (11, 11);
  IntM[11] b;
  foreach (i; 0 .. 11) {
    IntM ii = i;
    writeln ("? ", i); stdout.flush ();
    a[i][0] = 1;
    foreach (j; 1 .. 11) {
      a[i][j] = a[i][j-1] * ii;
    }
    int bi = readln.strip.to!int;
    b[i] = bi;
  }
  immutable IntM z = 0;
  foreach (i; 0 .. 11) {
    enforce (a[i][i] != z);
    foreach (j; i + 1 .. 11) {
      IntM aji = a[j][i];
      foreach (k; j .. 11) {
        a[j][k] *= a[i][i];
        a[j][k] -= a[i][k] * aji;
      }
      b[j] *= a[i][i];
      b[j] -= b[i] * aji;
    }
  }
  IntM[11] x;
  foreach_reverse (i; 0 .. 11) {
    x[i] = b[i];
    foreach (j; i + 1 .. 11) {
      x[i] -= a[i][j] * x[j];
    }
    x[i] *= a[i][i] ^^ (IntM.q - 2);
  }
  foreach (i; 0 .. IntM.q) {
    IntM r;
    IntM ii = i;
    foreach_reverse (j; 0 .. 11) {
      r *= ii;
      r += x[j];
    }
    if (r == z) {
      writeln ("! ", i);
      return;
    }
  }
  writeln ("! ", -1);
}


Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead.The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help.Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check.Remainder, on how do chess pieces move:   Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells.  Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells.  Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap".

The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≤ x0, y0 ≤ 109) — coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position.

The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise.

NotePicture for the first sample:    White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES".Picture for the second sample:    Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Pos {
    int x, y;
}

struct Figure {
    char type;
    Pos p;
}

int n;
Pos k;
Array!Figure bishops1, bishops2, hrooks, vrooks;

bool check(alias cmp, R)(const R a, char odd) {
    int left = 0, right = cast(int)a.length;
    while (left != right) {
        int mid = (left + right) >>> 1;
        if (cmp(k, a[mid].p))
            right = mid;
        else
            left = mid + 1;
    }
    if (left != a.length && a[left].type != odd)
        return true;
    if (left && a[left - 1].type != odd)
        return true;
    return false;
}

void main() {
    while (read(&n, &k.x, &k.y)) {
        bishops1.clear();
        bishops2.clear();
        hrooks.clear();
        vrooks.clear();
        const d1 = k.x - k.y, d2 = k.x + k.y;
        foreach (i; 0 .. n) {
            Figure f;
            read(&f.type, &f.p.x, &f.p.y);
            if (f.p.x - f.p.y == d1)
                bishops1.insertBack(f);
            else if (f.p.x + f.p.y == d2)
                bishops2.insertBack(f);
            if (f.p.y == k.y)
                hrooks.insertBack(f);
            else if (f.p.x == k.x)
                vrooks.insertBack(f);
        }
        bishops1[ ].sort!`a.p.x < b.p.x`;
        bishops2[ ].sort!`a.p.x < b.p.x`;
        hrooks[ ].sort!`a.p.x < b.p.x`;
        vrooks[ ].sort!`a.p.y < b.p.y`;
        bool b =
            bishops1[ ].check!((a, b) => a.x < b.x)('R') ||
            bishops2[ ].check!((a, b) => a.x < b.x)('R') ||
            hrooks[ ].check!((a, b) => a.x < b.x)('B') ||
            vrooks[ ].check!((a, b) => a.y < b.y)('B');
        write(b? "YES\n" : "NO\n");
    }
}