Mister B has a house in the middle of a giant plain field, which attracted aliens life. For convenience, aliens specified the Cartesian coordinate system on the field in such a way that Mister B's house has coordinates (0, 0). After that they sent three beacons to the field, but something went wrong. One beacon was completely destroyed, while the other two landed in positions with coordinates (m, 0) and (0, n), respectively, but shut down.Mister B was interested in this devices, so he decided to take them home. He came to the first beacon, placed at (m, 0), lifted it up and carried the beacon home choosing the shortest path. After that he came to the other beacon, placed at (0, n), and also carried it home choosing the shortest path. When first beacon was lifted up, the navigation system of the beacons was activated.Partially destroyed navigation system started to work in following way.At time moments when both survived beacons are at points with integer coordinates the system tries to find a location for the third beacon. It succeeds if and only if there is a point with integer coordinates such that the area of the triangle formed by the two survived beacons and this point is equal to s. In this case the system sends a packet of information with beacon positions to aliens, otherwise it doesn't.Compute how many packets of information system sent while Mister B was moving the beacons.

The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. The next 3·t lines describe t test cases.  Every test case is described in three lines as follows. Note that each parameter is given as a product of three factors. The first line of a test case contains three space-separated integers: n1, n2, n3 (1 ≤ ni ≤ 106) such that n = n1·n2·n3. The second line contains three space-separated integers: m1, m2, m3 (1 ≤ mi ≤ 106) such that m = m1·m2·m3. The third line contains three space-separated integers: s1, s2, s3 (1 ≤ si ≤ 106) such that s = s1·s2·s3. Note that for hacks only tests with t = 1 allowed.

Print t integers one per line — the answers for each test.

NoteFirst test case contains the following beacon positions: (2, 0) and (0, 2), s = 3. The following packets could be sent: ((2, 0), (0, 2), ( - 1, 0)), ((1, 0), (0, 2), (4, 0)), ((0, 0), (0, 2), (3, 1)), ((0, 0), (0, 1), ( - 6, 0)), where (b1, b2, p) has next description: b1 — first beacon position, b2 — second beacon position, p — some generated point.Second test case contains the following beacon initial positions: (4, 0) and (0, 5), s = 2. The following packets could be sent: ((4, 0), (0, 5), (0, 4)), ((3, 0), (0, 5), (2, 3)), ((2, 0), (0, 5), (2, 2)), ((1, 0), (0, 5), (1, 4)), ((0, 0), (0, 4), (0,  - 1)), ((0, 0), (0, 2), (2, 0)), ((0, 0), (0, 1), (4, 0)).

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

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

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

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

enum LIM = 10^^6 + 10;
int[] lpf, moe;

int[int] factorize(long[] as) {
  int[int] ret;
  foreach (a; as) {
    int aa = cast(int)(a);
    for (; aa > 1; ) {
      const p = lpf[aa];
      ++ret[p];
      aa /= p;
    }
  }
  return ret;
}

void main() {
  lpf = iota(LIM).array;
  moe = new int[LIM];
  moe[1 .. $] = 1;
  foreach (p; 2 .. LIM) {
    if (lpf[p] == p) {
      moe[p] *= -1;
      for (int n = 2 * p; n < LIM; n += p) {
        chmin(lpf[n], p);
        moe[n] *= -1;
        if (n / p % p == 0) {
          moe[n] = 0;
        }
      }
    }
  }

  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        auto N = new long[3];
        foreach (i; 0 .. 3) {
          N[i] = readInt();
        }
        auto M = new long[3];
        foreach (i; 0 .. 3) {
          M[i] = readInt();
        }
        auto S = new long[3];
        foreach (i; 0 .. 3) {
          S[i] = readInt();
        }

        const MM = M[0] * M[1] * M[2];
        const NN = N[0] * N[1] * N[2];
        const SS = S[0] * S[1] * S[2];
        const ns = factorize(N);
        const ss = factorize(S ~ [2L]);
        debug {
          writeln("ns = ", ns);
          writeln("ss = ", ss);
        }

        long ansX, ansY;

        {
          int len;
          int[] ps, es, fs;
          foreach (p, e; ns) {
            ps ~= p;
            es ~= e;
            fs ~= (p in ss) ? ss[p] : 0;
            ++len;
          }
          debug {
            writeln("ps = ", ps);
            writeln("es = ", es);
            writeln("fs = ", fs);
          }
          void dfsN(int i, long m, long s) {
            if (i == len) {
              ansX += m * s;
            } else {
              // 0 <= v_p(d e) <= es[i]
              long mm = m;
              foreach (j; 0 .. es[i] + 1) {
                // 0 <= v_p(d) <= fs[i], 0 <= v_p(e) <= 1
                long ss;
                if (j <= fs[i]) {
                  ss += s;
                }
                if (0 <= j - 1 && j - 1 <= fs[i]) {
                  ss -= s;
                }
                if (mm != 0 && ss != 0) {
                  dfsN(i + 1, mm, ss);
                }
                mm /= ps[i];
              }
            }
          }
          dfsN(0, MM, 1);
        }

        {
          int len;
          int[] ps, es;
          foreach (p, e; ss) {
            ps ~= p;
            es ~= e;
            ++len;
          }
          void dfsS(int i, long n) {
            if (n == 0) {
              return;
            } else if (i == len) {
              ++ansY;
            } else {
              long nn = n;
              foreach (j; 0 .. es[i] + 1) {
                dfsS(i + 1, nn);
                nn /= ps[i];
              }
            }
          }
          dfsS(0, NN);
        }

        writeln(ansX + ansY);

        debug {
          writefln("%s %s %s: ans %s %s", MM, NN, SS, ansX, ansY);

          // \sum_{d|n, d|2s} \sum_{e|n/d} \mu(e) [m / (d e)]
          long slwX;
          foreach (d; 1 .. NN + 1) {
            if (NN % d == 0 && (2 * SS) % d == 0) {
              foreach (e; 1 .. NN / d + 1) {
                if ((NN / d) % e == 0) {
                  slwX += moe[cast(int)(e)] * (MM / (d * e));
                }
              }
            }
          }
          writefln("%s %s %s: slw %s", MM, NN, SS, slwX);

          long brtX, brtY;
          foreach (x; 1 .. MM + 1) {
            if ((2 * SS) % gcd(x, NN) == 0) {
              ++brtX;
            }
          }
          foreach (y; 1 .. NN + 1) {
            if ((2 * SS) % y == 0) {
              ++brtY;
            }
          }
          writefln("%s %s %s: brt %s %s", MM, NN, SS, brtX, brtY);
        }
      }
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

enum LIM = 10^^6 + 10;
int[] lpf, moe;

int[int] factorize(long[] as) {
  int[int] ret;
  foreach (a; as) {
    int aa = cast(int)(a);
    for (; aa > 1; ) {
      const p = lpf[aa];
      ++ret[p];
      aa /= p;
    }
  }
  return ret;
}

void main() {
  lpf = iota(LIM).array;
  moe = new int[LIM];
  moe[1 .. $] = 1;
  foreach (p; 2 .. LIM) {
    if (lpf[p] == p) {
      moe[p] *= -1;
      for (int n = 2 * p; n < LIM; n += p) {
        chmin(lpf[n], p);
        moe[n] *= -1;
        if (n / p % p == 0) {
          moe[n] = 0;
        }
      }
    }
  }

  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        auto N = new long[3];
        foreach (i; 0 .. 3) {
          N[i] = readInt();
        }
        auto M = new long[3];
        foreach (i; 0 .. 3) {
          M[i] = readInt();
        }
        auto S = new long[3];
        foreach (i; 0 .. 3) {
          S[i] = readInt();
        }

        const MM = M[0] * M[1] * M[2];
        const ns = factorize(N);
        const ss = factorize(S ~ [2L]);
        debug {
          writeln("ns = ", ns);
          writeln("ss = ", ss);
        }

        long ansX, ansY;

        {
          int len;
          int[] ps, es, fs;
          foreach (p, e; ns) {
            ps ~= p;
            es ~= e;
            fs ~= (p in ss) ? ss[p] : 0;
            ++len;
          }
          debug {
            writeln("ps = ", ps);
            writeln("es = ", es);
            writeln("fs = ", fs);
          }
          void dfsN(int i, long m, long s) {
            if (i == len) {
              ansX += m * s;
            } else {
              // 0 <= v_p(d e) <= es[i]
              long mm = m;
              foreach (j; 0 .. es[i] + 1) {
                // 0 <= v_p(d) <= fs[i], 0 <= v_p(e) <= 1
                long ss;
                if (j <= fs[i]) {
                  ss += s;
                }
                if (0 <= j - 1 && j - 1 <= fs[i]) {
                  ss -= s;
                }
                if (mm != 0 && ss != 0) {
                  dfsN(i + 1, mm, ss);
                }
                mm /= ps[i];
              }
            }
          }
          dfsN(0, MM, 1);
        }

        {
          int len;
          int[] ps, es;
          foreach (p, e; ss) {
            ps ~= p;
            es ~= e;
            ++len;
          }
          void dfsS(int i, long m) {
            if (m == 0) {
              return;
            } else if (i == len) {
              ++ansY;
            } else {
              long mm = m;
              foreach (j; 0 .. es[i] + 1) {
                dfsS(i + 1, mm);
                mm /= ps[i];
              }
            }
          }
          dfsS(0, MM);
        }

        writeln(ansX + ansY);

        debug {

          const NN = N[0] * N[1] * N[2];
          const SS = S[0] * S[1] * S[2];
          writefln("%s %s %s: ans %s %s", MM, NN, SS, ansX, ansY);

          // \sum_{d|n, d|2s} \sum_{e|n/d} \mu(e) [m / (d e)]
          long slwX;
          foreach (d; 1 .. NN + 1) {
            if (NN % d == 0 && (2 * SS) % d == 0) {
              foreach (e; 1 .. NN / d + 1) {
                if ((NN / d) % e == 0) {
                  slwX += moe[cast(int)(e)] * (MM / (d * e));
                }
              }
            }
          }
          writefln("%s %s %s: slw %s", MM, NN, SS, slwX);

          long brtX, brtY;
          foreach (x; 1 .. MM + 1) {
            if ((2 * SS) % gcd(x, NN) == 0) {
              ++brtX;
            }
          }
          foreach (y; 1 .. NN + 1) {
            if ((2 * SS) % y == 0) {
              ++brtY;
            }
          }
          writefln("%s %s %s: brt %s %s", MM, NN, SS, brtX, brtY);
        }
      }
    }
  } catch (EOFException e) {
  }
}


Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.We define function f(x, l, r) as a bitwise OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the length of the arrays. The second line contains n integers ai (0 ≤ ai ≤ 109). The third line contains n integers bi (0 ≤ bi ≤ 109).

Print a single integer — the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

NoteBitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.In the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.In the second sample, the maximum value is obtained for l = 1 and r = 9.

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

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

void main() {
        IO cin;
        int t = 1;
        // t = cin.readInt;
        while (t--) {
                int n = cin.readInt;
                int[] a = new int[](n);
                int[] b = new int[](n);
                foreach (ref i; a) i = cin.readInt;
                foreach (ref i; b) i = cin.readInt;
                long maxOR = -999999;
                for (int i = 0; i < n; i++) {
                        long aOR, bOR;
                        for (int j = i; j < n; j++) {
                                aOR = aOR | a[j];
                                bOR = bOR | b[j];
                        }
                        maxOR = max(maxOR, aOR + bOR);

                }
                writeln(maxOR);
        }
}


Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller.Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array.Help Amr by choosing the smallest subsegment possible.

The first line contains one number n (1 ≤ n ≤ 105), the size of the array. The second line contains n integers ai (1 ≤ ai ≤ 106), representing elements of the array.

Output two integers l, r (1 ≤ l ≤ r ≤ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them.

NoteA subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≤ i ≤ r - l + 1

import std.stdio;
import std.c.stdio;
import std.math;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.typecons;

void main() {
  int n = readln().strip().to!int;
  int[] a = readln().split().map!(to!int).array;
  int[int] l, r, ct;
  foreach(int i, v; a) {
    if (v in l) {
      l[v] = min(l[v] ,i);
      r[v] = max(r[v] ,i);
      ct[v]++;
    } else {
      l[v] = i;
      r[v] = i;
      ct[v] = 1;
    }
  }
  int[] max_ks;
  int max_v = 0;
  foreach(k, v; ct) {
    if (max_v < v) {
      max_ks.length = 0;
      max_v = v;
    }
    if (max_v == v) {
      max_ks ~= k;
    }
  }
  int min_len = n + 1, min_k;
  foreach(k; max_ks) {
    if (r[k] - l[k] + 1 < min_len) {
      min_len = r[k] - l[k] + 1;
      min_k = k;
    }
  }
  writeln(l[min_k] + 1, ' ', r[min_k] + 1);
}

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

void main() {
    int n;
    readf("%d ", &n);
    auto a = readln().split().map!(to!int)().array();
    int[int] d, first, last;
    foreach (i, x; a) {
        d[x]++;
        if (x !in first)
            first[x] = i;
        last[x] = i;
    }
    Array!int keys;
    int result = 0;
    foreach (k, v; d)
        if (v > result) {
            keys.clear();
            keys ~= k;
            result = v;
        } else if (v == result)
            keys ~= k;
    int resX, resY;
    result = n;
    foreach (k; keys) {
        int x = first[k], y = last[k];
        if (y - x < result) {
            resX = x;
            resY = y;
            result = y - x;
        }
    }
    writeln(resX + 1, ' ', resY + 1);
}


Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water.It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows:  Pasha can boil the teapot exactly once by pouring there at most w milliliters of water;  Pasha pours the same amount of water to each girl;  Pasha pours the same amount of water to each boy;  if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does.Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends.

The first line of the input contains two integers, n and w (1 ≤ n ≤ 105, 1 ≤ w ≤ 109) — the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≤ ai ≤ 109, 1 ≤ i ≤ 2n) — the capacities of Pasha's tea cups in milliliters.

Print a single real number — the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.

NotePasha also has candies that he is going to give to girls but that is another task...

import std.stdio;
import std.c.stdio;
import std.math;
import std.algorithm;
import std.conv;
import std.string;
import std.array;

void main() {
  int n, w ;
  scanf("%d %d\n", &n, &w);
  int[] a = readln().split().map!(to!int).array;
  a.sort();
  real cap = min(real(a[0]), a[n] / 2.0);
  writefln("%.10f", min(real(w), cap * n + 2 * cap * n));
}

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

void main() {
    int n, w;
    readf("%d %d\n", &n, &w);

    int[] a = readln.chomp.split.map!(to!int).array.sort;

    int woman_min = a[0];
    int man_min = a[n];

    double base = 0.0;
    if (woman_min * 2 > man_min) {
        base = man_min / 2.0;
    } else {
        base = cast(double)woman_min;
    }

    double whole = base * 3 * n;

    if (whole <= w) {
        writefln("%.10f", base * 3 * n);
    } else {
        writeln(w);
    }
}


Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn. Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.

The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order. The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order. It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.

Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).

NoteIn the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.In the third example for any values l and r there is a situation when Stepan said the truth.

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,k,l,r;
	scanf("%d %d %d", &n, &l,&r);
	int [] arr = new int[n];
	int [] arr2 =new int[n];
	int[]  arr3 = new int[n];
	for(int i = 0; i<n; i++)
		scanf("%d", &arr[i]);
	for(int i = 0; i<n; i++)
		scanf("%d", &arr3[i]);
	int q;
	bool ok=0;
	for(int i = 0; i<l-1; i++)
	    if(arr3[i]!=arr[i])
	    {
	        printf("LIE");
	        return 0;
	    }
    for(int i = r; i<n; i++)
	    if(arr3[i]!=arr[i])
	    {
	        printf("LIE");
	        return 0;
	    }

	printf("TRUTH");
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,l,r, y = 0;
	scanf("%d %d %d", &n, &l, &r);
	l--;
	r--;
	int [] arr = new int[n];
	int [] arr2 = new int[n];
	int [] arr3 = new int[n];
	int [] arr4 = new int[n];
	for(int i = 0; i<n; i++)
		scanf("%d", &arr[i]);
	for(int i = 0; i<n; i++)
	    scanf("%d", &arr2[i]);
	int q;
	for(int i = 0; i<n; i++)
		if((i < l || i > r) && arr[i] != arr2[i]) y = 1;
	for(int i = 0; i<n; i++)
	    if(i >= l && i <= r) arr3[arr[i]]++, arr4[arr2[i]]++;
	for(int i = 0; i<n; i++)
	    if(arr3[i] != arr4[i]) y = 1;
	if (y == 1) printf("LIE");
	else printf("TRUTH");
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,l,r;
	scanf("%d %d %d", &n, &l, &r);
	l--;
	r--;
	int [] arr = new int[n];
	for(int i = 0; i<n; i++)
		scanf("%d", &arr[i]);
	int q;
	for(int i = 0; i<n; i++)
	{
	    scanf("%d", &q);
	    if(q != arr[i] && (i < l || i > r))
	    {
	        printf("LIE");
	        return 0;
	    }
	}
	printf("TRUTH");
	return 0;
}

module main;

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

int main(string[] argv) {
	int n, L, R;
	scanf("%d %d %d", &n, &L, &R);

	int [] arr = new int[1 + n];
	for (int i = 1; i <= n; i++)
		scanf("%d", &arr[i]);
	int [] arr2 = new int[1 + n];
	for (int i = 1; i <= n; i++)
		scanf("%d", &arr2[i]);

	int len = R - L + 1;
	int [] a = new int[len];
	int [] b = new int[len];

	int j = 0;
	for (int i = L; i <= R; ++i) {
		a[j] = arr[i];
		b[j] = arr2[i];
		j++;
	}

	sort(a);
	sort(b);

	int q = 1;
	for (int i = 0; i < len; ++i) {
		if (a[i] != b[i]) {
			q = 0;
		}
	}

	for (int i = 1; i <= L - 1; ++i) {
		if (arr[i] != arr2[i]) {
			q = 0;
		}
	}

	for (int i = R + 1; i <= n; ++i) {
		if (arr[i] != arr2[i]) {
			q = 0;
		}
	}

	if (q == 1)
		printf("TRUTH");
	else
		printf("LIE");

	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,l,r;
	scanf("%d %d %d", &n, &l, &r);
	int [] current = new int[n];
	int [] was = new int[n];
	for(int i = 0; i<n; i++)
		scanf("%d", &was[i]);
	for(int i = 0; i<n; i++)
		scanf("%d", &current[i]);
	bool outside = true;
	for(int i = 0; i<l - 1; i++){
	    outside &= (was[i] == current[i]);
    }
	for(int i = r; i<n; i++){
	    outside &= (was[i] == current[i]);
	}
	bool inside = true;
	int [] counts = new int[n];
	for(int i = l - 1; i<r; i++){
	    counts[was[i]]++;
	    counts[current[i]]--;
	}
	int cnt = 0;
	for(int i = 0; i<n; i++){
	    inside &= (counts[i] == 0);
	}
	if (inside && outside)
	    printf("TRUTH");
	else
	    printf("LIE");
	return 0;
}

module main;

import std.c.stdio;
import std.algorithm.sorting;

int main(string[] argv)
{
	int n,l,r;
	bool flag;
	flag = 1;
	scanf("%d %d %d", &n, &l, &r);
	int [] a = new int[n];
	int [] b = new int[n];
	for(int i = 0; i<n; i++)
		scanf("%d", &a[i]);
	for(int i = 0; i<n; i++)
		scanf("%d", &b[i]);
	for(int i = 0; i<l - 1; i++)
	    if (flag && a[i] != b[i])
	        flag = 0;
	for(int i = r; i<n; i++)
	    if (flag && a[i] != b[i])
	        flag = 0;
    a.sort();
    b.sort();
	for(int i = 0; i<n; i++)
	    if (flag && a[i] != b[i])
	        flag = 0;
    if (flag)
        printf("TRUTH");
    else
        printf("LIE");
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,l, r;
	scanf("%d %d %d", &n, &l, &r);
	int [] arr = new int[n];
	l = l - 1;
	r = r - 1;
	for(int i = 0; i<n; i++)
		scanf("%d", &arr[i]);
	bool ans = true;
	int j;
	for (int i = 0; i < n; i++) {
	    scanf("%d", &j);
	    if (i < l || i > r)
	        if (j != arr[i])
	            ans = false;
	}
	if (ans) printf("TRUTH");
	else printf("LIE");
	return 0;
}

import std.stdio : writeln, stdin;

import std.c.stdio;

int[] a = new int[111111];

void main()
{
    int n, l, r;
    scanf("%d", &n);
    scanf("%d", &l);
    scanf("%d", &r);
    int i;
    for (i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    int x;
    for (i = 0; i < n; ++i) {
        scanf("%d", &x);
        if ((i + 1 >= l) && (i + 1 <= r)) {
            continue;
        }
        if (a[i] != x) {
            writeln("LIE");
            return;
        }
    }
    writeln("TRUTH");
}

module main;

import std.stdio : writeln, stdin;
import core.stdc.stdio;

int main(string[] argv)
{
	int n, l, r;
	scanf("%d %d %d", &n, &l, &r);
	l = l - 1;
	r = r - 1;
	int [] arr = new int[n];
	int [] b = new int[n];
	for(int i = 0; i<n; i++)
		scanf("%d", &arr[i]);
	for (int i = 0; i < n; i++)
	    scanf("%d", &b[i]);
	for (int i = 0; i < l; i++) {
	    if (arr[i] != b[i]) {
	        writeln("LIE");
	        return 0;
	    }
	}
	for (int i = r + 1; i < n; i++) {
    	if (arr[i] != b[i]) {
    	        writeln("LIE");
    	        return 0;
    	    }
    }
	writeln("TRUTH");
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,k,l,r;
	scanf("%d %d %d", &n, &l, &r);
	int [] arr = new int[n];
	int [] arr2 = new int[n];
	int [] mk = new int[n+1];
	for(int i = 0; i<n; i++) scanf("%d", &arr[i]);
	for(int i = 0; i<n; i++) scanf("%d", &arr2[i]);
// 	for(int i = 0; i<n; i++) printf("%d ", arr[i]);
	for(int i = 0; i <= n; i++) mk[i] = 0;
	for(int i = l; i <= r; i++){
	    mk[arr[i-1]]++;
	    mk[arr2[i-1]]--;
	}
	for(int e = 0; e <=n; e++){
	    if(mk[e] != 0){
	        printf("LIE\n");
	        return 0;
	   }
	  }
	for(int e = 0; e < n; e++){
	    if(l <= e+1 && e+1 <= r) continue;
	    if(arr[e] != arr2[e]){
	        printf("LIE\n");
	        return 0;
	    }
	}
	  printf("TRUTH\n");
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    int[] vasha = new int[a+1];
    int[] vashab = new int[a+1];
    for(int i = 1; i <= a; i++)
    {
        scanf("%d",&vasha[i]);
    }
    for(int i = 1; i <= a; i++)
    {
        scanf("%d",&vashab[i]);
    }

    for(int i = 1; i <= a; i++)
    {
        if(i<b||i>c){
            if(vasha[i]!=vashab[i]){
                printf("LIE");
                return 0;
            }
        }
    }

    printf("TRUTH");
    return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,l,r;
	scanf("%d %d %d", &n, &l,&r);
	l-=1;
	r-=1;
	int [] arr = new int[n];
	int [] arr2=new int[n];
	for(int i = 0; i<n; i++)
		scanf("%d", &arr[i]);
	for(int i = 0; i<n; i++)
		scanf("%d", &arr2[i]);
	for(int i=0;i<n;i++){
	    if(i<l || i>r){
	        if(arr[i]!=arr2[i]){
	            printf("LIE");
	            return 0;
	        }
	    }
	}
	printf("TRUTH");
	return 0;
}

import std.stdio;

int main() {
  int a[123456];
  int b[123456];
  int n, l, r;
  readf(" %d %d %d", &n, &l, &r);
  for (int i = 1; i <= n; i++) {
    readf(" %d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    readf(" %d", &b[i]);
  }
  int ans = 0;
  for (int i = 1; i < l; i++) {
    if (a[i] != b[i]) {
      ans = 1;
    }
  }
  for (int i = r + 1; i <= n; i++) {
    if (a[i] != b[i]) {
      ans = 1;
    }
  }
  if (ans) {
    writeln("LIE");
  } else {
    writeln("TRUTH");
  }
  return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
    int n, l, r;
    scanf("%d %d %d", &n, &l, &r);
    l = l - 1; r = r - 1;
    int [] a = new int[n];
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    int [] b = new int[n];
    for (int i = 0; i < n; i++)
        scanf("%d", &b[i]);
    int ok = 0;
    for (int i = 0; i < n; i++)
        if ((i < l || i > r) && a[i] != b[i])
            ok = ok + 1;
    if (ok == 0)
        printf("TRUTH");
    else
        printf("LIE");
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,l,r;
	scanf("%d %d %d", &n, &l, &r);
	int [] a = new int[n+5];
	int [] b = new int[n+5];
	int [] f = new int[n+5];
	for(int i = 1; i<=n; i++)
		scanf("%d", &a[i]);
	for(int i = 1; i<=n; i++)
		scanf("%d", &b[i]);
	for (int i=1; i<l; i++) {
	    if (a[i]!=b[i]) {
	    printf("LIE\n");
	    return 0;
	    }
	}
	for (int i=r+1; i<=n; i++) {
	    if (a[i]!=b[i]) {
	    printf("LIE\n");
	    return 0;
	    }
	}
	for (int i=l; i<=r; i++) f[a[i]]++;
	for (int i=l; i<=r; i++) f[b[i]]--;
	for (int i=1; i<=n; i++) if (f[i]!=0) {
	printf("LIE\n");
	return 0;
	}
	printf("TRUTH\n");
	return 0;
}

module main;

import std.c.stdio;
import std.algorithm.sorting;

int main(string[] argv)
{
  int n, l, r;
  scanf("%d %d %d", &n, &l, &r);
  int [] a = new int[n];
  int [] b = new int[n];
  int [] aa = new int[r-l+1];
  int [] bb = new int[r-l+1];
  for(int i = 0; i<n; i++)
    scanf("%d", &a[i]);
  for(int i = 0; i<n; i++)
    scanf("%d", &b[i]);
  bool ok = 1;
  for (int i = 0; i < l-1; i++)
    if (a[i] != b[i])
      ok = 0;
  for (int i = r; i < n; i++)
    if (a[i] != b[i])
      ok = 0;
  for (int i = l; i <= r; i++) {
    aa[i-l] = a[i-1];
    bb[i-l] = b[i-1];
  }
  aa.sort();
  bb.sort();
  for (int i = 0; i < r-l+1; i++)
    if (aa[i] != bb[i])
      ok = 0;
  if (ok)
    printf("TRUTH");
  else
    printf("LIE");
  return 0;
}

import std.stdio;
int a[4000000];
int b[4000000];

int n, l, r;
int main() {
	readf("%s %s %s\n", &n, &l, &r);
	for (int i = 1; i < n; i++) readf("%s ", &a[i]);
	readf("%s\n", &a[n]);
	for (int i = 1; i < n; i++) readf("%s ", &b[i]);
	readf("%s", &b[n]);

	for (int i = 1;i <= l - 1; i++) if (a[i] != b[i]) {
		write("LIE");
		return 0;
	}
	for (int i = r + 1; i <= n; i++) if (a[i] != b[i]) {
		write("LIE");
		return 0;
	}
	write("TRUTH");

	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
    int n,l,r;
    scanf("%d%d%d",&n,&l,&r);
    l--;r--;
    int [] a = new int[n];
    for (int i=0;i<n;i++)
        scanf("%d",&a[i]);
    int [] b = new int[n];
    for (int i=0;i<n;i++)
        scanf("%d",&b[i]);
    int [] m = new int[n+1];
    for (int i=l;i<=r;i++){
        m[a[i]]++;
        m[b[i]]--;
    }
    for (int i=0;i<=n;i++){
        if (m[i]!=0){
            printf("LIE");
            return 0;
        }
    }
    for (int i=0;i<l;i++){
        if (a[i]!=b[i]){
            printf("LIE");
            return 0;
        }
    }
    for (int i=r+1;i<n;i++){
        if (a[i]!=b[i]){
            printf("LIE");
            return 0;
        }
    }
    printf("TRUTH");
	return 0;
}

module main;

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

int main(string[] argv)
{
	int n, l, r;
	bool ans = true;

	scanf("%d%d%d", &n, &l, &r);
	int [] a = new int[n];
	int [] b = new int[n];
	for(int i = 0; i < n; i++) scanf("%d", &a[i]);
	for(int i = 0; i < n; i++) scanf("%d", &b[i]);
	a[l-1..r].sort();
	b[l-1..r].sort();
	for(int i = 0; i < n; i++) if(a[i] != b[i]) ans = false;

	if(ans)
	    printf("TRUTH");
	else
	    printf("LIE");

	return 0;
}

module main;

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

int main(string[] argv) {
	int n, L, R;
	scanf("%d %d %d", &n, &L, &R);

	int [] arr = new int[n];
	for (int i = 0; i < n; i++)
		scanf("%d", &arr[i]);
	int [] arr2 = new int[n];
	for (int i = 0; i < n; i++)
		scanf("%d", &arr2[i]);

	int len = R - L + 1;
	int [] a = new int[len];
	int [] b = new int[len];

	int j = 0;
	for (int i = L - 1; i < R; ++i) {
		a[j] = arr[i];
		b[j] = arr2[i];
		j++;
	}

	sort(a);
	sort(b);

	int q = 1;
	for (int i = 0; i < len; ++i) {
		if (a[i] != b[i]) {
			q = 0;
		}
	}

	if (q == 1)
		printf("TRUTH");
	else
		printf("LIE");

	return 0;
}

module main;

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

int main(string[] argv) {
	int n, L, R;
	scanf("%d %d %d", &n, &L, &R);

	int [] arr = new int[1 + n];
	for (int i = 1; i <= n; i++)
		scanf("%d", &arr[i]);
	int [] arr2 = new int[1 + n];
	for (int i = 1; i <= n; i++)
		scanf("%d", &arr2[i]);

	int len = R - L + 1;
	int [] a = new int[len];
	int [] b = new int[len];

	int j = 0;
	for (int i = L; i <= R; ++i) {
		a[j] = arr[i];
		b[j] = arr2[i];
		j++;
	}

	sort(a);
	sort(b);

	int q = 1;
	for (int i = 0; i < len; ++i) {
		if (a[i] != b[i]) {
			q = 0;
		}
	}

	for (int i = 1; i < L; ++i) {
		if (a[i] != b[i]) {
			q = 0;
		}
	}

	for (int i = R + 1; i <= n; ++i) {
		if (a[i] != b[i]) {
			q = 0;
		}
	}

	if (q == 1)
		printf("TRUTH");
	else
		printf("LIE");

	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,k,l,r;
	scanf("%d %d", &n, &l, &r);
	int [] arr = new int[n];
	int [] arr2 = new int[n];
	int [] mk = new int[n+1];
	for(int i = 0; i<n; i++) scanf("%d", &arr[i]);
	for(int i = 0; i<n; i++) scanf("%d", &arr2[i]);
	for(int i = l; i <= r; i++){
	    mk[arr[i-1]]++;
	    mk[arr2[i-1]]--;
	}
	for(int e = 0; e <=n; e++){
	    if(mk[e] != 0){
	        printf("LIE\n");
	        return 0;
	   }
	  }
	  printf("TRUTH\n");
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,k,l,r;
	scanf("%d %d %d", &n, &l, &r);
	int [] arr = new int[n];
	int [] arr2 = new int[n];
	int [] mk = new int[n+1];
	for(int i = 0; i<n; i++) scanf("%d", &arr[i]);
	for(int i = 0; i<n; i++) scanf("%d", &arr2[i]);
// 	for(int i = 0; i<n; i++) printf("%d ", arr[i]);
	for(int i = 0; i <= n; i++) mk[i] = 0;
	for(int i = l; i <= r; i++){
	    mk[arr[i-1]]++;
	    mk[arr2[i-1]]--;
	}
	for(int e = 0; e <=n; e++){
	    if(mk[e] != 0){
	        printf("LIE\n");
	        return 0;
	   }
	  }
	  printf("TRUTH\n");
	return 0;
}

module main;

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

int main(string[] argv)
{
	int n, l, r;
	bool ans = true;

	scanf("%d%d%d", &n, &l, &r);
	int [] a = new int[r - l + 1];
	int [] b = new int[r - l + 1];
	for(int i = 1; i <= n; i++)
	{
	    int t;
		scanf("%d", &t);
		if(i < l || i > r) continue;
		a[i - l] = t;
	}
	for(int i = 1; i <= n; i++)
	{
	    int t;
		scanf("%d", &t);
		if(i < l || i > r) continue;
		b[i - l] = t;
	}
	a.sort();
	b.sort();
	for(int i = 0; i < r - l + 1; i++)
	if(a[i] != b[i]) ans = false;

	if(ans)
	    printf("TRUTH");
	else
	    printf("LIE");

	return 0;
}


There are $$$n$$$ cities and $$$m$$$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?New roads will also be one-way.

The first line of input consists of three integers $$$n$$$, $$$m$$$ and $$$s$$$ ($$$1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$$$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $$$1$$$ to $$$n$$$. The following $$$m$$$ lines contain roads: road $$$i$$$ is given as a pair of cities $$$u_i$$$, $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$). For each pair of cities $$$(u, v)$$$, there can be at most one road from $$$u$$$ to $$$v$$$. Roads in opposite directions between a pair of cities are allowed (i.e. from $$$u$$$ to $$$v$$$ and from $$$v$$$ to $$$u$$$).

Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $$$s$$$. If all the cities are already reachable from $$$s$$$, print 0.

NoteThe first example is illustrated by the following:  For example, you can add roads ($$$6, 4$$$), ($$$7, 9$$$), ($$$1, 7$$$) to make all the cities reachable from $$$s = 1$$$.The second example is illustrated by the following:  In this example, you can add any one of the roads ($$$5, 1$$$), ($$$5, 2$$$), ($$$5, 3$$$), ($$$5, 4$$$) to make all the cities reachable from $$$s = 5$$$.

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

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

    auto g = new int[][] (n+1);
    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        g[u] ~= v;
    }

    immutable int INF = 10 ^^ 9 + 7;

    auto disc = new int[] (n+1);
    auto lo = new int[] (n+1);
    auto clr = new int[] (n+1);
    int[] stk;
    disc[] = 0;
    lo[] = INF;
    clr[] = 0;
    int nclr = 1;
    int t = 1;

    void dfs(int v) {
        disc[v] = t++;
        lo[v] = disc[v];
        stk ~= v;
        foreach (u; g[v]) {
            if (disc[u] == 0) {
                dfs(u);
                lo[v] = min(lo[v], lo[u]);
            } else if (clr[u] == 0) {
                lo[v] = min(lo[v], lo[u]);
            }
        }

        if (disc[v] == lo[v]) {
            while (stk.back != v) {
                auto u = stk.back;
                stk.popBack();
                clr[u] = nclr;
            }

            stk.popBack();
            clr[v] = nclr;

            ++nclr;
        }
    }

    foreach (i; 1 .. n+1) {
        if (disc[i] == 0) {
            dfs(i);
        }
    }

    debug { clr.writeln; }

    auto ng = new int[][] (nclr);
    foreach (v; 1 .. n+1) {
        foreach (u; g[v]) {
            if (clr[v] != clr[u]) {
                ng[clr[v]] ~= clr[u];
            }
        }
    }

    foreach (v; 1 .. nclr) {
        ng[v] = ng[v].sort().uniq().array;
    }

    debug { ng.writeln; }

    auto isRoot = new bool[] (nclr);
    isRoot[] = true;

    foreach (adj; ng) {
        foreach (u; adj) { isRoot[u] = false; }
    }

    auto ans = isRoot.dropOne.count!(x => x).to!int - (isRoot[clr[s]] ? 1 : 0);
    ans.writeln;
}

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;

  int n, m, s;
  rd(n, m, s);
  s--;
  auto g = new int[][](n);
  foreach (_; 0 .. m) {
    int u, v;
    rd(u, v);
    g[u - 1] ~= (v - 1);
  }

  auto canReach = new bool[][](n, n);
  void dfs(int i, bool[] seen) {
    if (seen[i]) {
      return;
    }
    seen[i] = true;
    foreach (j; g[i]) {
      dfs(j, seen);
    }
  }

  foreach (i; 0 .. n) {
    dfs(i, canReach[i]);
  }
  auto root = new int[](n);
  fill(root, -1);
  int nn = 0;
  foreach (i; 0 .. n) {
    if (root[i] < 0) {
      root[i] = nn++;
    }
    foreach (j; 0 .. n) {
      if (canReach[i][j] && canReach[j][i]) {
        root[j] = root[i];
      }
    }
  }
  auto h = new int[][](nn);
  foreach (i; 0 .. n) {
    foreach (j; g[i]) {
      if (root[i] != root[j]) {
        h[root[i]] ~= root[j];
      }
    }
  }
  auto deg_in = new int[](nn);
  foreach (i; 0 .. nn) {
    foreach (j; h[i]) {
      deg_in[j]++;
    }
  }
  int ans = 0;
  foreach (i; 0 .. nn) {
    if (root[s] != i && deg_in[i] == 0) {
      ans++;
    }
  }
  writeln(ans);

}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

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

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

    auto g = new int[][] (n+1);
    auto isIn = new bool[] (n+1);
    isIn[] = false;
    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        g[u] ~= v;
        isIn[v] = true;
    }

    auto vis = new bool[] (n+1);
    vis[] = false;
    void dfs(int v) {
        vis[v] = true;
        foreach (u; g[v]) {
            if (!vis[u]) { dfs(u); }
        }
    }

    dfs(s);

    int ans = 0;
    foreach (v; 1 .. n+1) {
        if (vis[v]) { continue; }
        if (!isIn[v]) {
            ++ans;
            dfs(v);
        }
    }

    foreach (v; 1 .. n+1) {
        if (vis[v]) { continue; }

        ++ans;
        dfs(v);
    }

    ans.writeln;
}

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

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

    auto g = new int[][] (n+1);
    auto isRoot = new bool[] (n+1);
    isRoot[] = true;
    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        g[u] ~= v;
        isRoot[v] = false;
    }

    auto vis = new bool[] (n+1);
    vis[] = false;
    void dfs(int v) {
        vis[v] = true;
        foreach (u; g[v]) {
            if (!vis[u]) { dfs(u); }
        }
    }

    dfs(s);

    int ans = 0;
    foreach (v; 1 .. n+1) {
        if (vis[v]) { continue; }
        if (isRoot[v]) {
            ++ans;
            dfs(v);
        }
    }

    foreach (v; 1 .. n+1) {
        if (vis[v]) { continue; }

        ++ans;
        dfs(v);
    }

    ans.writeln;
}


Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.

The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane.  Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.

In the first line print an integer — the number of triangles with the non-zero area among the painted points.

NoteNote to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0).Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).Note to the third sample test. A single point doesn't form a single triangle.

import std.stdio;
import std.c.stdio;
import std.math;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.typecons;

void main() {
  alias Pt = Tuple!(int, "x", int, "y");
  int n;
  scanf("%d", &n);
  Pt[] ps = new Pt[n];
  foreach (i; 0..n) {
    scanf("%d%d", &ps[i].x, &ps[i].y);
  }
  long ans = 0;
  foreach (p; ps) {
    real[] a;
    foreach (q; ps) {
      if (p == q) continue;
      Pt d = Pt(q.x - p.x, q.y - p.y);
      real v = atan2(real(d.x), real(d.y));
      while (v < 1e-9) v += PI;
      a ~= v;
    }
    int m = cast(int)a.length;
    a.sort();
    int i = 0, j = 0;
    while (i < m && j < m) {
      while (i < m && abs(a[i] - a[j]) < 1e-9) {
        i++;
      }
      ans += max(0, (m - (i - j)) * (i - j));
      j = i;
      i = j;
    }
  }
  writeln(ans / 6);
}

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

immutable int INF = 10L ^^ 9 + 23;

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

    Tuple!(int, int)[] arr;
    foreach (_; 0 .. n) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;

        arr ~= tuple(a, b);
    }

    int [int][int] cnt;
    long subcnt = 0;
    foreach (i; 0 .. n) {
        cnt.clear();
        int x1 = arr[i][0], y1 = arr[i][1];
        foreach (j; i+1 .. n) {
            int x2 = arr[j][0], y2 = arr[j][1];

            int xd = x2 - x1, yd = y2 - y1;
            int g = gcd(abs(xd), abs(yd));
            xd /= g, yd /= g;
            if (xd < 0 || (xd == 0 && yd < 0)) {
                xd *= -1;
                yd *= -1;
            }

            if (xd in cnt) {
                subcnt += cnt[xd].get(yd, 0);
            }

            cnt[xd][yd] += 1;
        }
    }

    auto ans = n.to!long * (n-1) * (n-2) / 6 - subcnt;

    ans.writeln;
}

import std.stdio;
import std.c.stdio;
import std.math;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.typecons;

void main() {
  alias Pt = Tuple!(int, "x", int, "y");
  int n;
  scanf("%d", &n);
  Pt[] ps = new Pt[n];
  foreach (i; 0..n) {
    scanf("%d%d", &ps[i].x, &ps[i].y);
  }
  long ans = 0;
  foreach (p; ps) {
    Pt[] a;
    foreach (q; ps) {
      if (p != q) a ~= q;
    }
    int m = cast(int)a.length;
    bool compare(Pt q, Pt r) {
      Pt dq = Pt(q.x - p.x, q.y - p.y);
      Pt dr = Pt(r.x - p.x, r.y - p.y);
      return atan2(real(dq.x), real(dq.y)) < atan2(real(dr.x), real(dr.y));
    }
    a.sort!compare();
    int i = 0, j = 0;
    while (i < m && j < m) {
      while (i < m) {
        Pt dq = Pt(a[i].x - p.x, a[i].y - p.y);
        Pt dr = Pt(a[j].x - p.x, a[j].y - p.y);
        if (abs(atan2(real(dq.x), real(dq.y)) - atan2(real(dr.x), real(dr.y))) > 1e-9) break;
        i++;
      }
      ans += m - (i - j);
      j = i + 1;
      i = j;
    }
  }
  writeln(ans / 3);
}

import std.stdio;
import std.c.stdio;
import std.math;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.typecons;

void main() {
  alias Pt = Tuple!(int, "x", int, "y");
  int n;
  scanf("%d", &n);
  Pt[] ps = new Pt[n];
  foreach (i; 0..n) {
    scanf("%d%d", &ps[i].x, &ps[i].y);
  }
  long ans = 0;
  foreach (p; ps) {
    Pt[] a;
    foreach (q; ps) {
      if (p != q) a ~= q;
    }
    int m = cast(int)a.length;
    bool compare(Pt q, Pt r) {
      Pt dq = Pt(q.x - p.x, q.y - p.y);
      Pt dr = Pt(r.x - p.x, r.y - p.y);
      real v1 = atan2(real(dq.x), real(dq.y));
      real v2 = atan2(real(dr.x), real(dr.y));
      if (abs(v1 + PI / 2) < 1e-9) v1 = PI / 2;
      if (abs(v2 + PI / 2) < 1e-9) v2 = PI / 2;
      return v1 < v2 ;
    }
    a.sort!compare();
    int i = 0, j = 0;
    while (i < m && j < m) {
      while (i < m) {
        Pt dq = Pt(a[i].x - p.x, a[i].y - p.y);
        Pt dr = Pt(a[j].x - p.x, a[j].y - p.y);
        real v1 = atan2(real(dq.x), real(dq.y));
        real v2 = atan2(real(dr.x), real(dr.y));
        if (abs(v1 + PI / 2) < 1e-9) v1 = PI / 2;
        if (abs(v2 + PI / 2) < 1e-9) v2 = PI / 2;
        if (abs(v1 - v2) > 1e-9) break;
        i++;
      }
      ans += m - (i - j);
      j = i + 1;
      i = j;
    }
  }
  writeln(ans / 3);
}

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

immutable int INF = 10L ^^ 9 + 23;

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

    Tuple!(int, int)[] arr;
    foreach (_; 0 .. n) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;

        arr ~= tuple(a, b);
    }

    int [int] cntspec;
    int [double][double] cnt;
    foreach (i; 0 .. n) {
        int x1 = arr[i][0], y1 = arr[i][1];
        foreach (j; i+1 .. n) {
            int x2 = arr[j][0], y2 = arr[j][1];

            if (x1 == x2) { cntspec[x1] += 0; }
            else {
                double a = (y2 - y1).to!double / (x2 - x1);
                double b = y1.to!double - a * x1;
                cnt[a][b] += 0;
            }
        }
    }

    debug { cnt.writeln; }

    foreach (i; 0 .. n) {
        cntspec[arr[i][0]] += 1;

        foreach (a; cnt.keys) {
            double b = arr[i][1].to!double - arr[i][0] * a;
            cnt[a][b] += 1;
        }
    }

    debug { cnt.writeln; }

    auto f = (int x) => x.to!long * (x-1) * (x-2) / 6;
    long ans = f(n);

    long linearOpts(int[] vals) {
        return vals.filter!(x => x >= 3).map!f.sum(0L);
    }

    ans -= linearOpts(cntspec.values);
    ans -= cnt.values.map!(dct => linearOpts(dct.values)).sum(0L);

    ans.writeln;
}


There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.

The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.

Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.

NoteIn the first sample, the optimal order is: 10, 20, 30, 40, 50.In the second sample, the optimal order is: 100, 200, 100, 200.

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

void main(string[] args)
{
	auto n = to!int(strip(readln));
	int[1001] a;
	auto x = split(strip(readln));
	foreach (j; 0 .. n)
	{
		 a[to!int(x[j])]++;
	}

	int count;
	int ze = s(a);
	while(ze >= 1)
	{
		count +=  ze - 1;
		ze = s(a);
	}

	writeln(count);
}
int s(int[] a)
{
	int count;
	foreach (i, val ; a)
	{
		if (val >= 1)
			count++;
		a[i]--;
	}
	return count;
}


To become the king of Codeforces, Kuroni has to solve the following problem.He is given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. Help Kuroni to calculate $$$\prod_{1\le i<j\le n} |a_i - a_j|$$$. As result can be very big, output it modulo $$$m$$$.If you are not familiar with short notation, $$$\prod_{1\le i<j\le n} |a_i - a_j|$$$ is equal to $$$|a_1 - a_2|\cdot|a_1 - a_3|\cdot$$$ $$$\dots$$$ $$$\cdot|a_1 - a_n|\cdot|a_2 - a_3|\cdot|a_2 - a_4|\cdot$$$ $$$\dots$$$ $$$\cdot|a_2 - a_n| \cdot$$$ $$$\dots$$$ $$$\cdot |a_{n-1} - a_n|$$$. In other words, this is the product of $$$|a_i - a_j|$$$ for all $$$1\le i < j \le n$$$.

The first line contains two integers $$$n$$$, $$$m$$$ ($$$2\le n \le 2\cdot 10^5$$$, $$$1\le m \le 1000$$$) — number of numbers and modulo. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^9$$$).

Output the single number — $$$\prod_{1\le i<j\le n} |a_i - a_j| \bmod m$$$.

NoteIn the first sample, $$$|8 - 5| = 3 \equiv 3 \bmod 10$$$.In the second sample, $$$|1 - 4|\cdot|1 - 5|\cdot|4 - 5| = 3\cdot 4 \cdot 1 = 12 \equiv 0 \bmod 12$$$.In the third sample, $$$|1 - 4|\cdot|1 - 9|\cdot|4 - 9| = 3 \cdot 8 \cdot 5 = 120 \equiv 1 \bmod 7$$$.

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

void main ()
{
	int n, m;
	while (readf !(" %s %s") (n, m) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		if (n > m)
		{
			writeln (0);
			continue;
		}
		int res = 1;
		foreach (i, x; a)
		{
			foreach (j, y; a)
			{
				if (i < j)
				{
					res = (res * 1L * abs (y - x)) % m;
				}
			}
		}
		writeln (res);
	}
}

//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.functional;

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

pure
X genericPower(alias mul, X, Y) (X x, Y y, X one = 1.to!X)
  if (isUnsigned!Y) {
  X a = one, b = x;
  while (y > 0) {
    if (y & 1) {
      a = binaryFun!mul (a, b);
    }
    b = binaryFun!mul (b, b);
    y >>>= 1;
  }
  return a;
}

int test (uint[] a, const int m) {
  sort (a);
  a[] %= m;
  auto d = new ulong[m];
  auto x = new int[m];
  foreach (f, i; a) {
    if (f > 0) {
      int k = i;
      foreach (j; 0 .. m) {
        d[k] += x[j];
        if (--k < 0) k += m;
      }
    }
    ++x[i];
  }
  if (d[0] > 0) return 0;
  debug stderr.writeln (d);
  int mul (int i, int j) {
    return (i * j) % m;
  }
  int res = 1;
  foreach (k; 1 .. m) {
    res = mul (res, genericPower!(mul, int, ulong)(k, d[k]));
  }
  return res;
}

void main() {
  auto r = new InputReader ();
  //immutable nt = r.next!uint ();
  const n = r.next!uint ();
  const m = r.next!uint ();
  auto a = r.nextA!uint (n);
  writeln (test (a, m));
}

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

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

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

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

struct ModInt {
  static int M;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op)() const if (op == "-") { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op)(long a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  string toString() const { return x.to!string; }
}

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

      auto freq = new long[M];
      auto freq2 = new long[M];
      foreach (i; 0 .. N) {
        const a = A[i] % M;
        foreach (x; 0 .. a + 1) {
          freq2[a - x] += freq[x];
        }
        foreach (x; a + 1 .. M) {
          freq2[a - x + M] += freq[x];
        }
        ++freq[a];
      }

      ModInt ans = 1;
      foreach (x; 0 .. M) {
        ans *= ModInt(x)^^freq2[x];
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

//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.functional;

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

pure
X genericPower(alias mul, X, Y) (X x, Y y, X one = 1.to!X)
  if (isUnsigned!Y) {
  X a = one, b = x;
  while (y > 0) {
    if (y & 1) {
      a = binaryFun!mul (a, b);
    }
    b = binaryFun!mul (b, b);
    y >>>= 1;
  }
  return a;
}

int test (uint[] a, const int m) {
  sort (a);
  a[] %= m;
  auto d = new ulong[m];
  auto x = new int[m];
  foreach (f, i; a) {
    if (f > 0) {
      int k = i;
      foreach (j; 0 .. m) {
        d[k] += x[j];
        if (--k < 0) k += m;
      }
    }
    ++x[i];
  }
  /*
  auto c = new int[m];
  foreach (x; a) ++c[x % m];
  if (c[0] > 1) return 0;
  auto d = new ulong[m];
  foreach (i; 0 .. m) if (c[i] > 0) {
    foreach (j; 0 .. m) if (c[j] > 0) {
      if (i != j) {
        const int k = (m + i - j) % m;
        d[k] += c[i].to!ulong * c[j];
      }
    }
  }
  */
  debug stderr.writeln (d);
  int mul (int i, int j) {
    return (i * j) % m;
  }
  int res = 1;
  foreach (k; 1 .. m) {
    res = mul (res, genericPower!(mul, int, ulong)(k, d[k]));
  }
  return res;
}

void main() {
  auto r = new InputReader ();
  //immutable nt = r.next!uint ();
  const n = r.next!uint ();
  const m = r.next!uint ();
  auto a = r.nextA!uint (n);
  writeln (test (a, m));
}


As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes. The second line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ n, fi ≠ i), meaning that the i-th plane likes the fi-th.

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO». You can output any letter in lower case or in upper case.

NoteIn first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.In second example there are no love triangles.

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

void main(){

	int n;

	readf("%d", &n);
	readln;

	int[5010] a;

	for (int i = 1; i <= n; ++i){
		readf("%d", &a[i]);
		if (i < n) scanf(" "); else readln;
	}

	for (int i = 1; i < n; ++i){
		for (int j = i + 1; j <= n; ++j) if (a[i] == j){
			if (a[a[j]] == i){
				writeln("YES");
				return;
			}
		}
	}

	writeln("NO");
}


You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. You can perform operations on it.In one operation you can replace any element of the array $$$a_i$$$ with $$$\lfloor \frac{a_i}{2} \rfloor$$$, that is, by an integer part of dividing $$$a_i$$$ by $$$2$$$ (rounding down).See if you can apply the operation some number of times (possible $$$0$$$) to make the array $$$a$$$ become a permutation of numbers from $$$1$$$ to $$$n$$$ —that is, so that it contains all numbers from $$$1$$$ to $$$n$$$, each exactly once.For example, if $$$a = [1, 8, 25, 2]$$$, $$$n = 4$$$, then the answer is yes. You could do the following:  Replace $$$8$$$ with $$$\lfloor \frac{8}{2} \rfloor = 4$$$, then $$$a = [1, 4, 25, 2]$$$.  Replace $$$25$$$ with $$$\lfloor \frac{25}{2} \rfloor = 12$$$, then $$$a = [1, 4, 12, 2]$$$.  Replace $$$12$$$ with $$$\lfloor \frac{12}{2} \rfloor = 6$$$, then $$$a = [1, 4, 6, 2]$$$.  Replace $$$6$$$ with $$$\lfloor \frac{6}{2} \rfloor = 3$$$, then $$$a = [1, 4, 3, 2]$$$.

The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) —the number of test cases. Each test case contains exactly two lines. The first one contains an integer $$$n$$$ ($$$1 \le n \le 50$$$), the second one contains integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

For each test case, output on a separate line:   YES if you can make the array $$$a$$$ become a permutation of numbers from $$$1$$$ to $$$n$$$,  NO otherwise.  You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

NoteThe first test case is explained in the text of the problem statement.In the second test case, it is not possible to get a permutation.

void main() { runSolver(); }

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

  auto solve() {
    void subSolve(int n, int[] arr) {
      foreach(ref a; arr) {
        while(a > n) a /= 2;
      }

      auto counts = new int[](n + 1);
      foreach(a; arr) {
        while(a > 0) {
          counts[a]++;
          a /= 2;
        }
      }

      auto used = new bool[](n + 1);
      foreach(a; arr) {
        int minValue = 0;
        int minCounts = int.max;
        while(a > 0) {
          if (!used[a] && minCounts.chmin(counts[a])) minValue = a;
          a /= 2;
        }

        if (minValue > 0) used[minValue] = true;
      }

      bool ans = used[1..$].count(false) == 0;
      writeln(ans ? "YES" : "NO");
    }

    foreach(_; 0..QN) {
      auto N = scan!int;
      subSolve(N, scan!int(N));
    }
  }

  outputForAtCoder(&solve);
}

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

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

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

K binarySearch(K)(bool delegate(K) cond, K l, K r) { return binarySearch((K k) => k, cond, l, r); }
T binarySearch(T, K)(K delegate(T) fn, bool delegate(K) cond, T l, T r) {
  auto ok = l;
  auto ng = r;
  const T TWO = 2;

  bool again() {
    static if (is(T == float) || is(T == double) || is(T == real)) {
      return !ng.approxEqual(ok, 1e-08, 1e-08);
    } else {
      return abs(ng - ok) > 1;
    }
  }

  while(again()) {
    const half = (ng + ok) / TWO;
    const halfValue = fn(half);

    if (cond(halfValue)) {
      ok = half;
    } else {
      ng = half;
    }
  }

  return ok;
}

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;

bool can_convert(int from, int to)
{
  while (from > to) {
    from >>= 1;
  }
  return from == to;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    int n;
    readf!" %d "(n);
    auto a = readln.splitter.map!(to!int).array;
    DList!int l;
    foreach (x ; a)
      l.insertBack(x);

    bool good = true;

    foreach_reverse (target ; 1 .. n + 1) {
      bool converted = false;
      foreach (i ; 0 .. n) {
        auto x = l.front;
        l.removeFront;
        if (can_convert(x, target)) {
          converted = true;
          break;
        }
        l.insertBack(x);
      }
      if (!converted) {
        good = false;
        break;
      }
    }
    writeln(good ? "YES" : "NO");
  }
}

import std;

void main () {
    immutable tests = (){ uint t; readf!"%s\n"(t); return t; }();

    BigLoop: foreach (test_index; 0 .. tests) {
        immutable n = readln().chomp.to!uint;

         size_t[][uint] aa;
         foreach (i, val; readln.chomp.split(' ').to!(uint[])) {
             while (val) {
                 aa[val] ~= i;
                 val /= 2;
             }
         }

         bool[size_t] used;

         foreach (val; iota(n, 0, -1)) {
             if (val !in aa) {
                 writeln("NO");
                 continue BigLoop;
             }

             auto r = aa[val].filter!(x => x !in used);

             if (r.empty) {
                 writeln("NO");
                 continue BigLoop;
             }
             used[r.front] = true;
        }
         writeln("YES");
    }
}
// ""


It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.Given a connected graph with $$$n$$$ vertices, you can choose to either:  find an independent set that has exactly $$$\lceil\sqrt{n}\rceil$$$ vertices. find a simple cycle of length at least $$$\lceil\sqrt{n}\rceil$$$. An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$5 \le n \le 10^5$$$, $$$n-1 \le m \le 2 \cdot 10^5$$$) — the number of vertices and edges in the graph. Each of the next $$$m$$$ lines contains two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$) that mean there's an edge between vertices $$$u$$$ and $$$v$$$. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges.

If you choose to solve the first problem, then on the first line print "1", followed by a line containing $$$\lceil\sqrt{n}\rceil$$$ distinct integers not exceeding $$$n$$$, the vertices in the desired independent set. If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, $$$c$$$, representing the length of the found cycle, followed by a line containing $$$c$$$ distinct integers integers not exceeding $$$n$$$, the vertices in the desired cycle, in the order they appear in the cycle.

NoteIn the first sample:Notice that you can solve either problem, so printing the cycle $$$2-4-3-1-5-6$$$ is also acceptable.In the second sample:Notice that if there are multiple answers you can print any, so printing the cycle $$$2-5-6$$$, for example, is acceptable.In the third sample:

// Code By H~$~C

module main;

import core.stdc.stdio, core.stdc.stdlib, core.stdc.string;
import std.stdio, std.array, std.string, std.math;
import std.algorithm, std.range, std.random;
import std.conv, std.typecons;
alias to!(string) to_string;
alias to!(int) to_int;
alias to!(long) to_long;
alias to!(double) to_double;
immutable int inf = 0x3f3f3f3f;
immutable long lnf = 0x3f3f3f3f3f3f3f3f;
immutable typeof(null) NULL = null;

static const int Maxn = 100005;

int n, m;
int[][Maxn] g;
int[] s, one;
int[Maxn] dep;
bool[Maxn] bad;

void dfs(int u) {
  s ~= u;
  dep[u] = to_int(s.length);
  foreach (v; g[u]) {
    if (dep[v]) {
      int sz = dep[u] - dep[v] + 1;
      if (sz >= m) {
        writeln(2, '\n', sz);
        writefln!"%(%s %)"(s[dep[v] - 1 .. dep[u]]);
        exit(0);
      }
    }
    else {
      dfs(v);
    }
  }
  s.length--;
  if (!bad[u]) {
    one ~= u;
    foreach (v; g[u]) {
      bad[v] = true;
    }
  }
}

void main() {
  rndGen.seed(19260817);

  readf!" %s %s"(n, m);
  foreach (i; 0 .. m) {
    int u, v;
    readf!" %s %s"(u, v);
    g[u] ~= v;
    g[v] ~= u;
  }

  m = to!int(ceil(sqrt(to!double(n))));
  bad[] = false;
  dfs(1);
  writeln(1);
  writefln!"%(%s %)"(one[0 .. m]);
}

// Code By H~$~C

module main;

import core.stdc.stdio, core.stdc.stdlib, core.stdc.string;
import std.stdio, std.array, std.string, std.math;
import std.algorithm, std.range, std.random;
import std.conv, std.typecons;
alias to!(string) to_string;
alias to!(int) to_int;
alias to!(long) to_long;
alias to!(double) to_double;
immutable int inf = 0x3f3f3f3f;
immutable long lnf = 0x3f3f3f3f3f3f3f3f;
immutable typeof(null) NULL = null;

static const int Maxn = 100005;

int n, m;
int[][Maxn] g;
int[] s, one;
int[Maxn] dep;
bool[Maxn] bad;

void dfs(int u) {
  s ~= u;
  dep[u] = to_int(s.length);
  foreach (v; g[u]) {
    if (dep[v]) {
      int sz = dep[u] - dep[v] + 1;
      if (sz >= m) {
        writeln(2, '\n', sz);
        writefln!"%(%s %)"(s[dep[v] - 1 .. dep[u]]);
        exit(0);
      }
    }
    else {
      dfs(v);
    }
  }
  s.length--;
  if (!bad[u]) {
    one ~= u;
    foreach (v; g[u]) {
      bad[v] = true;
    }
  }
}

void main() {
  rndGen.seed(19260817);

  readf!" %s %s"(n, m);
  foreach (i; 0 .. m) {
    int u, v;
    readf!" %s %s"(u, v);
    g[u] ~= v;
    g[v] ~= u;
  }

  m = to!int(ceil(sqrt(to!double(n))));
  bad[] = false;
  dfs(1);
  writeln(1);
  writefln!"%(%s %)"(one);
}


Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases:  At least one of the chips at least once fell to the banned cell.  At least once two chips were on the same cell.  At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.

The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n.

Print a single integer — the maximum points Gerald can earn in this game.

NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).

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;

int [] [] g;
bool [] b;
int [] p;

bool bipart (int v)
{
	if (b[v])
	{
		return false;
	}
	b[v] = true;

	foreach (c; g[v])
	{
		if (c == NA)
		{
			continue;
		}
		if (p[c] == NA || bipart (p[c]))
		{
			p[c] = v;
			return true;
		}
	}

	return false;
}

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto r = new bool [n + 1];
		auto c = new bool [n + 1];
		r[] = false;
		c[] = false;
		r[2..n] = true;
		c[2..n] = true;
		foreach (i; 0..m)
		{
			int cr, cc;
			readf (" %s %s", &cr, &cc);
			r[cr] = false;
			c[cc] = false;
		}
		int add = 0;
		if (n & 1)
		{
			if (r[n / 2 + 1] || c[n / 2 + 1])
			{
				add++;
				r[n / 2 + 1] = false;
				c[n / 2 + 1] = false;
			}
		}

		auto rv = new int [] [] (n + 1, 2);
		auto cv = new int [] [] (n + 1, 2);
		g = new int [] [n * 4];
		int k = 0;
		foreach (i; 2..n)
		{
			if (r[i])
			{
				rv[i] = [k, k + 1];
				k += 2;
			}
			else
			{
				rv[i] = [NA, NA];
			}

			if (c[i])
			{
				cv[i] = [k, k + 1];
				k += 2;
			}
			else
			{
				cv[i] = [NA, NA];
			}
		}
		debug {writeln ("k = ", k);}

		int res = 0;
		int t = 0;
		foreach (i; 2..n)
		{
			if (r[i])
			{
				g[t] = [t ^ 1, cv[i][0], cv[n - i + 1][1]];
				t++;
				g[t] = [t ^ 1, cv[i][1], cv[n - i + 1][0]];
				t++;
			}
			if (c[i])
			{
				g[t] = [t ^ 1, rv[i][0], rv[n - i + 1][1]];
				t++;
				g[t] = [t ^ 1, rv[i][1], rv[n - i + 1][0]];
				t++;
			}
		}

		b = new bool [k];
		p = new int [k];
		p[] = NA;
		foreach (i; 0..k)
		{
			b[] = false;
			if (p[i] == NA && bipart (i))
			{
				res++;
			}
		}

		writeln (k - res + add);
	}
}

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

void play(){
    int n, m;
    n = rd!int;
    m = rd!int;
    auto rows = new ll[](n);
    auto cols = new ll[](n);
    rows[n-1] += 2;
    cols[n-1] += 2;
    rows[0] += 2;
    cols[0] += 2;
    foreach(i; 0..m){
        int xi = rd!int - 1;
        int yi = rd!int - 1;
        ++rows[xi];
        ++cols[yi];
    }
    ll cnt = 0, midd = 0;
    foreach(i; 0..n){
        if(!rows[i]) ++cnt;
        if(!cols[i]) ++cnt;

        if(i == n/2){
            if(!rows[i]) ++midd;
            if(!cols[i]) ++midd;
        }
    }
    if(n % 2){
        cnt -= midd;
        if(midd) ++cnt;
    }
    writeln(cnt);
}

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

/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
alias ll = long;
alias rbt = RedBlackTree;
alias sr = SortedRange;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }

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;

void main ()
{
	long n;
	while (readf (" %s", &n) > 0)
	{
		while (n % 3 == 0)
		{
			n /= 3;
		}
		long res = n / 3 + 1;
		writeln (res);
	}
}


An ant moves on the real line with constant speed of $$$1$$$ unit per second. It starts at $$$0$$$ and always moves to the right (so its position increases by $$$1$$$ each second).There are $$$n$$$ portals, the $$$i$$$-th of which is located at position $$$x_i$$$ and teleports to position $$$y_i < x_i$$$. Each portal can be either active or inactive. The initial state of the $$$i$$$-th portal is determined by $$$s_i$$$: if $$$s_i=0$$$ then the $$$i$$$-th portal is initially inactive, if $$$s_i=1$$$ then the $$$i$$$-th portal is initially active. When the ant travels through a portal (i.e., when its position coincides with the position of a portal):   if the portal is inactive, it becomes active (in this case the path of the ant is not affected);  if the portal is active, it becomes inactive and the ant is instantly teleported to the position $$$y_i$$$, where it keeps on moving as normal. How long (from the instant it starts moving) does it take for the ant to reach the position $$$x_n + 1$$$? It can be shown that this happens in a finite amount of time. Since the answer may be very large, compute it modulo $$$998\,244\,353$$$.

The first line contains the integer $$$n$$$ ($$$1\le n\le 2\cdot 10^5$$$) — the number of portals. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$ and $$$s_i$$$ ($$$1\le y_i < x_i\le 10^9$$$, $$$s_i\in\{0,1\}$$$) — the position of the $$$i$$$-th portal, the position where the ant is teleported when it travels through the $$$i$$$-th portal (if it is active), and the initial state of the $$$i$$$-th portal. The positions of the portals are strictly increasing, that is $$$x_1<x_2<\cdots<x_n$$$. It is guaranteed that the $$$2n$$$ integers $$$x_1, \, x_2, \, \dots, \, x_n, \, y_1, \, y_2, \, \dots, \, y_n$$$ are all distinct.

Output the amount of time elapsed, in seconds, from the instant the ant starts moving to the instant it reaches the position $$$x_n+1$$$. Since the answer may be very large, output it modulo $$$998\,244\,353$$$.

NoteExplanation of the first sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed $$$1$$$ and the time spent during the movement is written above the arrow). $$$$$$ 0 \stackrel{6}{\longrightarrow} 6 \leadsto 5 \stackrel{3}{\longrightarrow} 8 \leadsto 1 \stackrel{2}{\longrightarrow} 3 \leadsto 2 \stackrel{4}{\longrightarrow} 6 \leadsto 5 \stackrel{2}{\longrightarrow} 7 \leadsto 4 \stackrel{2}{\longrightarrow} 6 \leadsto 5 \stackrel{4}{\longrightarrow} 9 $$$$$$ Notice that the total time is $$$6+3+2+4+2+2+4=23$$$.Explanation of the second sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed $$$1$$$ and the time spent during the movement is written above the arrow). $$$$$$ 0 \stackrel{454971987}{\longrightarrow} 454971987 \leadsto 406874902 \stackrel{48097086}{\longrightarrow} 454971988 $$$$$$ Notice that the total time is $$$454971987+48097086=503069073$$$.Explanation of the third sample: Since all portals are initially off, the ant will not be teleported and will go straight from $$$0$$$ to $$$x_n+1=899754846+1=899754847$$$.

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

immutable int mod = 998_244_353;

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		alias Teleport = Tuple !(int, q{x}, int, q{y}, int, q{state});
		auto t = new Teleport [n];
		alias Event = Tuple !(int, q{pos}, int, q{num}, int, q{type});
		Event [] e;
		e.reserve (n * 2);
		foreach (int i, ref c; t)
		{
			readf !(" %s %s %s") (c.x, c.y, c.state);
			e ~= Event (c.x, i, 0);
			e ~= Event (c.y, i, 1);
		}
		e.schwartzSort !(v => v.pos);

		auto ptr = new int [2] [n];
		foreach (j; 0..n * 2)
		{
			ptr[e[j].num][e[j].type] = j;
		}

		auto r = new int [n * 2 + 1];

		void inc (int pos, int val)
		{
			for (pos += 1; pos <= n * 2; pos += (pos & -pos))
			{
				r[pos] += val - mod;
				r[pos] += (r[pos] < 0) * mod;
			}
		}

		int tot (int pos)
		{
			int res = 0;
			for (pos += 1; pos > 0; pos -= (pos & -pos))
			{
				res += r[pos] - mod;
				res += (res < 0) * mod;
			}
			return res;
		}

		int tot2 (int lo, int hi)
		in
		{
			debug {write (lo, " ", hi);}
		}
		out (res)
		{
			debug {writeln (": ", res);}
		}
		body
		{
			return (tot (hi - 1) + mod - tot (lo - 1)) % mod;

		}

		foreach (i; 0..n)
		{
			int value = (t[i].x - t[i].y) % mod;
			value = (value + tot2 (ptr[i][1], ptr[i][0])) % mod;
			inc (ptr[i][0], value);
		}
		debug {writeln (r);}

		int res = (t.back.x + 1) % mod;
		foreach (i; 0..n)
		{
			if (t[i].state)
			{
				auto pos = ptr[i][0];
				debug {writeln (i, ": ", tot2 (pos, pos + 1));}
				res = (res + tot2 (pos, pos + 1)) % mod;
			}
		}
		writeln (res);
	}
}

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

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

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

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

struct ModInt(uint M_) {
  import std.conv : to;
  alias M = M_;
  uint x;
  this(ModInt a) { x = a.x; }
  this(uint x_) { x = x_ % M; }
  this(ulong x_) { x = x_ % M; }
  this(int x_) { x = ((x_ %= cast(int)(M)) < 0) ? (x_ + cast(int)(M)) : x_; }
  this(long x_) { x = cast(uint)(((x_ %= cast(long)(M)) < 0) ? (x_ + cast(long)(M)) : x_); }
  ref ModInt opAssign(T)(inout(T) a) if (is(T == uint) || is(T == ulong) || is(T == int) || is(T == long)) { return this = ModInt(a); }
  ref ModInt opOpAssign(string op, T)(T a) {
    static if (is(T == ModInt)) {
      static if (op == "+") { x = ((x += a.x) >= M) ? (x - M) : x; }
      else static if (op == "-") { x = ((x -= a.x) >= M) ? (x + M) : x; }
      else static if (op == "*") { x = cast(uint)((cast(ulong)(x) * a.x) % M); }
      else static if (op == "/") { this *= a.inv(); }
      else static assert(false);
      return this;
    } else static if (op == "^^") {
      if (a < 0) return this = inv()^^(-a);
      ModInt b = this, c = 1U;
      for (long e = a; e; e >>= 1) { if (e & 1) c *= b; b *= b; }
      return this = c;
    } else {
      return mixin("this " ~ op ~ "= ModInt(a)");
    }
  }
  ModInt inv() const {
    uint a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const q = a / b; const c = a - q * b; a = b; b = c; const w = y - cast(int)(q) * z; y = z; z = w; }
    assert(a == 1); return ModInt(y);
  }
  ModInt opUnary(string op)() const {
    static if (op == "+") { return this; }
    else static if (op == "-") { ModInt a; a.x = x ? (M - x) : 0U; return a; }
    else static assert(false);
  }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op, T)(T a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  bool opCast(T: bool)() const { return (x != 0U); }
  string toString() const { return x.to!string; }
}

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

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

      long[] xs;
      xs ~= X;
      xs ~= Y;
      xs ~= 0;
      xs.sort;
      const xsLen = cast(int)(xs.length);
      xs ~= X[N - 1] + 1;

      auto es = new int[N];
      auto fs = new int[N];
      auto ids = new int[xsLen];
      ids[] = -1;
      foreach (i; 0 .. N) {
        es[i] = xs.lowerBound(X[i]);
        fs[i] = xs.lowerBound(Y[i]);
        ids[es[i]] = ids[fs[i]] = i;
      }
      debug {
        writeln("xs = ", xs);
        writeln("es = ", es);
        writeln("fs = ", fs);
        writeln("ids = ", ids);
      }

      auto ts = new Mint[xsLen];
      ts[xsLen - 1] = 1;
      foreach_reverse (e; 1 .. xsLen) {
        const i = ids[e];
        if (e == es[i]) {
          ts[e - 1] = ts[e] + (ts[e] - (S[i] ? 0 : 1));
        } else {
          ts[e - 1] = ts[e] - (ts[es[i]] - (S[i] ? 0 : 1));
        }
      }
      debug {
        writeln("ts = ", ts);
      }

      Mint ans;
      foreach (e; 0 .. xsLen) {
        ans += ts[e] * Mint(xs[e + 1] - xs[e]);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

immutable int mod = 998_244_353;

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		alias Teleport = Tuple !(int, q{x}, int, q{y}, int, q{state});
		auto t = new Teleport [n];
		alias Event = Tuple !(int, q{pos}, int, q{num}, int, q{type});
		Event [] e;
		e.reserve (n * 2);
		foreach (int i, ref c; t)
		{
			readf !(" %s %s %s") (c.x, c.y, c.state);
			e ~= Event (c.x, i, 0);
			e ~= Event (c.y, i, 1);
		}
		e.schwartzSort !(v => v.pos);

		auto ptr = new int [2] [n];
		foreach (j; 0..n * 2)
		{
			ptr[e[j].num][e[j].type] = j;
		}

		auto r = new int [n * 2 + 1];

		void inc (int pos, int val)
		{
			for (pos += 1; pos <= n * 2; pos += (pos & -pos))
			{
				r[pos] += val - mod;
				r[pos] += (r[pos] < 0) * mod;
			}
		}

		int tot (int pos)
		{
			int res = 0;
			for (pos += 1; pos > 0; pos -= (pos & -pos))
			{
				res += r[pos] - mod;
				res += (res < 0) * mod;
			}
			return res;
		}

		int tot2 (int lo, int hi)
		in
		{
			debug {write (lo, " ", hi);}
		}
		out (res)
		{
			debug {writeln (": ", res);}
		}
		body
		{
			return (tot (hi - 1) + mod - tot (lo - 1)) % mod;

		}

		foreach (i; 0..n)
		{
			int value = (t[i].x - t[i].y) % mod;
			value = (value + tot2 (ptr[i][1], ptr[i][0])) % mod;
			inc (ptr[i][0], value);
		}
		debug {writeln (r);}

		int res = t.back.x + 1;
		foreach (i; 0..n)
		{
			if (t[i].state)
			{
				auto pos = ptr[i][0];
				debug {writeln (i, ": ", tot2 (pos, pos + 1));}
				res = (res + tot2 (pos, pos + 1)) % mod;
			}
		}
		writeln (res);
	}
}


A sequence $$$a_1, a_2, \dots, a_k$$$ is called an arithmetic progression if for each $$$i$$$ from $$$1$$$ to $$$k$$$ elements satisfy the condition $$$a_i = a_1 + c \cdot (i - 1)$$$ for some fixed $$$c$$$.For example, these five sequences are arithmetic progressions: $$$[5, 7, 9, 11]$$$, $$$[101]$$$, $$$[101, 100, 99]$$$, $$$[13, 97]$$$ and $$$[5, 5, 5, 5, 5]$$$. And these four sequences aren't arithmetic progressions: $$$[3, 1, 2]$$$, $$$[1, 2, 4, 8]$$$, $$$[1, -1, 1, -1]$$$ and $$$[1, 2, 3, 3, 3]$$$.You are given a sequence of integers $$$b_1, b_2, \dots, b_n$$$. Find any index $$$j$$$ ($$$1 \le j \le n$$$), such that if you delete $$$b_j$$$ from the sequence, you can reorder the remaining $$$n-1$$$ elements, so that you will get an arithmetic progression. If there is no such index, output the number -1.

The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2\cdot10^5$$$) — length of the sequence $$$b$$$. The second line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$-10^9 \le b_i \le 10^9$$$) — elements of the sequence $$$b$$$.

Print such index $$$j$$$ ($$$1 \le j \le n$$$), so that if you delete the $$$j$$$-th element from the sequence, you can reorder the remaining elements, so that you will get an arithmetic progression. If there are multiple solutions, you are allowed to print any of them. If there is no such index, print -1.

NoteNote to the first example. If you delete the $$$4$$$-th element, you can get the arithmetic progression $$$[2, 4, 6, 8]$$$.Note to the second example. The original sequence is already arithmetic progression, so you can delete $$$1$$$-st or last element and you will get an arithmetical progression again.

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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto B = N.iota.map!(i => tuple(i, A[i])).array;

    if (N == 2) {
        writeln(1);
        return;
    }

    if (N == 3) {
        writeln(2);
        return;
    }

    int solve() {
        long d = B[1][1] - B[0][1];
        int cnt = 0;
        int ans = -1;
        long prev = B[1][1];

        foreach (i; 2..N) {
            if (B[i][1] - prev != d) {
                cnt += 1;
                if (cnt > 1) break;
                ans = B[i][0] + 1;
            } else {
                prev = B[i][1];
            }
        }

        if (cnt == 0) {
            return B[0][0] + 1;
        } else if (cnt == 1) {
            return ans;
        } else {
            return -1;
        }
    }

    B.sort!"a[1] < b[1]";
    int ans = solve();
    if (ans != -1) {
        ans.writeln;
        return;
    }

    B.sort!"a[1] > b[1]";
    ans = solve();
    if (ans != -1) {
        ans.writeln;
        return;
    }

    writeln(-1);
}

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

bool isAP (R) (R a)
{
	foreach (i; 2..a.length)
	{
		if (a[i - 0].value - a[i - 1].value !=
		    a[i - 1].value - a[i - 2].value)
		{
			return false;
		}
	}
	return true;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		alias Pair = Tuple !(int, q{value}, int, q{num});
		auto a = new Pair [n];
		foreach (i, ref x; a)
		{
			readf (" %s", &x.value);
			x.num = i + 1;
		}
		sort (a);

		int res = -1;
		if (isAP (a[1..$]))
		{
			res = 0;
		}
		else if (isAP (a[0..$ - 1]))
		{
			res = n - 1;
		}
		else
		{
			int d = a[$ - 1].value - a[0].value;
			if (d % (n - 2) == 0)
			{
				d /= n - 2;
				int j = -1;
				foreach (i; 0..n)
				{
					if (a[i].value != a[0].value + d * i)
					{
						j = i;
						break;
					}
				}
				if (isAP (a[0..j] ~ a[j + 1..$]))
				{
					res = j;
				}
			}
		}
		writeln (res >= 0 ? a[res].num : res);
	}
}

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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto B = N.iota.map!(i => tuple(i, A[i])).array;

    if (N == 2) {
        writeln(1);
        return;
    }

    if (N == 3) {
        writeln(2);
        return;
    }

    int solve() {
        long d = B[1][1] - B[0][1];
        int cnt = 0;
        int ans = -1;
        long prev = B[1][1];

        foreach (i; 2..N) {
            if (B[i][1] - prev != d) {
                cnt += 1;
                if (cnt > 1) break;
                ans = B[i][0] + 1;
            } else {
                prev = B[i][1];
            }
        }

        if (cnt == 0) {
            return 1;
        } else if (cnt == 1) {
            return ans;
        } else {
            return -1;
        }
    }

    B.sort!"a[1] < b[1]";
    int ans = solve();
    if (ans != -1) {
        ans.writeln;
        return;
    }
    B.sort!"a[1] > b[1]";
    ans = solve();
    if (ans != -1) {
        ans.writeln;
        return;
    }
    writeln(-1);
}