Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101. The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.

The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.

Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).

NoteIn the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let n = String.length s in
  let z = ref 0 in
  let o = ref 0 in
  let q = ref 0 in
  let z2 = ref 0 in
  let o2 = ref 0 in
  let q2 = ref 0 in
    for i = 0 to n - 3 do
      match s.[i] with
	| '0' -> incr z
	| '1' -> incr o
	| '?' -> incr q
	| _ -> assert false
    done;
    for i = n - 2 to n - 1 do
      match s.[i] with
	| '0' -> incr z2
	| '1' -> incr o2
	| '?' -> incr q2
	| _ -> assert false
    done;
    let ht = Hashtbl.create 4 in
    let solve' s =
      let s = ref s in
      let d = ref 1 in
	while String.length !s > 2 do
	  (*Printf.printf "asd %s %d\n" !s !d;*)
	  (try
	     let i = String.index !s (Char.chr (!d + Char.code '0')) in
	       s := String.sub !s 0 i ^
		 String.sub !s (i + 1) (String.length !s - i - 1);
	   with
	     | Not_found ->
		 s := String.sub !s 1 (String.length !s - 1)
	  );
	  d := 1 - !d;
	done;
	Hashtbl.replace ht !s ()
    in
    let solve _s z o q z2 o2 tail =
      (*for d = 0 to q do
	let z = z + d
	and o = o + (q - d) in*)
	  (*Printf.printf "asd zoq %d %d %d\n" z o q;*)
	  if n mod 2 = 0 then (
	    if z + q >= o + 4
	    then solve' ("0000" ^ tail);
	    if z < o + q + 4 && z + q >= o + 2
	    then solve' ("00" ^ tail);
	    if o + q >= z + 4
	    then solve' ("1111" ^ tail);
	    if o < z + q + 4 && o + q >= z + 2
	    then solve' ("11" ^ tail);
	    if z <= o + q && o <= z + q
	    then Hashtbl.replace ht tail ();
	  ) else (
	    if z + q >= o + 5
	    then solve' ("00000" ^ tail);
	    if z <= o + q + 5 && z + q >= o + 3
	    then solve' ("000" ^ tail);
	    if o + q >= z + 5
	    then solve' ("11111" ^ tail);
	    if o < z + q + 5 && o + q >= z + 3
	    then solve' ("111" ^ tail);
	    if z <= o + q + 1 && o + 1 <= z + q
	    then solve' ("0" ^ tail);
	    if z + 1 <= o + q && o <= z + 1 + q
	    then solve' ("1" ^ tail);
	  )
      (*done*)
    in
      (match s.[n - 2], s.[n - 1] with
	 | '?', '?' ->
	     solve s !z !o !q 2 0 "00";
	     solve s !z !o !q 1 1 "01";
	     solve s !z !o !q 1 1 "10";
	     solve s !z !o !q 0 2 "11";
	 | '?', _ ->
	     solve s !z !o !q (!z2 + 1) !o2 ("0" ^ String.make 1 s.[n - 1]);
	     solve s !z !o !q !z2 (!o2 + 1) ("1" ^ String.make 1 s.[n - 1]);
	 | _, '?' ->
	     solve s !z !o !q (!z2 + 1) !o2 (String.make 1 s.[n - 2] ^ "0");
	     solve s !z !o !q !z2 (!o2 + 1) (String.make 1 s.[n - 2] ^ "1");
	 | _ ->
	     solve s !z !o !q !z2 !o2 (String.sub s (n - 2) 2);
      );
      if Hashtbl.mem ht "00" then Printf.printf "00\n";
      if Hashtbl.mem ht "01" then Printf.printf "01\n";
      if Hashtbl.mem ht "10" then Printf.printf "10\n";
      if Hashtbl.mem ht "11" then Printf.printf "11\n";

    let _ =
      let sb = Scanf.Scanning.stdib in
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
      let n = String.length s in
      let z = ref 0 in
      let o = ref 0 in
      let q = ref 0 in
      let z2 = ref 0 in
      let o2 = ref 0 in
      let q2 = ref 0 in
        for i = 0 to n - 3 do
          match s.[i] with
            | '0' -> incr z
            | '1' -> incr o
            | '?' -> incr q
            | _ -> assert false
        done;
        for i = n - 2 to n - 1 do
          match s.[i] with
            | '0' -> incr z2
            | '1' -> incr o2
            | '?' -> incr q2
            | _ -> assert false
        done;
        let ht = Hashtbl.create 4 in
        let solve' s =
          let s = ref s in
          let d = ref 1 in
            while String.length !s > 2 do
              (*Printf.printf "asd %s\n" !s;*)
              (try
                 let i = String.index !s (Char.chr (!d + Char.code '0')) in
                   s := String.sub !s 0 i ^
                     String.sub !s (i + 1) (String.length !s - i - 1);
               with
                 | Not_found ->
                     s := String.sub !s 1 (String.length !s - 1)
              );
              d := 1 - !d;
            done;
            Hashtbl.replace ht !s ()
        in
        let solve _s z o q z2 o2 tail =
          for d = 0 to q do
            let z = z + d
            and o = o + (q - d) in
              (*Printf.printf "asd zoq %d %d %d\n" z o q;*)
              if n mod 2 = 0 then (
                if z >= o + 4
                then solve' ("0000" ^ tail)
                else if o >= z + 4
                then solve' ("1111" ^ tail)
                else if z >= o + 2
                then solve' ("00" ^ tail)
                else if o >= z + 2
                then solve' ("11" ^ tail)
                else if z = o
                then Hashtbl.replace ht tail ()
                else assert false
              ) else (
                if z >= o + 5
                then solve' ("00000" ^ tail)
                else if o >= z + 5
                then solve' ("11111" ^ tail)
                else if z >= o + 3
                then solve' ("000" ^ tail)
                else if o >= z + 3
                then solve' ("111" ^ tail)
                else if z = o + 1
                then solve' ("0" ^ tail)
                else if z + 1 = o
                then solve' ("1" ^ tail)
                else assert false
              )
          done
        in
          (match s.[n - 2], s.[n - 1] with
             | '?', '?' ->
                 solve s !z !o !q 2 0 "00";
                 solve s !z !o !q 1 1 "01";
                 solve s !z !o !q 1 1 "10";
                 solve s !z !o !q 0 2 "11";
             | '?', _ ->
                 solve s !z !o !q (!z2 + 1) !o2 ("0" ^ String.make 1 s.[n - 1]);
                 solve s !z !o !q !z2 (!o2 + 1) ("1" ^ String.make 1 s.[n - 1]);
             | _, '?' ->
                 solve s !z !o !q (!z2 + 1) !o2 (String.make 1 s.[n - 2] ^ "0");
                 solve s !z !o !q !z2 (!o2 + 1) (String.make 1 s.[n - 2] ^ "1");
             | _ ->
                 solve s !z !o !q !z2 !o2 (String.sub s (n - 2) 2);
          );
          if Hashtbl.mem ht "00" then Printf.printf "00\n";
          if Hashtbl.mem ht "01" then Printf.printf "01\n";
          if Hashtbl.mem ht "10" then Printf.printf "10\n";
          if Hashtbl.mem ht "11" then Printf.printf "11\n";

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let n = String.length s in
  let z = ref 0 in
  let o = ref 0 in
  let q = ref 0 in
  let z2 = ref 0 in
  let o2 = ref 0 in
  let q2 = ref 0 in
    for i = 0 to n - 3 do
      match s.[i] with
	| '0' -> incr z
	| '1' -> incr o
	| '?' -> incr q
	| _ -> assert false
    done;
    for i = n - 2 to n - 1 do
      match s.[i] with
	| '0' -> incr z2
	| '1' -> incr o2
	| '?' -> incr q2
	| _ -> assert false
    done;
    let ht = Hashtbl.create 4 in
    let solve' s =
      let s = ref s in
      let d = ref 1 in
	while String.length !s > 2 do
	  (*Printf.printf "asd %s\n" !s;*)
	  (try
	     let i = String.index !s (Char.chr (!d + Char.code '0')) in
	       s := String.sub !s 0 i ^
		 String.sub !s (i + 1) (String.length !s - i - 1);
	   with
	     | Not_found ->
		 s := String.sub !s 1 (String.length !s - 1)
	  );
	  d := 1 - !d;
	done;
	Hashtbl.replace ht !s ()
    in
    let solve _s z o q z2 o2 tail =
      (*for d = 0 to q do
	let z = z + d
	and o = o + (q - d) in*)
	  (*Printf.printf "asd zoq %d %d %d\n" z o q;*)
	  if n mod 2 = 0 then (
	    if z + q >= o + 4
	    then solve' ("0000" ^ tail);
	    if o + q >= z + 4
	    then solve' ("1111" ^ tail);
	    if z + q >= o + 2
	    then solve' ("00" ^ tail);
	    if o + q >= z + 2
	    then solve' ("11" ^ tail);
	    if 0 <= o - z + q && o - z <= q
	    then Hashtbl.replace ht tail ();
	  ) else (
	    if z + q >= o + 5
	    then solve' ("00000" ^ tail);
	    if o + q >= z + 5
	    then solve' ("11111" ^ tail);
	    if z + q >= o + 3
	    then solve' ("000" ^ tail);
	    if o + q >= z + 3
	    then solve' ("111" ^ tail);
	    if 0 <= o - z + q + 1 && o - z + 1 <= q
	    then solve' ("0" ^ tail);
	    if 0 <= o - z + q - 1 && o - z - 1 <= q
	    then solve' ("1" ^ tail);
	  )
      (*done*)
    in
      (match s.[n - 2], s.[n - 1] with
	 | '?', '?' ->
	     solve s !z !o !q 2 0 "00";
	     solve s !z !o !q 1 1 "01";
	     solve s !z !o !q 1 1 "10";
	     solve s !z !o !q 0 2 "11";
	 | '?', _ ->
	     solve s !z !o !q (!z2 + 1) !o2 ("0" ^ String.make 1 s.[n - 1]);
	     solve s !z !o !q !z2 (!o2 + 1) ("1" ^ String.make 1 s.[n - 1]);
	 | _, '?' ->
	     solve s !z !o !q (!z2 + 1) !o2 (String.make 1 s.[n - 2] ^ "0");
	     solve s !z !o !q !z2 (!o2 + 1) (String.make 1 s.[n - 2] ^ "1");
	 | _ ->
	     solve s !z !o !q !z2 !o2 (String.sub s (n - 2) 2);
      );
      if Hashtbl.mem ht "00" then Printf.printf "00\n";
      if Hashtbl.mem ht "01" then Printf.printf "01\n";
      if Hashtbl.mem ht "10" then Printf.printf "10\n";
      if Hashtbl.mem ht "11" then Printf.printf "11\n";

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let n = String.length s in
  let z = ref 0 in
  let o = ref 0 in
  let q = ref 0 in
  let z2 = ref 0 in
  let o2 = ref 0 in
  let q2 = ref 0 in
    for i = 0 to n - 3 do
      match s.[i] with
	| '0' -> incr z
	| '1' -> incr o
	| '?' -> incr q
	| _ -> assert false
    done;
    for i = n - 2 to n - 1 do
      match s.[i] with
	| '0' -> incr z2
	| '1' -> incr o2
	| '?' -> incr q2
	| _ -> assert false
    done;
    let ht = Hashtbl.create 4 in
    let solve' s =
      let s = ref s in
      let d = ref 1 in
	while String.length !s > 2 do
	  (*Printf.printf "asd %s %d\n" !s !d;*)
	  (try
	     let i = String.index !s (Char.chr (!d + Char.code '0')) in
	       s := String.sub !s 0 i ^
		 String.sub !s (i + 1) (String.length !s - i - 1);
	   with
	     | Not_found ->
		 s := String.sub !s 1 (String.length !s - 1)
	  );
	  d := 1 - !d;
	done;
	Hashtbl.replace ht !s ()
    in
    let solve _s z o q z2 o2 tail =
      (*for d = 0 to q do
	let z = z + d
	and o = o + (q - d) in*)
	  (*Printf.printf "asd zoq %d %d %d\n" z o q;*)
	  if n mod 2 = 0 then (
	    if z + q >= o + 4
	    then solve' ("0000" ^ tail)
	    else if z + q >= o + 2
	    then solve' ("00" ^ tail);
	    if o + q >= z + 4
	    then solve' ("1111" ^ tail)
	    else if o + q >= z + 2
	    then solve' ("11" ^ tail);
	    if z <= o + q && o <= z + q
	    then Hashtbl.replace ht tail ();
	  ) else (
	    if z + q >= o + 5
	    then solve' ("00000" ^ tail)
	    else if z + q >= o + 3
	    then solve' ("000" ^ tail);
	    if o + q >= z + 5
	    then solve' ("11111" ^ tail)
	    else if o + q >= z + 3
	    then solve' ("111" ^ tail);
	    if z <= o + q + 1 && o + 1 <= z + q
	    then solve' ("0" ^ tail);
	    if z + 1 <= o + q && o <= z + 1 + q
	    then solve' ("1" ^ tail);
	  )
      (*done*)
    in
      (match s.[n - 2], s.[n - 1] with
	 | '?', '?' ->
	     solve s !z !o !q 2 0 "00";
	     solve s !z !o !q 1 1 "01";
	     solve s !z !o !q 1 1 "10";
	     solve s !z !o !q 0 2 "11";
	 | '?', _ ->
	     solve s !z !o !q (!z2 + 1) !o2 ("0" ^ String.make 1 s.[n - 1]);
	     solve s !z !o !q !z2 (!o2 + 1) ("1" ^ String.make 1 s.[n - 1]);
	 | _, '?' ->
	     solve s !z !o !q (!z2 + 1) !o2 (String.make 1 s.[n - 2] ^ "0");
	     solve s !z !o !q !z2 (!o2 + 1) (String.make 1 s.[n - 2] ^ "1");
	 | _ ->
	     solve s !z !o !q !z2 !o2 (String.sub s (n - 2) 2);
      );
      if Hashtbl.mem ht "00" then Printf.printf "00\n";
      if Hashtbl.mem ht "01" then Printf.printf "01\n";
      if Hashtbl.mem ht "10" then Printf.printf "10\n";
      if Hashtbl.mem ht "11" then Printf.printf "11\n";


You are given a sequence of numbers a1, a2, ..., an, and a number m.Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m.

The first line contains two numbers, n and m (1 ≤ n ≤ 106, 2 ≤ m ≤ 103) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.

NoteIn the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.In the third sample test you need to choose two numbers 3 on the ends.In the fourth sample test you can take the whole subsequence.

exception Fini

let no_doubl l =
	let rec aux = function
		| a::b::q	-> if a=b
					then aux (b::q)
					else a::(aux (b::q))
		| l'		-> l'
	in aux (List.sort compare l)

let main () =
	let (n,m) = Scanf.scanf "%d %d" (fun i j -> (i,j)) and l = ref [] in
		try
		(
		for i = 1 to n do
			let j = Scanf.scanf " %d" (fun k -> k) in
				l := no_doubl (((j mod m) :: !l) @ (List.map (fun k -> (j+k) mod m) !l));
				if List.hd !l = 0
					then raise Fini;
		done; Printf.printf "NO"
		) with Fini -> Printf.printf "YES";;

main ();;


A and B are preparing themselves for programming contests.B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake.However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared — the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change.Can you help B find out exactly what two errors he corrected?

The first line of the input contains integer n (3 ≤ n ≤ 105) — the initial number of compilation errors. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the errors the compiler displayed for the first time.  The third line contains n - 1 space-separated integers b1, b2, ..., bn - 1 — the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one.  The fourth line contains n - 2 space-separated integers с1, с2, ..., сn - 2 — the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one.

Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively.

NoteIn the first test sample B first corrects the error number 8, then the error number 123.In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step.

open Printf
open Num

let n = Scanf.scanf "%d " (fun x -> x)

let my_hash = Hashtbl.create 1234567

let my_hash2 = Hashtbl.create 1234567

let () =
  for i = 0 to n-1 do
    let j = Scanf.scanf "%d " (fun x -> x) in
    Hashtbl.add my_hash j i
  done

let () =
  for i = 0 to n-2 do
    let j = Scanf.scanf "%d " (fun x -> x) in
    Hashtbl.add my_hash2 j i;
    Hashtbl.remove my_hash j
  done

let () = Hashtbl.iter (fun x y -> printf "%d\n" x) my_hash

let () =
  for i = 0 to n-3 do
    let j = Scanf.scanf "%d " (fun x -> x) in
    Hashtbl.remove my_hash2 j;
  done

let () =  Hashtbl.iter (fun x y -> printf "%d\n" x) my_hash2

open Printf
open Scanf

let increment h t d =
  let c = try Hashtbl.find h t with Not_found -> 0 in
  if c+d = 0 then Hashtbl.remove h t else Hashtbl.replace h t (c+d)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int()) in
  let b = Array.init (n-1) (fun _ -> read_int()) in
  let c = Array.init (n-2) (fun _ -> read_int()) in

  let ha = Hashtbl.create 1000 in

  for i=0 to n-1 do
    increment ha a.(i) 1
  done;

  for i=0 to n-2 do
    increment ha b.(i) (-1)
  done;

  if Hashtbl.length ha <> 1 then failwith "input wrong 1";
  let e1 = ref (-1) in
  Hashtbl.iter (fun k v -> e1 := k) ha;

  let hb = Hashtbl.create 1000 in

  for i=0 to n-2 do
    increment hb b.(i) 1
  done;

  for i=0 to n-3 do
    increment hb c.(i) (-1)
  done;

  if Hashtbl.length hb <> 1 then failwith "input wrong 2";
  let e2 = ref (-1) in
  Hashtbl.iter (fun k v -> e2 := k) hb;

  printf "%d\n%d\n" !e1 !e2

let read () = Scanf.scanf "%d " (fun n -> n)

let get_list l =
    let rec loop i list =
        if i > 0
        then
            let var = read () in
            loop (i - 1) (var :: list)
        else
            List.sort (Pervasives.compare) list
    in loop l []

let subtract_lists first second =
    let rec subtract l1 l2 =
        match l1, l2 with
        | [], _ -> []
        | _, [] -> l1
        | hd :: tl, hhd :: ttl when hd = hhd -> subtract tl ttl
        | hd :: tl, _ -> hd :: (subtract tl l2)
    in subtract first second

let solve () =
    let n = read () in
    let first = get_list n in
    let second = get_list (n - 1) in
    let third = get_list (n - 2) in
    let bug_one = subtract_lists first second in
    let bug_two = subtract_lists second third in
    match bug_one, bug_two with
    | hd :: tl, hhd :: ttl -> (hd, hhd)
    | _ -> failwith "Empty list"

let print (a, b) = Printf.printf "%d\n%d\n" a b
let () = print (solve ())

let _ = read_line () in
let ints _  =  read_line () |> Str.split (Str.regexp " +") |> List.map int_of_string |> List.sort compare in
let rec diff x y =
  match (x ,y) with
   | ([e],[])  -> e
   | ((c :: cs), (d :: ds)) when c != d -> c
   | ((a :: ax), (b :: bx)) when a = b -> diff ax bx
  | _ -> failwith "error" in

let first = ints () and
    second = ints () and
    third = ints () in
Printf.printf "%d\n%d" (diff first second) (diff second third)

let next_int() =
  Scanf.scanf "%d " (fun x->x);;

let read_list n f =
  let rec loop idx =
    if idx == n then
      []
    else
      f()::loop (idx+1) in
  loop 0;;

let findDiff l1 l2 =
  let rec loop l1 l2 =
    match l1, l2 with
    | h1::t1, h2::t2 ->
       if h1 == h2 then
         loop t1 t2
       else
         h1
    | h1::t1, [] -> h1
    | _, _ -> 0 in
  loop (List.sort compare l1) (List.sort compare l2);;

let _ =
  let n = read_int() in
  let l1 = read_list n next_int in
  let l2 = read_list (n-1) next_int in
  let l3 = read_list (n-2) next_int in
  print_int(findDiff l1 l2);
  print_newline();
  print_int(findDiff l2 l3);
  print_newline();;

let rec prem_dif = function
	|[a],[]		-> a
	|t::q,t'::q'	-> if t=t'
				then prem_dif (q,q')
				else t;;

let main () =
	let n = Scanf.scanf "%d " (fun i -> i) and prem = ref []
	and sec = ref [] and tro = ref [] in
		for i = 1 to n do
			prem := (Scanf.scanf "%d " (fun j -> j))::!prem;
		done;
		for i = 1 to n-1 do
			sec := (Scanf.scanf "%d " (fun j -> j))::!sec;
		done;
		for i = 1 to n-2 do
			tro := (Scanf.scanf "%d " (fun j -> j))::!tro;
		done;
		prem := List.sort compare !prem;
		sec := List.sort compare !sec;
		tro := List.sort compare !tro;
		Printf.printf "%d\n%d" (prem_dif (!prem,!sec)) (prem_dif (!sec,!tro));;

main ();;

let rec prem_dif = function
	|[a],[]		-> a
	|t::q,t'::q'	-> if t=t'
				then prem_dif (q,q')
				else t;;

let main () =
	let n = Scanf.scanf "%d " (fun i -> i) and prem = ref []
	and sec = ref [] and tro = ref [] in
		for i = 1 to n do
			prem := (Scanf.scanf "%d " (fun j -> j))::!prem;
		done;
		for i = 1 to n-1 do
			sec := (Scanf.scanf "%d " (fun j -> j))::!sec;
		done;
		for i = 1 to n-2 do
			tro := (Scanf.scanf "%d " (fun j -> j))::!tro;
		done;
		Printf.printf "%d\n%d" (prem_dif (!prem,!sec)) (prem_dif (!sec,!tro));;

main ();;


Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactly k empty cells into walls so that all the remaining cells still formed a connected area. Help him.

The first line contains three integers n, m, k (1 ≤ n, m ≤ 500, 0 ≤ k < s), where n and m are the maze's height and width, correspondingly, k is the number of walls Pavel wants to add and letter s represents the number of empty cells in the original maze. Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.

Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#"). It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.

open Printf
open Scanf

module Tset = Set.Make (struct type t = int*(int*int) let compare = compare end)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let read_pair () = let u = read_int() in (u,read_int())
let () =
  let n = read_int () in
  let m = read_int () in
  let k = read_int () in
  let maze = Array.init n (fun _ -> read_string()) in

  let is_open i j = maze.(i).[j] = '.' in

  let parent = Array.make_matrix n m None in

  let count = Array.make_matrix n m 0 in

  let avail (i,j) = i>=0 && j>=0 && i<n && j<m && (is_open i j) && parent.(i).(j) = None in

  let rec bfs list = if list = [] then () else (
(*
    printf "list = ";
    List.iter (fun (i,j) -> printf "(%d,%d) " i j) list;
    print_newline();
*)
    let list' = List.fold_left (
      fun ac (i,j) ->
	let moves = List.filter avail [(i+1,j);(i-1,j);(i,j+1);(i,j-1)] in
	List.fold_left (
	  fun ac (i',j') ->
	    parent.(i').(j') <- Some (i,j);
	    count.(i).(j) <- count.(i).(j) + 1;
	    (i',j')::ac
	) ac moves
    ) [] list
    in
    bfs list'
  )
  in

  let next (i,j) = if j<m-1 then (i,j+1) else (i+1,0) in

  let rec scan (i,j) = if (i,j)=(n,0) then failwith "can't find empty" else
      if is_open i j then (i,j) else scan (next (i,j))
  in

  let (starti,startj) = scan (0,0) in
  parent.(starti).(startj) <- Some (-1,-1);
  bfs [(starti,startj)];

  let rec build_set ac (i,j) = if (i,j) = (n,0) then ac else
      if is_open i j then build_set (Tset.add (count.(i).(j),(i,j)) ac) (next (i,j))
      else build_set ac (next (i,j))
  in

  let queue = build_set Tset.empty (0,0) in

  let rec markX queue k = if k=0 then () else
      let (c,(i,j)) = Tset.min_elt queue in
      if c <> 0 then failwith "can't find a leaf" else
	let queue = Tset.remove (c,(i,j)) queue in
	maze.(i).[j] <- 'X';
	match parent.(i).(j) with None -> failwith "no parent"
	  | Some (i', j') ->
	    let c = count.(i').(j') in
	    count.(i').(j') <- count.(i').(j') - 1;
	    if c=0 then failwith "fail decrement parent count" else
	      let queue = Tset.add (c-1, (i',j')) (Tset.remove (c,(i',j')) queue) in
	      markX queue (k-1)
  in

  markX queue k;

  for i=0 to n-1 do
    printf "%s\n" maze.(i)
  done


Pupils decided to go to amusement park. Some of them were with parents. In total, n people came to the park and they all want to get to the most extreme attraction and roll on it exactly once.Tickets for group of x people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is c1 + c2·(x - 1)2 (in particular, if the group consists of one person, then the price is c1). All pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group.

The first line contains three integers n, c1 and c2 (1 ≤ n ≤ 200 000, 1 ≤ c1, c2 ≤ 107) — the number of visitors and parameters for determining the ticket prices for a group. The second line contains the string of length n, which consists of zeros and ones. If the i-th symbol of the string is zero, then the i-th visitor is a pupil, otherwise the i-th person is an adult. It is guaranteed that there is at least one adult. It is possible that there are no pupils.

Print the minimum price of visiting the most extreme attraction for all pupils and their parents. Each of them should roll on the attraction exactly once.

NoteIn the first test one group of three people should go to the attraction. Then they have to pay 4 + 1 * (3 - 1)2 = 8.In the second test it is better to go to the attraction in two groups. The first group should consist of two adults (for example, the first and the second person), the second should consist of one pupil and one adult (the third and the fourth person). Then each group will have a size of two and for each the price of ticket is 7 + 2 * (2 - 1)2 = 9. Thus, the total price for two groups is 18.

open Printf;;
open String;;

let ( +| ) a b = Int64.add a b;;
let ( -| ) a b = Int64.sub a b;;
let ( *| ) a b = Int64.mul a b;;
let ( /| ) a b = Int64.div a b;;

let inp = Str.split (Str.regexp " ") (read_line());;
let n = int_of_string (List.nth inp 0);;
let c1 = Int64.of_string (List.nth inp 1);;
let c2 = Int64.of_string (List.nth inp 2);;
let s = read_line();;

let rec count_one idx =
    if idx = n then 0
    else (if (String.get s idx) = '1' then 1 else 0) + (count_one (idx + 1));;

let adults = count_one 0;;

let calc_single x = c1 +| (c2 *| ((x -| 1L) *| (x -| 1L)));;

let calc_total x =
    let nn = Int64.of_int n in
    let r = Int64.rem nn x in
    (r *| calc_single((nn /| x) +| 1L)) +|
    ((x -| r) *| calc_single(nn /| x));;

let rec process i ans =
    if i > adults then printf "%Ld\n" ans else
    let newcost = calc_total (Int64.of_int i) in
    process (i + 1) (if ans > newcost then newcost else ans);;

let _ = process 1 Int64.max_int;;

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let ( %% ) a b = Int64.rem a b

let long x = Int64.of_int x
let short x = Int64.to_int x

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) + a) 0
let minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let sq x = x ** x

let read_int _ = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string _ = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let n = read_int () in
  let c1 = long (read_int ()) in
  let c2 = long (read_int ()) in
  let s = read_string() in
  let m = sum 0 (n-1) (fun i -> if s.[i] = '0' then 0 else 1) in (* number of adults *)

  let answer = minf 1 m (fun g ->  (* g = number of groups *)
    let x1 = n/g in
    let x2 = x1 + 1 in
    let n2 = n - (g * x1) in
    let n1 = g - n2 in
    let (g,n1,n2,x1,x2) = (long g, long n1, long n2, long x1, long x2) in
    c1 ** g ++ c2 ** (n1 ** (sq (x1--1L)) ++ n2 ** (sq (x2 -- 1L)))
  ) in

  printf "%Ld\n" answer

open Printf;;
open String;;

let inp = Str.split (Str.regexp " ") (read_line());;
let n = int_of_string (List.nth inp 0);;
let c1 = Int64.of_string (List.nth inp 1);;
let c2 = Int64.of_string (List.nth inp 2);;
let s = read_line();;

let rec can_group len idx adult =
    let slen = (String.length s) in
    if idx = slen then true
    else if (idx mod len) = 0 && (not adult) then false
    else can_group len (idx + 1) (((String.get s idx) = '1')
        || ((idx mod len) > 0 && adult));;

let rec process i ans =
    if i > n then printf "%Ld\n" ans else
    let newcost =
        if (n mod i) = 0 && (can_group i 0 true) then
            let scost = Int64.add c1 (Int64.mul c2 (Int64.mul (Int64.of_int (i - 1)) (Int64.of_int (i - 1)))) in
            Int64.mul scost (Int64.div (Int64.of_int n) (Int64.of_int i))
        else Int64.max_int in
    process (i + 1) (if ans > newcost then newcost else ans);;

let _ = process 1 Int64.max_int;;

open Printf;;
open String;;

let inp = Str.split (Str.regexp " ") (read_line());;
let n = int_of_string (List.nth inp 0);;
let c1 = Int64.of_string (List.nth inp 1);;
let c2 = Int64.of_string (List.nth inp 2);;
let s = read_line();;

let rec can_group len idx adult =
    let slen = (String.length s) in
    if idx = slen then true
    else if (idx mod len) = 0 && (not adult) then false
    else can_group len (idx + 1) (((String.get s idx) = '1')
        || ((idx mod len) > 0 && adult));;

let rec process i ans =
    if i > n then printf "%Ld\n" ans else
    let newcost =
        if (n mod i) = 0 && (can_group i 0 true) then
            let scost = Int64.add c1 (Int64.mul c2 (Int64.mul (Int64.of_int (i - 1)) (Int64.of_int (i - 1)))) in
            Int64.mul scost (Int64.div (Int64.of_int n) (Int64.of_int i))
        else 1000000000000000000L in
    process (i + 1) (if ans > newcost then newcost else ans);;

let _ = process 1 1000000000000000000L;;


Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But there is a problem! The correspondence turned out to be encrypted. The detective tried really hard to decipher the correspondence, but he couldn't understand anything. At last, after some thought, he thought of something. Let's say there is a word s, consisting of |s| lowercase Latin letters. Then for one operation you can choose a certain position p (1 ≤ p < |s|) and perform one of the following actions:   either replace letter sp with the one that alphabetically follows it and replace letter sp + 1 with the one that alphabetically precedes it;  or replace letter sp with the one that alphabetically precedes it and replace letter sp + 1 with the one that alphabetically follows it. Let us note that letter "z" doesn't have a defined following letter and letter "a" doesn't have a defined preceding letter. That's why the corresponding changes are not acceptable. If the operation requires performing at least one unacceptable change, then such operation cannot be performed.Two words coincide in their meaning iff one of them can be transformed into the other one as a result of zero or more operations.Sherlock Holmes needs to learn to quickly determine the following for each word: how many words can exist that coincide in their meaning with the given word, but differs from the given word in at least one character? Count this number for him modulo 1000000007 (109 + 7).

The input data contains several tests. The first line contains the only integer t (1 ≤ t ≤ 104) — the number of tests. Next t lines contain the words, one per line. Each word consists of lowercase Latin letters and has length from 1 to 100, inclusive. Lengths of words can differ.

For each word you should print the number of different other words that coincide with it in their meaning — not from the words listed in the input data, but from all possible words. As the sought number can be very large, print its value modulo 1000000007 (109 + 7).

NoteSome explanations about the operation:  Note that for each letter, we can clearly define the letter that follows it. Letter "b" alphabetically follows letter "a", letter "c" follows letter "b", ..., "z" follows letter "y".  Preceding letters are defined in the similar manner: letter "y" precedes letter "z", ..., "a" precedes letter "b".  Note that the operation never changes a word's length. In the first sample you can obtain the only other word "ba". In the second sample you cannot obtain any other word, so the correct answer is 0.Consider the third sample. One operation can transform word "klmbfxzb" into word "klmcexzb": we should choose p = 4, and replace the fourth letter with the following one ("b"  →  "c"), and the fifth one — with the preceding one ("f"  →  "e"). Also, we can obtain many other words from this one. An operation can transform word "ya" only into one other word "xb". Word "ya" coincides in its meaning with words "xb", "wc", "vd", ..., "ay" (overall there are 24 other words). The word "klmbfxzb has many more variants — there are 3320092814 other words that coincide with in the meaning. So the answer for the first word equals 24 and for the second one equals 320092793 — the number 3320092814 modulo 109 + 7

let _ =
  let mm = 1000000007 in
  let m = 1000000007l in
  let a = Array.make_matrix (100 + 1) (2500 + 1) 0 in
  let () =
    a.(0).(0) <- 1;
    for i = 1 to 100 do
      let cur = ref 0 in
	for j = 0 to 2500 do
	  cur := (Int32.to_int
		    (Int32.rem
		       (Int32.add
			  (Int32.of_int !cur)
			  (Int32.of_int a.(i - 1).(j)))
		       m));
	  if j - 26 >= 0 then (
	    cur := (Int32.to_int
		      (Int32.rem
			 (Int32.sub
			    (Int32.of_int !cur)
			    (Int32.of_int a.(i - 1).(j - 26)))
			 m));
	    if !cur < 0
	    then cur := !cur + mm;
	  );
	  a.(i).(j) <- !cur;
	done
    done;
  in
  let sb = Scanf.Scanning.stdib in
  let cases = Scanf.bscanf sb "%d " (fun s -> s) in
    for ca = 1 to cases do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
      let n = String.length s in
      let b = Array.init n (fun i -> Char.code s.[i] - Char.code 'a') in
      let sum = ref 0 in
      let () =
	for i = 0 to n - 1 do
	  sum := !sum + b.(i)
	done;
      in
      let sum = !sum in
	let res = a.(n).(sum) - 1 in
	let res = if res < 0 then res + mm else res in
	  Printf.printf "%d\n" res;
    done


Madoka is a very strange girl, and therefore she suddenly wondered how many pairs of integers $$$(a, b)$$$ exist, where $$$1 \leq a, b \leq n$$$, for which $$$\frac{\operatorname{lcm}(a, b)}{\operatorname{gcd}(a, b)} \leq 3$$$.In this problem, $$$\operatorname{gcd}(a, b)$$$ denotes the greatest common divisor of the numbers $$$a$$$ and $$$b$$$, and $$$\operatorname{lcm}(a, b)$$$ denotes the smallest common multiple of the numbers $$$a$$$ and $$$b$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of the test cases follows. The first and the only line of each test case contains the integer $$$n$$$ ($$$1 \le n \le 10^8$$$).

For each test case output a single integer — the number of pairs of integers satisfying the condition.

NoteFor $$$n = 1$$$ there is exactly one pair of numbers — $$$(1, 1)$$$ and it fits.For $$$n = 2$$$, there are only $$$4$$$ pairs — $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$ and they all fit.For $$$n = 3$$$, all $$$9$$$ pair are suitable, except $$$(2, 3)$$$ and $$$(3, 2)$$$, since their $$$\operatorname{lcm}$$$ is $$$6$$$, and $$$\operatorname{gcd}$$$ is $$$1$$$, which doesn't fit the condition.

(* This is an OCaml editor.
   Enter your program here and send it to the toplevel using the "Eval code"
   button or [Ctrl-e]. *)

let i = read_int() in
let rec loop x=
  if x=0 then () else
    (let j = read_int() in print_int (j+ (j/2)*2 + (j/3)*2);print_newline() ;  loop (x-1) )

in loop i

let rec loop (t : int) =
  if t = 0 then
    ()
  else
    let n = read_int () in
    let () = print_endline (string_of_int (n + 2*(n / 2) + 2*(n/3))) in
    loop (t-1)
;;

let t = read_int () in loop t;;

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let ( %% ) a b = Int64.rem a b

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) + a) 0
let rec forall i j f = (i>j) || ((f i) && forall (i+1) j f)

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_string _ = bscanf Scanning.stdin " %s " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))
let read_tup _ = bscanf Scanning.stdin " %d %d %d " (fun x y z -> (x,y,z))

let rec inp (n : int) =
    if (n == 0)
    then []
    else
        let i = read_int () in
        let j = read_int () in
        let x = read_int () in

        (i - 1, j - 1, x) :: inp (n - 1)

let rec solve (n : int) (m : int) arr =
    if (m == 0)
    then Array.make n 0
    else
        let prev = solve n (m / 2) arr in
        let z = Array.make n 0 in
        let res = Array.make n 0 in
        let adj = Array.make n [] in
        let rec process aa =
            match aa with
                [] -> ()
             |  (i, j, x) :: xs ->
                 let () = (
                 if ((x land m) == 0)
                 then
                    (* let () = printf "A %d %d %d %d \n" i j x m in *)
                    let () = Array.set z i 1 in
                    Array.set z j 1
                else
                    (* let () = printf "B %d %d %d %d \n" i j x m in *)
                    if (i == j)
                    then Array.set res i m
                    else
                    (
                    let oldi = Array.get adj i in
                    let () = Array.set adj i (j :: oldi) in
                    let oldj = Array.get adj j in
                    let () = Array.set adj j (i :: oldj) in
                    ()
                    )
                ) in
                process xs
        in
        let () = process arr in

        let () =
            for i = 0 to (n - 1) do
                let rec uu aa =
                    (
                    match aa with
                        [] -> ()
                     |  x :: xs ->
                        let () = (
                        if ((Array.get z x) == 1)
                        then Array.set res i m
                        else ()
                        ) in
                        uu xs
                    )
                in
                let rec vv aa =
                    (
                    match aa with
                        [] -> ()
                     |  x :: xs ->
                        let () = Array.set res x m in
                        (* let () = printf "%d %d %d \n" m i x in *)
                        vv xs
                    )
                in
                let () = uu (Array.get adj i) in
                (
                if ((Array.get res i) == 0)
                then
                    (* let () = Array.set z i 1 in *)
                    vv (Array.get adj i)
                else
                    ()
                )
            done
            in

        let () =
            for i = 0 to (n - 1) do
                Array.set res i ((Array.get res i) + (Array.get prev i))
            done
        in
        res

let () =
    let t = read_int () in

    for i = 1 to t do
        let n = read_int () in
        let a = n / 2 in
        let b = n / 3 in
    	printf "%d\n" (n + a + a + b + b)
    done


Famil Door’s City map looks like a tree (undirected connected acyclic graph) so other people call it Treeland. There are n intersections in the city connected by n - 1 bidirectional roads.There are m friends of Famil Door living in the city. The i-th friend lives at the intersection ui and works at the intersection vi. Everyone in the city is unhappy because there is exactly one simple path between their home and work.Famil Door plans to construct exactly one new road and he will randomly choose one among n·(n - 1) / 2 possibilities. Note, that he may even build a new road between two cities that are already connected by one.He knows, that each of his friends will become happy, if after Famil Door constructs a new road there is a path from this friend home to work and back that doesn't visit the same road twice. Formally, there is a simple cycle containing both ui and vi. Moreover, if the friend becomes happy, his pleasure is equal to the length of such path (it's easy to see that it's unique). For each of his friends Famil Door wants to know his expected pleasure, that is the expected length of the cycle containing both ui and vi if we consider only cases when such a cycle exists.

The first line of the input contains integers n and m (2 ≤ n,  m ≤ 100 000) — the number of the intersections in the Treeland and the number of Famil Door's friends. Then follow n - 1 lines describing bidirectional roads. Each of them contains two integers ai and bi (1 ≤ ai, bi ≤ n) — the indices of intersections connected by the i-th road. Last m lines of the input describe Famil Door's friends. The i-th of these lines contain two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — indices of intersections where the i-th friend lives and works.

For each friend you should print the expected value of pleasure if he will be happy. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

NoteConsider the second sample.   Both roads (1, 2) and (2, 3) work, so the expected length if   Roads (1, 3) and (2, 3) make the second friend happy. Same as for friend 1 the answer is 2.5  The only way to make the third friend happy is to add road (2, 3), so the answer is 3

(* [@@@warning "-30"] *)

type vertex = {
  mutable adjacent : vertex array;
  mutable parent   : vertex;
  mutable depth    : int;
  mutable t_entr   : int;
  mutable t_exit   : int;
  mutable sigma_d  : int64;
  mutable sigma'_d : int64;
  mutable count    : int64;
          uf       : uf;
  mutable ancestor : vertex;
  mutable colored  : bool;
}
and uf = {
  mutable parent   : vertex;
  mutable rank     : int;
}

module H = Hashtbl.Make(struct type t = vertex let equal = (==) let hash v = v.t_entr end)

let none = -1

let dummy () =
  let rec v = {
    adjacent = [| |];
    parent   = v;
    depth    = none;
    t_entr   = none;
    t_exit   = none;
    sigma_d  = 0L;
    sigma'_d = 0L;
    count    = 0L;
    uf       = { parent = v; rank = 0 };
    ancestor = v;
    colored  = false
  }
  in
  v

let (+~), (-~), zero = Int64.(add, sub, zero)

let dfs1 root =
  let rec dfs d t p curr =
    curr.t_entr <- t;
    curr.depth <- d;
    curr.parent <- p;
    curr.count <- curr.count +~ 1L;
    let t =
      Array.fold_left
        (fun t v ->
           if v != p then begin
             let t = dfs (d + 1) (t + 1) curr v in
             curr.sigma_d <- curr.sigma_d +~ v.sigma_d +~ v.count;
             curr.count   <- curr.count +~ v.count;
             t
           end else t)
        t
        curr.adjacent
    in
    curr.t_exit <- t;
    t
  in
  ignore @@ dfs 0 0 root root

let dfs2 root =
  let rec dfs curr =
    let p = curr.parent in
    curr.sigma'_d <-
      if p != curr then
        (if p.parent != p then p.sigma'_d +~ root.count -~ p.count else 0L) +~
        p.sigma_d -~ curr.sigma_d -~ curr.count
      else
        0L;
    Array.iter (fun v -> if v != p then dfs v) curr.adjacent
  in
  dfs root

let tarjan_olca queries =
  let init v =
     v.uf.parent <- v;
     v.uf.rank   <- 0
  in
  let rec find v =
    if v.uf.parent == v then
      v
    else
      let p = find v.uf.parent in
      v.uf.parent <- p;
      p
  in
  let union u v =
    let u, v = find u, find v in
    if u.uf.rank > v.uf.rank then
      v.uf.parent <- u
    else if u.uf.rank < v.uf.rank then
      u.uf.parent <- v
    else if u != v then begin
      v.uf.parent <- u;
      u.uf.rank <- u.uf.rank + 1
    end
  in
  let rec tarjan u =
     init u;
     u.ancestor <- u;
     Array.iter
       (fun v ->
          if v != u.parent then begin
            tarjan v;
            union u v;
            (find u).ancestor <- u
          end)
       u.adjacent;
     u.colored <- true;
     try
       H.iter
         (fun v a ->
            if v.colored then
              let r = (find v).ancestor in
              a := r;
              H.find (H.find queries v) u := r)
         (H.find queries u)
     with
     | Not_found -> ()
  in
  tarjan

type data = { low : vertex; sub : vertex }

type path_kind = I of data | V

let search queries u v =
  let search u v =
    let rec search l r =
      if l = r then u.adjacent.(l)
      else
        let m = l + (r - l) / 2 in
        let m = if u.adjacent.(m) == u.parent then m + 1 else m in
        if      u.adjacent.(m).t_entr > v.t_entr then search l (m - 1)
        else if u.adjacent.(m).t_exit < v.t_exit then search (m + 1) r
        else u.adjacent.(m)
    in
    search 0 (Array.length u.adjacent - 1)
  in
  let kind =
    if      u.t_entr <= v.t_entr && u.t_exit >= v.t_exit then I { low = v; sub = search u v }
    else if v.t_entr <= u.t_entr && v.t_exit >= u.t_exit then I { low = u; sub = search v u }
    else V
  in
  kind, u.depth + v.depth - 2 * (!H.(find (find queries u) v)).depth

let solve queries root u v =
  let fl, (+), (/) = Int64.to_float, (+.), (/.) in
  match search queries u v with
  | V, n ->
    float_of_int n + 1. + fl u.sigma_d / fl u.count + fl v.sigma_d / fl v.count
  | I { low; sub } , n ->
    float_of_int n + 1. + fl low.sigma_d / fl low.count + fl sub.sigma'_d / fl (root.count -~ sub.count)

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let vertices = Array.init n (fun _ -> dummy ()) in
  let root = vertices.(0) in
  let edges = Array.make n [] in
  let add a i e = a.(i) <- e :: a.(i) in
  for _ = 1 to n - 1 do
    scanf "%d %d\n" @@ fun a b ->
    add edges (a - 1) vertices.(b - 1);
    add edges (b - 1) vertices.(a - 1)
  done;
  Array.iteri (fun i v -> v.adjacent <- Array.of_list edges.(i)) vertices;
  dfs1 root;
  dfs2 root;
  let queries, queries_list = H.create 32, ref [] in
  let add_query u v =
    H.replace
      (try H.find queries u with Not_found -> let h = H.create 16 in H.replace queries u h; h)
      v
      (ref root)
  in
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun u v ->
    let u, v = vertices.(u - 1), vertices.(v - 1) in
    add_query u v ;
    add_query v u;
    queries_list := (u, v) :: !queries_list
  done;
  tarjan_olca queries vertices.(0);
  List.(iter (fun (u, v) -> Printf.printf "%.7f\n" @@ solve queries root u v) @@ rev !queries_list)

module H = Hashtbl.Make(struct type t = int let equal = ((=) : int -> int -> _) let hash x = x end)

type tree = {
  verts    : int array;
  edges    : int array array;
  parent   : int array;
  depth    : int array;
  t_entr   : int array;
  t_exit   : int array;
  sigma_d  : int64 array;
  sigma'_d : int64 array;
  count    : int64 array;

  uf_parent : int array;
  uf_rank   : int array;
  ancestor  : int array;
  colored   : bool array;
  queries   : int ref H.t H.t;
}

let (+~), (-~), zero, none = Int64.(add, sub, zero, -1)

let incr a i n = a.(i) <- a.(i) +~ n

let dfs1 { verts; edges; parent; depth; t_entr; t_exit; sigma_d; count } =
  let rec dfs d t p curr =
    verts.(t) <- curr;
    t_entr.(curr) <- t;
    depth.(curr) <- d;
    parent.(curr) <- p;
    incr count curr 1L;
    let t =
      Array.fold_left
        (fun t v ->
           if v <> p then begin
             let t = dfs (d + 1) (t + 1) curr v in
             incr sigma_d curr @@ sigma_d.(v) +~ count.(v);
             incr count curr @@ count.(v);
             t
           end else t)
        t
        edges.(curr)
    in
    t_exit.(curr) <- t;
    t
  in
  ignore @@ dfs 0 0 none 0

let dfs2 { edges; parent; sigma'_d; sigma_d; count } =
  let rec dfs curr =
    let p = parent.(curr) in
    sigma'_d.(curr) <-
      if p <> none then
        (if parent.(p) <> none then sigma'_d.(p) +~ count.(0) -~ count.(p) else 0L) +~
        sigma_d.(p) -~ sigma_d.(curr) -~ count.(curr)
      else
        0L;
    Array.iter (fun v -> if v <> p then dfs v) edges.(curr)
  in
  dfs 0

let tarjan_olca { edges; parent; uf_parent; uf_rank; ancestor; colored; queries } =
  let init v =
     uf_parent.(v) <- v;
     uf_rank.(v)   <- 0
  in
  let rec find v =
    if uf_parent.(v) == v then
      v
    else
      let p = find uf_parent.(v) in
      uf_parent.(v) <- p;
      p
  in
  let union u v =
    let u, v = find u, find v in
    if uf_rank.(u) > uf_rank.(v) then
      uf_parent.(v) <- u
    else if uf_rank.(u) < uf_rank.(v) then
      uf_parent.(u) <- v
    else if u <> v then begin
      uf_parent.(v) <- u;
      uf_rank.(u) <- uf_rank.(u) + 1
    end
  in
  let rec tarjan u =
     init u;
     ancestor.(u) <- u;
     Array.iter
       (fun v ->
          if v <> parent.(u) then begin
            tarjan v;
            union u v;
            ancestor.(find u) <- u
          end)
       edges.(u);
     colored.(u) <- true;
     try
       H.iter
         (fun v a ->
            if colored.(v) then
              let r = ancestor.(find v) in
              a := r;
              H.find (H.find queries v) u := r)
         (H.find queries u)
     with
     | Not_found -> ()
  in
  tarjan 0

type data = { low : int; sub : int }

type path_kind = I of data | V

let search { verts; edges; parent; t_entr; t_exit; depth; queries } u v =
  let search u v =
    let rec search l r =
      if l = r then edges.(u).(l)
      else
        let m = l + (r - l) / 2 in
        let m = if edges.(u).(m) = parent.(u) then m + 1 else m in
        if      t_entr.(edges.(u).(m)) > t_entr.(v) then search l (m - 1)
        else if t_exit.(edges.(u).(m)) < t_exit.(v) then search (m + 1) r
        else edges.(u).(m)
    in
    search 0 (Array.length edges.(u) - 1)
  in
  let kind =
    if      t_entr.(u) <= t_entr.(v) && t_exit.(u) >= t_exit.(v) then I { low = v; sub = search u v }
    else if t_entr.(v) <= t_entr.(u) && t_exit.(v) >= t_exit.(u) then I { low = u; sub = search v u }
    else V
  in
  kind, depth.(u) + depth.(v) - 2 * depth.(!H.(find (find queries u) v))

let solve ({ sigma_d; sigma'_d; count } as t) u v =
  let fl, (+), (/) = Int64.to_float, (+.), (/.) in
  match search t u v with
  | V, n ->
    float_of_int n + 1. + fl sigma_d.(u) / fl count.(u) + fl sigma_d.(v) / fl count.(v)
  | I { low; sub } , n ->
    float_of_int n + 1. + fl sigma_d.(low) / fl count.(low) + fl sigma'_d.(sub) / fl (count.(0) -~ count.(sub))

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let edges = Array.make n [] in
  let add a i e = a.(i) <- e :: a.(i) in
  for _ = 1 to n - 1 do
    scanf "%d %d\n" @@ fun a b ->
    add edges (a - 1) (b - 1);
    add edges (b - 1) (a - 1)
  done;
  let t =
    Array.{
      verts    = make n none;
      edges    = init n (fun i -> Array.of_list edges.(i));
      parent   = make n none;
      depth    = make n none;
      t_entr   = make n none;
      t_exit   = make n none;
      sigma_d  = make n zero;
      sigma'_d = make n zero;
      count    = make n zero;

      uf_parent = make n none;
      uf_rank   = make n none;
      ancestor  = make n none;
      colored   = make n false;
      queries   = H.create 32;
    }
  in
  dfs1 t;
  dfs2 t;
  let queries = ref [] in
  let add_query u v =
    H.replace
      (try H.find t.queries u with Not_found -> let h = H.create 16 in H.replace t.queries u h; h)
      v
      (ref none)
  in
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun u v ->
    add_query (u - 1) (v - 1);
    add_query (v - 1) (u - 1);
    queries := (u - 1, v - 1) :: !queries
  done;
  tarjan_olca t;
  List.(iter (fun (u, v) -> Printf.printf "%.7f\n" @@ solve t u v) @@ rev !queries)

  (* keywords: dynamic programming, tree DP, union find, tarjan LCA *)
  (* see also ~/Sync/contest-work/NCPC-2014/particle/ *)

let order (a,b) = (min a b, max a b)

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let long x = Int64.of_int x
let float = Int64.to_float

open Printf
open Scanf

let read_pair () = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let (n,m) = read_pair () in

  let tree = Array.make n [] in

  for i=1 to n-1 do
    let (a,b) = read_pair () in
    let (a,b) = (a-1,b-1) in
    tree.(a) <- b::tree.(a);
    tree.(b) <- a::tree.(b);
  done;

  let qgraph = Array.make n [] in
  let query = Array.make m (0,0) in

  for i=0 to m-1 do
    let (a,b) = read_pair () in
    let (a,b) = (a-1,b-1) in
    query.(i) <- (a,b);
    qgraph.(a) <- b::qgraph.(a);
    qgraph.(b) <- a::qgraph.(b);
  done;

  (* ------------------ union find ------------------- *)
  let uf = Array.make n (-1) in

  let rec find i = if uf.(i) < 0 then i else
      let r = find uf.(i) in
      uf.(i) <- r;
      r
  in

  let union i j =
    (* assume i and j are both canonical elements *)
    (* true union by rank (not size) *)
    let (wi,wj) = (-uf.(i), -uf.(j)) in
    let (wi,wj,i,j) = if wi < wj then (wi,wj,i,j) else (wj,wi,j,i) in
    uf.(j) <- - ((if wi=wj then 1 else 0) + wj);
    uf.(i) <- j;
  in
  (* ---------------- end union find ------------------ *)

  (* pure version
  (* ------------------ off-line LCA ------------------- *)
  let lca = Hashtbl.create 100 in
  let mark = Array.make n false in
  let ancestor = Array.make n 0 in

  let rec tarjan p u =
    List.iter (fun v -> if v <> p then (
      tarjan u v;
      union (find u) (find v);
      ancestor.(find u) <- u
    )) tree.(u);

    mark.(u) <- true;

    List.iter (fun v ->
      if mark.(v) then Hashtbl.replace lca (order (u,v)) ancestor.(find v)
    ) qgraph.(u)
  in
  tarjan (-1) 0;

  (* Hashtbl.iter (fun (u,v) a -> printf "LCA(%d,%d) = %d\n" (u+1) (v+1) (a+1)) lca;  *)
  (* ---------------- end off-line LCA ------------------ *)   *)

  (* ------------------ customized off-line LCA ------------------- *)
  let nbr = Hashtbl.create 100 in
  let dist = Hashtbl.create 100 in
  let mark = Array.make n false in
  let on_stack = Array.make n false in
  let ancestor = Array.make n 0 in
  let current_child = Array.make n 0 in
  let parent = Array.make n 0 in
  let depth = Array.make n 0 in

  let rec tarjan p u d =
    parent.(u) <- p;
    depth.(u) <- d;
    on_stack.(u) <- true;
    List.iter (fun v -> if v <> p then (
      current_child.(u) <- v;
      tarjan u v (d+1);
      union (find u) (find v);
      ancestor.(find u) <- u
    )) tree.(u);

    on_stack.(u) <- false;
    mark.(u) <- true;

    List.iter (fun v ->
      if mark.(v) then (
	let lca = ancestor.(find v) in
	let distance = depth.(v) + depth.(u) - 2*depth.(lca) in
	Hashtbl.add dist (order (u,v)) distance;
	if u <> lca then Hashtbl.add nbr (u,v) p;
	if v <> lca then Hashtbl.add nbr (v,u) parent.(v)
      ) else (
	if on_stack.(v) then Hashtbl.add nbr (v,u) current_child.(v)
      )
    ) qgraph.(u)
  in
  tarjan (-1) 0 0;
  (* --------------------------------------------------------------- *)

  (* now count paths *)

  let info = Hashtbl.create 100 in

  let rec dfs p u =
    try Hashtbl.find info (p,u) with Not_found ->
      let answer =
	List.fold_left (fun (ac_len, ac_n) v ->
	  if v = p then (ac_len, ac_n) else
	    let (pathlen, nnodes) = dfs u v in
	    (ac_len ++ pathlen ++ nnodes, ac_n ++ nnodes)
	) (0L, 1L) tree.(u)
      in
      Hashtbl.add info (p,u) answer;
      answer
  in

  for i=0 to m-1 do
    let (u,v) = query.(i) in
    let nu = Hashtbl.find nbr (u,v) in
    let nv = Hashtbl.find nbr (v,u) in
    let (len_u, n_u) = dfs nu u in
    let (len_v, n_v) = dfs nv v in
    let ave_len_u = (float len_u) /. (float n_u) in
    let ave_len_v = (float len_v) /. (float n_v) in
    let path_dist = float (long (Hashtbl.find dist (order (u,v)))) in
    printf "%.8f\n" (1.0 +. ave_len_u +. ave_len_v +. path_dist)
  done;

type tree = {
  verts    : int array;
  edges    : int array array;
  parent   : int array;
  depth    : int array;
  t_entr   : int array;
  t_exit   : int array;
  sigma_d  : int64 array;
  sigma'_d : int64 array;
  count    : int64 array
}

let (+~), (-~), zero, none = Int64.(add, sub, zero, -1)

let incr a i n = a.(i) <- a.(i) +~ n

let dfs1 { verts; edges; parent; depth; t_entr; t_exit; sigma_d; count } =
  let rec dfs d t p curr =
    verts.(t) <- curr;
    t_entr.(curr) <- t;
    depth.(curr) <- d;
    parent.(curr) <- p;
    incr count curr 1L;
    let t =
      Array.fold_left
        (fun t v ->
           if v <> p then begin
             let t = dfs (d + 1) (t + 1) curr v in
             incr sigma_d curr @@ sigma_d.(v) +~ count.(v);
             incr count curr @@ count.(v);
             t
           end else t)
        t
        edges.(curr)
    in
    t_exit.(curr) <- t;
    t
  in
  ignore @@ dfs 0 0 none 0

let dfs2 { edges; parent; sigma'_d; sigma_d; count } =
  let rec dfs curr =
    let p = parent.(curr) in
    sigma'_d.(curr) <-
      if p <> none then
        (if parent.(p) <> none then sigma'_d.(p) +~ count.(0) -~ count.(p) else 0L) +~
        sigma_d.(p) -~ sigma_d.(curr) -~ count.(curr)
      else
        0L;
    Array.iter (fun v -> if v <> p then dfs v) edges.(curr)
  in
  dfs 0

type data = { low : int; sub : int }

type path_kind = I of data | V

let search { verts; edges; t_entr; t_exit; depth } u v =
  let search u v =
    let rec search l r =
      if l = r then edges.(u).(l)
      else
        let m = l + (r - l) / 2 in
        if      t_entr.(edges.(u).(m)) > t_entr.(v) then search l (m - 1)
        else if t_exit.(edges.(u).(m)) < t_exit.(u) then search (m + 1) r
        else edges.(u).(m)
    in
    search 0 (Array.length edges.(u) - 1)
  in
  let kind =
    if      t_entr.(u) <= t_entr.(v) && t_exit.(u) >= t_exit.(v) then I { low = v; sub = search u v }
    else if t_entr.(v) <= t_entr.(u) && t_exit.(v) >= t_exit.(u) then I { low = u; sub = search v u }
    else V
  in
  let rec search l r =
    if l = r then verts.(l)
    else
      let m = l + (r - l + 1) / 2 in
      (*Printf.eprintf "m=%d(%d) t_entr.(m)=%d t_exit.(m)=%d t_entr.(u)=%d t_entr.(v)=%d\n"
        m verts.(m) t_entr.(verts.(m)) t_exit.(verts.(m)) t_entr.(u) t_entr.(v);*)
      if t_entr.(verts.(m)) > min t_entr.(u) t_entr.(v) ||
         t_exit.(verts.(m)) < max t_exit.(u) t_exit.(v)
      then search l (m - 1)
      else search m r
  in
  (*Printf.eprintf "For u=%d v=%d search gave %d\n" u v (search 0 (Array.length verts - 1));*)
  kind, depth.(u) + depth.(v) - 2 * depth.(search 0 (Array.length verts - 1))

let solve ({ sigma_d; sigma'_d; count } as t) u v =
  let fl, (+), (/) = Int64.to_float, (+.), (/.) in
  match search t u v with
  | V, n ->
    float_of_int n + 1. + fl sigma_d.(u) / fl count.(u) + fl sigma_d.(v) / fl count.(v)
  | I { low; sub } , n ->
    (*Printf.eprintf "u=%d v=%d n=%d s=%d l=%d fp=%f num=%f den=%f\n"
      u v n sub low
      (float_of_int n + 1. + fl sigma_d.(low) / fl count.(low))
      (fl sigma'_d.(sub))
      (fl (count.(0) -~ count.(sub)));*)
    float_of_int n + 1. + fl sigma_d.(low) / fl count.(low) + fl sigma'_d.(sub) / fl (count.(0) -~ count.(sub))

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let edges = Array.make n [] in
  let add a i e = a.(i) <- e :: a.(i) in
  for _ = 1 to n - 1 do
    scanf "%d %d\n" @@ fun a b ->
    add edges (a - 1) (b - 1);
    add edges (b - 1) (a - 1)
  done;
  let t =
    Array.{
      verts    = make n none;
      edges    = init n (fun i -> Array.of_list edges.(i));
      parent   = make n none;
      depth    = make n none;
      t_entr   = make n none;
      t_exit   = make n none;
      sigma_d  = make n zero;
      sigma'_d = make n zero;
      count    = make n zero
    }
  in
  dfs1 t;
  dfs2 t;
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun u v -> Printf.printf "%.7f\n" @@ solve t (u - 1) (v - 1)
  done

type tree = {
  verts    : int array;
  edges    : int array array;
  parent   : int array;
  depth    : int array;
  t_entr   : int array;
  t_exit   : int array;
  sigma_d  : int64 array;
  sigma'_d : int64 array;
  count    : int64 array
}

let (+~), (-~), zero, none = Int64.(add, sub, zero, -1)

let incr a i n = a.(i) <- a.(i) +~ n

let dfs1 { verts; edges; parent; depth; t_entr; t_exit; sigma_d; count } =
  let rec dfs d t p curr =
    verts.(t) <- curr;
    t_entr.(curr) <- t;
    depth.(curr) <- d;
    parent.(curr) <- p;
    incr count curr 1L;
    let t =
      Array.fold_left
        (fun t v ->
           if v <> p then begin
             let t = dfs (d + 1) (t + 1) curr v in
             incr sigma_d curr @@ sigma_d.(v) +~ count.(v);
             incr count curr @@ count.(v);
             t
           end else t)
        t
        edges.(curr)
    in
    t_exit.(curr) <- t;
    t
  in
  ignore @@ dfs 0 0 none 0

let dfs2 { edges; parent; sigma'_d; sigma_d; count } =
  let rec dfs curr =
    let p = parent.(curr) in
    sigma'_d.(curr) <-
      if p <> none then
        (if parent.(p) <> none then sigma'_d.(p) +~ count.(0) -~ count.(p) else 0L) +~
        sigma_d.(p) -~ sigma_d.(curr) -~ count.(curr)
      else
        0L;
    Array.iter (fun v -> if v <> p then dfs v) edges.(curr)
  in
  dfs 0

type data = { low : int; sub : int }

type path_kind = I of data | V

let search { verts; edges; parent; t_entr; t_exit; depth } u v =
  let search u v =
    let rec search l r =
      if l = r then edges.(u).(l)
      else
        let m = l + (r - l) / 2 in
        let m = if edges.(u).(m) = parent.(u) then m + 1 else m in
        if      t_entr.(edges.(u).(m)) > t_entr.(v) then search l (m - 1)
        else if t_exit.(edges.(u).(m)) < t_exit.(v) then search (m + 1) r
        else edges.(u).(m)
    in
    search 0 (Array.length edges.(u) - 1)
  in
  let kind =
    if      t_entr.(u) <= t_entr.(v) && t_exit.(u) >= t_exit.(v) then I { low = v; sub = search u v }
    else if t_entr.(v) <= t_entr.(u) && t_exit.(v) >= t_exit.(u) then I { low = u; sub = search v u }
    else V
  in
  let rec search best l r =
    (*Printf.eprintf "best=%d l=%d r=%d\n" best l r;*)
    if l > r then verts.(best)
    else
      let m = l + (r - l + 1) / 2 in
      (*Printf.eprintf "m=%d(%d) t_entr.(m)=%d t_exit.(m)=%d t_entr.(u)=%d t_entr.(v)=%d\n"
        m verts.(m) t_entr.(verts.(m)) t_exit.(verts.(m)) t_entr.(u) t_entr.(v);*)
      if      t_entr.(verts.(m)) > min t_entr.(u) t_entr.(v) then search best l (m - 1)
      else if t_exit.(verts.(m)) < max t_exit.(u) t_exit.(v) then search best (m + 1) r
      else if m = r then verts.(m)
      else search m m r
  in
  (*Printf.eprintf "For u=%d v=%d search gave %d\n" u v (search 0 0 (Array.length verts - 1));*)
  kind, depth.(u) + depth.(v) - 2 * depth.(search 0 0 (Array.length verts - 1))

let solve ({ sigma_d; sigma'_d; count } as t) u v =
  let fl, (+), (/) = Int64.to_float, (+.), (/.) in
  match search t u v with
  | V, n ->
    float_of_int n + 1. + fl sigma_d.(u) / fl count.(u) + fl sigma_d.(v) / fl count.(v)
  | I { low; sub } , n ->
    (*Printf.eprintf "sub=%d" sub;*)
    (*Printf.eprintf "u=%d v=%d n=%d s=%d l=%d fp=%f num=%f den=%f\n"
      u v n sub low
      (float_of_int n + 1. + fl sigma_d.(low) / fl count.(low))
      (fl sigma'_d.(sub))
      (fl (count.(0) -~ count.(sub)));*)
    float_of_int n + 1. + fl sigma_d.(low) / fl count.(low) + fl sigma'_d.(sub) / fl (count.(0) -~ count.(sub))

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let edges = Array.make n [] in
  let add a i e = a.(i) <- e :: a.(i) in
  for _ = 1 to n - 1 do
    scanf "%d %d\n" @@ fun a b ->
    add edges (a - 1) (b - 1);
    add edges (b - 1) (a - 1)
  done;
  let t =
    Array.{
      verts    = make n none;
      edges    = init n (fun i -> Array.of_list edges.(i));
      parent   = make n none;
      depth    = make n none;
      t_entr   = make n none;
      t_exit   = make n none;
      sigma_d  = make n zero;
      sigma'_d = make n zero;
      count    = make n zero
    }
  in
  dfs1 t;
  dfs2 t;
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun u v -> Printf.printf "%.7f\n" @@ solve t (u - 1) (v - 1)
  done

type tree = {
  verts    : int array;
  edges    : int array array;
  parent   : int array;
  depth    : int array;
  t_entr   : int array;
  t_exit   : int array;
  sigma_d  : int64 array;
  sigma'_d : int64 array;
  count    : int64 array
}

let (+~), (-~), zero, none = Int64.(add, sub, zero, -1)

let incr a i n = a.(i) <- a.(i) +~ n

let dfs1 { verts; edges; parent; depth; t_entr; t_exit; sigma_d; count } =
  let rec dfs d t p curr =
    verts.(t) <- curr;
    t_entr.(curr) <- t;
    depth.(curr) <- d;
    parent.(curr) <- p;
    incr count curr 1L;
    let t =
      Array.fold_left
        (fun t v ->
           if v <> p then begin
             let t = dfs (d + 1) (t + 1) curr v in
             incr sigma_d curr @@ sigma_d.(v) +~ count.(v);
             incr count curr @@ count.(v);
             t
           end else t)
        t
        edges.(curr)
    in
    t_exit.(curr) <- t;
    t
  in
  ignore @@ dfs 0 0 none 0

let dfs2 { edges; parent; sigma'_d; sigma_d; count } =
  let rec dfs curr =
    let p = parent.(curr) in
    sigma'_d.(curr) <-
      if p <> none then
        (if parent.(p) <> none then sigma'_d.(p) +~ count.(0) -~ count.(p) else 0L) +~
        sigma_d.(p) -~ sigma_d.(curr) -~ count.(curr)
      else
        0L;
    Array.iter (fun v -> if v <> p then dfs v) edges.(curr)
  in
  dfs 0

type data = { low : int; sub : int }

type path_kind = I of data | V

let search { verts; edges; t_entr; t_exit; depth } u v =
  let search u v =
    let rec search l r =
      if l = r then edges.(u).(l)
      else
        let m = l + (r - l) / 2 in
        if      t_entr.(edges.(u).(m)) > t_entr.(v) then search l (m - 1)
        else if t_exit.(edges.(u).(m)) < t_exit.(u) then search (m + 1) r
        else edges.(u).(m)
    in
    search 0 (Array.length edges.(u) - 1)
  in
  let kind =
    if      t_entr.(u) <= t_entr.(v) && t_exit.(u) >= t_exit.(v) then I { low = v; sub = search u v }
    else if t_entr.(v) <= t_entr.(u) && t_exit.(v) >= t_exit.(u) then I { low = u; sub = search v u }
    else V
  in
  let rec search best l r =
    (*Printf.eprintf "best=%d l=%d r=%d\n" best l r;*)
    if l > r then verts.(best)
    else
      let m = l + (r - l + 1) / 2 in
      (*Printf.eprintf "m=%d(%d) t_entr.(m)=%d t_exit.(m)=%d t_entr.(u)=%d t_entr.(v)=%d\n"
        m verts.(m) t_entr.(verts.(m)) t_exit.(verts.(m)) t_entr.(u) t_entr.(v);*)
      if      t_entr.(verts.(m)) > min t_entr.(u) t_entr.(v) then search best l (m - 1)
      else if t_exit.(verts.(m)) < max t_exit.(u) t_exit.(v) then search best (m + 1) r
      else if m = r then verts.(m)
      else search m m r
  in
  (*Printf.eprintf "For u=%d v=%d search gave %d\n" u v (search 0 0 (Array.length verts - 1));*)
  kind, depth.(u) + depth.(v) - 2 * depth.(search 0 0 (Array.length verts - 1))

let solve ({ sigma_d; sigma'_d; count } as t) u v =
  let fl, (+), (/) = Int64.to_float, (+.), (/.) in
  match search t u v with
  | V, n ->
    float_of_int n + 1. + fl sigma_d.(u) / fl count.(u) + fl sigma_d.(v) / fl count.(v)
  | I { low; sub } , n ->
    (*Printf.eprintf "u=%d v=%d n=%d s=%d l=%d fp=%f num=%f den=%f\n"
      u v n sub low
      (float_of_int n + 1. + fl sigma_d.(low) / fl count.(low))
      (fl sigma'_d.(sub))
      (fl (count.(0) -~ count.(sub)));*)
    float_of_int n + 1. + fl sigma_d.(low) / fl count.(low) + fl sigma'_d.(sub) / fl (count.(0) -~ count.(sub))

let () =
  let open Scanf in
  scanf "%d %d\n" @@ fun n m ->
  let edges = Array.make n [] in
  let add a i e = a.(i) <- e :: a.(i) in
  for _ = 1 to n - 1 do
    scanf "%d %d\n" @@ fun a b ->
    add edges (a - 1) (b - 1);
    add edges (b - 1) (a - 1)
  done;
  let t =
    Array.{
      verts    = make n none;
      edges    = init n (fun i -> Array.of_list edges.(i));
      parent   = make n none;
      depth    = make n none;
      t_entr   = make n none;
      t_exit   = make n none;
      sigma_d  = make n zero;
      sigma'_d = make n zero;
      count    = make n zero
    }
  in
  dfs1 t;
  dfs2 t;
  for _ = 1 to m do
    scanf "%d %d\n" @@ fun u v -> Printf.printf "%.7f\n" @@ solve t (u - 1) (v - 1)
  done


You have array a that contains all integers from 1 to n twice. You can arbitrary permute any numbers in a.Let number i be in positions xi, yi (xi < yi) in the permuted array a. Let's define the value di = yi - xi — the distance between the positions of the number i. Permute the numbers in array a to minimize the value of the sum .

The only line contains integer n (1 ≤ n ≤ 5·105).

Print 2n integers — the permuted array a that minimizes the value of the sum s.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make (2 * n) n in
    for i = 1 to n - 1 do
      let d = n - i in
      let x =
	if n mod 2 = 0 then (
	  if d mod 2 = 0
	  then i / 2 - 1 + n
	  else i / 2
	) else (
	  if d mod 2 = 0
	  then i / 2
	  else i / 2 - 1 + n
	)
      in
      let y = x + d in
	a.(x) <- i;
	a.(y) <- i;
    done;
    for i = 0 to 2 * n - 1 do
      Printf.printf "%d " a.(i)
    done;
    Printf.printf "\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
    for i = 1 to n do
      Printf.printf "%d %d " i i
    done;
    Printf.printf "\n"


A sequence of $$$n$$$ non-negative integers ($$$n \ge 2$$$) $$$a_1, a_2, \dots, a_n$$$ is called good if for all $$$i$$$ from $$$1$$$ to $$$n-1$$$ the following condition holds true: $$$$$$a_1 \: \& \: a_2 \: \& \: \dots \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: \dots \: \& \: a_n,$$$$$$ where $$$\&$$$ denotes the bitwise AND operation.You are given an array $$$a$$$ of size $$$n$$$ ($$$n \geq 2$$$). Find the number of permutations $$$p$$$ of numbers ranging from $$$1$$$ to $$$n$$$, for which the sequence $$$a_{p_1}$$$, $$$a_{p_2}$$$, ... ,$$$a_{p_n}$$$ is good. Since this number can be large, output it modulo $$$10^9+7$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$), denoting the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output $$$t$$$ lines, where the $$$i$$$-th line contains the number of good permutations in the $$$i$$$-th test case modulo $$$10^9 + 7$$$.

NoteIn the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of $$$6$$$ permutations possible with numbers from $$$1$$$ to $$$3$$$: $$$[1,2,3]$$$, $$$[1,3,2]$$$, $$$[2,1,3]$$$, $$$[2,3,1]$$$, $$$[3,1,2]$$$, $$$[3,2,1]$$$.In the second test case, it can be proved that no permutation exists for which the sequence is good.In the third test case, there are a total of $$$36$$$ permutations for which the sequence is good. One of them is the permutation $$$[1,5,4,2,3]$$$ which results in the sequence $$$s=[0,0,3,2,0]$$$. This is a good sequence because   $$$ s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0$$$,  $$$ s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0$$$,  $$$ s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0$$$,  $$$ s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0$$$.

open Printf
open Scanf

let rec forall i j f = (i>j) || ((f i) && forall (i+1) j f)
let rec exists i j f = (i<=j) && ((f i) || exists (i+1) j f)

let p = 1_000_000_007L

(* mod safe for negative numbers *)
let ( %% ) x n = let y = Int64.rem x n in if y<0L then Int64.add y n else y
(* Now, arithmetic modulo p, assuming the arguments a and b are in [0,p-1] *)
let ( ** ) a b = Int64.rem (Int64.mul a b) p
let ( ++ ) a b = let y = Int64.add a b in if y >= p then Int64.sub y p else y
let ( -- ) a b = let y = Int64.sub a b in if y<0L then Int64.add y p else y
let long x = Int64.of_int x

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let sum i j f = fold i j (fun i a -> (f i) + a) 0

let prod i j f = fold i j (fun i a -> (f i) ** a) 1L

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))
let () =
  let cases = read_int () in
  for case = 1 to cases do
    let n = read_int() in
    let a = Array.init n read_int in
    let m = minf 0 (n-1) (fun i -> a.(i)) in
    (* the only way it can work is if m occurs at least twice
       and all other numbers are supersets of m *)
    let ms = sum 0 (n-1) (fun i -> if a.(i) = m then 1 else 0) in

    let badone = exists 0 (n-1) (fun i -> m land a.(i) <> m) in

    if ms < 2 || badone then printf "0\n" else (
      let ans = (long ms)**(long (ms-1))**(prod 1 (n-2) (fun i -> long i)) in
      printf "%Ld\n" ans
    )
  done


Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?

The first line contains integer n (1 ≤ n ≤ 50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.

Print the total minimum number of moves.

(**D. The Child and Sequence*)

open Scanf;;
open Num;;
open Printf;;

let n = Scanf.bscanf Scanf.Scanning.stdin "%d " ( fun x -> x);;

let imatch = Array.create n (num_of_int 0);;
let kmatch = ref (Num.num_of_int 0);;

for i = 0 to n-1 do
  let ni = Scanf.bscanf Scanf.Scanning.stdin "%d " ( fun x -> x) in
  imatch.(i) <- num_of_int ni; kmatch :=  (num_of_int ni) +/ !kmatch
done
;;

(*Remove the average*)
let av = div_num !kmatch (num_of_int n) in
for i = 0 to n - 1 do
  imatch.(i) <- imatch.(i) -/ av
done
;;

(*Array.iter (fun x -> print_int x; print_endline "") imatch;;*)

let sum_over i j =
  let total = ref (num_of_int 0) in
  for k = i to j do
    total := !total +/  imatch.(k)
  done; total
;;

let rec direct_cal nl nr rslt =
  if nl = nr then
    rslt +/ !(sum_over nl nr)
  else
    let mid = (nl + nr) / 2 in
    let a = !(sum_over nl mid) in
    imatch.(mid) <- imatch.(mid) -/  a;
    imatch.(mid+1)<-imatch.(mid+1) +/  a;
    rslt +/ (abs_num a) +/ (direct_cal nl mid (num_of_int 0)) +/
      (direct_cal (mid + 1) nr (num_of_int 0))
;;

let rslt = direct_cal 0 (n-1) (num_of_int 0) in
print_endline (string_of_num
                 (abs_num rslt) );;

(* earlier dubug
printf "%d %d\n" n !kmatch
*)


Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life «interesting graph and apples». An undirected graph is called interesting, if each of its vertices belongs to one cycle only — a funny ring — and does not belong to any other cycles. A funny ring is a cycle that goes through all the vertices just once. Moreover, loops are funny rings too.She has already drawn the apples and some of the graph edges. But now it is not clear, how to connect the rest of the vertices to get an interesting graph as a result. The answer should contain the minimal amount of added edges. And furthermore, the answer should be the lexicographically smallest one. The set of edges (x1, y1), (x2, y2), ..., (xn, yn), where xi ≤ yi, is lexicographically smaller than the set (u1, v1), (u2, v2), ..., (un, vn), where ui ≤ vi, provided that the sequence of integers x1, y1, x2, y2, ..., xn, yn is lexicographically smaller than the sequence u1, v1, u2, v2, ..., un, vn. If you do not cope, Hexadecimal will eat you. ...eat you alive.

The first line of the input data contains a pair of integers n and m (1 ≤ n ≤ 50, 0 ≤ m ≤ 2500) — the amount of vertices and edges respectively. The following lines contain pairs of numbers xi and yi (1 ≤ xi, yi ≤ n) — the vertices that are already connected by edges. The initial graph may contain multiple edges and loops.

In the first line output «YES» or «NO»: if it is possible or not to construct an interesting graph. If the answer is «YES», in the second line output k — the amount of edges that should be added to the initial graph. Finally, output k lines: pairs of vertices xj and yj, between which edges should be drawn. The result may contain multiple edges and loops. k can be equal to zero.

let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x)

let find djs x =
  let rec go x =
    if djs.(x) <> x then (
      djs.(x) <- djs.(djs.(x));
      go djs.(x)
    ) else
      x
  in go x

let () =
  let n = read_int 0 in
  let m = read_int 0 in
  let djs = Array.init n (fun x -> x) in
  let d = Array.make n 2 in
  let c = ref 0 in
  let rec go i =
    if i < m then (
      let x = read_int 0 - 1 in
      let y = read_int 0 - 1 in
      d.(x) <- d.(x)-1;
      d.(y) <- d.(y)-1;
      if d.(x) < 0 || d.(y) < 0 then (
        print_endline "NO";
        exit 0
      );
      let rx = find djs x in
      let ry = find djs y in
      if rx = ry then (
        incr c;
        if !c > 1 then (
          print_endline "NO";
          exit 0
        )
      ) else
        djs.(rx) <- ry;
      go (i+1)
    ) else if !c > 0 then
      print_endline (if n = m then "YES\n0" else "NO")
    else (
      Printf.printf "YES\n%d\n" (n-m);
      for x = 0 to n-1 do
        let rx = find djs x in
        for y = x+1 to n-1 do
          if d.(x) > 0 && d.(y) > 0 then (
            let ry = find djs y in
            if rx <> ry then (
              Printf.printf "%d %d\n" (x+1) (y+1);
              djs.(ry) <- rx;
              d.(x) <- d.(x)-1;
              d.(y) <- d.(y)-1
            )
          )
        done
      done;
      for x = 0 to n-1 do
        for j = 1 to d.(x) do
          Printf.printf "%d " (x+1)
        done
      done
    )
  in
  go 0

let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x)

let find djs x =
  let rec go x =
    if djs.(x) <> x then (
      djs.(x) <- djs.(djs.(x));
      go djs.(x)
    ) else
      x
  in go x

let () =
  let n = read_int 0 in
  let m = read_int 0 in
  let djs = Array.init n (fun x -> x) in
  let d = Array.make n 2 in
  let c = ref 0 in
  let rec go i =
    if i < m then (
      let x = read_int 0 - 1 in
      let y = read_int 0 - 1 in
      d.(x) <- d.(x)-1;
      d.(y) <- d.(y)-1;
      if d.(x) < 0 || d.(y) < 0 then (
        print_endline "NO";
        exit 0
      );
      let rx = find djs x in
      let ry = find djs y in
      if rx = ry then (
        incr c;
        if !c > 1 then (
          print_endline "NO";
          exit 0
        )
      );
      go (i+1)
    ) else if !c > 0 then
      print_endline (if n = m then "YES\n0" else "NO")
    else (
      Printf.printf "YES\n%d\n" (n-m);
      for x = 0 to n-1 do
        let rx = find djs x in
        for y = x+1 to n-1 do
          if d.(x) > 0 && d.(y) > 0 then (
            let ry = find djs y in
            if rx <> ry then (
              Printf.printf "%d %d\n" (x+1) (y+1);
              djs.(ry) <- rx;
              d.(x) <- d.(x)-1;
              d.(y) <- d.(y)-1
            )
          )
        done
      done;
      for x = 0 to n-1 do
        for j = 1 to d.(x) do
          Printf.printf "%d " (x+1)
        done
      done
    )
  in
  go 0

let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x)

let find djs x =
  let rec go x =
    if djs.(x) <> x then (
      djs.(x) <- djs.(djs.(x));
      go djs.(x)
    ) else
      x
  in go x

let () =
  let n = read_int 0 in
  let m = read_int 0 in
  let djs = Array.init n (fun x -> x) in
  let d = Array.make n 2 in
  let c = ref 0 in
  let rec go i =
    if i < m then (
      let x = read_int 0 - 1 in
      let y = read_int 0 - 1 in
      d.(x) <- d.(x)-1;
      d.(y) <- d.(y)-1;
      if d.(x) < 0 || d.(y) < 0 then (
        print_endline "NO";
        exit 0
      );
      let rx = find djs x in
      let ry = find djs y in
      if x = y then (
        incr c;
        if !c > 1 then (
          print_endline "NO";
          exit 0
        )
      );
      go (i+1)
    ) else if !c > 0 then
      print_endline (if n = m then "YES\n0" else "NO")
    else (
      Printf.printf "YES\n%d\n" (n-m);
      for x = 0 to n-1 do
        let rx = find djs x in
        for y = x+1 to n-1 do
          if d.(x) > 0 && d.(y) > 0 then (
            let ry = find djs y in
            if rx <> ry then (
              Printf.printf "%d %d\n" (x+1) (y+1);
              djs.(rx) <- ry;
              d.(x) <- d.(x)-1;
              d.(y) <- d.(y)-1
            )
          )
        done
      done;
      for x = 0 to n-1 do
        for j = 1 to d.(x) do
          Printf.printf "%d " (x+1)
        done
      done
    )
  in
  go 0


Given a sequence $$$a_1, a_2, \ldots, a_n$$$, find the minimum number of elements to remove from the sequence such that after the removal, the sum of every $$$2$$$ consecutive elements is even.

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

For each test case, print a single integer — the minimum number of elements to remove from the sequence such that the sum of every $$$2$$$ consecutive elements is even.

NoteIn the first test case, after removing $$$3$$$, the sequence becomes $$$[2,4,6,8]$$$. The pairs of consecutive elements are $$$\{[2, 4], [4, 6], [6, 8]\}$$$. Each consecutive pair has an even sum now. Hence, we only need to remove $$$1$$$ element to satisfy the condition asked.In the second test case, each consecutive pair already has an even sum so we need not remove any element.

open String

let read_ints() = Array.map (int_of_string) (Array.of_list (Str.split (Str.regexp " +") (read_line())));;
let n = read_int();;

for i = 0 to (n - 1) do
    let size = read_int() and
        arr = read_ints() and
        cnt = ref 0 in (
        for j = 0 to (size - 1) do
            if arr.(j) mod 2 = 0
            then cnt := !cnt + 1
        done;
        print_int (min (size - !cnt) !cnt)
    );
    print_newline ()
done

(* 2x-1+2y-1=2(x+y-1)
o + o = e, o+e=o
we either need all odd
or all even

e.g if we have odd anywhere in sequence,
the next number must be odd or it must be end of seq,
if it end of seq , the numbers before must be odd
*)

let read_int () = Scanf.scanf " %d" (fun x->x);;
let read_float () = Scanf.scanf " %f" (fun x->x);;

let rec read_seq = function
| 0 -> 0
| n ->
    let item = read_int () in
    if (item mod 2 ==0) then 1 + read_seq (n-1)
    else read_seq (n-1)
;;

let run_case = function
| () ->
    let n = read_int () in
    let even_count = read_seq n in
    if (even_count<=n-even_count) then even_count
    else n-even_count
;;

let rec iter_cases = function
| 0 -> 0
| n ->
    print_int (run_case ());
    print_newline ();
    iter_cases (n-1)
;;

let t= read_int () in
iter_cases t;;

open String
open Printf

let read_ints() = Array.map (int_of_string) (Array.of_list (Str.split (Str.regexp " +") (read_line())));;
let n = read_int();;

for i = 0 to (n - 1) do
    let size = read_int() and
        arr = read_ints() and
        cnt = ref 0 in (
        for j = 0 to (size - 1) do
            if arr.(j) mod 2 = 0
            then cnt := !cnt + 1
        done;
        print_int (min (size - !cnt) !cnt)
    )
done

(* 2x-1+2y-1=2(x+y-1)
o + o = e, o+e=o
we either need all odd
or all even

e.g if we have odd anywhere in sequence,
the next number must be odd or it must be end of seq,
if it end of seq , the numbers before must be odd
*)

let read_int () = Scanf.scanf " %d" (fun x->x);;
let read_float () = Scanf.scanf " %f" (fun x->x);;

let rec read_seq = function
| 0 -> 0
| n ->
    let item = read_int () in
    if (item mod 2 ==0) then 1 + read_seq (n-1)
    else read_seq (n-1)
;;

let run_case = function
| () ->
    let n = read_int () in
    let even_count = read_seq n in
    if (even_count<=n-even_count) then even_count
    else n-even_count
;;

let rec iter_cases = function
| 0 -> 0
| n ->
    print_int (run_case ());
    iter_cases (n-1)
;;

let t= read_int () in
iter_cases t;;


Allen is hosting a formal dinner party. $$$2n$$$ people come to the event in $$$n$$$ pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The $$$2n$$$ people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$), the number of pairs of people. The second line contains $$$2n$$$ integers $$$a_1, a_2, \dots, a_{2n}$$$. For each $$$i$$$ with $$$1 \le i \le n$$$, $$$i$$$ appears exactly twice. If $$$a_j = a_k = i$$$, that means that the $$$j$$$-th and $$$k$$$-th people in the line form a couple.

Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.

NoteIn the first sample case, we can transform $$$1 1 2 3 3 2 4 4 \rightarrow 1 1 2 3 2 3 4 4 \rightarrow 1 1 2 2 3 3 4 4$$$ in two steps. Note that the sequence $$$1 1 2 3 3 2 4 4 \rightarrow 1 1 3 2 3 2 4 4 \rightarrow 1 1 3 3 2 2 4 4$$$ also works in the same number of steps.The second sample case already satisfies the constraints; therefore we need $$$0$$$ swaps.

(* Code Forces : Suit And Tie *)

open Scanf
open Printf

let n = scanf "%d" (fun n -> n)

let l = Array.to_list (Array.init (2*n) (fun i -> scanf " %d" (fun c -> c)))

let rec simpl l =
  match l with
  | [] -> []
  | c::c'::t ->
    if c = c' then
      simpl t
    else
      l

let redux l = List.rev (simpl (List.rev (simpl l)))

let rec swap x l out =
  match l with
  | [] -> List.rev out
  | c::(c'::t as l') ->
    if c' = x then
      List.rev (List.rev_append (c'::c::t) out)
    else
      swap x l' (c::out)

let print l = List.iter (fun c -> printf "%d " c) l; print_newline ()

let rec compute l out =
  match redux l with
  | [] -> out
  | c::l' ->
    (*print l;*)
    let l'' = swap c l' [] in
    compute (c::l'') (out+1)

let () =
  let out = compute l 0 in
  printf "%d\n" out


We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.

The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier.

Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't.

NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".

module I64 = struct
let ( / ) = Int64.div
let ( * ) = Int64.mul
let ( + ) = Int64.add
let ( - ) = Int64.sub
let ( mod ) = Int64.rem
let zero = Int64.zero
let one = Int64.one
let two = Int64.of_int 2
end

let is_prime =
    let n = 1000000 in
    let a = Array.make (n + 1) true in
    let rec loop i =
        if i * i <= n
        then
            ((if a.(i)
            then
                let rec looper j =
                if j > n
                then
                    ()
                else
                    (Array.set a j false;
                    looper (j + i))
                in looper (i * i)
            else
                ());
            loop (i + 1))
        else
            ()
    in loop 2;
    Array.set a 0 false;
    Array.set a 1 false;

    fun i -> a.(i)

let find_sqrt n =
    let open I64 in
    let r = Pervasives.sqrt (Int64.to_float n) in
    let rec loop (+) (<) approx =
        if approx * approx < n
        then
            loop (+) (<) (approx + one)
        else
            approx
    in
    let r = loop (-) (>) (loop (+) (<) (Int64.of_float r)) in
    if r * r = n
    then
        Some (Int64.to_int r)
    else
        None

let is_tprime n =
    match (find_sqrt n) with
    | Some r -> is_prime r
    | None -> false

let () =
    let length = Scanf.scanf "%d " (fun n -> n) in
    let rec loop i =
        if i < length
        then
            let number = Scanf.scanf "%Ld " (fun n -> n) in
            (if is_tprime number
            then
                Printf.printf "YES\n"
            else
                Printf.printf "NO\n");
            loop (i + 1)
        else
            ()
    in loop 0

let split_words = Str.split (Str.regexp " ")

exception Break

let primes =
  let array = Array.make 1_000_001 true in
  array.(0) <- false;
  array.(1) <- false;
  begin try
    for i = 2 to Array.length array do
      if array.(i)
      then (
        if i * i >= Array.length array
        then (raise Break);
        for j = i to (Array.length array - 1) / i do
          array.(i * j) <- false
        done)
    done
  with
  | _ -> ()
  end;
  array
;;

let is_prime n = primes.(Int64.to_int n)

let is_t_prime n =
  let sq = Int64.of_float (sqrt (Int64.to_float n)) in
  Int64.mul sq sq = n && is_prime sq
;;

let () =
  ignore (input_line stdin);
  input_line stdin
  |> split_words
  |> List.map Int64.of_string
  |> List.iter (fun number ->
    if is_t_prime number
    then (print_endline "YES")
    else (print_endline "NO"))
;;

let sieve () =
  let nums = Array.make 1000001 true in
  nums.(0) <- false;
  nums.(1) <- false;
  for i = 2 to 1000000 do
    if nums.(i) then
      let rec walk j =
	if j > 1000000 then ()
	else begin
	  nums.(j) <- false;
	  walk (j + i)
	end in
      walk (i+i)
  done;
  nums

let main =
  let primes = sieve () in
  let n = Scanf.scanf "%d" (fun n -> n) in
  for i = 1 to n do
    let f = Scanf.scanf " %f" (fun f -> f) in
    let (ff, fi) = modf (sqrt f) in
    if ff > 0.000001 then
      Printf.printf "NO\n"
    else
      Printf.printf "%s\n" (if primes.(int_of_float fi) then "YES" else "NO")
  done;

module I64 = struct
let ( / ) = Int64.div
let ( * ) = Int64.mul
let ( + ) = Int64.add
let ( - ) = Int64.sub
let ( mod ) = Int64.rem
let zero = Int64.zero
let one = Int64.one
let two = Int64.of_int 2
end

let is_prime =
    let n = 1000000 in
    let a = Array.make (n + 1) true in
    let rec loop i =
        if i * i <= n
        then
            if a.(i)
            then
                let rec looper j =
                if j > n
                then
                    ()
                else
                    (Array.set a j false;
                    looper (j + i))
                in looper (i * i)
        else
            ()
    in loop 2;
    Array.set a 0 false;
    Array.set a 1 false;

    fun i -> a.(i)

let find_sqrt n =
    let open I64 in
    let r = Pervasives.sqrt (Int64.to_float n) in
    let rec loop (+) (<) approx =
        if approx * approx < n
        then
            loop (+) (<) (approx + one)
        else
            approx
    in
    let r = loop (-) (>) (loop (+) (<) (Int64.of_float r)) in
    if r * r = n
    then
        Some (Int64.to_int r)
    else
        None

let is_tprime n =
    match (find_sqrt n) with
    | Some r -> is_prime r
    | None -> false

let () =
    let length = Scanf.scanf "%d " (fun n -> n) in
    let rec loop i =
        if i < length
        then
            let number = Scanf.scanf "%Ld " (fun n -> n) in
            (if is_tprime number
            then
                Printf.printf "YES\n"
            else
                Printf.printf "NO\n");
            loop (i + 1)
        else
            ()
    in loop 0