You have an n × m rectangle table, its cells are not initially painted. Your task is to paint all cells of the table. The resulting picture should be a tiling of the table with squares. More formally:  each cell must be painted some color (the colors are marked by uppercase Latin letters);  we will assume that two cells of the table are connected if they are of the same color and share a side; each connected region of the table must form a square. Given n and m, find lexicographically minimum coloring of the table that meets the described properties.

The first line contains two integers, n and m (1 ≤ n, m ≤ 100).

Print lexicographically minimum coloring of the table that meets the described conditions.  One coloring (let's call it X) is considered lexicographically less than the other one (let's call it Y), if:   consider all the table cells from left to right and from top to bottom (first, the first cell in the first row, then the second cell in the first row and so on);  let's find in this order the first cell that has distinct colors in two colorings;  the letter that marks the color of the cell in X, goes alphabetically before the letter that marks the color of the cell in Y.

(* compiled on 64-bit system with ocamlopt -pp camlp4o $file *)

open Printf
open Scanf
open Array

module S = String
module Q = Queue
module T = Stack
module L = List

(* misc *)
let neg_compare x y = - compare x y
let min3 a b c = min a (min b c)
let mid3 a b c = if a < b then max a (min b c) else max b (min a c)
let max3 a b c = max a (max b c)
let min4 a b c d = min (min a b) (min c d)
let max4 a b c d = max (max a b) (max c d)
let itb a = a <> 0
let bti b = if b then 1 else 0
(* int *)
let intsqrt n =
  if n < 0 then raise (Invalid_argument "sqrt of negative int") else
  let rec bs a b = begin
    if b - a = 1 then a else
    let mid = (a+b)/2 in
    if mid*mid <= n then bs mid b else bs a mid
  end in
  bs 0 (1 lsl 31)
(* float *)
let round f = truncate (f +. 0.5)
let inv_float f = 1. /. f
(* functional *)
let id x = x
let const x _ = x
let (|>) x f = f x
let (^>) x f = f x
let (@@) a b = a b
let on g f a b = g (f a) (f b)
let rec fix f = f (fun x -> fix f x)
let ift cond a = if cond then a
let ifte cond a b = if cond then a else b
let rec for_ i len c f = if i = len then c else for_ (i+1) len (f i c) f
(* predicate *)
let is_odd x = x land 1 = 1
let is_even x = x land 1 = 0
(* option *)
let is_none = function | Some _ -> false | None -> true
let is_some = function | Some _ -> true | None -> false
let option x = function Some y -> y | None -> x
(* ref *)
let (+=) a b = a := !a + b
let (-=) a b = a := !a - b
let ( *= ) a b = a := !a * b
let assign_min x y = x := min !x y
let assign_max x y = x := max !x y
(* array *)
let set_min x i v = x.(i) <- min x.(i) v
let set_max x i v = x.(i) <- max x.(i) v
let swap arr i j = let t = arr.(i) in arr.(i) <- arr.(j); arr.(j) <- t
(* tuple *)
let (+:) (a,b) (c,d) = (a+c,b+d)
let (-:) (a,b) (c,d) = (a-c,b-d)
(* Arithmetic *)
let negate x = -x
let rec factorial n = if n = 0 then 1 else n * factorial (n-1)
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let rec extgcd a b =
  if b = 0 then 1,0,a else
  let x,y,g = extgcd b (a mod b) in
  y,x-(a/b)*y,g
let rec pow x n = if n = 0 then 1 else
  let n' = n/2 in
  let p = pow x n' in
  if is_odd n then x*p*p else p*p
let rec modpow m x n =
  if n = 0 then 1 else
  let n' = n/2 in
  let p = modpow m x n' in
  if is_even n then (p*p mod m) else
  (x*(p*p mod m) mod m)
let modinv m i = let _,x,_ = extgcd m i in x
(* IO *)
let is_digit c =
  let i = int_of_char c in
  48 <= i && i < 58
let is_space c = c = ' ' || c = '\n' || c = '\t' || c = '\r'
let read_char () = try input_char stdin with End_of_file -> '\000'
let rec read_letter () =
  let c = read_char() in
  if is_space c then read_letter() else c
let read_int () =
  let digit c = int_of_char c - 48 in
  let rec read n =
    let c = read_char() in
    if is_digit c then read (10*n + (digit c)) else
    n
  in
  let rec run c =
    if is_digit c then read (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -read (digit c) else run c
    end else
    if c = '\000' then 0 else
    run (read_char())
  in
  run (read_char())
let read_float () =
  let digit c = float (int_of_char c - 48) in
  let rec read_decimial m f =
    let c = read_char () in
    if is_digit c then read_decimial (m /. 10.) (f +. m *. digit c) else f
  in
  let rec read_integral n =
    let c = read_char () in
    if is_digit c then read_integral (10. *. n +. digit c) else
    if c = '.' then n +. read_decimial 0.1 0.0 else
    n
  in
  let rec run c = begin
    if is_digit c then read_integral (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -. read_integral (digit c) else
      run c
    end else
    if c = '.' then read_decimial 0.1 0.0 else
    if c = '\000' then 0.0 else
    run (read_char())
  end in
  run (read_char())
let read_word () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec read c =
    if is_space c || c = '\000' then contents buf else begin
      add_char buf c;
      read (read_char())
    end
  in
  let rec run c =
    if is_space c then run (read_char()) else
    if c = '\000' then "" else
    read c
  in
  run (read_char())
let read_line () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec run c =
    if c = '\n' || c = '\000' then contents buf else begin
      add_char buf c;
      run (read_char())
    end
  in
  run (read_char())
let newline () = print_newline()
let print_tuple s (x,y) = printf s x y
let print_array s arr = Array.iter (printf s) arr; print_newline()
let print_matrix s mat = Array.iter (print_array s) mat
let print_list s lis = List.iter (printf s) lis; print_newline()
;;

let nei = [| (1,0) ; (-1,0) ; (0,1) ; (0,-1) |]
let _ =
  let height = read_int() in
  let width = read_int() in

  let mat = make_matrix height width '\000' in

  let find_c i j =
    let set = for_ 0 (length nei) [] (fun k lis ->
      let dy,dx = nei.(k) in
      let i' = i + dy in
      let j' = j + dx in
      if 0 <= i' && i' < height
      && 0 <= j' && j' < width
      && mat.(i').(j') <> '\000' then
        mat.(i').(j') :: lis
      else
        lis
    ) in
    let colors = "ABCD" in
    let rec select_color i =
      let c = colors.[i] in
      if List.mem c set then select_color (i+1) else
      c
    in
    select_color 0
  in

  let find_h c i j =
    let rec run i' =
      if i' = height then 0 else begin
        let c' = mat.(i').(j) in
        if c' <> '\000' then 0 else
        1 + run (i'+1)
      end
    in
    run i
  in

  let find_w c i j =
    let rec run j' =
      if j' = width then 0 else begin
        let c' = mat.(i).(j') in
        let c'' = find_c i j' in
        if c' <> '\000' || c <> c'' then 0 else
        1 + run (j'+1)
      end
    in
    run j
  in

  let fill i j l c =
    for i = i to i + l -1 do
      for j = j to j + l -1 do
        mat.(i).(j) <- c
      done
    done
  in

  for i = 0 to height -1 do
    for j = 0 to width -1 do
      let c = mat.(i).(j) in
      if c = '\000' then begin
        let c = find_c i j in
        let h = find_h c i j in
        let w = find_w c i j in
        let l = min h w in
        fill i j l c
      end
    done
  done;

  print_matrix "%c" mat

(* compiled on 64-bit system with ocamlopt -pp camlp4o $file *)

open Printf
open Scanf
open Array

module S = String
module Q = Queue
module T = Stack
module L = List

(* misc *)
let neg_compare x y = - compare x y
let min3 a b c = min a (min b c)
let mid3 a b c = if a < b then max a (min b c) else max b (min a c)
let max3 a b c = max a (max b c)
let min4 a b c d = min (min a b) (min c d)
let max4 a b c d = max (max a b) (max c d)
let itb a = a <> 0
let bti b = if b then 1 else 0
(* int *)
let intsqrt n =
  if n < 0 then raise (Invalid_argument "sqrt of negative int") else
  let rec bs a b = begin
    if b - a = 1 then a else
    let mid = (a+b)/2 in
    if mid*mid <= n then bs mid b else bs a mid
  end in
  bs 0 (1 lsl 31)
(* float *)
let round f = truncate (f +. 0.5)
let inv_float f = 1. /. f
(* functional *)
let id x = x
let const x _ = x
let (|>) x f = f x
let (^>) x f = f x
let (@@) a b = a b
let on g f a b = g (f a) (f b)
let rec fix f = f (fun x -> fix f x)
let ift cond a = if cond then a
let ifte cond a b = if cond then a else b
let rec for_ i len c f = if i = len then c else for_ (i+1) len (f i c) f
(* predicate *)
let is_odd x = x land 1 = 1
let is_even x = x land 1 = 0
(* option *)
let is_none = function | Some _ -> false | None -> true
let is_some = function | Some _ -> true | None -> false
let option x = function Some y -> y | None -> x
(* ref *)
let (+=) a b = a := !a + b
let (-=) a b = a := !a - b
let ( *= ) a b = a := !a * b
let assign_min x y = x := min !x y
let assign_max x y = x := max !x y
(* array *)
let set_min x i v = x.(i) <- min x.(i) v
let set_max x i v = x.(i) <- max x.(i) v
let swap arr i j = let t = arr.(i) in arr.(i) <- arr.(j); arr.(j) <- t
(* tuple *)
let (+:) (a,b) (c,d) = (a+c,b+d)
let (-:) (a,b) (c,d) = (a-c,b-d)
(* Arithmetic *)
let negate x = -x
let rec factorial n = if n = 0 then 1 else n * factorial (n-1)
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let rec extgcd a b =
  if b = 0 then 1,0,a else
  let x,y,g = extgcd b (a mod b) in
  y,x-(a/b)*y,g
let rec pow x n = if n = 0 then 1 else
  let n' = n/2 in
  let p = pow x n' in
  if is_odd n then x*p*p else p*p
let rec modpow m x n =
  if n = 0 then 1 else
  let n' = n/2 in
  let p = modpow m x n' in
  if is_even n then (p*p mod m) else
  (x*(p*p mod m) mod m)
let modinv m i = let _,x,_ = extgcd m i in x
(* IO *)
let is_digit c =
  let i = int_of_char c in
  48 <= i && i < 58
let is_space c = c = ' ' || c = '\n' || c = '\t' || c = '\r'
let read_char () = try input_char stdin with End_of_file -> '\000'
let rec read_letter () =
  let c = read_char() in
  if is_space c then read_letter() else c
let read_int () =
  let digit c = int_of_char c - 48 in
  let rec read n =
    let c = read_char() in
    if is_digit c then read (10*n + (digit c)) else
    n
  in
  let rec run c =
    if is_digit c then read (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -read (digit c) else run c
    end else
    if c = '\000' then 0 else
    run (read_char())
  in
  run (read_char())
let read_float () =
  let digit c = float (int_of_char c - 48) in
  let rec read_decimial m f =
    let c = read_char () in
    if is_digit c then read_decimial (m /. 10.) (f +. m *. digit c) else f
  in
  let rec read_integral n =
    let c = read_char () in
    if is_digit c then read_integral (10. *. n +. digit c) else
    if c = '.' then n +. read_decimial 0.1 0.0 else
    n
  in
  let rec run c = begin
    if is_digit c then read_integral (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -. read_integral (digit c) else
      run c
    end else
    if c = '.' then read_decimial 0.1 0.0 else
    if c = '\000' then 0.0 else
    run (read_char())
  end in
  run (read_char())
let read_word () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec read c =
    if is_space c || c = '\000' then contents buf else begin
      add_char buf c;
      read (read_char())
    end
  in
  let rec run c =
    if is_space c then run (read_char()) else
    if c = '\000' then "" else
    read c
  in
  run (read_char())
let read_line () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec run c =
    if c = '\n' || c = '\000' then contents buf else begin
      add_char buf c;
      run (read_char())
    end
  in
  run (read_char())
let newline () = print_newline()
let print_tuple s (x,y) = printf s x y
let print_array s arr = Array.iter (printf s) arr; print_newline()
let print_matrix s mat = Array.iter (print_array s) mat
let print_list s lis = List.iter (printf s) lis; print_newline()
;;

let nei = [| (1,0) ; (-1,0) ; (0,1) ; (0,-1) |]
let _ =
  let height = read_int() in
  let width = read_int() in

  let mat = make_matrix height width '\000' in

  let find_c i j =
    let set = for_ 0 (length nei) [] (fun k lis ->
      let dy,dx = nei.(k) in
      let i' = i + dy in
      let j' = j + dx in
      if 0 <= i' && i' < height
      && 0 <= j' && j' < width
      && mat.(i').(j') <> '\000' then
        mat.(i').(j') :: lis
      else
        lis
    ) in
    let colors = "ABCD" in
    let rec select_color i =
      let c = colors.[i] in
      if List.mem c set then select_color (i+1) else
      c
    in
    let ans = select_color 0 in
    if i = 5 && j = 0 then begin
      L.iter (printf "%c") set;
      printf " return %c\n" ans;
    end;
    ans
  in

  let find_h c i j =
    let rec run i' =
      if i' = height then 0 else begin
        let c' = mat.(i').(j) in
        if c' <> '\000' then 0 else
        1 + run (i'+1)
      end
    in
    run i
  in

  let find_w c i j =
    let rec run j' =
      if j' = width then 0 else begin
        let c' = mat.(i).(j') in
        let c'' = find_c i j' in
        if c' <> '\000' || c <> c'' then 0 else
        1 + run (j'+1)
      end
    in
    run j
  in

  let fill i j l c =
    for i = i to i + l -1 do
      for j = j to j + l -1 do
        mat.(i).(j) <- c
      done
    done
  in

  for i = 0 to height -1 do
    for j = 0 to width -1 do
      let c = mat.(i).(j) in
      if c = '\000' then begin
        let c = find_c i j in
        let h = find_h c i j in
        let w = find_w c i j in
        let l = min h w in
        fill i j l c
      end
    done
  done;

  print_matrix "%c" mat

(* compiled on 64-bit system with ocamlopt -pp camlp4o $file *)

open Printf
open Scanf
open Array

module S = String
module Q = Queue
module T = Stack
module L = List

(* misc *)
let neg_compare x y = - compare x y
let min3 a b c = min a (min b c)
let mid3 a b c = if a < b then max a (min b c) else max b (min a c)
let max3 a b c = max a (max b c)
let min4 a b c d = min (min a b) (min c d)
let max4 a b c d = max (max a b) (max c d)
let itb a = a <> 0
let bti b = if b then 1 else 0
(* int *)
let intsqrt n =
  if n < 0 then raise (Invalid_argument "sqrt of negative int") else
  let rec bs a b = begin
    if b - a = 1 then a else
    let mid = (a+b)/2 in
    if mid*mid <= n then bs mid b else bs a mid
  end in
  bs 0 (1 lsl 31)
(* float *)
let round f = truncate (f +. 0.5)
let inv_float f = 1. /. f
(* functional *)
let id x = x
let const x _ = x
let (|>) x f = f x
let (^>) x f = f x
let (@@) a b = a b
let on g f a b = g (f a) (f b)
let rec fix f = f (fun x -> fix f x)
let ift cond a = if cond then a
let ifte cond a b = if cond then a else b
let rec for_ i len c f = if i = len then c else for_ (i+1) len (f i c) f
(* predicate *)
let is_odd x = x land 1 = 1
let is_even x = x land 1 = 0
(* option *)
let is_none = function | Some _ -> false | None -> true
let is_some = function | Some _ -> true | None -> false
let option x = function Some y -> y | None -> x
(* ref *)
let (+=) a b = a := !a + b
let (-=) a b = a := !a - b
let ( *= ) a b = a := !a * b
let assign_min x y = x := min !x y
let assign_max x y = x := max !x y
(* array *)
let set_min x i v = x.(i) <- min x.(i) v
let set_max x i v = x.(i) <- max x.(i) v
let swap arr i j = let t = arr.(i) in arr.(i) <- arr.(j); arr.(j) <- t
(* tuple *)
let (+:) (a,b) (c,d) = (a+c,b+d)
let (-:) (a,b) (c,d) = (a-c,b-d)
(* Arithmetic *)
let negate x = -x
let rec factorial n = if n = 0 then 1 else n * factorial (n-1)
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let rec extgcd a b =
  if b = 0 then 1,0,a else
  let x,y,g = extgcd b (a mod b) in
  y,x-(a/b)*y,g
let rec pow x n = if n = 0 then 1 else
  let n' = n/2 in
  let p = pow x n' in
  if is_odd n then x*p*p else p*p
let rec modpow m x n =
  if n = 0 then 1 else
  let n' = n/2 in
  let p = modpow m x n' in
  if is_even n then (p*p mod m) else
  (x*(p*p mod m) mod m)
let modinv m i = let _,x,_ = extgcd m i in x
(* IO *)
let is_digit c =
  let i = int_of_char c in
  48 <= i && i < 58
let is_space c = c = ' ' || c = '\n' || c = '\t' || c = '\r'
let read_char () = try input_char stdin with End_of_file -> '\000'
let rec read_letter () =
  let c = read_char() in
  if is_space c then read_letter() else c
let read_int () =
  let digit c = int_of_char c - 48 in
  let rec read n =
    let c = read_char() in
    if is_digit c then read (10*n + (digit c)) else
    n
  in
  let rec run c =
    if is_digit c then read (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -read (digit c) else run c
    end else
    if c = '\000' then 0 else
    run (read_char())
  in
  run (read_char())
let read_float () =
  let digit c = float (int_of_char c - 48) in
  let rec read_decimial m f =
    let c = read_char () in
    if is_digit c then read_decimial (m /. 10.) (f +. m *. digit c) else f
  in
  let rec read_integral n =
    let c = read_char () in
    if is_digit c then read_integral (10. *. n +. digit c) else
    if c = '.' then n +. read_decimial 0.1 0.0 else
    n
  in
  let rec run c = begin
    if is_digit c then read_integral (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -. read_integral (digit c) else
      run c
    end else
    if c = '.' then read_decimial 0.1 0.0 else
    if c = '\000' then 0.0 else
    run (read_char())
  end in
  run (read_char())
let read_word () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec read c =
    if is_space c || c = '\000' then contents buf else begin
      add_char buf c;
      read (read_char())
    end
  in
  let rec run c =
    if is_space c then run (read_char()) else
    if c = '\000' then "" else
    read c
  in
  run (read_char())
let read_line () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec run c =
    if c = '\n' || c = '\000' then contents buf else begin
      add_char buf c;
      run (read_char())
    end
  in
  run (read_char())
let newline () = print_newline()
let print_tuple s (x,y) = printf s x y
let print_array s arr = Array.iter (printf s) arr; print_newline()
let print_matrix s mat = Array.iter (print_array s) mat
let print_list s lis = List.iter (printf s) lis; print_newline()
;;

let _ =
  let height = read_int() in
  let width = read_int() in

  let mat = make_matrix height width '\000' in

  let find_h i j =
    let rec run i' =
      if i' = height || mat.(i').(j) <> '\000' then 0 else
      1 + run (i'+1)
    in
    run i
  in

  let find_w i j =
    let rec run j' =
      if j' = width || mat.(i).(j') <> '\000' then 0 else
      1 + run (j'+1)
    in
    run j
  in

  let find_c i j =
    let set = ref [] in
    if 0 < i then set := mat.(i-1).(j) :: !set;
    if 0 < j then set := mat.(i).(j-1) :: !set;
    if 0 < i && 0 < j then set := mat.(i-1).(j-1) :: !set;
    let colors = "ABCD" in
    let rec select_color i =
      let c = colors.[i] in
      if List.mem c !set then select_color (i+1) else
      c
    in
    select_color 0
  in

  let fill i j l c =
    for i = i to i + l -1 do
      for j = j to j + l -1 do
        mat.(i).(j) <- c
      done
    done
  in

  for i = 0 to height -1 do
    for j = 0 to width -1 do
      let c = mat.(i).(j) in
      if c = '\000' then begin
        let h = find_h i j in
        let w = find_w i j in
        let l = min h w in
        let c = find_c i j in
        fill i j l c
      end
    done
  done;

  for i = 0 to height -1 do
    for j = 0 to width -1 do
      printf "%c" mat.(i).(j)
    done;
    newline()
  done


You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase.A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not.A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same.Find the length of the longest good subsequence of $$$s$$$.

The first line of the input contains integers $$$n$$$ ($$$1\le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet.

Print the only integer — the length of the longest good subsequence of string $$$s$$$.

NoteIn the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length.In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.

open Printf open Scanf
module MyInt = struct let (+) = (+) let (-) = (-) let (/) = (/) let ( * ) = ( * ) let (mod) = (mod) let (%) = (mod) let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v end
module MyInt64 = struct
	let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (+) p q = Int64.add p q let (-) p q = Int64.sub p q let ( * ) p q = Int64.mul p q let (/) p q = Int64.div p q let (+=) r v = r := !r + v let (-=) r v = r := !r - v let ( *=) r v= r := !r * v let (/=) r v = r := !r / v let labs p = if p < 0L then -1L*p else p let (~|) p = Int64.to_int p let (~~|) p = Int64.of_int p let input_i64_array n = Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let print_i64_endline n = n |> Int64.to_string |> print_endline let (mod) m n = m - (m/n) * n let (%) = (mod) let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n) let lcm m n = (m*n) / gcd m n let rep from_ to_ fbod = let i,f = ref ~|from_,ref true in while !i <= ~|to_ && !f do match fbod ~~| !i with | `Break -> f := false | _ -> i := !i +$ 1; done
	let string_of_list ?(separator=" ") f ls = let rec f0 a s = match a with | [] -> s | [h] -> s ^ (f h) ^ separator | h::t -> f0 t (s ^ (f h) ^ separator) in f0 ls "" let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) [] let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
	let (@@@) = (@) let (@) a i = a.(~|i) let (<@) a (i,v) = a.(~|i) <- v
	let ceildiv m n = (m+n-1L) / n
end open MyInt64

let () =
	let n,k = get_2_i64 0 in
	let a = scanf " %s" (fun v -> v) |> char_list_of_string |> Array.of_list
	in
	let alp = Array.make (~|k) 0L in
	Array.iter (fun v ->
		let p = (int_of_char v) -$ (int_of_char 'A')
		in
		(* printf "  %c:%d\n" v p *)
		alp.(p) <- alp.(p) + 1L;
	) a;
	Array.fold_left (fun u v ->
		min u v
	) Int64.max_int alp
	|> ( * ) k
	|> printf "%Ld\n"


Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 × n table).At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 × a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other.After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit").But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating — find Bob's first move, such that after it you can be sure that Alice cheated.

The first line of the input contains three integers: n, k and a (1 ≤ n, k, a ≤ 2·105) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other. The second line contains integer m (1 ≤ m ≤ n) — the number of Bob's moves. The third line contains m distinct integers x1, x2, ..., xm, where xi is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n.

Print a single integer — the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print "-1".

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let k = read_int () in
  let a = read_int () in
  let m = read_int () in
  let x = Array.init m (fun _ -> -1 + read_int()) in

  let feasible i =
  (* returns true iff bob's moves 0...i are feasible. *)
    let mark = Array.make n false in
    for j=0 to i do
      mark.(x.(j)) <- true
    done;

    let place_one_block j =
      (* return the place to start the next block, or -1 if
	 can't place any block *)
      let rec pl j placed =
	if placed = a then j+1
	else if j>=n then -1
	else if mark.(j) then pl (j+1) 0
	else pl (j+1) (placed+1)
      in
      pl j 0
    in

    let rec fill j b_placed =
      if b_placed = k then true else
	let j' = place_one_block j in
	if j' < 0 then false else fill j' (b_placed+1)
    in

    fill 0 0
  in

  let rec bsearch lo hi =
    (* lo < hi and lo is possible, hi is impossible *)
    if lo+1 = hi then hi else
      let m = (lo + hi) / 2 in
      if feasible m then bsearch m hi else bsearch lo m
  in

  let answer =
    if feasible (m-1) then -1 else 1 + bsearch (-1) (m-1)
  in

  printf "%d\n" answer;

module RangeMap = Map.Make(
    struct
        type t = int * int
        let compare (l1, r1) (l2, r2) =
            if r1 < l2 then -1
            else if r2 < l1 then 1
            else 0
    end)

let add elt s = RangeMap.add elt elt s

(* n - size of playing field
 * k - number of ships
 * a - width of ship
 * m - number of moves *)
let () = Scanf.scanf "%d %d %d\n%d\n" (fun n k a m ->
    (* count number of ships that can fit in a k-wide space *)
    let count k = (k + 1) / (a + 1) in

    (* we want to maintain a bunch of intervals over which
     * it's possible for ships to fit *)
    let t = RangeMap.singleton (1, n) (1, n) in

    let rem = ref (count n) in

    let rec loop t i =
        if i <> m then
            let t' = Scanf.scanf "%d%_c" (fun x ->
                if RangeMap.mem (x, x) t
                then begin
                    let l, r = RangeMap.find (x, x) t in
                    rem := !rem - (count (r - l + 1)) + (count (x - l)) + (count (r - x));
                    add (l, x - 1) (add (x + 1, r) (RangeMap.remove (x, x) t))
                end else t)
            in
            if !rem < k
            then Printf.printf "%d\n" (i + 1)
            else loop t' (i + 1)
    in
    loop t 0;

    if !rem >= k then Printf.printf "-1\n")


You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence.We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 ≤ p1 < p2 < ... < pk ≤ |s|) a subsequence of string s = s1s2... s|s|.String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y|, if either |x| > |y| and x1 = y1, x2 = y2, ... , x|y| = y|y|, or exists such number r (r < |x|, r < |y|), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 > yr + 1. Characters in lines are compared like their ASCII codes.

The single line contains a non-empty string s, consisting only of lowercase English letters. The string's length doesn't exceed 105.

Print the lexicographically maximum subsequence of string s.

NoteLet's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters).The first sample: aBaBBAThe second sample: abbCbCCaCbbCBaaBA

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let n = String.length s in
  let c2i c = Char.code c - Char.code 'a' in
  let a = Array.make n 0 in
  let b = Array.make_matrix 26 n (-1) in
    for i = 0 to n - 1 do
      a.(i) <- c2i s.[i]
    done;
    for i = n - 2 downto 0 do
      for j = 0 to 25 do
	b.(j).(i) <- b.(j).(i + 1)
      done;
      b.(a.(i + 1)).(i) <- i + 1
    done;
    let pos =
      if a.(0) = 25
      then 0
      else (
	let s = ref (-1) in
	  for i = 0 to 25 do
	    if b.(i).(0) >= 0
	    then s := b.(i).(0)
	  done;
	  if !s < 0 || a.(0) >= a.(!s)
	  then 0
	  else !s
      )
    in
    let buf = Buffer.create n in
      let pos = ref pos in
	while !pos >= 0 do
	  Buffer.add_char buf s.[!pos];
	  let p = !pos in
	    pos := -1;
	    for i = 0 to 25 do
	      if b.(i).(p) >= 0
	      then pos := b.(i).(p)
	    done;
	done;
	Printf.printf "%s\n" (Buffer.contents buf)

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let n = String.length s in
  let c2i c = Char.code c - Char.code 'a' in
  let a = Array.make n 0 in
  let b = Array.make_matrix 26 n (-1) in
    for i = 0 to n - 1 do
      a.(i) <- c2i s.[i]
    done;
    for i = n - 2 downto 0 do
      for j = 0 to 25 do
	b.(j).(i) <- b.(j).(i + 1)
      done;
      b.(a.(i + 1)).(i) <- i + 1
    done;
    let pos =
      if a.(0) = 25
      then 0
      else (
	let s = ref (-1) in
	  for i = 0 to 25 do
	    if b.(i).(0) >= 0
	    then s := b.(i).(0)
	  done;
	  !s
      )
    in
    let buf = Buffer.create n in
      let pos = ref pos in
	while !pos >= 0 do
	  Buffer.add_char buf s.[!pos];
	  let p = !pos in
	    pos := -1;
	    for i = 0 to 25 do
	      if b.(i).(p) >= 0
	      then pos := b.(i).(p)
	    done;
	done;
	Printf.printf "%s\n" (Buffer.contents buf)


You are given several queries. Each query consists of three integers $$$p$$$, $$$q$$$ and $$$b$$$. You need to answer whether the result of $$$p/q$$$ in notation with base $$$b$$$ is a finite fraction.A fraction in notation with base $$$b$$$ is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of queries. Next $$$n$$$ lines contain queries, one per line. Each line contains three integers $$$p$$$, $$$q$$$, and $$$b$$$ ($$$0 \le p \le 10^{18}$$$, $$$1 \le q \le 10^{18}$$$, $$$2 \le b \le 10^{18}$$$). All numbers are given in notation with base $$$10$$$.

For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.

open Printf
open Scanf

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

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

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

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

let is_finite p q b =
  let g = gcd p q in
  let (p,q) = (p // g, q // g) in

  (*  printf "in is_finite  g=%Ld  p=%Ld  q=%Ld\n"  g  p  q; *)

  if p %% q = 0L then true else (
    let rec loop q =
      let g = gcd b q in
(*      printf "g = %Ld  q = %Ld\n" g q; *)
      if g = 1L then q = 1L else
	let rec xloop q = if q %% g = 0L then xloop (q // g) else q in
	loop (xloop (q // g))
    in
    loop q
  )

let () =
  let n = read_int () in

  for i=1 to n do
    let p = read_long() in
    let q = read_long() in
    let b = read_long() in
    if is_finite p q b then printf "Finite\n" else printf "Infinite\n"
  done


Ksusha is a beginner coder. Today she starts studying arrays. She has array a1, a2, ..., an, consisting of n positive integers.Her university teacher gave her a task. Find such number in the array, that all array elements are divisible by it. Help her and find the number!

The first line contains integer n (1 ≤ n ≤ 105), showing how many numbers the array has. The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the array elements.

Print a single integer — the number from the array, such that all array elements are divisible by it. If such number doesn't exist, print -1. If there are multiple answers, you are allowed to print any of them.

open List
open Printf
open Scanf

let inf= 1070000000;;

let rec getlist n =
	if n=0 then []
	else scanf " %d" (fun x->x)::getlist (n-1);;

let min a b = if a<b then a else b;;

let rec min_ a =
	if a=[] then inf
	else min (hd a) (min_ (tl a));;

let n = scanf "%d" (fun x->x)
in let a = getlist n
in let ans = min_ a
in printf "%d\n"
	(if for_all (fun x -> x mod ans = 0) a
		then ans
		else (-1));;

open List
open Printf
open Scanf

let inf= 1070000000;;

let rec getlist n =
	if n=0 then []
	else scanf " %d" (fun x->x)::getlist (n-1);;

let min a b = if a<b then a else b;;

let rec min_ a =
	if a=[] then inf
	else min (hd a) (min_ (tl a));;

let n = scanf "%d" (fun x->x)
in let a = getlist n
in let ans = min_ a
in printf "%d\n"
	(if for_all (fun x -> x mod ans = 0) a
		then ans
		else -1);;


Consider a football tournament where n teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the i-th team has color xi and the kit for away games of this team has color yi (xi ≠ yi).In the tournament, each team plays exactly one home game and exactly one away game with each other team (n(n - 1) games in total). The team, that plays the home game, traditionally plays in its home kit. The team that plays an away game plays in its away kit. However, if two teams has the kits of the same color, they cannot be distinguished. In this case the away team plays in its home kit.Calculate how many games in the described tournament each team plays in its home kit and how many games it plays in its away kit.

The first line contains a single integer n (2 ≤ n ≤ 105) — the number of teams. Next n lines contain the description of the teams. The i-th line contains two space-separated numbers xi, yi (1 ≤ xi, yi ≤ 105; xi ≠ yi) — the color numbers for the home and away kits of the i-th team.

For each team, print on a single line two space-separated integers — the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input.

let read_int()=Scanf.scanf "%d " (fun x->x);;
let n=read_int();;
let home=Array.make (n+1) 0;;
let away=Array.make (n+1) 0;;
let color=Array.make 100001 0;;
let color2=Array.make 100001 0;;
for i=1 to n-1 do
   let x=read_int() and y=read_int() in
   home.(i)<-x;
   away.(i)<-y;
   color.(x)<-color.(x)+1;
   color2.(y)<-color2.(y)+1
done;;
let x=read_int() and y=Scanf.scanf "%d" (fun x->x) in
   begin
   home.(n)<-x;
   away.(n)<-y;
   color.(x)<-color.(x)+1;
   color2.(y)<-color2.(y)+1
   end;;
for i=1 to n do
   Printf.printf "%d " (n-1+color.(away.(i)));
   Printf.printf "%d\n" (n-1-color.(away.(i)))
done;;

(* compiled on 64-bit system with ocamlopt -pp camlp4o $file *)

open Printf
open Scanf
open Array

module S = String
module Q = Queue
module T = Stack
module L = List

(* misc *)
let neg_compare x y = - compare x y
let min3 a b c = min a (min b c)
let mid3 a b c = if a < b then max a (min b c) else max b (min a c)
let max3 a b c = max a (max b c)
let min4 a b c d = min (min a b) (min c d)
let max4 a b c d = max (max a b) (max c d)
let itb a = a <> 0
let bti b = if b then 1 else 0
(* int *)
let intsqrt n =
  if n < 0 then raise (Invalid_argument "sqrt of negative int") else
  let rec bs a b = begin
    if b - a = 1 then a else
    let mid = (a+b)/2 in
    if mid*mid <= n then bs mid b else bs a mid
  end in
  bs 0 (1 lsl 31)
(* float *)
let round f = truncate (f +. 0.5)
let inv_float f = 1. /. f
(* functional *)
let id x = x
let const x _ = x
let (|>) x f = f x
let (^>) x f = f x
let (@@) a b = a b
let on g f a b = g (f a) (f b)
let rec fix f = f (fun x -> fix f x)
let ift cond a = if cond then a
let ifte cond a b = if cond then a else b
let rec for_ i len c f = if i = len then c else for_ (i+1) len (f i c) f
(* predicate *)
let is_odd x = x land 1 = 1
let is_even x = x land 1 = 0
(* option *)
let is_none = function | Some _ -> false | None -> true
let is_some = function | Some _ -> true | None -> false
let default x = function Some y -> y | None -> x
(* ref *)
let (+=) a b = a := !a + b
let (-=) a b = a := !a - b
let ( *= ) a b = a := !a * b
let assign_min x y = x := min !x y
let assign_max x y = x := max !x y
(* array *)
let set_min x i v = x.(i) <- min x.(i) v
let set_max x i v = x.(i) <- max x.(i) v
let swap arr i j = let t = arr.(i) in arr.(i) <- arr.(j); arr.(j) <- t
(* tuple *)
let (+:) (a,b) (c,d) = (a+c,b+d)
let (-:) (a,b) (c,d) = (a-c,b-d)
(* Arithmetic *)
let negate x = -x
let rec factorial n = if n = 0 then 1 else n * factorial (n-1)
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let rec extgcd a b =
  if b = 0 then 1,0,a else
  let x,y,g = extgcd b (a mod b) in
  y,x-(a/b)*y,g
let rec pow x n = if n = 0 then 1 else
  let n' = n/2 in
  let p = pow x n' in
  if is_odd n then x*p*p else p*p
let rec modpow m x n =
  if n = 0 then 1 else
  let n' = n/2 in
  let p = modpow m x n' in
  if is_even n then (p*p mod m) else
  (x*(p*p mod m) mod m)
let modinv m i = let _,x,_ = extgcd m i in x
(* IO *)
let is_digit c =
  let i = int_of_char c in
  48 <= i && i < 58
let is_space c = c = ' ' || c = '\n' || c = '\t' || c = '\r'
let read_char () = try input_char stdin with End_of_file -> '\000'
let rec read_letter () =
  let c = read_char() in
  if is_space c then read_letter() else c
let read_int () =
  let digit c = int_of_char c - 48 in
  let rec read n =
    let c = read_char() in
    if is_digit c then read (10*n + (digit c)) else
    n
  in
  let rec run c =
    if is_digit c then read (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -read (digit c) else run c
    end else
    if c = '\000' then 0 else
    run (read_char())
  in
  run (read_char())
let read_float () =
  let digit c = float (int_of_char c - 48) in
  let rec read_decimial m f =
    let c = read_char () in
    if is_digit c then read_decimial (m /. 10.) (f +. m *. digit c) else f
  in
  let rec read_integral n =
    let c = read_char () in
    if is_digit c then read_integral (10. *. n +. digit c) else
    if c = '.' then n +. read_decimial 0.1 0.0 else
    n
  in
  let rec run c = begin
    if is_digit c then read_integral (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -. read_integral (digit c) else
      run c
    end else
    if c = '.' then read_decimial 0.1 0.0 else
    if c = '\000' then 0.0 else
    run (read_char())
  end in
  run (read_char())
let read_word () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec read c =
    if is_space c || c = '\000' then contents buf else begin
      add_char buf c;
      read (read_char())
    end
  in
  let rec run c =
    if is_space c then run (read_char()) else
    if c = '\000' then "" else
    read c
  in
  run (read_char())
let read_line () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec run c =
    if c = '\n' || c = '\000' then contents buf else begin
      add_char buf c;
      run (read_char())
    end
  in
  run (read_char())
let newline () = print_newline()
let print_tuple s (x,y) = printf s x y
let print_array s arr = Array.iter (printf s) arr; print_newline()
let print_matrix s mat = Array.iter (print_array s) mat
let print_list s lis = List.iter (printf s) lis; print_newline()
;;

let _ =
  let len = read_int() in
  let ch = make len 0 in
  let ca = make len 0 in

  for i = 0 to len-1 do
    ch.(i) <- read_int();
    ca.(i) <- read_int();
  done;

  sort compare ch;

  let rec run i =
    if i = len then () else begin
      let rec lower_bound a b =
        if b - a = 1 then b else
        let mid = (a+b)/2 in
        if ch.(mid) >= ca.(i) then lower_bound a mid else lower_bound mid b
      in
      let rec upper_bound a b =
        if b - a = 1 then b else
        let mid = (a+b)/2 in
        if ch.(mid) <= ca.(i) then upper_bound mid b else upper_bound a mid
      in
      let n = upper_bound (-1) len - lower_bound (-1) len in
      printf "%d %d\n" (len-1+n) (len-1-n);
      run (i+1)
    end
  in
  run 0

(* compiled on 64-bit system with ocamlopt -pp camlp4o $file *)

open Printf
open Scanf
open Array

module S = String
module Q = Queue
module T = Stack
module L = List

(* misc *)
let neg_compare x y = - compare x y
let min3 a b c = min a (min b c)
let mid3 a b c = if a < b then max a (min b c) else max b (min a c)
let max3 a b c = max a (max b c)
let min4 a b c d = min (min a b) (min c d)
let max4 a b c d = max (max a b) (max c d)
let itb a = a <> 0
let bti b = if b then 1 else 0
(* int *)
let intsqrt n =
  if n < 0 then raise (Invalid_argument "sqrt of negative int") else
  let rec bs a b = begin
    if b - a = 1 then a else
    let mid = (a+b)/2 in
    if mid*mid <= n then bs mid b else bs a mid
  end in
  bs 0 (1 lsl 31)
(* float *)
let round f = truncate (f +. 0.5)
let inv_float f = 1. /. f
(* functional *)
let id x = x
let const x _ = x
let (|>) x f = f x
let (^>) x f = f x
let (@@) a b = a b
let on g f a b = g (f a) (f b)
let rec fix f = f (fun x -> fix f x)
let ift cond a = if cond then a
let ifte cond a b = if cond then a else b
let rec for_ i len c f = if i = len then c else for_ (i+1) len (f i c) f
(* predicate *)
let is_odd x = x land 1 = 1
let is_even x = x land 1 = 0
(* option *)
let is_none = function | Some _ -> false | None -> true
let is_some = function | Some _ -> true | None -> false
let default x = function Some y -> y | None -> x
(* ref *)
let (+=) a b = a := !a + b
let (-=) a b = a := !a - b
let ( *= ) a b = a := !a * b
let assign_min x y = x := min !x y
let assign_max x y = x := max !x y
(* array *)
let set_min x i v = x.(i) <- min x.(i) v
let set_max x i v = x.(i) <- max x.(i) v
let swap arr i j = let t = arr.(i) in arr.(i) <- arr.(j); arr.(j) <- t
(* tuple *)
let (+:) (a,b) (c,d) = (a+c,b+d)
let (-:) (a,b) (c,d) = (a-c,b-d)
(* Arithmetic *)
let negate x = -x
let rec factorial n = if n = 0 then 1 else n * factorial (n-1)
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let rec extgcd a b =
  if b = 0 then 1,0,a else
  let x,y,g = extgcd b (a mod b) in
  y,x-(a/b)*y,g
let rec pow x n = if n = 0 then 1 else
  let n' = n/2 in
  let p = pow x n' in
  if is_odd n then x*p*p else p*p
let rec modpow m x n =
  if n = 0 then 1 else
  let n' = n/2 in
  let p = modpow m x n' in
  if is_even n then (p*p mod m) else
  (x*(p*p mod m) mod m)
let modinv m i = let _,x,_ = extgcd m i in x
(* IO *)
let is_digit c =
  let i = int_of_char c in
  48 <= i && i < 58
let is_space c = c = ' ' || c = '\n' || c = '\t' || c = '\r'
let read_char () = try input_char stdin with End_of_file -> '\000'
let rec read_letter () =
  let c = read_char() in
  if is_space c then read_letter() else c
let read_int () =
  let digit c = int_of_char c - 48 in
  let rec read n =
    let c = read_char() in
    if is_digit c then read (10*n + (digit c)) else
    n
  in
  let rec run c =
    if is_digit c then read (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -read (digit c) else run c
    end else
    if c = '\000' then 0 else
    run (read_char())
  in
  run (read_char())
let read_float () =
  let digit c = float (int_of_char c - 48) in
  let rec read_decimial m f =
    let c = read_char () in
    if is_digit c then read_decimial (m /. 10.) (f +. m *. digit c) else f
  in
  let rec read_integral n =
    let c = read_char () in
    if is_digit c then read_integral (10. *. n +. digit c) else
    if c = '.' then n +. read_decimial 0.1 0.0 else
    n
  in
  let rec run c = begin
    if is_digit c then read_integral (digit c) else
    if c = '-' then begin
      let c = read_char() in
      if is_digit c then -. read_integral (digit c) else
      run c
    end else
    if c = '.' then read_decimial 0.1 0.0 else
    if c = '\000' then 0.0 else
    run (read_char())
  end in
  run (read_char())
let read_word () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec read c =
    if is_space c || c = '\000' then contents buf else begin
      add_char buf c;
      read (read_char())
    end
  in
  let rec run c =
    if is_space c then run (read_char()) else
    if c = '\000' then "" else
    read c
  in
  run (read_char())
let read_line () =
  let open Buffer in
  let buf = create 1_000_000 in
  let rec run c =
    if c = '\n' || c = '\000' then contents buf else begin
      add_char buf c;
      run (read_char())
    end
  in
  run (read_char())
let newline () = print_newline()
let print_tuple s (x,y) = printf s x y
let print_array s arr = Array.iter (printf s) arr; print_newline()
let print_matrix s mat = Array.iter (print_array s) mat
let print_list s lis = List.iter (printf s) lis; print_newline()
;;

let _ =
  let len = read_int() in
  let ch = make len 0 in
  let ca = make len 0 in

  for i = 0 to len-1 do
    ch.(i) <- read_int();
    ca.(i) <- read_int();
  done;

  sort compare ch;

  let rec run i =
    if i = len then () else begin
      let rec lower_bound a b =
        if b - a = 1 then b else
        let mid = (a+b)/2 in
        if ch.(mid) >= ca.(i) then lower_bound a mid else lower_bound mid b
      in
      let rec upper_bound a b =
        if b - a = 1 then b else
        let mid = (a+b)/2 in
        if ch.(mid) <= ca.(i) then upper_bound mid b else upper_bound a mid
      in
      let n = upper_bound 0 len - lower_bound (-1) len in
      printf "%d %d\n" (len-1+n) (len-1-n);
      run (i+1)
    end
  in
  run 0


The scientists have recently discovered wormholes — objects in space that allow to travel very long distances between galaxies and star systems. The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j. Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of galaxies within reach. Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.

Print a single real value — the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.  Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

NoteIn the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is .

let n = Scanf.scanf "%d" (fun x->x) ;;
let m = Array.make_matrix n n 0. ;;
for i = 0 to n-1 do
for j = 0 to n-1 do
    m.(i).(j) <- Scanf.scanf " %f" (fun x -> x *. 0.01)
done done ;;

let e pg pgeg = (pgeg +. 1.) /. pg -. 1. ;;

let rec emin (h::t) =
    let (_, pg, pgeg) = h in
    let xh = e pg pgeg in
    match t with
        | [] -> (xh, h, [])
        | t -> let (xmn, vmn, rest) = emin t in
            if xh < xmn
                then (xh, h, t)
                else (xmn, vmn, h::rest)
;;
let rec f i x nd =
    if i = 0
        then x
    else
        let nd2 = List.rev_map (fun (j, pg, egpg)
            -> let apg = (1. -. pg) *. m.(j).(i) in
            (j, pg +. apg, egpg +. apg *. (1. +. x))
        ) nd in
        let (x2, (j, _, _), nd3) = emin nd2 in
        f j x2 nd3
;;

let rec n_zero = function
    | 0 -> []
    | n -> (n-1, 0., 0.)::n_zero (n-1)
;;
print_float @@ f (n-1) 0. (n_zero (n-1))


Given an array $$$a=[a_1,a_2,\dots,a_n]$$$ of $$$n$$$ positive integers, you can do operations of two types on it:  Add $$$1$$$ to every element with an odd index. In other words change the array as follows: $$$a_1 := a_1 +1, a_3 := a_3 + 1, a_5 := a_5+1, \dots$$$.  Add $$$1$$$ to every element with an even index. In other words change the array as follows: $$$a_2 := a_2 +1, a_4 := a_4 + 1, a_6 := a_6+1, \dots$$$.Determine if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers. In other words, determine if you can make all elements of the array have the same parity after any number of operations.Note that you can do operations of both types any number of times (even none). Operations of different types can be performed a different number of times.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 50$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^3$$$) — the elements of the array. Note that after the performed operations the elements in the array can become greater than $$$10^3$$$.

Output $$$t$$$ lines, each of which contains the answer to the corresponding test case. As an answer, output "YES" if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

NoteFor the first test case, we can increment the elements with an even index, obtaining the array $$$[1, 3, 1]$$$, which contains only odd numbers, so the answer is "YES".For the second test case, we can show that after performing any number of operations we won't be able to make all elements have the same parity, so the answer is "NO".For the third test case, all elements already have the same parity so the answer is "YES".For the fourth test case, we can perform one operation and increase all elements at odd positions by $$$1$$$, thus obtaining the array $$$[1001, 1, 1001, 1, 1001]$$$, and all elements become odd so the answer is "YES".

let id x = x

module S = struct
  open String

  let split_on_char sep s =
    let r = ref [] in
    let j = ref (String.length s) in
    for i = length s - 1 downto 0 do
      if unsafe_get s i = sep then begin
        r := sub s (i + 1) (!j - i - 1) :: !r;
        j := i
      end
    done;
    sub s 0 !j :: !r
end

let alternating xs =
  let (result, _) =
    List.fold_left
      (fun prevResult curr ->
        match prevResult with
        | (false, _) -> (false, Some curr)
        | (true, Some x) -> (x <> curr, Some curr)
        | (true, None) -> (true, Some curr))
      (true, None)
      xs
  in
  result

let eq xs =
  let (result, _) =
    List.fold_left
      (fun prevResult curr ->
        match prevResult with
        | (false, _) -> (false, Some curr)
        | (true, Some x) -> (x = curr, Some curr)
        | (true, None) -> (true, Some curr))
      (true, None)
      xs
  in
  result

let solve () =
  let _ = Scanf.scanf "%d\n" id in
  let xs =
    Scanf.scanf "%s@\n" (fun x -> x)
    |> S.split_on_char ' '
    |> List.filter (fun x -> x <> "")
    |> List.map int_of_string
    |> List.map (fun x -> x mod 2)
  in
  if eq xs || alternating xs then
    print_endline "YES"
  else
    print_endline "NO"

let () =
  let t = Scanf.scanf "%d\n" id in
  for i = 1 to t do
    solve ()
  done

let getInt () = Scanf.scanf " %d" (fun i -> i);;

let getList(n): int list =
  let rec getList2 ((n: int), (acc: int list)) = match n with
	  | 0 -> List.rev acc
	  | _ -> getList2 (n-1, getInt() :: acc)
  in getList2(n, [])
let rec f((arr: int list), (curr: int)) =
    match arr with
    | [] -> true
    | v1::(v2 :: xs) -> if (v1 mod 2) = curr then f(xs, curr) else false
    | v1::[] -> if (v1 mod 2) = curr then true else false;;

let main() =
  let t = getInt() in
  for i = 1 to t do
    let n = getInt() in let (arr: int list) = getList(n)
  in let a = (f(arr, 0) || f(arr, 1)) in let b = (f(List.tl(arr),0) || f(List.tl(arr), 1)) in
  if (a && b) then print_endline("YES")
  else print_endline("NO")
  done;;

let x = f([2;2;2;2], 0);;
main();;


This is an interactive problem!An arithmetic progression or arithmetic sequence is a sequence of integers such that the subtraction of element with its previous element ($$$x_i - x_{i - 1}$$$, where $$$i \ge 2$$$) is constant — such difference is called a common difference of the sequence.That is, an arithmetic progression is a sequence of form $$$x_i = x_1 + (i - 1) d$$$, where $$$d$$$ is a common difference of the sequence.There is a secret list of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$.It is guaranteed that all elements $$$a_1, a_2, \ldots, a_n$$$ are between $$$0$$$ and $$$10^9$$$, inclusive.This list is special: if sorted in increasing order, it will form an arithmetic progression with positive common difference ($$$d > 0$$$). For example, the list $$$[14, 24, 9, 19]$$$ satisfies this requirement, after sorting it makes a list $$$[9, 14, 19, 24]$$$, which can be produced as $$$x_n = 9 + 5 \cdot (n - 1)$$$.Also you are also given a device, which has a quite discharged battery, thus you can only use it to perform at most $$$60$$$ queries of following two types: Given a value $$$i$$$ ($$$1 \le i \le n$$$), the device will show the value of the $$$a_i$$$. Given a value $$$x$$$ ($$$0 \le x \le 10^9$$$), the device will return $$$1$$$ if an element with a value strictly greater than $$$x$$$ exists, and it will return $$$0$$$ otherwise.Your can use this special device for at most $$$60$$$ queries. Could you please find out the smallest element and the common difference of the sequence? That is, values $$$x_1$$$ and $$$d$$$ in the definition of the arithmetic progression. Note that the array $$$a$$$ is not sorted.

NoteNote that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.The list in the example test is $$$[14, 24, 9, 19]$$$.

open Printf
open String

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let split_string s =
  let s = trim s in

  let n = String.length s in
  let li = fold 0 (n-1)
    (fun i ac -> if s.[i] = ' ' then i::ac else ac) []
  in

  let rec chop prev li ac =
    match li with [] -> List.rev ac
      | i::tail ->
	chop i tail ((sub s (prev+1) (i-prev-1))::ac)
  in

  chop (-1) (List.rev (n::li)) []

let read_int() =
  int_of_string(List.hd(split_string(read_line())))

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

let bound = 1_000_000_000

let () =
  let n = read_int() in
  let rec bsearch lo hi = (* lo < maxval <= hi *)
    if lo+1 = hi then hi else
      let m = lo + (hi-lo)/2 in
      printf "> %d\n%!" m;
      if read_int() = 1 then bsearch m hi else bsearch lo m
  in
  let maxa = bsearch (-1) bound in

  Random.self_init();

  let rec loop i g = if i=0 then g else (
    printf "? %d\n%!" (1+Random.int n);
    let r = read_int() in
    let g = if r = maxa then g else gcd g (abs (r-maxa)) in
    loop (i-1) g
  ) in

  let diff = loop 30 0 in

  printf "! %d %d\n%!" (maxa - (n-1) * diff) diff


Santa has $$$n$$$ candies and he wants to gift them to $$$k$$$ kids. He wants to divide as many candies as possible between all $$$k$$$ kids. Santa can't divide one candy into parts but he is allowed to not use some candies at all.Suppose the kid who recieves the minimum number of candies has $$$a$$$ candies and the kid who recieves the maximum number of candies has $$$b$$$ candies. Then Santa will be satisfied, if the both conditions are met at the same time:  $$$b - a \le 1$$$ (it means $$$b = a$$$ or $$$b = a + 1$$$);  the number of kids who has $$$a+1$$$ candies (note that $$$a+1$$$ not necessarily equals $$$b$$$) does not exceed $$$\lfloor\frac{k}{2}\rfloor$$$ (less than or equal to $$$\lfloor\frac{k}{2}\rfloor$$$). $$$\lfloor\frac{k}{2}\rfloor$$$ is $$$k$$$ divided by $$$2$$$ and rounded down to the nearest integer. For example, if $$$k=5$$$ then $$$\lfloor\frac{k}{2}\rfloor=\lfloor\frac{5}{2}\rfloor=2$$$.Your task is to find the maximum number of candies Santa can give to kids so that he will be satisfied.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. The $$$i$$$-th test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 10^9$$$) — the number of candies and the number of kids.

For each test case print the answer on it — the maximum number of candies Santa can give to kids so that he will be satisfied.

NoteIn the first test case, Santa can give $$$3$$$ and $$$2$$$ candies to kids. There $$$a=2, b=3,a+1=3$$$.In the second test case, Santa can give $$$5, 5, 4$$$ and $$$4$$$ candies. There $$$a=4,b=5,a+1=5$$$. The answer cannot be greater because then the number of kids with $$$5$$$ candies will be $$$3$$$.In the third test case, Santa can distribute candies in the following way: $$$[1, 2, 2, 1, 1, 2, 1]$$$. There $$$a=1,b=2,a+1=2$$$. He cannot distribute two remaining candies in a way to be satisfied.In the fourth test case, Santa can distribute candies in the following way: $$$[3, 3]$$$. There $$$a=3, b=3, a+1=4$$$. Santa distributed all $$$6$$$ candies.

let f n k =
  let a = n / k in (* minimum candies *)
  let r = n - a * k in (* remainder *)
  a * k + min r (k / 2)

let () =
  let n = read_int () in
  for i = 1 to n do
    let t = Scanf.scanf "%d %d\n" f in
    Printf.printf "%d\n" t
  done


Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a friend of Iahub, managed to get hold of the problem before the contest. Because he wants to make sure Iahub will be the one qualified, he tells Iahub the following task.You're given an (1-based) array a with n elements. Let's define function f(i, j) (1 ≤ i, j ≤ n) as (i - j)2 + g(i, j)2. Function g is calculated by the following pseudo-code:int g(int i, int j) {    int sum = 0;    for (int k = min(i, j) + 1; k <= max(i, j); k = k + 1)        sum = sum + a[k];    return sum;}Find a value mini ≠ j  f(i, j).Probably by now Iahub already figured out the solution to this problem. Can you?

The first line of input contains a single integer n (2 ≤ n ≤ 100000). Next line contains n integers a[1], a[2], ..., a[n] ( - 104 ≤ a[i] ≤ 104).

Output a single integer — the value of mini ≠ j  f(i, j).

(* This is a thinly veiled 2D closest pair problem.  I'll use
   Sariel's linear time algorithm.  http://sarielhp.org/blog/?p=51

*)

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 ( ++ ) a b = Int64.add a b

let sq x = x *. x

let closest_pair n p =
  let dist i j =
    let (xi,yi) = p.(i) in
    let (xj,yj) = p.(j) in
    sqrt ((sq (xi -. xj)) +. (sq (yi -. yj)))
  in

  let truncate x r = int_of_float (floor (x /. r)) in
  let boxify (x,y) r = (truncate x r, truncate y r) in

  let getbox h box = try Hashtbl.find h box with Not_found -> [] in

  let add_to_h h i box =
    Hashtbl.replace h box (i::(getbox h box))
  in

  let make_grid i r =
    (* put points p.(0) ... p.(i) into a new grid of size r.
       it has already been established that the closest pair
       in that point set has distance r *)
    let h = Hashtbl.create 10 in

    for j=0 to i do
      add_to_h h j (boxify p.(j) r)
    done;
    h
  in

  let rec loop h i r =
    (* already built the table for points 0...i, and they have dist r *)

    if i=n-1 then r else
      let i = i+1 in
      let (ix,iy) = boxify p.(i) r in
      let li = ref [] in
      for x = ix-1 to ix+1 do
	for y = iy-1 to iy+1 do
	  li := (getbox h (x,y)) @ !li
	done
      done;

      let r' = List.fold_left (
	fun ac j -> min (dist i j) ac
      ) max_float !li in

      if r' < r then (
	loop (make_grid i r') i r'
      ) else (
	add_to_h h i (boxify p.(i) r);
	loop h i r
      )
  in

  let r0 = dist 0 1 in
  loop (make_grid 1 r0) 1 r0

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_long()) in

  let s = Array.copy a in

  for i=1 to n-1 do
    s.(i) <- s.(i-1) ++ s.(i)
  done;

  let p = Array.init n (fun i -> (float i, Int64.to_float s.(i))) in

  Random.init 78458348;

  let swap i j =
    let (pi,pj) = (p.(i),p.(j)) in
    p.(i) <- pj;
    p.(j) <- pi
  in

  for i=0 to n-2 do
    let r = Random.int (n-i) in
    swap i (i+r)
  done;

  let dist = closest_pair n p in

  let idist2 = Int64.of_float (floor ((sq dist) +. 0.5)) in

  printf "%Ld\n" idist2

(* This is a thinly veiled 2D closest pair problem.  I'll use
   Sariel's linear time algorithm.  http://sarielhp.org/blog/?p=51

*)

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 ( ++ ) a b = Int64.add a b

let sq x = x *. x

let closest_pair n p =
  let dist i j =
    let (xi,yi) = p.(i) in
    let (xj,yj) = p.(j) in
    sqrt ((sq (xi -. xj)) +. (sq (yi -. yj)))
  in

  let truncate x r = int_of_float (floor (x /. r)) in
  let boxify (x,y) r = (truncate x r, truncate y r) in

  let getbox h box = try Hashtbl.find h box with Not_found -> [] in

  let add_to_h h i box =
    Hashtbl.replace h box (i::(getbox h box))
  in

  let make_grid i r =
    (* put points p.(0) ... p.(i) into a new grid of size r.
       it has already been established that the closest pair
       in that point set has distance r *)
    let h = Hashtbl.create 10 in

    for j=0 to i do
      add_to_h h j (boxify p.(j) r)
    done;
    h
  in

  let rec loop h i r =
    (* already built the table for points 0...i, and they have dist r *)

    if i=n-1 then r else
      let i = i+1 in
      let (ix,iy) = boxify p.(i) r in
      let li = ref [] in
      for x = ix-1 to ix+1 do
	for y = iy-1 to iy+1 do
	  li := (getbox h (x,y)) @ !li
	done
      done;

      let r' = List.fold_left (
	fun ac j -> min (dist i j) ac
      ) max_float !li in

      if r' < r then
	loop (make_grid i r') i r'
      else
	loop h i r
  in

  let r0 = dist 0 1 in
  loop (make_grid 1 r0) 1 r0

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_long()) in

  let s = Array.copy a in

  for i=1 to n-1 do
    s.(i) <- s.(i-1) ++ s.(i)
  done;

  let p = Array.init n (fun i -> (float i, Int64.to_float s.(i))) in

  Random.init 78458348;

  let swap i j =
    let (pi,pj) = (p.(i),p.(j)) in
    p.(i) <- pj;
    p.(j) <- pi
  in

  for i=0 to n-2 do
    let r = Random.int (n-i) in
    swap i (i+r)
  done;

  let dist = closest_pair n p in

  let idist2 = Int64.of_float (floor ((sq dist) +. 0.5)) in

  printf "%Ld\n" idist2


Little Vasya loves orange juice very much. That's why any food and drink in his kitchen necessarily contains orange juice. There are n drinks in his fridge, the volume fraction of orange juice in the i-th drink equals pi percent.One day Vasya decided to make himself an orange cocktail. He took equal proportions of each of the n drinks and mixed them. Then he wondered, how much orange juice the cocktail has.Find the volume fraction of orange juice in the final drink.

The first input line contains a single integer n (1 ≤ n ≤ 100) — the number of orange-containing drinks in Vasya's fridge. The second line contains n integers pi (0 ≤ pi ≤ 100) — the volume fraction of orange juice in the i-th drink, in percent. The numbers are separated by a space.

Print the volume fraction in percent of orange juice in Vasya's cocktail. The answer will be considered correct if the absolute or relative error does not exceed 10  - 4.

NoteNote to the first sample: let's assume that Vasya takes x milliliters of each drink from the fridge. Then the volume of pure juice in the cocktail will equal  milliliters. The total cocktail's volume equals 3·x milliliters, so the volume fraction of the juice in the cocktail equals , that is, 66.(6) percent.

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

let input () =
    let n = read () in
    let rec loop i juices =
        if i < n
        then
            let var = Pervasives.float (read ()) in
            loop (i + 1) (var :: juices)
        else
            (Pervasives.float n, juices)
    in loop 0 []

let solve (n, juices) =
    let sum = List.fold_left (fun n m -> n +. m) 0.0 juices in
    sum /. n

let print result = Printf.printf "%.10f\n" result
let () = print (solve (input ()))


All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi — a coordinate on the Ox axis. No two cities are located at a single point.Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in).Strange but true, the cost of sending the letter is exactly equal to the distance between the sender's city and the recipient's city.For each city calculate two values mini and maxi, where mini is the minimum cost of sending a letter from the i-th city to some other city, and maxi is the the maximum cost of sending a letter from the i-th city to some other city

The first line of the input contains integer n (2 ≤ n ≤ 105) — the number of cities in Lineland. The second line contains the sequence of n distinct integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109), where xi is the x-coordinate of the i-th city. All the xi's are distinct and follow in ascending order.

Print n lines, the i-th line must contain two integers mini, maxi, separated by a space, where mini is the minimum cost of sending a letter from the i-th city, and maxi is the maximum cost of sending a letter from the i-th city.

let (--) = Int64.sub
let zero = Int64.zero

let read_int () = Scanf.scanf "%d " (fun n -> n)
let read_int64 () = Scanf.scanf "%Ld " (fun n -> n)
let print (min, max) = Printf.printf "%Ld %Ld\n" min max

let make_array () =
    let n = read_int () in
    let array = Array.make n zero in
    let rec loop i array =
        if i < n
        then
            let city = read_int64 () in
            let () = Array.set array i city in
            loop (i + 1) array
        else
            (n, array)
    in loop 0 array

let get_min_max (n, array) i curr =
    let min =
        if i = 0
        then
            array.(i + 1) -- curr
        else
            if i = n - 1
            then
                curr -- array.(i - 1)
            else
                Pervasives.min (curr -- array.(i - 1)) (array.(i + 1) -- curr)
    in
    let max = Pervasives.max (curr -- array.(0)) (array.(n - 1) -- curr) in
    print (min, max)

let () =
    let (n, cities) = make_array () in
    Array.iteri (get_min_max (n, cities)) cities

let (--) = Int64.sub
let zero = Int64.zero

let read_int () = Scanf.scanf "%d " (fun n -> n)
let read_int64 () = Scanf.scanf "%Ld " (fun n -> n)
let print (min, max) = Printf.printf "%Ld %Ld\n" min max

let make_array () =
    let n = read_int () in
    let array = Array.make n zero in
    let rec loop i array =
        if i < n
        then
            let city = read_int64 () in
            let () = Array.set array i city in
            loop (i + 1) array
        else
            (n, array)
    in loop 0 array

let get_first_last (n, array) =
    let first = array.(0) in
    let last = array.(n - 1) in
    (first, last)

let get_min_max (first, last) (n, array) i curr =
    if i = 0
    then
        let min = array.(i + 1) -- curr in
        let max = last -- curr in
        print (min, max)
    else
        if i = n - 1
        then
            let min = curr -- array.(i - 1) in
            let max = curr -- first in
            print (min, max)
        else
            let min = Pervasives.min (curr -- array.(i - 1)) (array.(i + 1) -- curr) in
            let max = Pervasives.max (curr -- first) (last -- curr) in
            print (min, max)

let () =
    let (n, cities) = make_array () in
    let (first, last) = get_first_last (n, cities) in
    Array.iteri (get_min_max (first, last) (n, cities)) cities

let read_int () = Scanf.scanf "%d " (fun n -> n)
let read_int64 () = Scanf.scanf "%Ld " (fun n -> n)

let make_array () =
    let n = read_int () in
    let array = Array.make n Int64.zero in
    let rec loop i array =
        if i < n
        then
            let city = read_int64 () in
            let () = Array.set array i city in
            loop (i + 1) array
        else
            (n, array)
    in loop 0 array

let get_min_max (n, array) =
    let first = array.(0) in
    let last = array.(n - 1) in
    let rec loop i list =
        if i < n
        then
            let curr = array.(i) in
            let min_max =
                if i = 0
                then
                    let min = Int64.sub array.(i + 1) curr in
                    let max = Int64.sub last curr in
                    (min, max)
                else
                    if i = n - 1
                    then
                        let min = Int64.sub curr array.(i - 1) in
                        let max = Int64.sub curr first in
                        (min, max)
                    else
                        let min = Pervasives.min (Int64.sub curr array.(i - 1)) (Int64.sub array.(i + 1) curr) in
                        let max = Pervasives.max (Int64.sub curr first) (Int64.sub last curr) in
                        (min, max)
            in loop (i + 1) (min_max :: list)
        else
            List.rev list
    in loop 0 []

let print list = List.iter (fun (min, max) -> Printf.printf "%Ld %Ld\n" min max) list

let () = print (get_min_max (make_array ()))

open Printf
open Scanf

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

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

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let x = Array.init n (fun _ -> long (read_int())) in

  Array.sort compare x;

  let inf = 1_000_000_000_000L in
  let closest i =
    let li = [] in
    let li = if i=0 then li else (x.(i) -- x.(i-1))::li in
    let li = if i=n-1 then li else (x.(i+1) -- x.(i))::li in
    List.fold_left (fun ac x -> min ac x) inf li
  in

  let farthest i =
    let li = [x.(n-1) -- x.(i); x.(i) -- x.(0)] in
    List.fold_left (fun ac x -> max ac x) 0L li
  in

  for i=0 to n-1 do
    printf "%Ld %Ld\n" (closest i) (farthest i)
  done;

let () = Scanf.scanf "%d\n" (fun n ->
    let a = Array.init n (fun _ -> Scanf.scanf "%Ld%_c" (fun x -> x)) in
    let (--) = Int64.sub in
    Array.iteri (fun i x ->
        let mn, mx =
            if i = 0
            then (a.(1) -- x, a.(n - 1) -- x)
            else if i = n - 1
            then (x -- a.(i - 1), x -- a.(0))
            else (min (x -- a.(i - 1)) (a.(i + 1) -- x),
                  max (x -- a.(0)) (a.(n - 1) -- x))
        in Printf.printf "%Ld %Ld\n" mn mx) a)

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

let make_array () =
    let n = read () in
    let array = Array.make n 0 in
    let rec loop i array =
        if i < n
        then
            let city = read () in
            let () = Array.set array i city in
            loop (i + 1) array
        else
            (n, array)
    in loop 0 array

let get_min_max (n, array) =
    let first = array.(0) in
    let last = array.(n - 1) in
    let rec loop i list =
        if i < n
        then
            let curr = array.(i) in
            let min_max =
                if i = 0
                then
                    let min = array.(i + 1) - curr in
                    let max = last - curr in
                    (min, max)
                else
                    if i = n - 1
                    then
                        let min = curr - array.(i - 1) in
                        let max = curr - first in
                        (min, max)
                    else
                        let min = Pervasives.min (curr - array.(i - 1)) (array.(i + 1) - curr) in
                        let max = Pervasives.max (curr - first) (last - curr) in
                        (min, max)
            in loop (i + 1) (min_max :: list)
        else
            List.rev list
    in loop 0 []

let print list = List.iter (fun (min, max) -> Printf.printf "%d %d\n" min max) list

let () = print (get_min_max (make_array ()))

let () = Scanf.scanf "%d\n" (fun n ->
    let a = Array.init n (fun _ -> Scanf.scanf "%d%_c" (fun x -> x)) in
    Array.iteri (fun i x ->
        let mn, mx =
            if i = 0
            then (a.(1) - x, a.(n - 1) - x)
            else if i = n - 1
            then (x - a.(i - 1), x - a.(0))
            else (min (x - a.(i - 1)) (a.(i + 1) - x),
                  max (x - a.(0)) (a.(n - 1) - x))
        in Printf.printf "%d %d\n" mn mx) a)


Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon.The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous.Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions:   climb one area up;  climb one area down;  jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall. If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon.The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on.The level is considered completed if the ninja manages to get out of the canyon.After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question.

The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly. The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area. The third line describes the right wall in the same format. It is guaranteed that the first area of the left wall is not dangerous.

Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes).

NoteIn the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon. In the second sample there's no way the ninja can get out of the canyon.

exception Found

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let kk = Scanf.bscanf sb "%d " (fun s -> s) in
  let sl = Scanf.bscanf sb "%s " (fun s -> s) in
  let sr = Scanf.bscanf sb "%s " (fun s -> s) in
  let s = [| sl; sr |] in
  let used = Array.make_matrix 2 n false in
  (*let d = Array.make_matrix 2 n (-1) in*)
  let q1 = Array.make (n * 2) (0, 0) in
  let l1 = ref 0 in
  let q2 = Array.make (n * 2) (0, 0) in
  let l2 = ref 0 in
  let rec bfs_step k q1 l1 q2 l2 =
    l2 := 0;
    for i = 0 to !l1 - 1 do
      let (x, y) = q1.(i) in
	if y - 1 > k && s.(x).[y - 1] <> 'X' then (
	  if not used.(x).(y - 1) then (
	    used.(x).(y - 1) <- true;
	    q2.(!l2) <- (x, y - 1);
	    incr l2
	  );
	);
	if y + 1 >= n
	then raise Found;
	if s.(x).[y + 1] <> 'X' then (
	  if not used.(x).(y + 1) then (
	    used.(x).(y + 1) <- true;
	    q2.(!l2) <- (x, y + 1);
	    incr l2
	  );
	);
	if y + kk >= n
	then raise Found;
	if s.(1 - x).[y + kk] <> 'X' then (
	  if not used.(1 - x).(y + kk) then (
	    used.(1 - x).(y + kk) <- true;
	    q2.(!l2) <- (1 - x, y + kk);
	    incr l2
	  );
	);
    done;
    if !l2 > 0
    then bfs_step (k + 1) q2 l2 q1 l1
  in
    used.(0).(0) <- true;
    l1 := 0;
    l2 := 0;
    q1.(!l1) <- (0, 0);
    incr l1;
    try
      bfs_step 0 q1 l1 q2 l2;
      Printf.printf "NO\n"
    with
      | Found ->
	  Printf.printf "YES\n"

module NodeSet = Set.Make (struct
    type t = int * bool
    let compare x y = compare x y
end)

let rec bfs que nodes level visited n k =
    if NodeSet.is_empty que then
        false
    else if NodeSet.max_elt que >= (n,false) then
        true
    else if level > n then
        false
    else
        let nque,visited = NodeSet.fold (fun (x,b) (nque,visited) ->
            if NodeSet.mem (x,b) visited then
                nque,visited
            else
                let visited = NodeSet.add (x,b) visited in
                let next = [(x+1,b);(x-1,b);(x+k,not b)] in
                List.fold_left (fun e (x,b) ->
                    if x > level && (NodeSet.mem (x,b) nodes|| x > n) then
                     (*   let () = print_endline ("("^string_of_int x^","
                                                   ^string_of_bool b^","
                                                   ^string_of_int level^")")in
                      *)  NodeSet.add (x,b) e
                    else
                        e) nque next,visited) que (NodeSet.empty,visited) in
        bfs nque nodes (level+1) visited n k

let solve s1 s2 n k =
    let nodes = ref NodeSet.empty in
    let i = ref 0 in
    let () = String.iter (function
        | '-' -> nodes := NodeSet.add (!i,false) !nodes; i := !i+1;
        |  _  -> i := !i+1) s1 in
    let () = i := 0 in
    let () = String.iter (function
        | '-' -> nodes := NodeSet.add (!i,true) !nodes; i := !i+1;
        |  _  -> i := !i+1) s2 in
    bfs (NodeSet.singleton (0,false)) !nodes 0 NodeSet.empty (n-1) k

let main () =
    let rval = Scanf.bscanf Scanf.Scanning.stdib "%d %d\n%s\n%s\n"
                            (fun n k s1 s2 -> solve s1 s2 n k) in
    if rval then
        print_endline "YES"
    else
        print_endline "NO"

let _ = main ()

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

let () =
    let n = read_int () and k = read_int () in
    let a = Array.make_matrix n 2 ' ' in
    for i = 0 to n - 1 do
        a.(i).(0) <- read_char ()
    done;
    for i = 0 to n - 1 do
        a.(i).(1) <- read_char ()
    done;
    let bfs start =
        let visited = Array.make_matrix n 2 false
        and time = Array.make_matrix n 2 0
        and q = Queue.create ()
        and ans = ref false in
        visited.(fst start).(snd start) <- true;
        Queue.push start q;
        while not (Queue.is_empty q) do
            let (v, side) = Queue.pop q in
            if time.(v).(side) <= v then begin
                if v + k >= n then ans := true;
                let go i j =
                    0 <= i && i <= n - 1 && a.(i).(j) <> 'X' && not visited.(i).(j) in
                if go (v - 1) side then begin
                    visited.(v- 1).(side) <- true;
                    time.(v - 1).(side) <- time.(v).(side) + 1;
                    Queue.push ((v - 1), side) q
                end;
                if go (v + 1) side then begin
                    visited.(v + 1).(side) <- true;
                    time.(v + 1).(side) <- time.(v).(side) + 1;
                    Queue.push ((v + 1), side) q
                end;
                if go (v + k) ((side + 1) mod 2) then begin
                    visited.(v + k).((side + 1) mod 2) <- true;
                    time.(v + k).((side + 1) mod 2) <- time.(v).(side) + 1;
                    Queue.push ((v + k), ((side + 1) mod 2)) q
                end;
            end
        done;
        !ans in
    if bfs (0, 0) then printf "YES\n"
    else printf "NO"

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

let () =
    let n = read_int () and k = read_int () in
    let a = Array.make_matrix n 2 ' ' in
    for i = 0 to n - 1 do
        a.(i).(0) <- read_char ()
    done;
    for i = 0 to n - 1 do
        a.(i).(1) <- read_char ()
    done;
    let bfs start =
        let queue = Array.make (2 * n) (0, 0) and front = ref 0 and back = ref 0 in
        let push w =
            queue.(!back) <- w;
            back := !back + 1 in
        let pop () =
            let x = queue.(!front) in
            front := !front + 1;
            x in
        let visited = Array.make_matrix n 2 false
        and time = Array.make_matrix n 2 0 in
        let ans = ref false in
        visited.(fst start).(snd start) <- true;
        push start;
        while not (!back = !front) do
            let (v, side) = pop () in
            if time.(v).(side) <= v then begin
                if v + k >= n then ans := true;
                let go i j =
                    0 <= i && i <= n - 1 && a.(i).(j) <> 'X' && not visited.(i).(j) in
                if go (v - 1) side then begin
                    visited.(v - 1).(side) <- true;
                    time.(v - 1).(side) <- time.(v).(side) + 1;
                    push ((v - 1), side)
                end;
                if go (v + 1) side then begin
                    visited.(v + 1).(side) <- true;
                    time.(v + 1).(side) <- time.(v).(side) + 1;
                    push ((v + 1), side)
                end;
                if go (v + k) ((side + 1) mod 2) then begin
                    visited.(v + k).((side + 1) mod 2) <- true;
                    time.(v + k).((side + 1) mod 2) <- time.(v).(side) + 1;
                    push ((v + k), ((side + 1) mod 2))
                end;
            end
        done;
        !ans in
    if bfs (0, 0) then printf "YES\n"
    else printf "NO"


Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store. Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group.Jzzhu wonders how to get the maximum possible number of groups. Can you help him?

A single integer n (1 ≤ n ≤ 105), the number of the apples.

The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers — the numbers of apples in the current group. If there are several optimal answers you can print any of them.

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let sieve n =
  let group = Array.make (n+1) [] in
  let sieve = Array.make (n+1) true in

  for i=2 to n do
    if sieve.(i) then (  (* is a prime *)
      let rec loop j = if j > n then () else (
	if sieve.(j) then (
	  group.(i) <- j::group.(i);
	  sieve.(j) <- false;
	);
	loop (j+i)
      ) in
      loop i
    )
  done;
  group

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  if n<3 then printf "0\n" else
    let group = sieve n in
    let mark = Array.init (n+1) (fun i -> i land 1 = 0) in
    let m = n/2 in (* no group beyond m need be considered *)

    for i=3 to m do
      if (List.length group.(i)) land 1 = 1 then (
	group.(i) <- (2*i) :: group.(i);
	mark.(2*i) <- false;
      )
    done;

    let g2 = fold 2 n (fun i ac -> if mark.(i) then i::ac else ac) [] in
    group.(2) <- if List.length g2 land 1 = 0 then g2 else List.tl g2;

    let size = fold 2 m (fun i ac -> ac + List.length group.(i)) 0 in
    printf "%d\n" (size/2);

    for i=2 to m do
      let rec loop li = match li with [] -> ()
	| a::(b::tail) ->
	  printf "%d %d\n" a b;
	  loop tail
	| _ -> failwith "odd-length list"
      in
      loop group.(i)
    done


During the quarantine, Sicromoft has more free time to create the new functions in "Celex-2021". The developers made a new function GAZ-GIZ, which infinitely fills an infinite table to the right and down from the upper left corner as follows:   The cell with coordinates $$$(x, y)$$$ is at the intersection of $$$x$$$-th row and $$$y$$$-th column. Upper left cell $$$(1,1)$$$ contains an integer $$$1$$$.The developers of the SUM function don't sleep either. Because of the boredom, they teamed up with the developers of the RAND function, so they added the ability to calculate the sum on an arbitrary path from one cell to another, moving down or right. Formally, from the cell $$$(x,y)$$$ in one step you can move to the cell $$$(x+1, y)$$$ or $$$(x, y+1)$$$. After another Dinwows update, Levian started to study "Celex-2021" (because he wants to be an accountant!). After filling in the table with the GAZ-GIZ function, he asked you to calculate the quantity of possible different amounts on the path from a given cell $$$(x_1, y_1)$$$ to another given cell $$$(x_2, y_2$$$), if you can only move one cell down or right.Formally, consider all the paths from the cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$ such that each next cell in the path is located either to the down or to the right of the previous one. Calculate the number of different sums of elements for all such paths.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 57179$$$) — the number of test cases. Each of the following $$$t$$$ lines contains four natural numbers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 \le x_1 \le x_2 \le 10^9$$$, $$$1 \le y_1 \le y_2 \le 10^9$$$) — coordinates of the start and the end cells.

For each test case, in a separate line, print the number of possible different sums on the way from the start cell to the end cell.

NoteIn the first test case there are two possible sums: $$$1+2+5=8$$$ and $$$1+3+5=9$$$.

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 long x = Int64.of_int x
let short x = Int64.to_int x

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %Ld %Ld " (fun x y -> (x,y))
let () =
  let cases = read_int () in

  for case = 1 to cases do
    let (x1,y1) = read_pair () in
    let (x2,y2) = read_pair () in

    let max_min_diff = (x2 -- x1) ** (y2 -- y1) in
    printf "%Ld\n" (max_min_diff ++ 1L)
  done


Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task.You are given two matrices $$$A$$$ and $$$B$$$ of size $$$n \times m$$$, each of which consists of $$$0$$$ and $$$1$$$ only. You can apply the following operation to the matrix $$$A$$$ arbitrary number of times: take any submatrix of the matrix $$$A$$$ that has at least two rows and two columns, and invert the values in its corners (i.e. all corners of the submatrix that contain $$$0$$$, will be replaced by $$$1$$$, and all corners of the submatrix that contain $$$1$$$, will be replaced by $$$0$$$). You have to answer whether you can obtain the matrix $$$B$$$ from the matrix $$$A$$$.  An example of the operation. The chosen submatrix is shown in blue and yellow, its corners are shown in yellow. Ramesses don't want to perform these operations by himself, so he asks you to answer this question.A submatrix of matrix $$$M$$$ is a matrix which consist of all elements which come from one of the rows with indices $$$x_1, x_1+1, \ldots, x_2$$$ of matrix $$$M$$$ and one of the columns with indices $$$y_1, y_1+1, \ldots, y_2$$$ of matrix $$$M$$$, where $$$x_1, x_2, y_1, y_2$$$ are the edge rows and columns of the submatrix. In other words, a submatrix is a set of elements of source matrix which form a solid rectangle (i.e. without holes) with sides parallel to the sides of the original matrix. The corners of the submatrix are cells $$$(x_1, y_1)$$$, $$$(x_1, y_2)$$$, $$$(x_2, y_1)$$$, $$$(x_2, y_2)$$$, where the cell $$$(i,j)$$$ denotes the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 500$$$) — the number of rows and the number of columns in matrices $$$A$$$ and $$$B$$$. Each of the next $$$n$$$ lines contain $$$m$$$ integers: the $$$j$$$-th integer in the $$$i$$$-th line is the $$$j$$$-th element of the $$$i$$$-th row of the matrix $$$A$$$ ($$$0 \leq A_{ij} \leq 1$$$).  Each of the next $$$n$$$ lines contain $$$m$$$ integers: the $$$j$$$-th integer in the $$$i$$$-th line is the $$$j$$$-th element of the $$$i$$$-th row of the matrix $$$B$$$ ($$$0 \leq B_{ij} \leq 1$$$).

Print "Yes" (without quotes) if it is possible to transform the matrix $$$A$$$ to the matrix $$$B$$$ using the operations described above, and "No" (without quotes), if it is not possible. You can print each letter in any case (upper or lower).

NoteThe examples are explained below.  Example 1.   Example 2.   Example 3.

let () =
  let n, m = Scanf.scanf "%d %d\n" (fun x y -> (x, y)) in
  let a =
    Array.init n
      (fun _ ->
         Array.init m
           (fun _ -> Scanf.scanf "%d " (fun x -> x))
      )
  in
  let diff =
    Array.init n
      (fun i ->
         Array.init m
           (fun j ->
              let x = Scanf.scanf "%d " (fun x -> x) in
              x = a.(i).(j)
           )
      )
  in
  let acc = ref true in
  for i = 0 to n-2 do
    let n_f = Array.fold_left (fun acc x -> if x then acc else acc + 1) 0 diff.(i) in
    if n_f mod 2 = 0 then
      Array.iteri (fun j x -> if not x then diff.(i+1).(j) <- not diff.(i+1).(j)) diff.(i)
    else acc := false
  done;
  if Array.fold_left (&&) !acc diff.(n-1) then Printf.printf "Yes" else Printf.printf "No"
;;


There are two infinite sources of water:  hot water of temperature $$$h$$$;  cold water of temperature $$$c$$$ ($$$c < h$$$). You perform the following procedure of alternating moves:  take one cup of the hot water and pour it into an infinitely deep barrel;  take one cup of the cold water and pour it into an infinitely deep barrel;  take one cup of the hot water $$$\dots$$$  and so on $$$\dots$$$ Note that you always start with the cup of hot water.The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.You want to achieve a temperature as close as possible to $$$t$$$. So if the temperature in the barrel is $$$t_b$$$, then the absolute difference of $$$t_b$$$ and $$$t$$$ ($$$|t_b - t|$$$) should be as small as possible.How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $$$t$$$? If there are multiple answers with the minimum absolute difference, then print the smallest of them.

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 3 \cdot 10^4$$$) — the number of testcases. Each of the next $$$T$$$ lines contains three integers $$$h$$$, $$$c$$$ and $$$t$$$ ($$$1 \le c < h \le 10^6$$$; $$$c \le t \le h$$$) — the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.

For each testcase print a single positive integer — the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $$$t$$$.

NoteIn the first testcase the temperature after $$$2$$$ poured cups: $$$1$$$ hot and $$$1$$$ cold is exactly $$$20$$$. So that is the closest we can achieve.In the second testcase the temperature after $$$7$$$ poured cups: $$$4$$$ hot and $$$3$$$ cold is about $$$29.857$$$. Pouring more water won't get us closer to $$$t$$$ than that.In the third testcase the temperature after $$$1$$$ poured cup: $$$1$$$ hot is $$$18$$$. That's exactly equal to $$$t$$$.

open Printf
open Scanf

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

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

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

let () =
  let cases = read_int () in
  for case = 1 to cases do
    let h = read_long() in
    let c = read_long() in
    let t = read_long() in

    let solve () =
      let condition ell =
	ell ** h ++ (ell -- 1L) ** c <= (2L ** ell -- 1L) ** t
      in

      let score ell =
	let numerator = Int64.to_float (ell ** h ++ (ell -- 1L) ** c) in
	let denom = Int64.to_float (2L ** ell -- 1L) in
	let tee =  Int64.to_float t in
	abs_float (tee -. numerator /. denom)
      in

      let rec bsearch lo hi =
      (* the condtion holds for hi, does not hold for lo *)
	if lo ++ 1L = hi then hi else
	  let m = (lo ++ hi)//2L in
	  if condition m then bsearch lo m else bsearch m hi
      in

      let rec findhi hi = if condition hi then hi else findhi (hi ++ hi) in

      if t = h then 1L
      else if 2L ** t <= h ++ c then 2L
      else (
	let ell = bsearch 0L (findhi 1L) in
	let ell = if score (ell -- 1L) <= score ell then (ell -- 1L) else ell in
	2L ** ell -- 1L
      )
    in

    printf "%Ld\n" (solve())

  done


Kawashiro Nitori is a girl who loves competitive programming.One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.Given a string $$$s$$$ and a parameter $$$k$$$, you need to check if there exist $$$k+1$$$ non-empty strings $$$a_1,a_2...,a_{k+1}$$$, such that $$$$$$s=a_1+a_2+\ldots +a_k+a_{k+1}+R(a_k)+R(a_{k-1})+\ldots+R(a_{1}).$$$$$$ Here $$$+$$$ represents concatenation. We define $$$R(x)$$$ as a reversed string $$$x$$$. For example $$$R(abcd) = dcba$$$. Note that in the formula above the part $$$R(a_{k+1})$$$ is intentionally skipped.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 100$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case description contains two integers $$$n$$$, $$$k$$$ ($$$1\le n\le 100$$$, $$$0\le k\le \lfloor \frac{n}{2} \rfloor$$$)  — the length of the string $$$s$$$ and the parameter $$$k$$$. The second line of each test case description contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase English letters.

For each test case, print "YES" (without quotes), if it is possible to find $$$a_1,a_2,\ldots,a_{k+1}$$$, and "NO" (without quotes) otherwise. You can print letters in any case (upper or lower).

7
5 1
qwqwq
2 1
ab
3 1
ioi
4 2
icpc
22 0
dokidokiliteratureclub
19 8
imteamshanghaialice
6 3
aaaaaa

NoteIn the first test case, one possible solution is $$$a_1=qw$$$ and $$$a_2=q$$$.In the third test case, one possible solution is $$$a_1=i$$$ and $$$a_2=o$$$.In the fifth test case, one possible solution is $$$a_1=dokidokiliteratureclub$$$.

let solution len str k =
    let rec helper i =
        if i = k
        then true
        else if str.[i] = str.[len-i-1]
        then helper (i+1)
        else false in
    if k * 2 = len
    then false
    else helper 0

let test_case () =
    let len::k::_ =
        read_line ()
        |> Str.split (Str.regexp " ")
        |> List.map int_of_string in
    let str = read_line () in
    (if solution len str k then "YES" else "NO")
    |> print_endline

let () =
    let nb_cases = read_int () in
    for i = 1 to nb_cases do
        test_case ()
    done