Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on $$$n - 2$$$ intermediate planets. Formally: we number all the planets from $$$1$$$ to $$$n$$$. $$$1$$$ is Earth, $$$n$$$ is Mars. Natasha will make exactly $$$n$$$ flights: $$$1 \to 2 \to \ldots n \to 1$$$.Flight from $$$x$$$ to $$$y$$$ consists of two phases: take-off from planet $$$x$$$ and landing to planet $$$y$$$. This way, the overall itinerary of the trip will be: the $$$1$$$-st planet $$$\to$$$ take-off from the $$$1$$$-st planet $$$\to$$$ landing to the $$$2$$$-nd planet $$$\to$$$ $$$2$$$-nd planet $$$\to$$$ take-off from the $$$2$$$-nd planet $$$\to$$$ $$$\ldots$$$ $$$\to$$$ landing to the $$$n$$$-th planet $$$\to$$$ the $$$n$$$-th planet $$$\to$$$ take-off from the $$$n$$$-th planet $$$\to$$$ landing to the $$$1$$$-st planet $$$\to$$$ the $$$1$$$-st planet.The mass of the rocket together with all the useful cargo (but without fuel) is $$$m$$$ tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that $$$1$$$ ton of fuel can lift off $$$a_i$$$ tons of rocket from the $$$i$$$-th planet or to land $$$b_i$$$ tons of rocket onto the $$$i$$$-th planet. For example, if the weight of rocket is $$$9$$$ tons, weight of fuel is $$$3$$$ tons and take-off coefficient is $$$8$$$ ($$$a_i = 8$$$), then $$$1.5$$$ tons of fuel will be burnt (since $$$1.5 \cdot 8 = 9 + 3$$$). The new weight of fuel after take-off will be $$$1.5$$$ tons. Please note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well.Help Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn't need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 1000$$$) — number of planets. The second line contains the only integer $$$m$$$ ($$$1 \le m \le 1000$$$) — weight of the payload. The third line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 1000$$$), where $$$a_i$$$ is the number of tons, which can be lifted off by one ton of fuel. The fourth line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le 1000$$$), where $$$b_i$$$ is the number of tons, which can be landed by one ton of fuel.  It is guaranteed, that if Natasha can make a flight, then it takes no more than $$$10^9$$$ tons of fuel.

If Natasha can fly to Mars through $$$(n - 2)$$$ planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number $$$-1$$$. It is guaranteed, that if Natasha can make a flight, then it takes no more than $$$10^9$$$ tons of fuel. The answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$. Formally, let your answer be $$$p$$$, and the jury's answer be $$$q$$$. Your answer is considered correct if $$$\frac{|p - q|}{\max{(1, |q|)}} \le 10^{-6}$$$.

NoteLet's consider the first example.Initially, the mass of a rocket with fuel is $$$22$$$ tons. At take-off from Earth one ton of fuel can lift off $$$11$$$ tons of cargo, so to lift off $$$22$$$ tons you need to burn $$$2$$$ tons of fuel. Remaining weight of the rocket with fuel is $$$20$$$ tons. During landing on Mars, one ton of fuel can land $$$5$$$ tons of cargo, so for landing $$$20$$$ tons you will need to burn $$$4$$$ tons of fuel. There will be $$$16$$$ tons of the rocket with fuel remaining. While taking off from Mars, one ton of fuel can raise $$$8$$$ tons of cargo, so to lift off $$$16$$$ tons you will need to burn $$$2$$$ tons of fuel. There will be $$$14$$$ tons of rocket with fuel after that. During landing on Earth, one ton of fuel can land $$$7$$$ tons of cargo, so for landing $$$14$$$ tons you will need to burn $$$2$$$ tons of fuel. Remaining weight is $$$12$$$ tons, that is, a rocket without any fuel.In the second case, the rocket will not be able even to take off from Earth.

let option_get ~default = function
  | Some a -> a
  | None -> default

let bisearch_num (callback: float -> bool) ((l, r): float * float): float option =
  let rec f l r =
    let m = (l +. r) /. 2. in
    (* ATTENTION: this base case ... *)
    if abs_float (r -. l) <= 1e-6 then if callback r then Some r else None
    else if callback m then f l m else f m r
  in
  f l r

let () =
  (* n - #planet including Earth & Mars *)
  (* mr - mass of payload *)
  let n, mr = Scanf.scanf " %d %f" (fun x y -> x, y) in
  let a = Array.init n (fun _ -> Scanf.scanf " %f" (fun i -> i)) in
  let b = Array.init n (fun _ -> Scanf.scanf " %f" (fun i -> i)) in

  let f rem =
    let rec f i rem =
      if i = n then rem
      else
        let rem = rem -. (rem +. mr) /. b.(i) in
        let rem = rem -. (rem +. mr) /. a.(i) in
        f (i + 1) rem
    in f 0 rem >= 0.
  in
  bisearch_num f (0., 1_000_000_001.) |>
  option_get ~default:(-1.) |>
  Printf.printf "%.06f\n"

let option_get ~default = function
  | Some a -> a
  | None -> default

let bisearch_num (callback: float -> bool) ((l, r): float * float): float option =
  let rec f l r =
    let m = (l +. r) /. 2. in
    (* ATTENTION: this base case ... *)
    if abs_float (r -. l) <= 1e-6 then if callback r then Some r else None
    else if callback m then f l m else f m r
  in
  f l r

let () =
  (* n - #planet including Earth & Mars *)
  (* mr - mass of payload *)
  let n, mr = Scanf.scanf " %d %f" (fun x y -> x, y) in
  let a = Array.init n (fun _ -> Scanf.scanf " %f" (fun i -> i)) in
  let b = Array.init n (fun _ -> Scanf.scanf " %f" (fun i -> i)) in

  let f rem =
    let rec f i rem =
      if i = n then rem
      else
        let rem = rem -. (rem +. mr) /. b.(i) in
        let rem = rem -. (rem +. mr) /. a.(i) in
        f (i + 1) rem
    in f 0 rem >= 0.
  in
  bisearch_num f (0., 1_000_000_000.) |>
  option_get ~default:(-1.) |>
  Printf.printf "%.06f\n"

let option_get ~default = function
  | Some a -> a
  | None -> default

let bisearch_num (callback: float -> bool) ((l, r): float * float): float option =
  let rec f l r =
    let m = (l +. r) /. 2. in
    if abs_float (r -. l) <= 1e-6 then if callback m then Some m else None
    else if callback m then f l m else f m r
  in f l r

let () =
  (* n - #planet including Earth & Mars *)
  (* mr - mass of payload *)
  let n, mr = Scanf.scanf " %d %f" (fun x y -> x, y) in
  let a = Array.init n (fun _ -> Scanf.scanf " %f" (fun i -> i)) in
  let b = Array.init n (fun _ -> Scanf.scanf " %f" (fun i -> i)) in

  let f rem =
    let rec f i rem =
    if i = n then rem
    else
        let rem = rem -. (rem +. mr) /. b.(i) in
        let rem = rem -. (rem +. mr) /. a.(i) in
        f (i + 1) rem
    in f 0 rem >= 0.
  in
  bisearch_num f (0., 1_000_000_000.) |>
  option_get ~default:(-1.) |>
  Printf.printf "%.06f\n"


According to a new ISO standard, a flag of every country should have a chequered field n × m, each square should be of one of 10 colours, and the flag should be «striped»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland's government asked you to find out whether their flag meets the new ISO standard.

The first line of the input contains numbers n and m (1 ≤ n, m ≤ 100), n — the amount of rows, m — the amount of columns on the flag of Berland. Then there follows the description of the flag: each of the following n lines contain m characters. Each character is a digit between 0 and 9, and stands for the colour of the corresponding square.

Output YES, if the flag meets the new ISO standard, and NO otherwise.

exception No;;
let check_one_line l=
  List.fold_left (fun a b ->
                   if a !=b
                   then raise No
                   else a)
                 (List.hd l)
                 l;;

let check_two_line l =
  List.fold_left (fun a b ->
                   check_one_line b;
                   if (List.hd a)==(List.hd b)
                   then raise No else b)
                 [10;10]
                 l;;

let rec get_line_wrap m =
  let rec get_line m r =
  if m==0 then r
  else get_line (m-1) ((Scanf.scanf "%c" (fun c -> Char.code c - Char.code '0'))::r)
  in
  let
    a = get_line m [];
  in
  Scanf.scanf "%c" (fun c -> c);
  a;;

let get_lines_wrap n m =
  let rec get_lines n m r=
    if n<=0 then r
    else get_lines (n-1) m ((get_line_wrap m) :: r)
  in
  get_lines n m [];;

let () =
  let l = Scanf.scanf "%d %d\n" get_lines_wrap in
  try
    check_two_line l;Printf.printf "Yes\n"
  with No -> Printf.printf "No\n";;

exception No;;
let check_one_line l=
  List.fold_left (fun a b ->
                   if a !=b
                   then raise No
                   else a)
                 (List.hd l)
                 l;;

let check_two_line l =
  List.fold_left (fun a b ->
                   check_one_line b;
                   if (List.hd a)==(List.hd b)
                   then raise No else b)
                 [10;10]
                 l;;

let rec get_line m =
  if m==0 then []
  else (Scanf.scanf "%c" (fun c -> Char.code c - Char.code '0')):: (get_line (m-1));;

let rec get_line_wrap m =
  let
    a = get_line m;
  in
  Scanf.scanf "%c" (fun c -> c);
  a;;

let rec get_lines n m =
  if n<=0 then []
  else get_line_wrap m :: get_lines (n-1) m;;

let () =
  let l = Scanf.scanf "%d %d\n" get_lines in
  try
    check_two_line l;Printf.printf "Yes\n"
  with No -> Printf.printf "No\n";;

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

let () =
  let n = read_int 0 in
  let m = read_int 0 in
  let a = Array.init n read_str in
  let rec go i j =
    if i = n then
      true
    else if j = m then
      go (i+1) 0
    else if j > 0 && a.(i).[j-1] <> a.(i).[j] then
      false
    else if i > 0 && j = 0 && a.(i-1).[0] = a.(i).[0] then
      false
    else
      go i (j+1)
  in
  print_endline (if go 0 0 then "YES" else "NO")


Petya has recently started working as a programmer in the IT city company that develops computer games.Besides game mechanics implementation to create a game it is necessary to create tool programs that can be used by game designers to create game levels. Petya's first assignment is to create a tool that allows to paint different arrows on the screen.A user of this tool will choose a point on the screen, specify a vector (the arrow direction) and vary several parameters to get the required graphical effect. In the first version of the program Petya decided to limit parameters of the arrow by the following: a point with coordinates (px, py), a nonzero vector with coordinates (vx, vy), positive scalars a, b, c, d, a > c.The produced arrow should have the following properties. The arrow consists of a triangle and a rectangle. The triangle is isosceles with base of length a and altitude of length b perpendicular to the base. The rectangle sides lengths are c and d. Point (px, py) is situated in the middle of the triangle base and in the middle of side of rectangle that has length c. Area of intersection of the triangle and the rectangle is zero. The direction from (px, py) point to the triangle vertex opposite to base containing the point coincides with direction of (vx, vy) vector.Enumerate the arrow points coordinates in counter-clockwise order starting from the tip.

The only line of the input contains eight integers px, py, vx, vy ( - 1000 ≤ px, py, vx, vy ≤ 1000, vx2 + vy2 > 0), a, b, c, d (1 ≤ a, b, c, d ≤ 1000, a > c).

Output coordinates of the arrow points in counter-clockwise order. Each line should contain two coordinates, first x, then y. Relative or absolute error should not be greater than 10 - 9.

let _ =
  let sb = Scanf.Scanning.stdib in
  let px = Scanf.bscanf sb "%f " (fun s -> s) in
  let py = Scanf.bscanf sb "%f " (fun s -> s) in
  let vx = Scanf.bscanf sb "%f " (fun s -> s) in
  let vy = Scanf.bscanf sb "%f " (fun s -> s) in
  let a = Scanf.bscanf sb "%f " (fun s -> s) in
  let b = Scanf.bscanf sb "%f " (fun s -> s) in
  let c = Scanf.bscanf sb "%f " (fun s -> s) in
  let d = Scanf.bscanf sb "%f " (fun s -> s) in
  let sqr x = x *. x in
  let norm = sqrt (sqr vx +. sqr vy) in
  let vx = vx /. norm in
  let vy = vy /. norm in
  let print x y =
    Printf.printf "%.9f %.9f\n"
      (px +. x *. vx -. y *. vy)
      (py +. x *. vy +. y *. vx)
  in
    print b 0.0;
    print 0.0 (a /. 2.0);
    print 0.0 (c /. 2.0);
    print (-.d) (c /. 2.0);
    print (-.d) (-.c /. 2.0);
    print 0.0 (-.c /. 2.0);
    print 0.0 (-.a /. 2.0);


One must train much to do well on wizardry contests. So, there are numerous wizardry schools and magic fees.One of such magic schools consists of n tours. A winner of each tour gets a huge prize. The school is organised quite far away, so one will have to take all the prizes home in one go. And the bags that you've brought with you have space for no more than k huge prizes.Besides the fact that you want to take all the prizes home, you also want to perform well. You will consider your performance good if you win at least l tours.In fact, years of organizing contests proved to the organizers that transporting huge prizes is an issue for the participants. Alas, no one has ever invented a spell that would shrink the prizes... So, here's the solution: for some tours the winner gets a bag instead of a huge prize. Each bag is characterized by number ai — the number of huge prizes that will fit into it.You already know the subject of all tours, so you can estimate the probability pi of winning the i-th tour. You cannot skip the tour under any circumstances.Find the probability that you will perform well on the contest and will be able to take all won prizes home (that is, that you will be able to fit all the huge prizes that you won into the bags that you either won or brought from home).

The first line contains three integers n, l, k (1 ≤ n ≤ 200, 0 ≤ l, k ≤ 200) — the number of tours, the minimum number of tours to win, and the number of prizes that you can fit in the bags brought from home, correspondingly. The second line contains n space-separated integers, pi (0 ≤ pi ≤ 100) — the probability to win the i-th tour, in percents. The third line contains n space-separated integers, ai (1 ≤ ai ≤ 200) — the capacity of the bag that will be awarded to you for winning the i-th tour, or else -1, if the prize for the i-th tour is a huge prize and not a bag.

Print a single real number — the answer to the problem. The answer will be accepted if the absolute or relative error does not exceed 10 - 6.

NoteIn the first sample we need either win no tour or win the third one. If we win nothing we wouldn't perform well. So, we must to win the third tour. Other conditions will be satisfied in this case. Probability of wining the third tour is 0.3.In the second sample we win the only tour with probability 1.0, and go back home with bag for it.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let l = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make n 0.0 in
  let a = Array.make n 0 in
  let () =
    for i = 0 to n - 1 do
      p.(i) <- float_of_int (Scanf.bscanf sb "%d " (fun s -> s)) /. 100.0;
    done;
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
    done;
  in
  let b = Array.init (n + 1)
    (fun _ -> Array.make_matrix (n + 1) (2 * n + 1) 0.0)
  in
  let k = min k n in
    b.(0).(0).(k + n) <- 1.0;
    for i = 0 to n - 1 do
      for j = 0 to n do
	for m = 0 to 2 * n do
	  if b.(i).(j).(m) <> 0.0 then (
	    if a.(i) < 0
	    then (
	      b.(i + 1).(j + 1).(m - 1) <-
		b.(i + 1).(j + 1).(m - 1) +. b.(i).(j).(m) *. p.(i);
	      b.(i + 1).(j).(m) <-
		b.(i + 1).(j).(m) +. b.(i).(j).(m) *. (1.0 -. p.(i));
	    ) else (
	      let aa = m + a.(i) in
	      let aa = if aa > 2 * n then 2 * n else aa in
		b.(i + 1).(j + 1).(aa) <-
		  b.(i + 1).(j + 1).(aa) +. b.(i).(j).(m) *. p.(i);
		b.(i + 1).(j).(m) <-
		  b.(i + 1).(j).(m) +. b.(i).(j).(m) *. (1.0 -. p.(i));
	    )
	  )
	done
      done
    done;
    let res = ref 0.0 in
      for i = l to n do
	for j = n to 2 * n do
	  res := !res +. b.(n).(i).(j)
	done
      done;
      Printf.printf "%.9f\n" !res


You are given two integers $$$a$$$ and $$$b$$$. In one turn, you can do one of the following operations:   Take an integer $$$c$$$ ($$$c > 1$$$ and $$$a$$$ should be divisible by $$$c$$$) and replace $$$a$$$ with $$$\frac{a}{c}$$$;  Take an integer $$$c$$$ ($$$c > 1$$$ and $$$b$$$ should be divisible by $$$c$$$) and replace $$$b$$$ with $$$\frac{b}{c}$$$. Your goal is to make $$$a$$$ equal to $$$b$$$ using exactly $$$k$$$ turns.For example, the numbers $$$a=36$$$ and $$$b=48$$$ can be made equal in $$$4$$$ moves:   $$$c=6$$$, divide $$$b$$$ by $$$c$$$ $$$\Rightarrow$$$ $$$a=36$$$, $$$b=8$$$;  $$$c=2$$$, divide $$$a$$$ by $$$c$$$ $$$\Rightarrow$$$ $$$a=18$$$, $$$b=8$$$;  $$$c=9$$$, divide $$$a$$$ by $$$c$$$ $$$\Rightarrow$$$ $$$a=2$$$, $$$b=8$$$;  $$$c=4$$$, divide $$$b$$$ by $$$c$$$ $$$\Rightarrow$$$ $$$a=2$$$, $$$b=2$$$. For the given numbers $$$a$$$ and $$$b$$$, determine whether it is possible to make them equal using exactly $$$k$$$ turns.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. Each test case is contains three integers $$$a$$$, $$$b$$$ and $$$k$$$ ($$$1 \le a, b, k \le 10^9$$$).

For each test case output:    "Yes", if it is possible to make the numbers $$$a$$$ and $$$b$$$ equal in exactly $$$k$$$ turns;  "No" otherwise.  The strings "Yes" and "No" can be output in any case.

module Stdlib = Pervasives
let std_in = Stdlib.stdin and std_out = Stdlib.stdout;;
(* let std_in = Stdlib.open_in "input.txt" and std_out = Stdlib.stdout;; *)

let identity x = x;;
let tup3 x1 x2 x3 = (x1, x2, x3);;

let rec repeat f state = function
  | 0 -> ()
  | n -> let () = f state in repeat f state (n - 1);;

let create_sieve n =
  let primes = Array.make (n + 1) 0 and primes_count = ref 0 and ancs = Array.make (n + 1) 0 in
  let () =
    for i = 2 to n do
      begin
        if ancs.(i) = 0 then
          let () = ancs.(i) <- i and () = primes.(!primes_count) <- i in incr primes_count
        else ();
        let j = ref 0 in
        while !j < !primes_count && i * primes.(!j) <= n && primes.(!j) <= ancs.(i) do
          let () = ancs.(i * primes.(!j)) <- primes.(!j) in incr j
        done
      end
    done
  in
  (Array.sub primes 0 !primes_count |> Array.to_list, ancs, n)

let rec fact_folder ancs value acc =
  if value = 1 then acc else fact_folder ancs (value / ancs.(value)) (acc + 1);;

let rec helper ((_, ancs, primlimit) as sieve) primes value acc =
  match primes with
  | [] -> if value = 1 then acc else acc + 1
  | prime::primes_tail as primes ->
    if value <= primlimit then fact_folder ancs value acc
    else if value mod prime = 0 then helper sieve primes (value / prime) (acc + 1)
    else helper sieve primes_tail value acc;;

let calc_primes_count ((primes, _, _) as sieve) value = helper sieve primes value 0

let solution (inp, outp, sieve) =
  let (a, b, c) = let line = input_line inp in Scanf.sscanf line "%d %d %d" tup3 in
  let result =
    if c = 1 then a != b && (a mod b = 0 || b mod a = 0)
    else
      let count_a = calc_primes_count sieve a and count_b = calc_primes_count sieve b in
      count_a + count_b >= c
  in
  let answer = if result then "YES\n" else "NO\n" in
  let () = Printf.fprintf outp "%s" answer in
  ();;

let () =
  let inp = std_in and outp = std_out in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  let primlimit = 31623 in
  let sieve = create_sieve primlimit in
  let state = (inp, outp, sieve) in
  repeat solution state tests_count;;

module Stdlib = Pervasives
let std_in = Stdlib.stdin and std_out = Stdlib.stdout;;
(* let std_in = Stdlib.open_in "input.txt" and std_out = Stdlib.stdout;; *)

let identity x = x;;
let tup3 x1 x2 x3 = (x1, x2, x3);;

let rec repeat f state = function
  | 0 -> ()
  | n -> let () = f state in repeat f state (n - 1);;

let create_sieve n =
  let primes = Array.make (n + 1) 0 and primes_count = ref 0 and ancs = Array.make (n + 1) 0 in
  let () =
    for i = 2 to n do
      begin
        if ancs.(i) = 0 then
          let () = ancs.(i) <- i and () = primes.(!primes_count) <- i in incr primes_count
        else ();
        let j = ref 0 in
        while !j < !primes_count && i * primes.(!j) <= n && primes.(!j) <= ancs.(i) do
          let () = ancs.(i * primes.(!j)) <- primes.(!j) in incr j
        done
      end
    done
  in
  (Array.sub primes 0 !primes_count |> Array.to_list, ancs, n)

let rec fact_folder ancs value acc =
  if value = 1 then acc else fact_folder ancs (value / ancs.(value)) (acc + 1);;

let rec helper ((_, ancs, primlimit) as sieve) primes value acc =
  match primes with
  | [] -> if value = 1 then acc else acc + 1
  | prime::primes_tail as primes ->
    if value <= primlimit then fact_folder ancs value acc
    else if value mod prime = 0 then helper sieve primes (value / prime) (acc + 1)
    else helper sieve primes_tail value acc;;

let calc_primes_count ((primes, _, _) as sieve) value = helper sieve primes value 0

let solution (inp, outp, sieve) =
  let (a, b, c) = let line = input_line inp in Scanf.sscanf line "%d %d %d" tup3 in
  let result =
    if c = 1 then
      if a != b && (a mod b = 0 || b mod a = 0) then true else false
    else
      let count_a = calc_primes_count sieve a and count_b = calc_primes_count sieve b in
      count_a + count_b >= c
  in
  let answer = if result then "YES\n" else "NO\n" in
  let () = Printf.fprintf outp "%s" answer in
  ();;

let () =
  let inp = std_in and outp = std_out in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  let primlimit = 31623 in
  let sieve = create_sieve primlimit in
  let state = (inp, outp, sieve) in
  repeat solution state tests_count;;

module Stdlib = Pervasives
let std_in = Stdlib.stdin and std_out = Stdlib.stdout;;
(* let std_in = Stdlib.open_in "input.txt" and std_out = Stdlib.stdout;; *)

let identity x = x;;
let tup3 x1 x2 x3 = (x1, x2, x3);;

let rec repeat f state = function
  | 0 -> ()
  | n ->
    let () = f state in
    repeat f state (n - 1);;

type sieve = int list * int array * int;;

let create_sieve n =
  let primes = Array.make (n + 1) 0 and primes_count = ref 0 and ancs = Array.make (n + 1) 0 in
  let () =
    for i = 2 to n do
      begin
        if ancs.(i) = 0 then
          let () = ancs.(i) <- i and () = primes.(!primes_count) <- i in
          incr primes_count
        else ();
        let j = ref 0 in
        while !j < !primes_count && i * primes.(!j) <= n && primes.(!j) <= ancs.(i) do
          let () = ancs.(i * primes.(!j)) <- primes.(!j) in
          incr j
        done
      end
    done
  in
  (Array.sub primes 0 !primes_count |> Array.to_list, ancs, n)

let rec fact_folder ancs value acc =
  if value = 1 then acc
  else fact_folder ancs (value / ancs.(value)) (acc + 1);;

  let rec helper ((_, ancs, primlimit) as sieve) primes value acc =
    match primes with
    | [] -> if value = 1 then acc else acc + 1
    | prime::primes_tail as primes ->
      if value <= primlimit then fact_folder ancs value acc
      else if value mod prime = 0 then helper sieve primes (value / prime) (acc + 1)
      else helper sieve primes_tail value acc;;

let calc_primes_count ((primes, _, _) as sieve) value =
  helper sieve primes value 0

let solution (inp, outp, sieve) =
  let (a, b, c) = let line = input_line inp in Scanf.sscanf line "%d %d %d" tup3 in
  let result =
    if c = 1 then
      if a != b && (a mod b = 0 || b mod a = 0) then true else false
    else
      let count_a = calc_primes_count sieve a and count_b = calc_primes_count sieve b in
      count_a + count_b >= c
  in
  let answer = if result then "YES\n" else "NO\n" in
  let () = Printf.fprintf outp "%s" answer in
  ();;

let () =
  let inp = std_in and outp = std_out in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  let primlimit = 31623 in
  let sieve = create_sieve primlimit in
  let state = (inp, outp, sieve) in
  repeat solution state tests_count;;

module Stdlib = Pervasives
let std_in = Stdlib.stdin and std_out = Stdlib.stdout;;
(* let std_in = Stdlib.open_in "input.txt" and std_out = Stdlib.stdout;; *)
let identity x = x;;
let tup3 x1 x2 x3 = (x1, x2, x3);;

let rec repeat f state = function
  | 0 -> ()
  | n ->
    let () = f state in
    repeat f state (n - 1);;

type sieve = int list * int array * int;;

let create_sieve n =
  let primes = Array.make (n + 1) 0 and primes_count = ref 0 and ancs = Array.make (n + 1) 0 in
  let () =
    for i = 2 to n do
      begin
        if ancs.(i) = 0 then
          let () = ancs.(i) <- i and () = primes.(!primes_count) <- i in
          incr primes_count
        else ();
        let j = ref 0 in
        while !j < !primes_count && i * primes.(!j) <= n && primes.(!j) <= ancs.(i) do
          let () = ancs.(i * primes.(!j)) <- primes.(!j) in
          incr j
        done
      end
    done
  in
  (Array.sub primes 0 !primes_count |> Array.to_list, ancs, n)

let rec fact_folder ancs value acc =
  if value = 1 then acc
  else fact_folder ancs (value / ancs.(value)) (acc + 1);;

  let rec helper ((_, ancs, primlimit) as sieve) primes value acc =
    match primes with
    | [] -> if value = 1 then acc else acc + 1
    | prime::primes_tail as primes ->
      if value <= primlimit then fact_folder ancs value acc
      else if value mod prime = 0 then helper sieve primes (value / prime) (acc + 1)
      else helper sieve primes_tail value acc;;

let calc_primes_count ((primes, _, _) as sieve) value =
  helper sieve primes value 0

let solution (inp, outp, sieve) =
  let (a, b, c) = let line = input_line inp in Scanf.sscanf line "%d %d %d" tup3 in
  let result =
    if c = 1 then
      if a != b && (a mod b = 0 || b mod a = 0) then true else false
    else
      let count_a = calc_primes_count sieve a and count_b = calc_primes_count sieve b in
      count_a + count_b >= c
  in
  let answer = if result then "YES" else "NO" in
  let () = Printf.fprintf outp "%s\n" answer in
  ();;

let () =
  let inp = std_in and outp = std_out in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  let primlimit = 31623 in
  let sieve = create_sieve primlimit in
  let state = (inp, outp, sieve) in
  repeat solution state tests_count;;

module Stdlib = Pervasives
let std_in = Stdlib.stdin and std_out = Stdlib.stdout;;
(* let std_in = Stdlib.open_in "input.txt" and std_out = Stdlib.stdout;; *)
let identity x = x;;
let tup3 x1 x2 x3 = (x1, x2, x3);;

let gc_defaults = Gc.get ();;

let () =
  Gc.set
  { gc_defaults with
    minor_heap_size = 1024 * 1024
  ; major_heap_increment = 1024 * 1024
  };;

let rec repeat f state = function
  | 0 -> ()
  | n ->
    let () = f state in
    repeat f state (n - 1);;

type sieve = int list * int array * int;; (* primes, ancestors, primlimit *)

let create_sieve n =
  let primes = Array.make (n + 1) 0 and primes_count = ref 0 and ancs = Array.make (n + 1) 0 in
  let () =
    for i = 2 to n do
      begin
        if ancs.(i) = 0 then
          let () = ancs.(i) <- i and () = primes.(!primes_count) <- i in
          incr primes_count
        else ();
        let j = ref 0 in
        while !j < !primes_count && i * primes.(!j) <= n && primes.(!j) <= ancs.(i) do
          let () = ancs.(i * primes.(!j)) <- primes.(!j) in
          incr j
        done
      end
    done
  in
  ( Array.sub primes 0 !primes_count |> Array.to_list , ancs , n )

let calc_primes_count ((primes, _, _) as sieve) value =
  let rec fact_folder ancs value acc =
    if value = 1 then acc
    else fact_folder ancs (value / ancs.(value)) (acc + 1)
  in
  let rec helper ((_, ancs, primlimit) as sieve) primes value acc =
    match primes with
    | [] -> if value = 1 then acc else acc + 1
    | prime::primes_tail as primes ->
      if value <= primlimit then fact_folder ancs value acc
      else if value mod prime = 0 then helper sieve primes (value / prime) (acc + 1)
      else helper sieve primes_tail value acc
  in
  helper sieve primes value 0

let solution (inp, outp, sieve) =
  let (_primes, _ancs, _primlimit) = sieve in
  let (a, b, c) = let line = input_line inp in Scanf.sscanf line "%d %d %d" tup3 in
  let result =
    if c = 1 then
      if a != b && (a mod b = 0 || b mod a = 0) then true else false
    else
      let count_a = calc_primes_count sieve a and count_b = calc_primes_count sieve b in
      count_a + count_b >= c
  in
  let answer = if result then "YES" else "NO" in
  let () = Printf.fprintf outp "%s\n" answer in
(*   let () = Printf.fprintf outp "Val A: %d; Val B: %d\n" a b in
  let () = Printf.fprintf outp "Count A: %d; Count B: %d\n" count_a count_b in
 *)  ();;

let () =
  let inp = std_in and outp = std_out in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  (* let primlimit = 20 in *)
  let primlimit = 31623 in
  let sieve = create_sieve primlimit in
  let state = (inp, outp, sieve) in
  repeat solution state tests_count;;

module Stdlib = Pervasives
let std_in = Stdlib.stdin and std_out = Stdlib.stdout;;
(* let std_in = Stdlib.open_in "input.txt" and std_out = Stdlib.stdout;; *)
let identity x = x;;
let tup3 x1 x2 x3 = (x1, x2, x3);;

let gc_defaults = Gc.get ();;

let () =
  Gc.set
  { gc_defaults with
    minor_heap_size = 1024 * 1024 * 16
  ; major_heap_increment = 1024 * 1024 * 32
  };;

let rec repeat f state = function
  | 0 -> ()
  | n ->
    let () = f state in
    repeat f state (n - 1);;

type sieve = int list * int array * int;; (* primes, ancestors, primlimit *)

let create_sieve n =
  let primes = Array.make (n + 1) 0 and primes_count = ref 0 and ancs = Array.make (n + 1) 0 in
  let () =
    for i = 2 to n do
      begin
        if ancs.(i) = 0 then
          let () = ancs.(i) <- i and () = primes.(!primes_count) <- i in
          incr primes_count
        else ();
        let j = ref 0 in
        while !j < !primes_count && i * primes.(!j) <= n && primes.(!j) <= ancs.(i) do
          let () = ancs.(i * primes.(!j)) <- primes.(!j) in
          incr j
        done
      end
    done
  in
  ( Array.sub primes 0 !primes_count |> Array.to_list , ancs , n )

let calc_primes_count ((primes, _, _) as sieve) value =
  let rec fact_folder ancs value acc =
    if value = 1 then acc
    else fact_folder ancs (value / ancs.(value)) (acc + 1)
  in
  let rec helper ((_, ancs, primlimit) as sieve) primes value acc =
    match primes with
    | [] -> if value = 1 then acc else acc + 1
    | prime::primes_tail as primes ->
      if value <= primlimit then fact_folder ancs value acc
      else if value mod prime = 0 then helper sieve primes (value / prime) (acc + 1)
      else helper sieve primes_tail value acc
  in
  helper sieve primes value 0

let solution (inp, outp, sieve) =
  let (_primes, _ancs, _primlimit) = sieve in
  let (a, b, c) = let line = input_line inp in Scanf.sscanf line "%d %d %d" tup3 in
  let result =
    if c = 1 then
      if a != b && (a mod b = 0 || b mod a = 0) then true else false
    else
      let count_a = calc_primes_count sieve a and count_b = calc_primes_count sieve b in
      count_a + count_b >= c
  in
  let answer = if result then "YES" else "NO" in
  let () = Printf.fprintf outp "%s\n" answer in
(*   let () = Printf.fprintf outp "Val A: %d; Val B: %d\n" a b in
  let () = Printf.fprintf outp "Count A: %d; Count B: %d\n" count_a count_b in
 *)  ();;

let () =
  let inp = std_in and outp = std_out in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  (* let primlimit = 20 in *)
  let primlimit = 31623 in
  let sieve = create_sieve primlimit in
  let state = (inp, outp, sieve) in
  repeat solution state tests_count;;

module Stdlib = Pervasives
let std_in = Stdlib.stdin and std_out = Stdlib.stdout;;
(* let std_in = Stdlib.open_in "input.txt" and std_out = Stdlib.stdout;; *)
let identity x = x;;
let tup3 x1 x2 x3 = (x1, x2, x3);;

let rec repeat f state = function
  | 0 -> ()
  | n ->
    let () = f state in
    repeat f state (n - 1);;

type sieve = int list * int array * int;; (* primes, ancestors, primlimit *)

let create_sieve n =
  let primes = Array.make (n + 1) 0 and primes_count = ref 0 and ancs = Array.make (n + 1) 0 in
  let () =
    for i = 2 to n do
      begin
        if ancs.(i) = 0 then
          let () = ancs.(i) <- i and () = primes.(!primes_count) <- i in
          incr primes_count
        else ();
        let j = ref 0 in
        while !j < !primes_count && i * primes.(!j) <= n && primes.(!j) <= ancs.(i) do
          let () = ancs.(i * primes.(!j)) <- primes.(!j) in
          incr j
        done
      end
    done
  in
  ( Array.sub primes 0 !primes_count |> Array.to_list , ancs , n )

let calc_primes_count ((primes, _, _) as sieve) value =
  let rec fact_folder ancs value acc =
    if value = 1 then acc
    else fact_folder ancs (value / ancs.(value)) (acc + 1)
  in
  let rec helper ((_, ancs, primlimit) as sieve) primes value acc =
    match primes with
    | [] -> if value = 1 then acc else acc + 1
    | prime::primes_tail as primes ->
      if value <= primlimit then fact_folder ancs value acc
      else if value mod prime = 0 then helper sieve primes (value / prime) (acc + 1)
      else helper sieve primes_tail value acc
  in
  helper sieve primes value 0

let solution (inp, outp, sieve) =
  let (_primes, _ancs, _primlimit) = sieve in
  let (a, b, c) = let line = input_line inp in Scanf.sscanf line "%d %d %d" tup3 in
  let result =
    if c = 1 then
      if a != b && (a mod b = 0 || b mod a = 0) then true else false
    else
      let count_a = calc_primes_count sieve a and count_b = calc_primes_count sieve b in
      count_a + count_b >= c
  in
  let answer = if result then "YES" else "NO" in
  let () = Printf.fprintf outp "%s\n" answer in
(*   let () = Printf.fprintf outp "Val A: %d; Val B: %d\n" a b in
  let () = Printf.fprintf outp "Count A: %d; Count B: %d\n" count_a count_b in
 *)  ();;

let () =
  let inp = std_in and outp = std_out in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  (* let primlimit = 20 in *)
  let primlimit = 31623 in
  let sieve = create_sieve primlimit in
  let state = (inp, outp, sieve) in
  repeat solution state tests_count;;

module Stdlib = Pervasives
let std_in = Stdlib.stdin and std_out = Stdlib.stdout;;
(* let std_in = Stdlib.open_in "input.txt" and std_out = Stdlib.stdout;; *)
let identity x = x;;
let tup3 x1 x2 x3 = (x1, x2, x3);;

let rec repeat f state = function
  | 0 -> ()
  | n ->
    let () = f state in
    repeat f state (n - 1);;

type sieve = int list * int array * int;; (* primes, ancestors, primlimit *)

let create_sieve n =
  let primes = Array.make (n + 1) 0 and primes_count = ref 0 and ancs = Array.make (n + 1) 0 in
  let () =
    for i = 2 to n do
      begin
        if ancs.(i) = 0 then
          let () = ancs.(i) <- i and () = primes.(!primes_count) <- i in
          incr primes_count
        else ();
        let j = ref 0 in
        while !j < !primes_count && i * primes.(!j) <= n && primes.(!j) <= ancs.(i) do
          let () = ancs.(i * primes.(!j)) <- primes.(!j) in
          incr j
        done
      end
    done
  in
  ( Array.sub primes 0 !primes_count |> Array.to_list , ancs , n )

let calc_primes_count ((primes, _, _) as sieve) value =
  let rec fact_folder ancs value acc =
    if value = 1 then acc
    else fact_folder ancs (value / ancs.(value)) (acc + 1)
  in
  let rec helper ((_, ancs, primlimit) as sieve) primes value acc =
    match primes with
    | [] -> if value = 1 then acc else acc + 1
    | prime::primes_tail as primes ->
      if value <= primlimit then fact_folder ancs value acc
      else if value mod prime = 0 then helper sieve primes (value / prime) (acc + 1)
      else helper sieve primes_tail value acc
  in
  helper sieve primes value 0

let solution (inp, outp, sieve) =
  let (_primes, _ancs, _primlimit) = sieve in
  let (a, b, c) = let line = input_line inp in Scanf.sscanf line "%d %d %d" tup3 in
  let count_a = calc_primes_count sieve a and count_b = calc_primes_count sieve b in
  let result =
    if c = 1 then if a != b && (a mod b = 0 || b mod a = 0) then true else false
    else count_a + count_b >= c
  in
  let () =
    if result then Printf.fprintf outp "YES\n"
    else Printf.fprintf outp "NO\n"
  in
(*   let () = Printf.fprintf outp "Val A: %d; Val B: %d\n" a b in
  let () = Printf.fprintf outp "Count A: %d; Count B: %d\n" count_a count_b in
 *)  ();;

let () =
  let inp = std_in and outp = std_out in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  (* let primlimit = 20 in *)
  let primlimit = 31623 in
  let sieve = create_sieve primlimit in
  let state = (inp, outp, sieve) in
  repeat solution state tests_count;;

(* 6:26 *)

open Printf
open Scanf

let rec gcd a b = if a=0 then b else gcd (b mod a) a

let factor n =
  let rec loop n p ac =
    if n=1 then ac
    else if p*p > n then (n::ac)
    else if n mod p = 0 then loop (n/p) p (p::ac)
    else loop n (p+1) ac
  in
    List.rev (loop n 2 [])

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)
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 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 (a,b) = read_pair () in
    let k = read_int () in

    let g = gcd a b in

    let answer =
      if k = 1 then if a=b then false else g=a || g=b else
	let nfa = List.length (factor a) in
	let nfb = List.length (factor b) in
	let maxmoves = nfa + nfb in
	let minmoves = if a=b then 0 else if g = a || g = b then 1 else 2 in
	if k < minmoves then false
	else if k > maxmoves then false
	else if k = maxmoves then true
	else if k = maxmoves-1 then nfa >= 2 || nfb >=2
	else true
    in
    if answer then printf "YES\n" else printf "NO\n"
  done

module Stdlib = Pervasives
let std_in = Stdlib.stdin and std_out = Stdlib.stdout;;
(* let std_in = Stdlib.open_in "input.txt" and std_out = Stdlib.stdout;; *)
let identity x = x;;
let tup3 x1 x2 x3 = (x1, x2, x3);;

let rec repeat f state = function
  | 0 -> ()
  | n ->
    let () = f state in
    repeat f state (n - 1);;

type sieve = int list * int array * int;; (* primes, ancestors, primlimit *)

let create_sieve n =
  let primes = Array.make (n + 1) 0 and primes_count = ref 0 and ancs = Array.make (n + 1) 0 in
  let () =
    for i = 2 to n do
      begin
        if ancs.(i) = 0 then
          let () = ancs.(i) <- i and () = primes.(!primes_count) <- i in
          incr primes_count
        else ();
        let j = ref 0 in
        while !j < !primes_count && i * primes.(!j) <= n && primes.(!j) <= ancs.(i) do
          let () = ancs.(i * primes.(!j)) <- primes.(!j) in
          incr j
        done
      end
    done
  in
  ( Array.sub primes 0 !primes_count |> Array.to_list , ancs , n )

let calc_primes_count ((primes, _, _) as sieve) value =
  let rec fact_folder ancs value acc =
    if value = 1 then acc
    else fact_folder ancs (value / ancs.(value)) (acc + 1)
  in
  let rec helper ((_, ancs, primlimit) as sieve) primes value acc =
    match primes with
    | [] -> if value = 1 then acc else acc + 1
    | prime::primes_tail as primes ->
      if value <= primlimit then fact_folder ancs value acc
      else if value mod prime = 0 then helper sieve primes (value / prime) (acc + 1)
      else helper sieve primes_tail value acc
  in
  helper sieve primes value 0

let solution (inp, outp, sieve) =
  let (_primes, _ancs, _primlimit) = sieve in
  let (a, b, c) = let line = input_line inp in Scanf.sscanf line "%d %d %d" tup3 in
  let count_a = calc_primes_count sieve a and count_b = calc_primes_count sieve b in
  let result =
    if c mod 2 = 1 && a = b then false
    else
      let add_a = if a > 1 then 1 else 0 in
      let add_b = if b > 1 then 1 else 0 in
      count_a + count_b >= c - add_a - add_b
  in
  let () =
    if result then Printf.fprintf outp "YES\n"
    else Printf.fprintf outp "NO\n"
  in
(*   let () = Printf.fprintf outp "Val A: %d; Val B: %d\n" a b in
  let () = Printf.fprintf outp "Count A: %d; Count B: %d\n" count_a count_b in
 *)  ();;

let () =
  let inp = std_in and outp = std_out in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  (* let primlimit = 20 in *)
  let primlimit = 31623 in
  let sieve = create_sieve primlimit in
  let state = (inp, outp, sieve) in
  repeat solution state tests_count;;

(* 6:26 *)

open Printf
open Scanf

let rec gcd a b = if a=0 then b else gcd (b mod a) a

let factor n =
  let rec loop n p ac =
    if n=1 then ac
    else if p*p > n then (n::ac)
    else if n mod p = 0 then loop (n/p) p (p::ac)
    else loop n (p+1) ac
  in
    List.rev (loop n 2 [])

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)
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 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 (a,b) = read_pair () in
    let k = read_int () in

    let g = gcd a b in

    let answer =
      let nfa = List.length (factor a) in
      let nfb = List.length (factor b) in
      let maxmoves = nfa + nfb in
      let minmoves = if a=b then 0 else if g = a || g = b then 1 else 2 in
      if k < minmoves then false
      else if k > maxmoves then false
      else if k = maxmoves then true
      else if k = maxmoves-1 then nfa >= 2 || nfb >=2
      else true
    in
    if answer then printf "YES\n" else printf "NO\n"
  done


You are given a binary string $$$s$$$ consisting of $$$n$$$ zeros and ones.Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones).Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100".You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$s$$$. The second line of the test case contains $$$n$$$ characters '0' and '1' — the string $$$s$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer: in the first line print one integer $$$k$$$ ($$$1 \le k \le n$$$) — the minimum number of subsequences you can divide the string $$$s$$$ to. In the second line print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le k$$$), where $$$a_i$$$ is the number of subsequence the $$$i$$$-th character of $$$s$$$ belongs to. If there are several answers, you can print any.

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdin "%d " (fun x -> x)
let read_str _ = bscanf Scanning.stdin "%s " (fun x -> x)

let solve() =
    let n = read_int() in
    let s = read_str() in
    let ans_arr = Array.make n 0 in
    let answer = ref 0 in
    let list_0 = Array.make n 0 in
    let list_1 = Array.make n 0 in
    let id_list_0 = ref (-1) in
    let id_list_1 = ref (-1) in
    for i = 0 to n - 1 do
        if s.[i] = '0' then (
            if !id_list_1 = -1 then (
                answer := !answer + 1;
                ans_arr.(i) <- !answer;
            ) else (
                ans_arr.(i) <- list_1.(!id_list_1);
                id_list_1 := !id_list_1 - 1;
            );
            id_list_0 := !id_list_0 + 1;
            list_0.(!id_list_0) <- ans_arr.(i);
        ) else (
            if !id_list_0 = -1 then (
                answer := !answer + 1;
                ans_arr.(i) <- !answer;
            ) else (
                ans_arr.(i) <- list_0.(!id_list_0);
                id_list_0 := !id_list_0 - 1;
            );
            id_list_1 := !id_list_1 + 1;
            list_1.(!id_list_1) <- ans_arr.(i);
        )
    done;
    printf "%d\n" !answer;
    for i = 0 to n - 1 do
        printf "%d " ans_arr.(i)
    done;
    printf "\n"

let () =
    let cases = read_int() in
    for case = 1 to cases do
        solve()
    done


There are $$$n$$$ pieces of tangerine peel, the $$$i$$$-th of them has size $$$a_i$$$. In one step it is possible to divide one piece of size $$$x$$$ into two pieces of positive integer sizes $$$y$$$ and $$$z$$$ so that $$$y + z = x$$$.You want that for each pair of pieces, their sizes differ strictly less than twice. In other words, there should not be two pieces of size $$$x$$$ and $$$y$$$, such that $$$2x \le y$$$. What is the minimum possible number of steps needed to satisfy the condition?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains the integer $$$n$$$ ($$$1 \le n \le 100$$$). Then one line follows, containing $$$n$$$ integers $$$a_1 \le a_2 \le \ldots \le a_n$$$ ($$$1 \le a_i \le 10^7$$$).

For each test case, output a single line containing the minimum number of steps.

NoteIn the first test case, we initially have a piece of size $$$1$$$, so all final pieces must have size $$$1$$$. The total number of steps is: $$$0 + 1 + 2 + 3 + 4 = 10$$$.In the second test case, we have just one piece, so we don't need to do anything, and the answer is $$$0$$$ steps.In the third test case, one of the possible cut options is: $$$600,\ 900,\ (600 | 700),\ (1000 | 1000),\ (1000 | 1000 | 550)$$$. You can see this option in the picture below. The maximum piece has size $$$1000$$$, and it is less than $$$2$$$ times bigger than the minimum piece of size $$$550$$$. $$$4$$$ steps are done. We can show that it is the minimum possible number of steps.

open Printf
open Scanf

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

let () =
        let t = read_int () in
        for ct = 1 to t do
                let n = read_int() in
                let a = Array.init n (fun i->read_int() ) in
                let min in_a = Array.fold_left (fun a b -> if a < b then a else b) in_a.(0) in_a in
                let mina = min a in
                let sum in_a = Array.fold_left (fun a b -> a+b) 0 in_a in

                let b = Array.map (fun x -> (x+2*mina-2) / (2*mina-1) - 1 ) a in

                printf "%d\n" (sum b)
        done;


Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai.Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7).To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7).

The first line contains an integer q representing the number of queries (1 ≤ q ≤ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≤ f ≤ n ≤ 105).

For each query, output a single integer in a line corresponding to the answer of each query.

NoteFor first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1].For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.

open Printf
open Scanf

let m = 1_000_000_007L

let maxn = 100_000

let factor n =
  (* return a list (in decreasing order) of the primes that divide n *)
  let rec loop n p prev ac =
    if n=1 then ac
    else if p*p > n then (if n=prev then ac else (n::ac))
    else if n mod p = 0 then loop (n/p) p p (if p=prev then ac else (p::ac))
    else loop n (p+1) 0 ac
  in
    loop n 2 0 []

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

let factorial = Array.make (maxn+1) 1L
let invfact = Array.make (maxn+1) 1L

let inv a =
  let rec egcd a b =
    (* given a and b this returns (x, y, g) where g = gcd(a,b) and ax+by=g  *)
    if a = 0 then (0,1,b) else
      let (x,y,g) = egcd (b mod a) a in
      (y-(b/a)*x, x, g)
  in
  let mint = Int64.to_int m in
  let (x,_,_) = egcd a mint in
  if x >= 0 then x else x+mint

let init_factorials () =
  for i=1 to maxn do
    let ii = Int64.of_int i in
    factorial.(i) <- factorial.(i-1) ** ii;
    invfact.(i) <- invfact.(i-1) ** (Int64.of_int (inv i))
  done

let pirates n f =
  (* this is just n-1 choose f-1. *)
  (* we assume that f>=1 and n>=f *)
  factorial.(n-1) ** invfact.(f-1) ** invfact.(n-f)

let solve (n,f) =
  let factors = Array.of_list (factor n) in
  let nprimes = Array.length factors in
  let rec loop i n =
    if n < f then 0L
    else if i=nprimes then pirates n f
    else (loop (i+1) n) -- (loop (i+1) (n/factors.(i)))
  in
  loop 0 n

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = let u = read_int() in (u,read_int())
let () =
  let q = read_int () in
  init_factorials ();
  for i=0 to q-1 do
    printf "%Ld\n" (solve (read_pair()))
  done

open Printf
open Scanf

let m = 1_000_000_007L

let maxn = 100_000

let factor n =
  (* return a list (in decreasing order) of the primes that divide n *)
  let rec loop n p prev ac =
    if n=1 then ac
    else if p*p > n then (if n=prev then ac else (n::ac))
    else if n mod p = 0 then loop (n/p) p p (if p=prev then ac else (p::ac))
    else loop n (p+1) 0 ac
  in
    loop n 2 0 []

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

let factorial = Array.make (maxn+1) 1L
let invfact = Array.make (maxn+1) 1L

let inv a =
  let rec egcd a b =
    (* given a and b this returns (x, y, g) where g = gcd(a,b) and ax+by=g  *)
    if a = 0 then (0,1,b) else
      let (x,y,g) = egcd (b mod a) a in
      (y-(b/a)*x, x, g)
  in
  let mint = Int64.to_int m in
  let (x,_,_) = egcd a mint in
  (x+mint) mod mint

let init_factorials () =
  for i=1 to maxn do
    let ii = Int64.of_int i in
    factorial.(i) <- factorial.(i-1) ** ii;
    invfact.(i) <- invfact.(i-1) ** (Int64.of_int (inv i))
  done

let pirates n f =
  (* this is just n-1 choose f-1. *)
  (* we assume that f>=1 and n>=f *)
  factorial.(n-1) ** invfact.(f-1) ** invfact.(n-f)

let solve (n,f) =
  let factors = Array.of_list (factor n) in
  let nprimes = Array.length factors in
  let rec loop i n =
    if n < f then 0L
    else if i=nprimes then pirates n f
    else (loop (i+1) n) -- (loop (i+1) (n/factors.(i)))
  in
  loop 0 n

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair () = let u = read_int() in (u,read_int())
let () =
  let q = read_int () in
  init_factorials ();
  for i=0 to q-1 do
    printf "%Ld\n" (solve (read_pair()))
  done


Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections.  Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars. 2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).

The first line of input contains a single integer q (1 ≤ q ≤ 1 000). The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u. 1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line.

For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.

NoteIn the example testcase:Here are the intersections used:    Intersections on the path are 3, 1, 2 and 4.  Intersections on the path are 4, 2 and 1.  Intersections on the path are only 3 and 6.  Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94.  Intersections on the path are 6, 3 and 1.  Intersections on the path are 3 and 7. Passing fee of the road between them is 0.  Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).

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

open Printf
open Scanf

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

let () =
  let q = read_int () in

  let h = Hashtbl.create 10 in

  let get t =
    try Hashtbl.find h t with Not_found -> 0L
  in

  let increment t w =
    Hashtbl.replace h t ((get t) ++ w)
  in

  let add_cost u v w =
    let rec loop u v = if u <> v then
	let (u,v) = (max u v, min u v) in
	increment u w;
	loop (u//2L) v
    in
    loop u v
  in

  let compute_cost u v =
    let rec loop u v ac = if u = v then ac else
	let (u,v) = (max u v, min u v) in
	loop (u//2L) v (ac ++ (get u))
    in
    loop u v 0L
  in

  for op = 1 to q do
    match read_int() with
      | 1 ->
	let u = read_long() in
	let v = read_long() in
	let w = read_long() in
	add_cost u v w
      | 2 ->
	let u = read_long() in
	let v = read_long() in
	printf "%Ld\n" (compute_cost u v)
      | _ -> failwith "bad input"
  done


Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks.Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair.Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".

The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.

Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.

NoteIn the first sample you should remove two letters b.In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix 27 27 true in
  let res = ref 0 in
  let c2i c = Char.code c - Char.code 'a' in
    for i = 1 to k do
      let t = Scanf.bscanf sb "%s " (fun s -> s) in
	a.(c2i t.[0]).(c2i t.[1]) <- false;
	a.(c2i t.[1]).(c2i t.[0]) <- false;
    done;
    let prev = ref 26 in
    let prevc = ref 0 in
    let cur = ref (c2i s.[0]) in
    let curc = ref 1 in
      for i = 1 to String.length s - 1 do
	let c = c2i s.[i] in
(*Printf.printf "asd %d: %d %d %d %d  %d\n" c !prev !prevc !cur !curc !res;*)
	  if c = !prev && not a.(!prev).(!cur) then (
	    let t = !cur
	    and tc = !curc in
	      cur := !prev;
	      curc := !prevc;
	      prev := t;
	      prevc := tc;
	      incr curc;
	  ) else if !curc = 0 then (
	    cur := c;
	    curc := 1;
	  ) else if c = !cur then (
	    incr curc;
	  ) else (
	    if not a.(!prev).(!cur) then (
	      if !prevc < !curc
	      then res := !res + !prevc
	      else res := !res + !curc
	    );
	    prev := !cur;
	    prevc := !curc;
	    cur := c;
	    curc := 1;
	  )
      done;
      if not a.(!prev).(!cur) then (
	if !prevc < !curc
	then res := !res + !prevc
	else res := !res + !curc
      );
      Printf.printf "%d\n" !res;

let main () =
    let s = read_line () in
    let k = read_int () in
    let forbid = Array.create_matrix 26 26 true in
    for i = 1 to k do
        let c = read_line () in
        let a1 = int_of_char c.[0] - int_of_char 'a' in
        let a2 = int_of_char c.[1] - int_of_char 'a' in
        forbid.(a1).(a2) <- false;
        forbid.(a2).(a1) <- false
    done;
    let rec loop arr i =
        if i < 0 then arr else (
            let arr2 = Array.copy arr in
            let c = int_of_char s.[i] - int_of_char 'a' in
            arr2.(c) <- 1;
            for j = 0 to 25 do
                if arr.(j) >= 0 && forbid.(j).(c) then (
                    arr2.(c) <- max arr2.(c) (arr.(j) + 1)
                )
            done;
            loop arr2 (i - 1)
        )
    in
    let arr = loop (Array.create 26 (-1)) (String.length s - 1) in
    let ans = String.length s - Array.fold_left max 0 arr in
    Printf.printf "%d\n" ans
;;
main ()

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix 26 26 true in
  let res = ref 0 in
  let c2i c = Char.code c - Char.code 'a' in
    for i = 1 to k do
      let t = Scanf.bscanf sb "%s " (fun s -> s) in
	a.(c2i t.[0]).(c2i t.[1]) <- false;
	a.(c2i t.[1]).(c2i t.[0]) <- false;
    done;
    let prev = ref (c2i s.[0]) in
    let prevc = ref 1 in
    let cur = ref 0 in
    let curc = ref 0 in
      for i = 1 to String.length s - 1 do
	let c = c2i s.[i] in
(*Printf.printf "asd %d: %d %d %d %d\n" c !prev !prevc !cur !curc;*)
	  if c = !prev then (
	    let t = !cur
	    and tc = !curc in
	      cur := !prev;
	      curc := !prevc;
	      prev := t;
	      prevc := tc;
	      incr curc;
	  ) else if !curc = 0 then (
	    cur := c;
	    curc := 1;
	  ) else if c = !cur then (
	    incr curc;
	  ) else (
	    if not a.(!prev).(!cur) then (
	      if !prevc < !curc
	      then res := !res + !prevc
	      else res := !res + !curc
	    );
	    prev := !cur;
	    prevc := !curc;
	    cur := c;
	    curc := 1;
	  )
      done;
      if not a.(!prev).(!cur) then (
	if !prevc < !curc
	then res := !res + !prevc
	else res := !res + !curc
      );
      Printf.printf "%d\n" !res;

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix 26 26 true in
  let res = ref 0 in
  let c2i c = Char.code c - Char.code 'a' in
    for i = 1 to k do
      let t = Scanf.bscanf sb "%s " (fun s -> s) in
	a.(c2i t.[0]).(c2i t.[1]) <- false;
	a.(c2i t.[1]).(c2i t.[0]) <- false;
    done;
    let prev = ref (c2i s.[0]) in
    let prevc = ref 1 in
    let cur = ref 0 in
    let curc = ref 0 in
      for i = 1 to String.length s - 1 do
	let c = c2i s.[i] in
(*Printf.printf "asd %d: %d %d %d %d\n" c !prev !prevc !cur !curc;*)
	  if c = !prev then (
	    incr prevc;
	  ) else if !curc = 0 then (
	    cur := c;
	    curc := 1;
	  ) else if c = !cur then (
	    incr curc;
	  ) else (
	    if not a.(!prev).(!cur) then (
	      if !prevc < !curc
	      then res := !res + !prevc
	      else res := !res + !curc
	    );
	    prev := !cur;
	    prevc := !curc;
	    cur := c;
	    curc := 0;
	  )
      done;
      if not a.(!prev).(!cur) then (
	if !prevc < !curc
	then res := !res + !prevc
	else res := !res + !curc
      );
      Printf.printf "%d\n" !res;

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix 26 26 true in
  let res = ref 0 in
  let c2i c = Char.code c - Char.code 'a' in
    for i = 1 to k do
      let t = Scanf.bscanf sb "%s " (fun s -> s) in
	a.(c2i t.[0]).(c2i t.[1]) <- false;
	a.(c2i t.[1]).(c2i t.[0]) <- false;
    done;
    let prev = ref (c2i s.[0]) in
      for i = 1 to String.length s - 1 do
	let c = c2i s.[i] in
	  if a.(!prev).(c) then (
	    prev := c
	  ) else (
	    incr res
	  )
      done;
      Printf.printf "%d\n" !res;

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix 26 26 true in
  let res = ref 0 in
  let c2i c = Char.code c - Char.code 'a' in
    for i = 1 to k do
      let t = Scanf.bscanf sb "%s " (fun s -> s) in
	a.(c2i t.[0]).(c2i t.[1]) <- false;
	a.(c2i t.[1]).(c2i t.[0]) <- false;
    done;
    let prev = ref (c2i s.[0]) in
    let prevc = ref 1 in
    let cur = ref 0 in
    let curc = ref 0 in
      for i = 1 to String.length s - 1 do
	let c = c2i s.[i] in
(*Printf.printf "asd %d: %d %d %d %d\n" c !prev !prevc !cur !curc;*)
	  if c = !prev then (
	    incr prevc;
	  ) else if !curc = 0 then (
	    cur := c;
	    curc := 1;
	  ) else if c = !cur then (
	    incr curc;
	  ) else (
	    if not a.(!prev).(!cur) then (
	      if !prevc < !curc
	      then res := !res + !prevc
	      else res := !res + !curc
	    );
	    prev := !cur;
	    prevc := !curc;
	    cur := c;
	    curc := 1;
	  )
      done;
      if not a.(!prev).(!cur) then (
	if !prevc < !curc
	then res := !res + !prevc
	else res := !res + !curc
      );
      Printf.printf "%d\n" !res;

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix 26 26 true in
  let res = ref 0 in
  let c2i c = Char.code c - Char.code 'a' in
    for i = 1 to k do
      let t = Scanf.bscanf sb "%s " (fun s -> s) in
	a.(c2i t.[0]).(c2i t.[1]) <- false;
	a.(c2i t.[1]).(c2i t.[0]) <- false;
    done;
    let prev = ref (c2i s.[0]) in
    let prevc = ref 1 in
    let cur = ref 0 in
    let curc = ref 0 in
      for i = 1 to String.length s - 1 do
	let c = c2i s.[i] in
(*Printf.printf "asd %d: %d %d %d %d  %d\n" c !prev !prevc !cur !curc !res;*)
	  if c = !prev && not a.(!prev).(!cur) then (
	    let t = !cur
	    and tc = !curc in
	      cur := !prev;
	      curc := !prevc;
	      prev := t;
	      prevc := tc;
	      incr curc;
	  ) else if !curc = 0 then (
	    cur := c;
	    curc := 1;
	  ) else if c = !cur then (
	    incr curc;
	  ) else (
	    if not a.(!prev).(!cur) then (
	      if !prevc < !curc
	      then res := !res + !prevc
	      else res := !res + !curc
	    );
	    prev := !cur;
	    prevc := !curc;
	    cur := c;
	    curc := 1;
	  )
      done;
      if not a.(!prev).(!cur) then (
	if !prevc < !curc
	then res := !res + !prevc
	else res := !res + !curc
      );
      Printf.printf "%d\n" !res;


Vanya and his friend Vova play a computer game where they need to destroy n monsters to pass a level. Vanya's character performs attack with frequency x hits per second and Vova's character performs attack with frequency y hits per second. Each character spends fixed time to raise a weapon and then he hits (the time to raise the weapon is 1 / x seconds for the first character and 1 / y seconds for the second one). The i-th monster dies after he receives ai hits. Vanya and Vova wonder who makes the last hit on each monster. If Vanya and Vova make the last hit at the same time, we assume that both of them have made the last hit.

The first line contains three integers n,x,y (1 ≤ n ≤ 105, 1 ≤ x, y ≤ 106) — the number of monsters, the frequency of Vanya's and Vova's attack, correspondingly. Next n lines contain integers ai (1 ≤ ai ≤ 109) — the number of hits needed do destroy the i-th monster.

Print n lines. In the i-th line print word "Vanya", if the last hit on the i-th monster was performed by Vanya, "Vova", if Vova performed the last hit, or "Both", if both boys performed it at the same time.

NoteIn the first sample Vanya makes the first hit at time 1 / 3, Vova makes the second hit at time 1 / 2, Vanya makes the third hit at time 2 / 3, and both boys make the fourth and fifth hit simultaneously at the time 1.In the second sample Vanya and Vova make the first and second hit simultaneously at time 1.

let (@@) f a = f a
let (|>) x f = f x

let () =
  let open Int64 in
  let ( * ) = mul in
  let ( / ) = div in
  let ( + ) = add in
  let ( - ) = sub in
  let gcd a b =
    let rec gcd a b =
      if rem a b = zero then b
      else gcd b (rem a b)
    in
    gcd (max a b) (min a b)
  in
  Scanf.scanf "%d %Ld %Ld" @@ fun n x y ->
  let gcd = gcd x y in
  let x = x / gcd and y = y / gcd in
  let lcm = x * y in
  let solve a =
    let a = rem a (x + y) in
    let dich f r t =
      let rec dich l r =
        if r - l < (of_int 2) then r
        else
          let p = l + (r - l) / (of_int 2) in
          let f = f p in
          if f < t then dich p r
          else dich l p
      in
      if f zero = t then zero
      else dich zero r
   in
   let r = dich (fun c -> c / x + c / y) lcm a in
   match rem r x = zero, rem r y = zero with
   | true, true -> "Both"
   | true, _ -> "Vova"
   | _, true -> "Vanya"
   | false, false -> assert false
  in
  for i = 1 to n do
    print_endline (Scanf.scanf " %Ld" @@ solve)
  done


Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number).The seller can obtain array b from array a if the following conditions hold: bi > 0; 0 ≤ ai - bi ≤ k for all 1 ≤ i ≤ n.Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).

The first line contains two integers n and k (1 ≤ n ≤ 3·105; 1 ≤ k ≤ 106). The second line contains n integers ai (1 ≤ ai ≤ 106) — array a.

In the single line print a single number — the maximum possible beauty of the resulting array.

NoteIn the first sample we can obtain the array:3 6 9 12 12 15In the second sample we can obtain the next array:7 21 49 14 77

let (|>) x f = f x ;;
let (@@) f x = f x ;;

let ident x = x ;;

let char_to_int x = int_of_char x - int_of_char '0' ;;
let int_to_char x = char_of_int (x + int_of_char '0') ;;

module List = struct
  include ListLabels ;;

  let rec iteri i f = function
    | [] -> ()
    | a::l -> f i a; iteri (i + 1) f l

  let iteri ~f l = iteri 0 f l
end
;;

module Array = struct
  include ArrayLabels ;;

  let reduce ~f xs =
    let acc = ref xs.(0) in
    for i = 1 to length xs - 1 do
      acc := f !acc xs.(i)
    done;
    !acc
  ;;
end
;;

module String = struct
  include StringLabels ;;

  let map_to_list ~f ss =
    let res = ref [] in
    iter ss ~f:(fun c -> res := f c :: !res);
    List.rev !res
  ;;

  let of_char_list ss =
    let res = String.create @@ List.length ss in
    List.iteri ss ~f:(fun i c -> res.[i] <- c);
    res
  ;;

  let iteri ~f a =
    for i = 0 to length a - 1 do f i (unsafe_get a i) done
  ;;
end
;;

module Float = struct
  let (+) = (+.)
  let (-) = (-.)
  let ( * ) = ( *. )
  let (/) = (/.)
end
;;

module Int64 = struct
  include Int64
  let (+) = add
  let (-) = sub
  let ( * ) = mul
  let (/) = div
end
;;

let fold_for ?(skip=1) min max ~init ~f =
  let acc = ref init in
  let cur = ref min in
  while !cur < max do
    acc := f !acc !cur;
    cur := !cur + skip;
  done;
  !acc
;;

let iter_for ?(skip=1) min max ~f =
  let cur = ref min in
  while !cur < max do
    f !cur;
    cur := !cur + skip;
  done
;;

let () =
  let n, k = Scanf.scanf "%d %d " (fun n k -> n, k) in
  let arr = Array.init n ~f:(fun _ -> Scanf.scanf "%d " ident) in
  let sum = Array.create 2_000_001 0 in
  Array.iter arr ~f:(fun x -> sum.(x) <- sum.(x) + 1);
  let mx = Array.reduce ~f:max arr in
  let mn = Array.reduce ~f:min arr in
  iter_for 1 (Array.length sum - 1) ~f:(fun i -> sum.(i) <- sum.(i) + sum.(i - 1));
  Printf.printf "%d\n" @@ fold_for 2 (mn + 1) ~init:1 ~f:(fun acc_mx div ->
    let in_n = fold_for ~skip:div div (mx + 1)
      ~init:0 ~f:(fun acc i -> acc + sum.(min (i + k) (i + div - 1)) - sum.(i - 1)) in
    if in_n = n then div else acc_mx)
;;


T is a complete binary tree consisting of n vertices. It means that exactly one vertex is a root, and each vertex is either a leaf (and doesn't have children) or an inner node (and has exactly two children). All leaves of a complete binary tree have the same depth (distance from the root). So n is a number such that n + 1 is a power of 2.In the picture you can see a complete binary tree with n = 15.  Vertices are numbered from 1 to n in a special recursive way: we recursively assign numbers to all vertices from the left subtree (if current vertex is not a leaf), then assign a number to the current vertex, and then recursively assign numbers to all vertices from the right subtree (if it exists). In the picture vertices are numbered exactly using this algorithm. It is clear that for each size of a complete binary tree exists exactly one way to give numbers to all vertices. This way of numbering is called symmetric.You have to write a program that for given n answers q queries to the tree.Each query consists of an integer number ui (1 ≤ ui ≤ n) and a string si, where ui is the number of vertex, and si represents the path starting from this vertex. String si doesn't contain any characters other than 'L', 'R' and 'U', which mean traverse to the left child, to the right child and to the parent, respectively. Characters from si have to be processed from left to right, considering that ui is the vertex where the path starts. If it's impossible to process a character (for example, to go to the left child of a leaf), then you have to skip it. The answer is the number of vertex where the path represented by si ends.For example, if ui = 4 and si = «UURL», then the answer is 10.

The first line contains two integer numbers n and q (1 ≤ n ≤ 1018, q ≥ 1). n is such that n + 1 is a power of 2. The next 2q lines represent queries; each query consists of two consecutive lines. The first of these two lines contains ui (1 ≤ ui ≤ n), the second contains non-empty string si. si doesn't contain any characters other than 'L', 'R' and 'U'. It is guaranteed that the sum of lengths of si (for each i such that 1 ≤ i ≤ q) doesn't exceed 105.

Print q numbers, i-th number must be the answer to the i-th query.

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

let f x = Int64.logand x (Int64.neg x)

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let q = Scanf.bscanf sb "%d " (fun s -> s) in
    for _c = 1 to q do
      let u = Scanf.bscanf sb "%Ld " (fun s -> s) in
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
      let u = ref u in
	for i = 0 to String.length s - 1 do
	  match s.[i] with
	    | 'U' ->
		let d = f !u in
		let v1 = !u -| d
		and v2 = !u +| d in
		  if v1 <= n && f v1 = 2L *| d
		  then u := v1
		  else if v2 <= n && f v2 = 2L *| d
		  then u := v2
	    | 'L' ->
		let d = f !u in
		  if d > 1L
		  then u := !u -| d /| 2L;
	    | 'R' ->
		let d = f !u in
		  if d > 1L
		  then u := !u +| d /| 2L;
	    | _ -> assert false
	done;
	Printf.printf "%Ld\n" !u
    done;


Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.The decreasing coefficient of permutation p1, p2, ..., pn is the number of such i (1 ≤ i < n), that pi > pi + 1.You have numbers n and k. Your task is to print the permutation of length n with decreasing coefficient k.

The single line contains two space-separated integers: n, k (1 ≤ n ≤ 105, 0 ≤ k < n) — the permutation length and the decreasing coefficient.

In a single line print n space-separated integers: p1, p2, ..., pn — the permutation of length n with decreasing coefficient k.  If there are several permutations that meet this condition, print any of them. It is guaranteed that the permutation with the sought parameters exists.

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

let() =
  let n = read_int 0 in
  let k = read_int 0 in
  for i = 1 to n - k - 1 do
    Printf.printf "%d " i
  done;

  Printf.printf "%d " n;
  for i = n - k + 1 to n do
     Printf.printf "%d " (n+n-k-i);
  done;


This is an interactive problem.Homer likes arrays a lot and he wants to play a game with you. Homer has hidden from you a permutation $$$a_1, a_2, \dots, a_n$$$ of integers $$$1$$$ to $$$n$$$. You are asked to find any index $$$k$$$ ($$$1 \leq k \leq n$$$) which is a local minimum. For an array $$$a_1, a_2, \dots, a_n$$$, an index $$$i$$$ ($$$1 \leq i \leq n$$$) is said to be a local minimum if $$$a_i < \min\{a_{i-1},a_{i+1}\}$$$, where $$$a_0 = a_{n+1} = +\infty$$$. An array is said to be a permutation of integers $$$1$$$ to $$$n$$$, if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.Initially, you are only given the value of $$$n$$$ without any other information about this permutation.At each interactive step, you are allowed to choose any $$$i$$$ ($$$1 \leq i \leq n$$$) and make a query with it. As a response, you will be given the value of $$$a_i$$$. You are asked to find any index $$$k$$$ which is a local minimum after at most $$$100$$$ queries.

NoteIn the example, the first line contains an integer $$$5$$$ indicating that the length of the array is $$$n = 5$$$.The example makes five "?" queries, after which we conclude that the array is $$$a = [3,2,1,4,5]$$$ and $$$k = 3$$$ is local minimum.

open Printf

type dir = DOWN | UP | LMIN | LMAX

let read_int _ = int_of_string (read_line())

let () =
  let n = read_int() in

  let probe i =
    if i=0 then n+1 else if i=n+1 then n+1 else (
      printf "? %d\n%!" i;
      read_int()
    )
  in

  let evaluate i =
    let a = probe (i-1) in
    let b = probe i in
    let c = probe (i+1) in
    match (a<b, b<c) with
      | (true,true) -> UP
      | (false,true) -> LMIN
      | (true,false) -> LMAX
      | _ -> DOWN
  in

  let rec bsearch lo hi =
    (* lo is a DOWN, hi is an UP, also lo + 2 <= hi *)
    if lo + 2 > hi then failwith "range bad";
    let m = (lo+hi)/2 in
    let x = evaluate m in
    match x with
      | UP | LMAX -> bsearch lo m
      | DOWN -> bsearch m hi
      | LMIN -> m
  in

  let answer =
    if n=1 then 1 else
      match (evaluate 1, evaluate n) with
	| (LMIN,_) -> 1
	| (_,LMIN) -> n
	| (DOWN,UP) -> bsearch 1 n
	| _ -> failwith "something wrong"

  in
  printf "! %d\n%!" answer


Tom is interested in power consumption of his favourite laptop. His laptop has three modes. In normal mode laptop consumes P1 watt per minute. T1 minutes after Tom moved the mouse or touched the keyboard for the last time, a screensaver starts and power consumption changes to P2 watt per minute. Finally, after T2 minutes from the start of the screensaver, laptop switches to the "sleep" mode and consumes P3 watt per minute. If Tom moves the mouse or touches the keyboard when the laptop is in the second or in the third mode, it switches to the first (normal) mode. Tom's work with the laptop can be divided into n time periods [l1, r1], [l2, r2], ..., [ln, rn]. During each interval Tom continuously moves the mouse and presses buttons on the keyboard. Between the periods Tom stays away from the laptop. Find out the total amount of power consumed by the laptop during the period [l1, rn].

The first line contains 6 integer numbers n, P1, P2, P3, T1, T2 (1 ≤ n ≤ 100, 0 ≤ P1, P2, P3 ≤ 100, 1 ≤ T1, T2 ≤ 60). The following n lines contain description of Tom's work. Each i-th of these lines contains two space-separated integers li and ri (0 ≤ li < ri ≤ 1440, ri < li + 1 for i < n), which stand for the start and the end of the i-th period of work.

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

let n = read_int ();;
let p1 = read_int ();;
let p2 = read_int ();;
let p3 = read_int ();;
let t1 = read_int ();;
let t2 = read_int ();;

let power_of_period x =
	if x > t1 + t2 then p1 * t1 + p2 * t2 + p3 * (x - t1 - t2)
	else if x > t1 then p1 * t1 + p2 * (x - t1)
	else x * p1

let ans = ref 0;;
let last = ref (-1);;

for i = 1 to n do
	let (l, r) = Scanf.scanf "%d %d " (fun x y -> (x, y)) in begin
		ans := !ans + power_of_period (l - (if !last = -1 then l else !last)) + (r - l) * p1;
		last := r;
	end
done;;

Printf.printf "%d\n" !ans;;

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

let () =
  let n = read_int 0 in
  let p1 = read_int 0 in
  let p2 = read_int 0 in
  let p3 = read_int 0 in
  let t1 = read_int 0 in
  let t2 = read_int 0 in
  let rec go i t s =
    if i >= n then
      s
    else
      let l = read_int 0 in
      let r = read_int 0 in
      let _,s' = if i > 0 then List.fold_left (fun (t,s) (pi,ti) ->
          let d = min ti (l-t) in t+d, s+d*pi
        ) (t,s) [p1,t1; p2,t2; p3,max_int] else t,s in
      go (i+1) r (s'+(r-l)*p1)
  in
  Printf.printf "%d\n" (go 0 0 0)


While walking down the street Vanya saw a label "Hide&Seek". Because he is a programmer, he used & as a bitwise AND for these two words represented as a integers in base 64 and got new word. Now Vanya thinks of some string s and wants to know the number of pairs of words of length |s| (length of s), such that their bitwise AND is equal to s. As this number can be large, output it modulo 109 + 7.To represent the string as a number in numeral system with base 64 Vanya uses the following rules:  digits from '0' to '9' correspond to integers from 0 to 9;  letters from 'A' to 'Z' correspond to integers from 10 to 35;  letters from 'a' to 'z' correspond to integers from 36 to 61;  letter '-' correspond to integer 62;  letter '_' correspond to integer 63.

The only line of the input contains a single word s (1 ≤ |s| ≤ 100 000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'.

Print a single integer — the number of possible pairs of words, such that their bitwise AND is equal to string s modulo 109 + 7.

NoteFor a detailed definition of bitwise AND we recommend to take a look in the corresponding article in Wikipedia.In the first sample, there are 3 possible solutions:   z&_ = 61&63 = 61 = z  _&z = 63&61 = 61 = z  z&z = 61&61 = 61 = z

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

let _ =
  let mm = 1000000007 in
  let mmm = Int64.of_int mm in
  let mmod n =
    let r = Int64.rem n mmm in
      if r < 0L
      then r +| mmm
      else r
  in
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let n = String.length s in
  let res = ref 1L in
    for i = 0 to n - 1 do
      let c = s.[i] in
      let d =
	match c with
	  | '0'..'9' -> Char.code c - Char.code '0'
	  | 'A'..'Z' -> Char.code c - Char.code 'A' + 10
	  | 'a'..'z' -> Char.code c - Char.code 'a' + 36
	  | '-' -> 62
	  | '_' -> 63
	  | _ -> assert false
      in
	for i = 0 to 5 do
	  if d land (1 lsl i) = 0 then (
	    res := mmod (!res *| 3L)
	  )
	done;
    done;
    Printf.printf "%Ld\n" !res

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 rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac + (f i))
let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)

let () =
  let s = read_string () in
  let n = String.length s in

  let rec bitcount x = if x=0 then 0 else (x land 1) + bitcount (x/2) in

  let zeros = sum 0 (n-1) (fun i ->
    let c = s.[i] in
    let numb =
      if '0' <= c && c <= '9' then (int_of_char c) - (int_of_char '0')
      else if 'A' <= c && c <= 'Z' then 10 + (int_of_char c) - (int_of_char 'A')
      else if 'a' <= c && c <= 'z' then 36 + (int_of_char c) - (int_of_char 'a')
      else if c = '-' then 62
      else if c = '_' then 63
      else failwith "bad input"
    in
    6 - (bitcount numb)
  ) 0 in

  let m = 1_000_000_007L in

  let rec ( ^ ) e p =
    if p=0 then 1L else
      let a = e^(p/2) in
      let a2 = (a**a) %% m in
      if (p land 1)=0 then a2 else (e**a2) %% m
  in

  printf "%Ld\n" (3L ^ zeros)


Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himself from the herd as the best cowmputer scientist in all of Bovinia. Kevin knows his submission time for each problem, the number of wrong submissions that he made on each problem, and his total numbers of successful and unsuccessful hacks. Because Codeforces scoring is complicated, Kevin wants you to write a program to compute his final score.Codeforces scores are computed as follows: If the maximum point value of a problem is x, and Kevin submitted correctly at minute m but made w wrong submissions, then his score on that problem is . His total score is equal to the sum of his scores for each problem. In addition, Kevin's total score gets increased by 100 points for each successful hack, but gets decreased by 50 points for each unsuccessful hack.All arithmetic operations are performed with absolute precision and no rounding. It is guaranteed that Kevin's final score is an integer.

The first line of the input contains five space-separated integers m1, m2, m3, m4, m5, where mi (0 ≤ mi ≤ 119) is the time of Kevin's last submission for problem i. His last submission is always correct and gets accepted. The second line contains five space-separated integers w1, w2, w3, w4, w5, where wi (0 ≤ wi ≤ 10) is Kevin's number of wrong submissions on problem i. The last line contains two space-separated integers hs and hu (0 ≤ hs, hu ≤ 20), denoting the Kevin's numbers of successful and unsuccessful hacks, respectively.

Print a single integer, the value of Kevin's final score.

NoteIn the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets  of the points on each problem. So his score from solving problems is . Adding in 10·100 = 1000 points from hacks, his total score becomes 3930 + 1000 = 4930.

let read1 () = Scanf.scanf "%d\n" ( fun x -> x );;
let read2 () = Scanf.scanf "%d %d\n" ( fun x y -> x, y );;
let read3 () = Scanf.scanf "%d %d %d\n" ( fun x y z -> x, y, z);;
let read5 () = Scanf.scanf "%d %d %d %d %d\n" ( fun x y z a b-> x, y, z, a, b);;
let read x = (if x = 0 then Scanf.scanf "%d\n" else Scanf.scanf "%d ") ( fun x -> x );;

let (+$) x y =
  if x > max_int - y then max_int
  else x + y;;

let rec ( *$ ) x y =
  if y = 0 then 0 else
  if y mod 2 = 1 then x *$ (y-1) +$ x
  else let a = x *$ (y/2) in a +$ a;;

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

let licz m w x =
  max (75*x) ( (250 - m)*x - 250*50*w );;

let _ =
  let (a,b,c,d,e) = read5 ()
  and (f,g,h,i,j) = read5 ()
  and (hs, hu) = read2 ()
  in
  let suma = (licz a f 500) + (licz b g 1000) + (licz c h 1500) + (licz d i 2000) + (licz e j 2500) in
  Printf.printf "%d\n" ( suma/250 + (2*hs-hu)*50 ) ;;

let score m' w' x' =
    let m = float m' and w = float w' and x = float x' in
 max (0.3 *. x) ((1. -. m /. 250.) *. x -. 50. *. w)

let main () =
    let (m1,m2,m3,m4,m5,w1,w2,w3,w4,w5,hs,hu) = Scanf.scanf
    "%d %d %d %d %d %d %d %d %d %d %d %d"
    (fun a b c d e f g h i j k l -> (a,b,c,d,e,f,g,h,i,j,k,l)) in
         let sc = score m1 w1 500 +. score m2 w2 1000 +. score m3 w3 1500 +. score m4 w4 2000 +. score m5 w5 2500 +. 100. *. (float hs) -. 50. *. (float hu) in
             Printf.printf "%d" (int_of_float sc);;

main ();;

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> 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 () =
  let m = Array.init 5 (fun _ -> read_int()) in
  let w = Array.init 5 (fun _ -> read_int()) in
  let hs = read_int() in
  let hu = read_int() in

  let x = [|500; 1000; 1500; 2000; 2500|] in

  let score i =
    max ((3*x.(i)) / 10) ((x.(i) / 250) * (250-m.(i)) - 50*w.(i))
  in

  let total_score = (sum 0 4 score) + 100*hs - 50*hu in

  printf "%d\n" total_score


Polycarp was presented with some sequence of integers $$$a$$$ of length $$$n$$$ ($$$1 \le a_i \le n$$$). A sequence can make Polycarp happy only if it consists of different numbers (i.e. distinct numbers).In order to make his sequence like this, Polycarp is going to make some (possibly zero) number of moves.In one move, he can:   remove the first (leftmost) element of the sequence. For example, in one move, the sequence $$$[3, 1, 4, 3]$$$ will produce the sequence $$$[1, 4, 3]$$$, which consists of different numbers.Determine the minimum number of moves he needs to make so that in the remaining sequence all elements are different. In other words, find the length of the smallest prefix of the given sequence $$$a$$$, after removing which all values in the sequence will be unique.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the given sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the given sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print your answer on a separate line — the minimum number of elements that must be removed from the beginning of the sequence so that all remaining elements are different.

NoteThe following are the sequences that will remain after the removal of prefixes:  $$$[1, 4, 3]$$$;  $$$[1]$$$;  $$$[1]$$$;  $$$[6, 5, 4, 3, 2, 1]$$$;  $$$[2, 1]$$$. It is easy to see that all the remaining sequences contain only distinct elements. In each test case, the shortest matching prefix was removed.

let read_int () = Scanf.scanf " %d" (fun x->x);;
let rec read_list = function
| 0 -> []
| n -> (read_int ()) :: read_list (n-1)
;;
let run_case = function
| (n) ->
    let seq = read_list (n) in
    let count_arr = Array.make n 0 in
    let rec get_rsuffix = function
    | [] -> []
    | x::xs ->
        count_arr.(x-1) <- (count_arr.(x-1)+1);
        if (count_arr.(x-1)<2) then x::get_rsuffix (xs)
        else []
    in
    let rsuffix = get_rsuffix (seq) in
    n - List.length (rsuffix)
;;

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

let cases = read_int () in
iter_cases (cases);;

let read_int () = Scanf.scanf " %d" (fun x->x);;
let rec read_list = function
| 0 -> []
| n -> (read_int ()) :: read_list (n-1)
;;
let run_case = function
| (n) ->
    let rseq = List.rev (read_list (n)) in
    let count_arr = Array.make n 0 in
    let rec get_rsuffix = function
    | [] -> []
    | x::xs ->
        count_arr.(x-1) <- (count_arr.(x-1)+1);
        if (count_arr.(x-1)<2) then x::get_rsuffix (xs)
        else []
    in
    let suffix = List.rev (get_rsuffix (rseq)) in
    n - List.length (suffix)
;;

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

let cases = read_int () in
iter_cases (cases);;


The research center Q has developed a new multi-core processor. The processor consists of n cores and has k cells of cache memory. Consider the work of this processor.At each cycle each core of the processor gets one instruction: either do nothing, or the number of the memory cell (the core will write an information to the cell). After receiving the command, the core executes it immediately. Sometimes it happens that at one cycle, multiple cores try to write the information into a single cell. Unfortunately, the developers did not foresee the possibility of resolving conflicts between cores, so in this case there is a deadlock: all these cores and the corresponding memory cell are locked forever. Each of the locked cores ignores all further commands, and no core in the future will be able to record an information into the locked cell. If any of the cores tries to write an information into some locked cell, it is immediately locked.The development team wants to explore the deadlock situation. Therefore, they need a program that will simulate the processor for a given set of instructions for each core within m cycles . You're lucky, this interesting work is entrusted to you. According to the instructions, during the m cycles define for each core the number of the cycle, during which it will become locked. It is believed that initially all cores and all memory cells are not locked.

The first line contains three integers n, m, k (1 ≤ n, m, k ≤ 100). Then follow n lines describing instructions. The i-th line contains m integers: xi1, xi2, ..., xim (0 ≤ xij ≤ k), where xij is the instruction that must be executed by the i-th core at the j-th cycle. If xij equals 0, then the corresponding instruction is «do nothing». But if xij is a number from 1 to k, then the corresponding instruction is «write information to the memory cell number xij». We assume that the cores are numbered from 1 to n, the work cycles are numbered from 1 to m and the memory cells are numbered from 1 to k.

Print n lines. In the i-th line print integer ti. This number should be equal to 0 if the i-th core won't be locked, or it should be equal to the number of the cycle when this core will be locked.

let read_int()=Scanf.scanf " %d" (fun x->x);;
let n=read_int() and m=read_int() and k=read_int();;
let tab=Array.make (k+1) false;;
let temp=Array.make (k+1) 0;;
let block=Array.make (n+1) 0;;
let mat=Array.make_matrix (n+1) (m+1) 0;;

for i=1 to n do
   for j=1 to m do
      mat.(i).(j)<-read_int()
   done
done;;

let init vec i j=
for p=i to j do
   vec.(p)<-0
done;;

for j=1 to m do
   init temp 1 k;
   for i=1 to n do
      if (block.(i)=0)&&(mat.(i).(j)<>0) then temp.(mat.(i).(j))<-temp.(mat.(i).(j))+1
   done;
   for i=1 to n do
      if (block.(i)=0)&&((temp.(mat.(i).(j))>1)||tab.(mat.(i).(j)))&&(mat.(i).(j)<>0) then (block.(i)<-j;tab.(mat.(i).(j))<-true)
   done
done;;

for i=1 to n do
   print_int block.(i);
   if i<>n then print_newline()
done;;

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m 0 in
  let res = Array.make n 0 in
    for i = 0 to n - 1 do
      for j = 0 to m - 1 do
	let k = Scanf.bscanf sb "%d " (fun s -> s) in
	  a.(i).(j) <- k
      done
    done;
    let b = Array.make k false in
      for j = 0 to m - 1 do
	let c = Array.make k 0 in
	  for i = 0 to n - 1 do
	    if res.(i) = 0 && a.(i).(j) <> 0 then (
	      c.(a.(i).(j) - 1) <- c.(a.(i).(j) - 1) + 1
	    )
	  done;
	  for i = 0 to n - 1 do
	    if res.(i) = 0 && a.(i).(j) <> 0 &&
	      (c.(a.(i).(j) - 1) > 1 || b.(a.(i).(j) - 1))
	    then (
	      res.(i) <- j + 1;
	      b.(a.(i).(j) - 1) <- true;
	    )
	  done;
      done;
      for i = 0 to n - 1 do
	Printf.printf "%d\n" res.(i);
      done

let read_int()=Scanf.scanf " %d" (fun x->x);;
let n=read_int() and m=read_int() and k=read_int();;
let tab=Array.make (k+1) false;;
let temp=Array.make (k+1) 0;;
let block=Array.make (n+1) 0;;
let mat=Array.make_matrix (n+1) (m+1) 0;;

for i=1 to n do
   for j=1 to m do
      mat.(i).(j)<-read_int()
   done
done;;

let init vec i j=
for p=i to j do
   vec.(p)<-0
done;;

for j=1 to m do
   init temp 1 k;
   for i=1 to n do
      if (block.(i)=0)&&(mat.(i).(j)<>0) then temp.(mat.(i).(j))<-temp.(mat.(i).(j))+1
   done;
   for i=1 to n do
      if ((block.(i)=0)&&(temp.(mat.(i).(j))>1)||tab.(mat.(i).(j)))&&(mat.(i).(j)<>0) then (block.(i)<-j;tab.(mat.(i).(j))<-true)
   done
done;;

for i=1 to n do
   print_int block.(i);
   if i<>n then print_newline()
done;;

let read_int()=Scanf.scanf " %d" (fun x->x);;
let n=read_int() and m=read_int() and k=read_int();;
let tab=Array.make (k+1) false;;
let temp=Array.make (k+1) 0;;
let block=Array.make (n+1) 0;;
let mat=Array.make_matrix (n+1) (m+1) 0;;

for i=1 to n do
   for j=1 to m do
      mat.(i).(j)<-read_int()
   done
done;;

let init vec i j=
for p=i to j do
   vec.(p)<-0
done;;

for j=1 to m do
   init temp 1 k;
   for i=1 to n do
      if block.(i)=0 then temp.(mat.(i).(j))<-temp.(mat.(i).(j))+1
   done;
   for i=1 to n do
      if (block.(i)=0)&&(temp.(mat.(i).(j))>1)||tab.(mat.(i).(j)) then (block.(i)<-j;tab.(mat.(i).(j))<-true)
   done
done;;

for i=1 to n do
   print_int block.(i);
   if i<>n then print_newline()
done;;