Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through $$$n \cdot k$$$ cities. The cities are numerated from $$$1$$$ to $$$n \cdot k$$$, the distance between the neighboring cities is exactly $$$1$$$ km.Sergey does not like beetles, he loves burgers. Fortunately for him, there are $$$n$$$ fast food restaurants on the circle, they are located in the $$$1$$$-st, the $$$(k + 1)$$$-st, the $$$(2k + 1)$$$-st, and so on, the $$$((n-1)k + 1)$$$-st cities, i.e. the distance between the neighboring cities with fast food restaurants is $$$k$$$ km.Sergey began his journey at some city $$$s$$$ and traveled along the circle, making stops at cities each $$$l$$$ km ($$$l > 0$$$), until he stopped in $$$s$$$ once again. Sergey then forgot numbers $$$s$$$ and $$$l$$$, but he remembers that the distance from the city $$$s$$$ to the nearest fast food restaurant was $$$a$$$ km, and the distance from the city he stopped at after traveling the first $$$l$$$ km from $$$s$$$ to the nearest fast food restaurant was $$$b$$$ km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.Now Sergey is interested in two integers. The first integer $$$x$$$ is the minimum number of stops (excluding the first) Sergey could have done before returning to $$$s$$$. The second integer $$$y$$$ is the maximum number of stops (excluding the first) Sergey could have done before returning to $$$s$$$.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 100\,000$$$) — the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively. The second line contains two integers $$$a$$$ and $$$b$$$ ($$$0 \le a, b \le \frac{k}{2}$$$) — the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.

Print the two integers $$$x$$$ and $$$y$$$.

NoteIn the first example the restaurants are located in the cities $$$1$$$ and $$$4$$$, the initial city $$$s$$$ could be $$$2$$$, $$$3$$$, $$$5$$$, or $$$6$$$. The next city Sergey stopped at could also be at cities $$$2, 3, 5, 6$$$. Let's loop through all possible combinations of these cities. If both $$$s$$$ and the city of the first stop are at the city $$$2$$$ (for example, $$$l = 6$$$), then Sergey is at $$$s$$$ after the first stop already, so $$$x = 1$$$. In other pairs Sergey needs $$$1, 2, 3$$$, or $$$6$$$ stops to return to $$$s$$$, so $$$y = 6$$$.In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so $$$l$$$ is $$$2$$$, $$$4$$$, or $$$6$$$. Thus $$$x = 1$$$, $$$y = 3$$$.In the third example there is only one restaurant, so the possible locations of $$$s$$$ and the first stop are: $$$(6, 8)$$$ and $$$(6, 4)$$$. For the first option $$$l = 2$$$, for the second $$$l = 8$$$. In both cases Sergey needs $$$x=y=5$$$ stops to go to $$$s$$$.

module Prelude = struct
  let scanf = Scanf.scanf
  let printf = Printf.printf

  module Int = struct
    type t = int
    let compare = compare
  end
end

open Prelude

module IntSet = Set.Make(Int)

let gen_prime_t = Array.create 100000 0
let gen_prime (upto: int): int array =
  let rec f (n: int) (s: int): int =
    if n > upto
    then s
    else
      let rec is_prime i =
        if i >= s then true
        else
          let t = Array.get gen_prime_t i in
          if t > n / 2 then true
          else if n mod t = 0 then false
          else is_prime (i + 1)
      in f (n + 2) (if is_prime 0 then (Array.set gen_prime_t s n; s + 1) else s)
  in
  Array.set gen_prime_t 0 2;
  let l = f 3 1 in Array.sub gen_prime_t 0 l

module PrimeFactors = struct
  type item = { factor: int; count: int; }
  type t = item list

  let to_string (src: t) =
    src |>
    List.map (fun { factor; count } -> Printf.sprintf "(%d * %d)" count factor) |>
    String.concat " "

  let get_all_factors (src: t): int list =
    let rec outer (n: int) (src: item list) (ret: int list): int list =
      match src with
      | [] -> n :: ret
      | item :: items ->
        let rec inner (i: int) (n: int) (ret: int list): int list =
          if i > item.count
          then ret
          else
            let ret = outer n items ret in
            inner (i + 1) (n * item.factor) ret
        in inner 0 n ret
    in outer 1 src []

end

let factorize (primes: int array) (src: int64): PrimeFactors.t =
  let rec f (cur: int64) (i: int) (ret: PrimeFactors.t) =
    if i >= Array.length primes then ret else
      let rec g (cur: int64) (n: int): int64 * int = Int64.(
        let t = Array.get primes i in
        if (rem cur (of_int t)) = zero
        then g (div cur (of_int t)) (n + 1)
        else (cur, n)
      ) in
      let cur, t = g cur 0 in
      let ret = if t = 0 then ret else { factor = Array.get primes i; count = t } :: ret in
      f cur (i + 1) ret
  in f src 0 []

let rec gcd (lhs: int64) (rhs: int64): int64 =
  if rhs = Int64.zero
  then lhs
  (* NOTE: we don't need the second extra check, the rem will do it ... *)
  (* if lhs > rhs then gcd rhs Int64.(rem lhs rhs) *)
  else gcd rhs Int64.(rem lhs rhs)

let _ =
  (* let primes = gen_prime 100000 in *)
  (* printf "gen complete\n";
   * let factors = factorize primes (Int64.of_int 720720) in
   * PrimeFactors.get_all_factors factors |> List.iter (printf "%d\n")
   * printf "%s\n" (factorize primes (Int64.of_int 8400) |> PrimeFactors.to_string) *)
  let n, k, a, b = scanf "%d %d %d %d" (fun a b c d -> a, b, c, d) in Int64.(
  let total = (mul (of_int n) (of_int k)) in
  let do_try (posb: int64) (maxv, minv): int64 * int64 =
    let time =
      let l = (sub posb (of_int a)) in
      let actual_l = gcd total l in
      (div total actual_l)
    in
    let maxv = if Int64.compare time maxv > 0 then time else maxv in
    let minv = if Int64.compare time minv < 0 then time else minv in
    (maxv, minv)
  in
  let rec test_cycle (posb: int64) (maxv, minv): int64 * int64 =
    if posb > (add total (of_int a))
    then (maxv, minv)
    else let tu = do_try posb (maxv, minv) in test_cycle (add posb (of_int k)) tu
  in
  let tu = (Int64.min_int, Int64.max_int) in
  let tu = test_cycle (sub (of_int k) (of_int b)) tu in
  let tu = test_cycle (add (of_int k) (of_int b)) tu in
  let (maxv, minv) = tu in
  printf "%Ld %Ld\n" minv maxv)
  (* let factors = PrimeFactors.get_all_factors (factorize primes total) in
   * let rec f (factors: int list) (maxv: int64) (minv: int64): int64 * int64 =
   *   match factors with
   *   | [] -> maxv, minv
   *   | l :: factors ->
   *     let dist2 = (rem (add (of_int a) (of_int l)) (of_int k)) in
   *     if (dist2 = (of_int b)) || (dist2 = (of_int (k - b)))
   *     then
   *       let time = (div total (of_int l)) in
   *       let maxv = if Int64.compare time maxv > 0 then time else maxv in
   *       let minv = if Int64.compare time minv < 0 then time else minv in
   *       f factors maxv minv
   *     else f factors maxv minv
   * in
   * let maxv, minv = f factors min_int max_int in *)

module Prelude = struct
  let scanf = Scanf.scanf
  let printf = Printf.printf

  module Int = struct
    type t = int
    let compare = compare
  end
end

open Prelude

module IntSet = Set.Make(Int)

let gen_prime_t = Array.create 100000 0
let gen_prime (upto: int): int array =
  let rec f (n: int) (s: int): int =
    if n > upto
    then s
    else
      let rec is_prime i =
        if i >= s then true
        else
          let t = Array.get gen_prime_t i in
          if t > n / 2 then true
          else if n mod t = 0 then false
          else is_prime (i + 1)
      in f (n + 2) (if is_prime 0 then (Array.set gen_prime_t s n; s + 1) else s)
  in
  Array.set gen_prime_t 0 2;
  let l = f 3 1 in Array.sub gen_prime_t 0 l

module PrimeFactors = struct
  type item = { factor: int; count: int; }
  type t = item list

  let to_string (src: t) =
    src |>
    List.map (fun { factor; count } -> Printf.sprintf "(%d * %d)" count factor) |>
    String.concat " "

  let get_all_factors (src: t): int list =
    let rec outer (n: int) (src: item list) (ret: int list): int list =
      match src with
      | [] -> n :: ret
      | item :: items ->
        let rec inner (i: int) (n: int) (ret: int list): int list =
          if i > item.count
          then ret
          else
            let ret = outer n items ret in
            inner (i + 1) (n * item.factor) ret
        in inner 0 n ret
    in outer 1 src []

end

let factorize (primes: int array) (src: int64): PrimeFactors.t =
  let rec f (cur: int64) (i: int) (ret: PrimeFactors.t) =
    if i >= Array.length primes then ret else
      let rec g (cur: int64) (n: int): int64 * int = Int64.(
        let t = Array.get primes i in
        if (rem cur (of_int t)) = zero
        then g (div cur (of_int t)) (n + 1)
        else (cur, n)
      ) in
      let cur, t = g cur 0 in
      let ret = if t = 0 then ret else { factor = Array.get primes i; count = t } :: ret in
      f cur (i + 1) ret
  in f src 0 []

let rec gcd (lhs: int64) (rhs: int64): int64 =
  if rhs = Int64.zero
  then lhs
  (* NOTE: we don't need the second extra check, the rem will do it ... *)
  (* if lhs > rhs then gcd rhs Int64.(rem lhs rhs) *)
  else gcd rhs Int64.(rem lhs rhs)

let _ =
  (* let primes = gen_prime 100000 in *)
  (* printf "gen complete\n";
   * let factors = factorize primes (Int64.of_int 720720) in
   * PrimeFactors.get_all_factors factors |> List.iter (printf "%d\n")
   * printf "%s\n" (factorize primes (Int64.of_int 8400) |> PrimeFactors.to_string) *)
  let n, k, a, b = scanf "%d %d %d %d" (fun a b c d -> a, b, c, d) in Int64.(
  let total = (mul (of_int n) (of_int k)) in
  let do_try (posb: int64) (maxv, minv): int64 * int64 =
    let time =
      let l = (sub posb (of_int a)) in
      let actual_l = gcd total l in
      (div total actual_l)
    in
    let maxv = if Int64.compare time maxv > 0 then time else maxv in
    let minv = if Int64.compare time minv < 0 then time else minv in
    (maxv, minv)
  in
  let rec test_cycle (posb: int64) (maxv, minv): int64 * int64 =
    if posb > (add total (of_int a))
    then (maxv, minv)
    else let tu = do_try posb (maxv, minv) in test_cycle (add posb (of_int k)) tu
  in
  let tu = (Int64.min_int, Int64.max_int) in
  let tu = test_cycle (sub (of_int k) (of_int b)) tu in
  let tu = test_cycle (add (of_int k) (of_int b)) tu in
  let (maxv, minv) = tu in
  (* let factors = PrimeFactors.get_all_factors (factorize primes total) in
   * let rec f (factors: int list) (maxv: int64) (minv: int64): int64 * int64 =
   *   match factors with
   *   | [] -> maxv, minv
   *   | l :: factors ->
   *     let dist2 = (rem (add (of_int a) (of_int l)) (of_int k)) in
   *     if (dist2 = (of_int b)) || (dist2 = (of_int (k - b)))
   *     then
   *       let time = (div total (of_int l)) in
   *       let maxv = if Int64.compare time maxv > 0 then time else maxv in
   *       let minv = if Int64.compare time minv < 0 then time else minv in
   *       f factors maxv minv
   *     else f factors maxv minv
   * in
   * let maxv, minv = f factors min_int max_int in *)
  printf "%Ld %Ld\n" minv maxv)

module Prelude = struct
  let scanf = Scanf.scanf
  let printf = Printf.printf

  module Int = struct
    type t = int
    let compare = compare
  end
end

open Prelude

module IntSet = Set.Make(Int)

let gen_prime_t = Array.create 100000 0
let gen_prime (upto: int): int array =
  let rec f (n: int) (s: int): int =
    if n > upto
    then s
    else
      let rec is_prime i =
        if i >= s then true
        else
          let t = Array.get gen_prime_t i in
          if t > n / 2 then true
          else if n mod t = 0 then false
          else is_prime (i + 1)
      in f (n + 2) (if is_prime 0 then (Array.set gen_prime_t s n; s + 1) else s)
  in
  Array.set gen_prime_t 0 2;
  let l = f 3 1 in Array.sub gen_prime_t 0 l

module PrimeFactors = struct
  type item = { factor: int; count: int; }
  type t = item list

  let to_string (src: t) =
    src |>
    List.map (fun { factor; count } -> Printf.sprintf "(%d * %d)" count factor) |>
    String.concat " "

  let get_all_factors (src: t): int list =
    let rec outer (n: int) (src: item list) (ret: int list): int list =
      match src with
      | [] -> n :: ret
      | item :: items ->
        let rec inner (i: int) (n: int) (ret: int list): int list =
          if i > item.count
          then ret
          else
            let ret = outer n items ret in
            inner (i + 1) (n * item.factor) ret
        in inner 0 n ret
    in outer 1 src []

end

let factorize (primes: int array) (src: int64): PrimeFactors.t =
  let rec f (cur: int64) (i: int) (ret: PrimeFactors.t) =
    if i >= Array.length primes then ret else
      let rec g (cur: int64) (n: int): int64 * int = Int64.(
        let t = Array.get primes i in
        if (rem cur (of_int t)) = zero
        then g (div cur (of_int t)) (n + 1)
        else (cur, n)
      ) in
      let cur, t = g cur 0 in
      let ret = if t = 0 then ret else { factor = Array.get primes i; count = t } :: ret in
      f cur (i + 1) ret
  in f src 0 []

let rec gcd (lhs: int64) (rhs: int64): int64 =
  if rhs = Int64.zero
  then lhs
  (* NOTE: we don't need the second extra check, the rem will do it ... *)
  (* if lhs > rhs then gcd rhs Int64.(rem lhs rhs) *)
  else gcd rhs Int64.(rem lhs rhs)

let _ =
  (* let primes = gen_prime 100000 in *)
  (* printf "gen complete\n";
   * let factors = factorize primes (Int64.of_int 720720) in
   * PrimeFactors.get_all_factors factors |> List.iter (printf "%d\n")
   * printf "%s\n" (factorize primes (Int64.of_int 8400) |> PrimeFactors.to_string) *)
  let n, k, a, b = scanf "%d %d %d %d" (fun a b c d -> a, b, c, d) in Int64.(
  let n, k, a, b = of_int n, of_int k, of_int a, of_int b in
  let total = (mul n k) in
  let do_try (posb: int64) (maxv, minv): int64 * int64 =
    let time =
      let l = (sub posb a) in
      let actual_l = gcd total l in
      (div total actual_l)
    in
    let maxv = if Int64.compare time maxv > 0 then time else maxv in
    let minv = if Int64.compare time minv < 0 then time else minv in
    (maxv, minv)
  in
  let rec test_cycle (posb: int64) (maxv, minv): int64 * int64 =
    if posb > (add total a)
    then (maxv, minv)
    else let tu = do_try posb (maxv, minv) in test_cycle (add posb k) tu
  in
  let tu = (Int64.min_int, Int64.max_int) in
  let tu = tu |> test_cycle (add k b) |> test_cycle (sub k b) in
  let (maxv, minv) = tu in
  printf "%Ld %Ld\n" minv maxv)
  (* let factors = PrimeFactors.get_all_factors (factorize primes total) in
   * let rec f (factors: int list) (maxv: int64) (minv: int64): int64 * int64 =
   *   match factors with
   *   | [] -> maxv, minv
   *   | l :: factors ->
   *     let dist2 = (rem (add (of_int a) (of_int l)) (of_int k)) in
   *     if (dist2 = (of_int b)) || (dist2 = (of_int (k - b)))
   *     then
   *       let time = (div total (of_int l)) in
   *       let maxv = if Int64.compare time maxv > 0 then time else maxv in
   *       let minv = if Int64.compare time minv < 0 then time else minv in
   *       f factors maxv minv
   *     else f factors maxv minv
   * in
   * let maxv, minv = f factors min_int max_int in *)

module Prelude = struct
  let scanf = Scanf.scanf
  let printf = Printf.printf

  module Int = struct
    type t = int
    let compare = compare
  end
end

open Prelude

module IntSet = Set.Make(Int)

let gen_prime_t = Array.create 100000 0
let gen_prime (upto: int): int array =
  let rec f (n: int) (s: int): int =
    if n > upto
    then s
    else
      let rec is_prime i =
        if i >= s then true
        else
          let t = Array.get gen_prime_t i in
          if t > n / 2 then true
          else if n mod t = 0 then false
          else is_prime (i + 1)
      in f (n + 2) (if is_prime 0 then (Array.set gen_prime_t s n; s + 1) else s)
  in
  Array.set gen_prime_t 0 2;
  let l = f 3 1 in Array.sub gen_prime_t 0 l

module PrimeFactors = struct
  type item = { factor: int; count: int; }
  type t = item list

  let to_string (src: t) =
    src |>
    List.map (fun { factor; count } -> Printf.sprintf "(%d * %d)" count factor) |>
    String.concat " "

  let get_all_factors (src: t): int list =
    let rec outer (n: int) (src: item list) (ret: int list): int list =
      match src with
      | [] -> n :: ret
      | item :: items ->
        let rec inner (i: int) (n: int) (ret: int list): int list =
          if i > item.count
          then ret
          else
            let ret = outer n items ret in
            inner (i + 1) (n * item.factor) ret
        in inner 0 n ret
    in outer 1 src []

end

let factorize (primes: int array) (src: int64): PrimeFactors.t =
  let rec f (cur: int64) (i: int) (ret: PrimeFactors.t) =
    if i >= Array.length primes then ret else
      let rec g (cur: int64) (n: int): int64 * int = Int64.(
        let t = Array.get primes i in
        if (rem cur (of_int t)) = zero
        then g (div cur (of_int t)) (n + 1)
        else (cur, n)
      ) in
      let cur, t = g cur 0 in
      let ret = if t = 0 then ret else { factor = Array.get primes i; count = t } :: ret in
      f cur (i + 1) ret
  in f src 0 []

  (* NOTE: we don't need the second extra check, the rem will do it ... *)
  (* if lhs > rhs then gcd rhs Int64.(rem lhs rhs) *)
let rec gcd (lhs: int64) (rhs: int64): int64 =
  Int64.(if rhs = zero then lhs else gcd rhs (rem lhs rhs))

let _ =
  (* let primes = gen_prime 100000 in *)
  (* printf "gen complete\n";
   * let factors = factorize primes (Int64.of_int 720720) in
   * PrimeFactors.get_all_factors factors |> List.iter (printf "%d\n")
   * printf "%s\n" (factorize primes (Int64.of_int 8400) |> PrimeFactors.to_string) *)
  let n, k, a, b = scanf "%d %d %d %d" (fun a b c d -> a, b, c, d) in Int64.(
  let n, k, a, b = of_int n, of_int k, of_int a, of_int b in
  let total = (mul n k) in
  let do_try (posb: int64) (maxv, minv): int64 * int64 =
    let time = (div total (gcd total (sub posb a))) in (* let l = (sub posb a) in let actual_l = gcd total l in *)
    ((if time > maxv then time else maxv), (if time < minv then time else minv))
  in
  let rec test_cycle (posb: int64) (maxv, minv): int64 * int64 =
    if posb > (add total a)
    then (maxv, minv)
    else test_cycle (add posb k) (do_try posb (maxv, minv))
  in
  let tu = (Int64.min_int, Int64.max_int) in
  let tu = tu |> test_cycle (add k b) |> test_cycle (sub k b) in
  let (maxv, minv) = tu in
  printf "%Ld %Ld\n" minv maxv)
  (* let factors = PrimeFactors.get_all_factors (factorize primes total) in
   * let rec f (factors: int list) (maxv: int64) (minv: int64): int64 * int64 =
   *   match factors with
   *   | [] -> maxv, minv
   *   | l :: factors ->
   *     let dist2 = (rem (add (of_int a) (of_int l)) (of_int k)) in
   *     if (dist2 = (of_int b)) || (dist2 = (of_int (k - b)))
   *     then
   *       let time = (div total (of_int l)) in
   *       let maxv = if Int64.compare time maxv > 0 then time else maxv in
   *       let minv = if Int64.compare time minv < 0 then time else minv in
   *       f factors maxv minv
   *     else f factors maxv minv
   * in
   * let maxv, minv = f factors min_int max_int in *)

module Prelude = struct
  let scanf = Scanf.scanf
  let printf = Printf.printf

  module Int = struct
    type t = int
    let compare = compare
  end
end

open Prelude

module IntSet = Set.Make(Int)

let gen_prime_t = Array.create 100000 0
let gen_prime (upto: int): int array =
  let rec f (n: int) (s: int): int =
    if n > upto
    then s
    else
      let rec is_prime i =
        if i >= s then true
        else
          let t = Array.get gen_prime_t i in
          if t > n / 2 then true
          else if n mod t = 0 then false
          else is_prime (i + 1)
      in f (n + 2) (if is_prime 0 then (Array.set gen_prime_t s n; s + 1) else s)
  in
  Array.set gen_prime_t 0 2;
  let l = f 3 1 in Array.sub gen_prime_t 0 l

module PrimeFactors = struct
  type item = { factor: int; count: int; }
  type t = item list

  let to_string (src: t) =
    src |>
    List.map (fun { factor; count } -> Printf.sprintf "(%d * %d)" count factor) |>
    String.concat " "

  let get_all_factors (src: t): int list =
    let rec outer (n: int) (src: item list) (ret: int list): int list =
      match src with
      | [] -> n :: ret
      | item :: items ->
        let rec inner (i: int) (n: int) (ret: int list): int list =
          if i > item.count
          then ret
          else
            let ret = outer n items ret in
            inner (i + 1) (n * item.factor) ret
        in inner 0 n ret
    in outer 1 src []

end

let factorize (primes: int array) (src: int64): PrimeFactors.t =
  let rec f (cur: int64) (i: int) (ret: PrimeFactors.t) =
    if i >= Array.length primes then ret else
      let rec g (cur: int64) (n: int): int64 * int = Int64.(
        let t = Array.get primes i in
        if (rem cur (of_int t)) = zero
        then g (div cur (of_int t)) (n + 1)
        else (cur, n)
      ) in
      let cur, t = g cur 0 in
      let ret = if t = 0 then ret else { factor = Array.get primes i; count = t } :: ret in
      f cur (i + 1) ret
  in f src 0 []

let _ =
  let primes = gen_prime 100000 in
  (* printf "gen complete\n";
   * let factors = factorize primes (Int64.of_int 720720) in
   * PrimeFactors.get_all_factors factors |> List.iter (printf "%d\n")
   * printf "%s\n" (factorize primes (Int64.of_int 8400) |> PrimeFactors.to_string) *)
  let n, k, a, b = scanf "%d %d %d %d" (fun a b c d -> a, b, c, d) in Int64.(
  let total = (mul (of_int n) (of_int k)) in
  let factors = PrimeFactors.get_all_factors (factorize primes total) in
  let rec f (factors: int list) (maxv: int64) (minv: int64): int64 * int64 =
    match factors with
    | [] -> maxv, minv
    | l :: factors ->
      let dist2 = (rem (add (of_int a) (of_int l)) (of_int k)) in
      if (dist2 = (of_int b)) || (dist2 = (of_int (k - b)))
      then
        let time = (div total (of_int l)) in
        let maxv = if Int64.compare time maxv > 0 then time else maxv in
        let minv = if Int64.compare time minv < 0 then time else minv in
        f factors maxv minv
      else f factors maxv minv
  in
  let maxv, minv = f factors min_int max_int in
  printf "%Ld %Ld\n" minv maxv)


Let's write all the positive integer numbers one after another from $$$1$$$ without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...Your task is to print the $$$k$$$-th digit of this sequence.

The first and only line contains integer $$$k$$$ ($$$1 \le k \le 10000$$$) — the position to process ($$$1$$$-based index).

Print the $$$k$$$-th digit of the resulting infinite sequence.

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

let () =

  let n = ref (Scanf.scanf "%Ld\n" (fun x -> x)) in
  let i = ref 1 in
  let k = ref 0L in
  let res = ref 0L in
  while (!n >= 0L) do
    k := 9L;
    for _j = 1 to !i - 1 do
      k := !k * 10L
    done;

    res := !res + !k;
    n := !n -- !k * Int64.of_int !i;
    incr i;
  done;
  decr i;
  n := !n + !k * Int64.of_int !i;
  res := !res -- !k;

  while (!n > 0L) do
    n := !n -- Int64.of_int !i;
    res := !res + 1L;
  done;
  let res_str = string_of_int (Int64.to_int !res) in
  try
    Printf.printf "%c" res_str.[Int64.to_int (Int64.of_int !i + !n)-1]
  with _ ->     Printf.printf "%c" res_str.[Int64.to_int (Int64.of_int !i + !n)-2]
;;

let () =

  let n = ref (Scanf.scanf "%d\n" (fun x -> x)) in
  let i = ref 1 in
  let k = ref 0 in
  let res = ref 0 in
  while (!n >= 0) do
    k := 9;
    for _j = 1 to !i - 1 do
      k := !k * 10
    done;

    res := !res + !k;
    n := !n - !k * !i;
    incr i;
  done;
  decr i;
  n := !n + !k * !i;
  res := !res - !k;

  Printf.printf "%d %d %d\n" !n !res !i;
  let ref j = 0 in
  while (!n > 0) do
    n := !n - !i;
    incr res;
  done;
  (* n := !n + !i; *)
  (* decr res; *)
  (* Printf.printf "%d %d %d\n" !n !res !i; *)
  (* Printf.printf "%d " !res; *)
  let res_str = string_of_int !res in
  Printf.printf "%c" res_str.[!i + !n-1]
;;


Alice has a lovely piece of cloth. It has the shape of a square with a side of length $$$a$$$ centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length $$$b$$$ centimeters (where $$$b < a$$$). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).Alice would like to know whether the area of her cloth expressed in square centimeters is prime. Could you help her to determine it?

The first line contains a number $$$t$$$ ($$$1 \leq t \leq 5$$$) — the number of test cases. Each of the next $$$t$$$ lines describes the $$$i$$$-th test case. It contains two integers $$$a$$$ and $$$b~(1 \leq b < a \leq 10^{11})$$$ — the side length of Alice's square and the side length of the square that Bob wants.

Print $$$t$$$ lines, where the $$$i$$$-th line is the answer to the $$$i$$$-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO". You can print each letter in an arbitrary case (upper or lower).

NoteThe figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is $$$6^2 - 5^2 = 36 - 25 = 11$$$, which is prime, so the answer is "YES".  In the second case, the area is $$$16^2 - 13^2 = 87$$$, which is divisible by $$$3$$$.  In the third case, the area of the remaining piece is $$$61690850361^2 - 24777622630^2 = 3191830435068605713421$$$. This number is not prime because $$$3191830435068605713421 = 36913227731 \cdot 86468472991 $$$.In the last case, the area is $$$34^2 - 33^2 = 67$$$.

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 let abs v = abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) let min_int,max_int = min_int,max_int end
module MyInt64 = struct
	let (%$) = (mod) 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 repb 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; !f let repm from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod !m (~~| !i) with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; !m let repmb from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod (~~| !i) !m with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; (!m,!f) 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
	let abs v = Int64.abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = let open Int64 in (logand),(logor),(logxor),lognot,(fun u v -> shift_left u (~|v)),(fun u v -> shift_right u (~|v)),(fun u v -> shift_right_logical u (~|v)) let min_int,max_int = Int64.min_int,Int64.max_int let get_string _ = scanf " %s" (fun v -> v)
	let i32,i64 = Int64.to_int,Int64.of_int let alen a = i64 @@ Array.length a
	let dump_array a = a |> Array.to_list |> string_of_list Int64.to_string |> print_endline let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y) let repi 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
end open MyInt64

let is_prime l =
	(* printf "%Ld\n" l; *)
	if l = 2L then true else
	let imax = 1L + (Int64.of_float @@ sqrt @@ Int64.to_float l)
	in let i = ref 3L
	in let f = ref true in
	while !f && !i <= imax do
		(* printf "%Ld: %Ld mod %Ld = %Ld: %Ld\n" !i l !i (l mod !i) imax; *)
		if l mod !i = 0L then (
			f := false;
		);
		i += 2L;
	done;
	(* if !f then f := not (l mod 2L = 0L); *)
	f := !f && (l mod 2L <> 0L);
	!f

let () =
	let t = get_i64 0 in
	rep 1L t (fun i ->
		let a,b = get_2_i64 0 in
		(
			if b = 0L && a = 1L then "NO"
			else if a - b = 1L && is_prime (a+b)
			then "YES" else "NO"
		) |> print_endline; `Ok
	)

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 let abs v = abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) let min_int,max_int = min_int,max_int end
module MyInt64 = struct
	let (%$) = (mod) 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 repb 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; !f let repm from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod !m (~~| !i) with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; !m let repmb from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod (~~| !i) !m with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; (!m,!f) 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
	let abs v = Int64.abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = let open Int64 in (logand),(logor),(logxor),lognot,(fun u v -> shift_left u (~|v)),(fun u v -> shift_right u (~|v)),(fun u v -> shift_right_logical u (~|v)) let min_int,max_int = Int64.min_int,Int64.max_int let get_string _ = scanf " %s" (fun v -> v)
	let i32,i64 = Int64.to_int,Int64.of_int let alen a = i64 @@ Array.length a
	let dump_array a = a |> Array.to_list |> string_of_list Int64.to_string |> print_endline let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y) let repi 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
end open MyInt64

(* let is_prime l =
	printf "%Ld\n" l;
	let imax = 1L + (Int64.of_float @@ sqrt @@ Int64.to_float l)
	in let i = ref 3L
	in let f = ref true in
	while !f && !i <= imax do
		printf "%Ld: %Ld mod %Ld = %Ld: %Ld\n" !i l !i (l mod !i) imax;
		if l mod !i = 0L then (
			f := false;
		);
		i += 2L;
	done;
	if !f then f := not (l mod 2L = 0L);
	!f *)

let () =
	let t = get_i64 0 in
	rep 1L t (fun i ->
		let a,b = get_2_i64 0 in
		let p,q = a+b, a-b in
		(
			if
			(* is_prime p && is_prime q *)
			q = 1L
			then "YES" else "NO"
		) |> print_endline; `Ok
	)

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 let abs v = abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) let min_int,max_int = min_int,max_int end
module MyInt64 = struct
	let (%$) = (mod) 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 repb 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; !f let repm from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod !m (~~| !i) with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; !m let repmb from_ to_ m_init fbod = let i,f,m = ref ~|from_,ref true,ref m_init in while !i <= ~|to_ && !f do match fbod (~~| !i) !m with | `Break -> f := false | `Break_m m' -> (f := false; m := m') | `Ok m' -> (i := !i +$ 1; m := m') done; (!m,!f) 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
	let abs v = Int64.abs v let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) = let open Int64 in (logand),(logor),(logxor),lognot,(fun u v -> shift_left u (~|v)),(fun u v -> shift_right u (~|v)),(fun u v -> shift_right_logical u (~|v)) let min_int,max_int = Int64.min_int,Int64.max_int let get_string _ = scanf " %s" (fun v -> v)
	let i32,i64 = Int64.to_int,Int64.of_int let alen a = i64 @@ Array.length a
	let dump_array a = a |> Array.to_list |> string_of_list Int64.to_string |> print_endline let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y) let repi 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
end open MyInt64

(* let is_prime l =
	printf "%Ld\n" l;
	let imax = 1L + (Int64.of_float @@ sqrt @@ Int64.to_float l)
	in let i = ref 3L
	in let f = ref true in
	while !f && !i <= imax do
		printf "%Ld: %Ld mod %Ld = %Ld: %Ld\n" !i l !i (l mod !i) imax;
		if l mod !i = 0L then (
			f := false;
		);
		i += 2L;
	done;
	if !f then f := not (l mod 2L = 0L);
	!f *)

let () =
	let t = get_i64 0 in
	rep 1L t (fun i ->
		let a,b = get_2_i64 0 in
		(
			if b = 0L && a = 1L then "NO"
			else if a - b = 1L
			then "YES" else "NO"
		) |> print_endline; `Ok
	)


Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. He played a lot of games today with his friend Arseny. The friends became tired and didn't finish the last game. It was Ilya's turn in the game when they left it. Determine whether Ilya could have won the game by making single turn or not. The rules of tic-tac-toe on the 4 × 4 field are as follows. Before the first turn all the field cells are empty. The two players take turns placing their signs into empty cells (the first player places Xs, the second player places Os). The player who places Xs goes first, the another one goes second. The winner is the player who first gets three of his signs in a row next to each other (horizontal, vertical or diagonal).

The tic-tac-toe position is given in four lines. Each of these lines contains four characters. Each character is '.' (empty cell), 'x' (lowercase English letter x), or 'o' (lowercase English letter o). It is guaranteed that the position is reachable playing tic-tac-toe, and it is Ilya's turn now (in particular, it means that the game is not finished). It is possible that all the cells are empty, it means that the friends left without making single turn.

Print single line: "YES" in case Ilya could have won by making single turn, and "NO" otherwise.

NoteIn the first example Ilya had two winning moves: to the empty cell in the left column and to the leftmost empty cell in the first row.In the second example it wasn't possible to win by making single turn.In the third example Ilya could have won by placing X in the last row between two existing Xs.In the fourth example it wasn't possible to win by making single turn.

open Printf
open Scanf

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

let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let board = Array.init 4 (fun _ -> read_string()) in

  let isx i j = (0 <= i && i < 4) && (0 <= j && j < 4) && board.(i).[j] = 'x' in
  let isempty i j = board.(i).[j] = '.' in

  let dir = [|(0,1);(1,0);(0,-1);(-1,0);(1,1);(1,-1);(-1,-1);(-1,1)|] in

  let windir i j d =
    let (dx,dy) = dir.(d) in
    (isx (i+dx) (j+dy)) && (isx (i+2*dx) (j+2*dy)) ||
      (isx (i+dx) (j+dy)) && (isx (i-dx) (j-dy))
  in

  let win i j =
    if not (isempty i j) then false else
      exists 0 7 (fun d -> windir i j d)
  in

  if exists 0 3 (fun i -> exists 0 3 (fun j -> win i j)) then printf "YES\n" else printf "NO\n";


There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names.Political parties find their number in the ballot highly important. Overall there are m possible numbers: 1, 2, ..., m. Each of these 7 parties is going to be assigned in some way to exactly one number, at that, two distinct parties cannot receive the same number.The Little Elephant Political Party members believe in the lucky digits 4 and 7. They want to evaluate their chances in the elections. For that, they need to find out, how many correct assignments are there, such that the number of lucky digits in the Little Elephant Political Party ballot number is strictly larger than the total number of lucky digits in the ballot numbers of 6 other parties. Help the Little Elephant Political Party, calculate this number. As the answer can be rather large, print the remainder from dividing it by 1000000007 (109 + 7).

A single line contains a single positive integer m (7 ≤ m ≤ 109) — the number of possible numbers in the ballot.

In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

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

let mval = 1_000_000_007L

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

(*------------create binomial--------------*)
let init_binomial n m =
  (* compute an array b so that b.(p).(q) = (p choose q),
     where 0 <= p <= n and 0 <= q <= m. *)
  let b = Array.make_matrix (n+1) (m+1) 0 in
    for i=0 to n do for j=0 to min i m do
      b.(i).(j) <- if j=0 then 1 else b.(i-1).(j-1) + b.(i-1).(j)
    done done;
    b
let binomial = init_binomial 9 9

let rec power a i =
  if i=0 then 1 else
    let p = power a (i/2) in
      p * p * (if i land 1 = 0 then 1 else a)

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum64 i j f = fold i j (fun i a -> Int64.rem ((f i) ++ a) mval) 0L
let sum i j f = fold i j (fun i a -> (f i) + a) 0

let m = read_int()
let rec dlist n = if n=0 then [] else (n mod 10)::(dlist (n/10))
let digits = List.rev (dlist m)
let ndigits = List.length digits
(* Now make a table a.(l) that will store the number of numbers
   in the range 1...m with l lucky digits *)
let a = Array.make (ndigits+1) 0L

let f d l z_not_allowed =
  (* the number of numbers with d digits with l lucky digits *)
  (* z_not_allowed = true if zero is not an allowed choice *)
  if l<0 || l>d then 0 else
    let a = binomial.(d).(l) * (power 2 l) * (power 8 (d-l)) in
      if z_not_allowed && l=0 then a-1 else a

let lcount k = if k=4 || k=7 then 1 else 0

let rec dloop dlist l first =
  match dlist with [] -> if l=0 then 1 else 0
    | dig::dlist ->
	let d = List.length dlist in
	let a = sum 0 (dig-1) (fun k -> f d (l - (lcount k)) (first && k=0)) in
	  a + (dloop dlist (l - (lcount dig)) false)

let () =
  for l=0 to ndigits do
    a.(l) <- Int64.of_int (dloop digits l true)
  done

(* now we've computed the a.() as descrbed above *)

let rec loop el p =
  (* el = excess lucky you can use, p=parties left to do *)
  if el < 0 then 0L
  else if p=0 then 1L
  else
    let max_luckiness = if p=7 then ndigits else min el ndigits in
      sum64 0 max_luckiness (
	fun ml ->
	  if a.(ml) = 0L then 0L else
	    let aml = a.(ml) in
	      a.(ml) <- a.(ml) -- 1L;
	      let new_el = if p=7 then ml-1 else el-ml in
	      let an = Int64.rem (aml ** (loop new_el (p-1))) mval in
		a.(ml) <- a.(ml) ++ 1L;
		an
      )

let () = Printf.printf "%s\n" (Int64.to_string (loop 0 7))


An n × n table a is defined as follows:  The first row and the first column contain ones, that is: ai, 1 = a1, i = 1 for all i = 1, 2, ..., n.  Each of the remaining numbers in the table is equal to the sum of the number above it and the number to the left of it. In other words, the remaining elements are defined by the formula ai, j = ai - 1, j + ai, j - 1. These conditions define all the values in the table.You are given a number n. You need to determine the maximum value in the n × n table defined by the rules above.

The only line of input contains a positive integer n (1 ≤ n ≤ 10) — the number of rows and columns of the table.

Print a single line containing a positive integer m — the maximum value in the table.

NoteIn the second test the rows of the table look as follows: {1, 1, 1, 1, 1},  {1, 2, 3, 4, 5},  {1, 3, 6, 10, 15},  {1, 4, 10, 20, 35},  {1, 5, 15, 35, 70}.

let solve () =
    let n = Scanf.scanf "%d" (fun n -> n) in
    let rec f i =
        if i = 0
        then
            1
        else
            (f (i - 1) * (i * 2) * ((i * 2) - 1)/ i )/ i in
    f (n - 1)

let print result = Printf.printf "%d\n" result
let () = print (solve ())

let solve () =
    let n = Scanf.scanf "%d" (fun n -> n) in
    let rec loop i prev =
        if i < n
        then
            let rec looper j current =
                if j < n
                then
                    (let element = (Array.get prev j) + (Array.get current (j - 1)) in
                    Array.set current j element;
                    looper (j + 1) current)
                else
                    loop (i + 1) current
            in looper 1 (Array.make n 1)
        else
            Array.get prev (n - 1)
    in loop 1 (Array.make n 1)

let print result = Printf.printf "%d\n" result
let () = print (solve ())

open Printf
open Scanf

let ( ++ ) a b = Int64.add a b

(*------------create binomial--------------*)
let init_binomial n m =
  (* compute an array b so that b.(p).(q) = (p choose q),
     where 0 <= p <= n and 0 <= q <= m. *)
  let b = Array.make_matrix (n+1) (m+1) 0L in
    for i=0 to n do for j=0 to min i m do
      b.(i).(j) <- if j=0 then 1L else b.(i-1).(j-1) ++ b.(i-1).(j)
    done done;
    b

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

  let b = init_binomial (2*(n-1)) (2*(n-1)) in

  printf "%Ld\n" (b.(2*(n-1)).(n-1))

let () = Scanf.scanf "%d\n" (fun n ->
    let arr = Array.make_matrix n n 1 in
    for i = 1 to n - 1 do
        for j = 1 to n - 1 do
            arr.(i).(j) <- arr.(i-1).(j) + arr.(i).(j-1)
        done
    done;
    Printf.printf "%d\n" arr.(n-1).(n-1))


Vasya is an active Internet user. One day he came across an Internet resource he liked, so he wrote its address in the notebook. We know that the address of the written resource has format: <protocol>://<domain>.ru[/<context>] where:  <protocol> can equal either "http" (without the quotes) or "ftp" (without the quotes),  <domain> is a non-empty string, consisting of lowercase English letters,  the /<context> part may not be present. If it is present, then <context> is a non-empty string, consisting of lowercase English letters. If string <context> isn't present in the address, then the additional character "/" isn't written. Thus, the address has either two characters "/" (the ones that go before the domain), or three (an extra one in front of the context).When the boy came home, he found out that the address he wrote in his notebook had no punctuation marks. Vasya must have been in a lot of hurry and didn't write characters ":", "/", ".".Help Vasya to restore the possible address of the recorded Internet resource.

The first line contains a non-empty string that Vasya wrote out in his notebook. This line consists of lowercase English letters only.  It is guaranteed that the given string contains at most 50 letters. It is guaranteed that the given string can be obtained from some correct Internet resource address, described above.

Print a single line — the address of the Internet resource that Vasya liked. If there are several addresses that meet the problem limitations, you are allowed to print any of them.

NoteIn the second sample there are two more possible answers: "ftp://httpruru.ru" and "ftp://httpru.ru/ru".

let substr str s e =
  String.sub str s (e - s)

let find str substr pos =
  let sub_len = String.length substr in
  let rec check i =
    if i + sub_len <= String.length str then
      if (String.sub str i sub_len) = substr then
        Some i
      else check (i + 1)
    else None
  in
  check pos

let punc str =
  let protocol =
    match substr str 0 3 with
    | "ftp" as p -> p | _ -> "http"
  in
  let p_size = String.length protocol in
  let domain =
    let ru_start = find str "ru" (p_size + 1) in
    match ru_start with
    | None -> assert false
    | Some pos -> substr str p_size pos
  in
  let contstart = p_size + String.length domain + 2 in
  let strlen = String.length str in
  let content =
    if strlen - contstart > 0 then
      String.concat "" ["/"; substr str contstart strlen]
    else ""
  in
  String.concat "" [protocol; "://"; domain; ".ru"; content]

let () =
read_line () |> punc |> print_endline


Professor GukiZ makes a new robot. The robot are in the point with coordinates (x1, y1) and should go to the point (x2, y2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position.

The first line contains two integers x1, y1 ( - 109 ≤ x1, y1 ≤ 109) — the start position of the robot. The second line contains two integers x2, y2 ( - 109 ≤ x2, y2 ≤ 109) — the finish position of the robot.

Print the only integer d — the minimal number of steps to get the finish position.

NoteIn the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its y coordinate and get the finish position.In the second example robot should simultaneously increase x coordinate and decrease y coordinate by one three times.

let dist = function
  | [[x1; y1] ; [x2; y2]] ->
      begin
        let open Int64 in
        max
          (abs (sub x1 x2))
          (abs (sub y1 y2))
      end
  | _ -> Int64.zero

let () =
  let line1 = read_line () in
  let line2 = read_line () in
  [ line1; line2 ]
  |> List.map (fun x -> List.map Int64.of_string  @@ Str.split (Str.regexp " ") x)
  |> dist |> Int64.to_string
  |> print_string
  |> print_newline

let xint() = Scanf.scanf " %ld" (fun x -> x)

let () =
  let x1 = xint() and y1 = xint() in
  let x2 = xint() and y2 = xint() in
  let abs x =
    if Int32.compare x 0l < 0 then Int32.sub 0l x
    else x
  in
  let x = abs (Int32.sub x2 x1) in
  let y = abs (Int32.sub y2 y1) in
  Printf.printf "%ld\n" (
    if Int32.compare x y > 0 then x
    else y
  )

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

let _ =
  let sb = Scanf.Scanning.stdib in
  let x1 = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let y1 = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let x2 = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let y2 = Scanf.bscanf sb "%Ld " (fun s -> s) in
  let res = max (Int64.abs (x1 -| x2)) (Int64.abs (y1 -| y2)) in
    Printf.printf "%Ld\n" res

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let abs = Int64.abs

let read_pair () = bscanf Scanning.stdib " %Ld %Ld " (fun x y -> (x,y))
let () =
  let (x1,y1) = read_pair () in
  let (x2,y2) = read_pair () in

  printf "%Ld\n" (max (abs (x1--x2)) (abs (y1--y2)))

let dist = function
  | [[x1; y1] ; [x2; y2]] -> begin
    max
    (abs (x1 - x2))
    (abs (y1 - y2))
  end
  | _ -> -1

let () =
  let line1 = read_line () in
  let line2 = read_line () in
  [ line1; line2 ]
  |> List.map (fun x -> List.map int_of_string  @@ Str.split (Str.regexp " ") x)
  |> dist
  |> print_int
  |> print_newline

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

let () =
  let x1 = xint() and y1 = xint() in
  let x2 = xint() and y2 = xint() in
  let abs x =
    if x < 0 then -x
    else x
  in
  let x = abs (x2 - x1) in
  let y = abs (y2 - y1) in
  Printf.printf "%d\n" (
    if x > y then x
    else y
  )


Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.A block with side a has volume a3. A tower consisting of blocks with sides a1, a2, ..., ak has the total volume a13 + a23 + ... + ak3.Limak is going to build a tower. First, he asks you to tell him a positive integer X — the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed X.Limak asks you to choose X not greater than m. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize X.Can you help Limak? Find the maximum number of blocks his tower can have and the maximum X ≤ m that results this number of blocks.

The only line of the input contains one integer m (1 ≤ m ≤ 1015), meaning that Limak wants you to choose X between 1 and m, inclusive.

Print two integers — the maximum number of blocks in the tower and the maximum required total volume X, resulting in the maximum number of blocks.

NoteIn the first sample test, there will be 9 blocks if you choose X = 23 or X = 42. Limak wants to maximize X secondarily so you should choose 42.In more detail, after choosing X = 42 the process of building a tower is:  Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15.  The second added block has side 2, so the remaining volume is 15 - 8 = 7.  Finally, Limak adds 7 blocks with side 1, one by one. So, there are 9 blocks in the tower. The total volume is is 33 + 23 + 7·13 = 27 + 8 + 7 = 42.

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

let cube x = x ** x ** x

let cube_lower_bound x =
  (* return the maximum number i such that i^3 <= x *)
  let rec bsearch lo hi = (* lo ^ 3 <= x < hi^3 *)
    if lo ++ 1L = hi then lo else
      let mid = (lo ++ hi) // 2L in
      if x < cube mid then bsearch lo mid else bsearch mid hi
  in
  bsearch 0L 1_000_000L

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

let () =
  let m = read_long () in

  let ntable = 1000 in
  let ftable = Array.make (ntable+1) 0 in
  let table = Array.make (ntable+1) (0,0L) in

  for j=1 to ntable do
    let x = long j in
    let i = cube_lower_bound x in
    ftable.(j) <- 1 + ftable.(short (x -- (cube i)));
  done;

  let rec scan i (best_len, best_x) =
    if i>ntable then () else (
      table.(i) <- if ftable.(i) >= best_len then (ftable.(i), long i) else (best_len, best_x);
      scan (i+1) table.(i)
    )
  in

  scan 1 (0, 0L);

  let rec g m full_range =
    if m <= long ntable then table.(short m) else
      let i = cube_lower_bound m in
      let (i1,x1) = g (m -- (cube i)) false in
      let i1 = i1+1 in
      let x1 = x1 ++ (cube i) in
      if full_range then (i1,x1) else
	let (i2,x2) = g ((cube i)--1L) true in
	max (i1,x1) (i2,x2)
  in

  let (len,x) = g m false in
  printf "%d %Ld\n" len x


Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium. The scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan.

The first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol "|" indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale.  The second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet.  It is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters.

If you cannot put all the weights on the scales so that the scales were in equilibrium, print string "Impossible". Otherwise, print the description of the resulting scales, copy the format of the input. If there are multiple answers, print any of them.

exception Unbalanceable;;
let rec balance left right free =
    if String.length left > String.length right then
        let swap (a, b) = (b, a) in
        swap (balance right left free)
    else
        let diff = (String.length right) - (String.length left) in
        let rest = (String.length free) - diff in
        if (rest < 0) || (rest mod 2 <> 0) then
            raise Unbalanceable
        else
            let split_point = diff + rest / 2 in
            (String.concat "" [left; Str.string_before free split_point],
            String.concat "" [right; Str.string_after free split_point])
;;

let split_on_delim s delim =
    let l = Str.split_delim (Str.regexp delim) s in
    match l with
    | h :: t -> (h, String.concat "" t)
    | _ -> failwith "Delim not found"
;;

let main () =
    let (left, right) = split_on_delim (read_line ()) "|" in
    let free_weight = read_line () in
    try
        let (left, right) = balance left right free_weight in
        Printf.printf "%s|%s\n" left right
    with Unbalanceable ->
        Printf.printf "Impossible\n"
;;
let _ = main ();;

exception Unbalanceable;;
let rec balance left right free =
    if String.length left > String.length right then
        let swap (a, b) = (b, a) in
        swap (balance right left free)
    else
        let diff = (String.length right) - (String.length left) in
        let rest = (String.length free) - diff in
        if (rest < 0) || (rest mod 2 <> 0) then
            raise Unbalanceable
        else
            let split_point = diff + rest / 2 in
            (String.concat "" [left; Str.string_before free split_point],
            String.concat "" [right; Str.string_after free split_point])
;;

let explode s =
    let rec exp i l =
        if i < 0 then l else exp (i - 1) (s.[i] :: l) in
    exp (String.length s - 1) [];;

let split_on_delim s delim =
    let l = Str.split (Str.regexp delim) s in
    match l with
    | h :: t -> (h, String.concat "" t)
    | _ -> failwith "Delim not found"
;;

let main () =
    let (left, right) = split_on_delim (read_line ()) "|" in
    let free_weight = read_line () in
    try
        let (left, right) = balance left right free_weight in
        Printf.printf "%s|%s\n" left right
    with Unbalanceable ->
        Printf.printf "Impossible\n"
;;
let _ = main ();;


Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.

The first line contains integer n (1 ≤ n ≤ 20). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 25) — the number of times Greg repeats the exercises.

Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise. It is guaranteed that the input is such that the answer to the problem is unambiguous.

NoteIn the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.

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

let n = read_int () ;;

let a =
    let rec loop i n f =
        if i = n then f []
        else let ai = read_int () in loop (i + 1) n (fun l -> f (ai :: l))
    in loop 0 n (fun x -> x) ;;

let count l =
    let rec loop i (chest, biceps, back) = function
        | [] -> (chest, biceps, back)
        | h :: t ->
            if i mod 3 = 0 then loop (i + 1) (chest + h, biceps, back) t
            else if i mod 3 = 1 then loop (i + 1) (chest, biceps + h, back) t
            else loop (i + 1) (chest, biceps, back + h) t
    in loop 0 (0, 0, 0) l ;;

let strongest_muscle (chest, biceps, back) =
    if chest > biceps && chest > back then "chest"
    else if biceps > chest && biceps > back then "biceps"
    else "back" ;;

Printf.printf "%s\n" (strongest_muscle (count a)) ;;

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

let () =
  let n = read_int() in
  let a = Array.init n (fun i -> read_int()) in
  let c = Array.make 3 0 in
    for i=0 to n-1 do
      let e = i mod 3 in
	c.(e) <- c.(e) + a.(i);
    done;

    let m = max (max c.(0) c.(1)) c.(2) in
    let s = if c.(0) = m then "chest"
    else if c.(1) = m then "biceps"
    else "back" in

      Printf.printf "%s\n" s


Devu is a renowned classical singer. He is invited to many big functions/festivals. Recently he was invited to "All World Classical Singing Festival". Other than Devu, comedian Churu was also invited.Devu has provided organizers a list of the songs and required time for singing them. He will sing n songs, ith song will take ti minutes exactly. The Comedian, Churu will crack jokes. All his jokes are of 5 minutes exactly.People have mainly come to listen Devu. But you know that he needs rest of 10 minutes after each song. On the other hand, Churu being a very active person, doesn't need any rest.You as one of the organizers should make an optimal sсhedule for the event. For some reasons you must follow the conditions:  The duration of the event must be no more than d minutes;  Devu must complete all his songs;  With satisfying the two previous conditions the number of jokes cracked by Churu should be as many as possible. If it is not possible to find a way to conduct all the songs of the Devu, output -1. Otherwise find out maximum number of jokes that Churu can crack in the grand event.

The first line contains two space separated integers n, d (1 ≤ n ≤ 100; 1 ≤ d ≤ 10000). The second line contains n space-separated integers: t1, t2, ..., tn (1 ≤ ti ≤ 100).

If there is no way to conduct all the songs of Devu, output -1. Otherwise output the maximum number of jokes that Churu can crack in the grand event.

NoteConsider the first example. The duration of the event is 30 minutes. There could be maximum 5 jokes in the following way:  First Churu cracks a joke in 5 minutes.  Then Devu performs the first song for 2 minutes.  Then Churu cracks 2 jokes in 10 minutes.  Now Devu performs second song for 2 minutes.  Then Churu cracks 2 jokes in 10 minutes.  Now finally Devu will perform his last song in 1 minutes.  Total time spent is 5 + 2 + 10 + 2 + 10 + 1 = 30 minutes.Consider the second example. There is no way of organizing Devu's all songs. Hence the answer is -1.

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

let get_sum songs =
    let rec loop i acc =
        if i < songs
        then
            let song = read () in
            loop (i + 1) (acc + song)
        else
            acc
    in loop 0 0

let input () =
    let songs = read () in
    let time_limit = read () in
    let time_songs = get_sum songs in
    (songs, time_limit, time_songs)

let solve (songs, time_limit, time_songs) =
    let time_pauses = 10 * (songs - 1) in
    let time_needed = time_pauses + time_songs in
    if time_needed <= time_limit
    then
        (time_limit - time_songs) / 5
    else
        -1

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

let read_int()=Scanf.scanf "%d " (fun x->x);;
let n=read_int();;
let d=read_int();;
let nb=ref 0;;
let j=ref 0;;
for i=1 to n-1 do
   let x=read_int() in
   nb:= !nb+10+x;
   j:= !j+2;
done;;
let x=Scanf.scanf "%d" (fun x->x);;
nb:= !nb+x;;
let a=d- !nb;;
if a<0 then print_int (-1) else print_int(!j+(a/5));;

let (|>) x f = f x

let main =
  let (n, d) = Scanf.scanf "%d %d\n" (fun n d -> (n,d)) in
  let rec aux n acc =
    match n with
        0 -> acc
      | n -> aux (n-1) (acc + (Scanf.scanf "%d " (fun n -> n)))
  in
  let sum = aux n 0 in
    if (n-1)*10 + sum > d then Printf.printf "-1\n"
  else Printf.printf "%d\n" ((d-sum)/5)


It seems like the year of 2013 came only yesterday. Do you know a curious fact? The year of 2013 is the first year after the old 1987 with only distinct digits.Now you are suggested to solve the following problem: given a year number, find the minimum year number which is strictly larger than the given one and has only distinct digits.

The single line contains integer y (1000 ≤ y ≤ 9000) — the year number.

Print a single integer — the minimum year number that is strictly larger than y and all it's digits are distinct. It is guaranteed that the answer exists.

let good year =
    let n1,n2,n3,n4=(year mod 10000)/1000,(year mod 1000)/100,(year mod 100)/10,(year mod 10) in
    if (n1<>n2) && (n1<>n3) && (n1<>n4) && (n2<>n3) && (n2<>n4) && (n3<>n4) then true
    else false;;
let rec solve year =
	if (good year) then year
    else solve (year+1);;
let year = read_int ();;
print_int (solve (year+1));;

let str_to_list s =
    let rec loop i l =
        if i < 0
        then l
        else loop (i - 1) ((Pervasives.int_of_char s.[i] - Pervasives.int_of_char '0') :: l) in
    loop (String.length s - 1) []

let input () = Scanf.scanf "%s" (fun s -> (List.rev (str_to_list s)))

let find_allowed year =
    let rec loop year allowed =
        match year with
        |(int :: tail) -> loop tail (List.filter((<>) int) allowed)
        |[] -> allowed
    in loop year [0;1;2;3;4;5;6;7;8;9]

let is_good year =
    List.length (find_allowed year) = 10 - List.length year

let rec increase year =
    let allowed = find_allowed year in
    match year with
    |(ones :: tail) -> (match (List.filter(fun a -> a > ones) allowed) with
        |(min :: _) when is_good tail -> (min :: tail)
        |_ -> let temp = increase tail in
        (List.hd(find_allowed temp) :: temp))
    |[] -> [1]

let solve input = List.rev (increase input)

let () = List.iter(Printf.printf "%d") (solve (input()))

let rec dig n ak =
	if n = 0 then ak
	else dig (n / 10) ((n mod 10)::ak)
;;
let good n =
	let li = List.sort compare (dig n [])
	and krok (last, ans) e =
		if e = last then (e, false)
		else (e, ans)
	in snd (List.fold_left krok (10, true) li)
;;

let rec solve n =
	if good n then n
	else solve (n + 1)
;;
let year n = solve (n + 1);;
let x = read_int ();;
print_int (year x);;

open Printf

exception Found

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

let y = ref (n+1)

let not_distinct year =
  let flag = ref false in
  try
  let s = string_of_int year in
  let l = String.length s in
  for i = 0 to l-1 do
    for j =i+1 to l -1 do
      let x = String.get s i in
      let y1 = String.get s j in
      if(x==y1) then begin flag := true; raise Found end
    done;
  done;
  !flag;
  with Found -> !flag
;;

let () =
  while( (not_distinct !y) ) do
    incr y
  done
;;

printf "%d\n" !y

let str_to_list s =
    let rec loop i l =
        if i < 0
        then l
        else loop (i - 1) ((Pervasives.int_of_char s.[i] - Pervasives.int_of_char '0') :: l) in
    loop (String.length s - 1) []

let input () = Scanf.scanf "%s" (fun s -> (List.rev (str_to_list s)))

let find_allowed year =
    let rec loop year allowed =
        match year with
        |(int :: tail) -> loop tail (List.filter((<>) int) allowed)
        |[] -> allowed
    in loop year [0; 1; 2; 3; 4; 5; 6; 7; 8; 9]

let rec increase year =
    let allowed = find_allowed year in
    match year with
    |(ones :: tail) -> (match (List.filter(fun a -> a > ones) allowed) with
        |(min :: _) -> (min :: tail)
        |[] -> let temp = increase tail in
        (List.hd(find_allowed temp) :: temp))
    |[] -> [1]

let solve input = List.rev (increase input)

let () = List.iter(Printf.printf "%d") (solve (input()))


The input contains two integers a, b (1 ≤ a ≤ 10, 0 ≤ b ≤ 22·a - 1) separated by a single space.

Output two integers separated by a single space.

let _ =
  let sb = Scanf.Scanning.stdib in
  let x = Scanf.bscanf sb "%f " (fun s -> s) in
    for a' = 1 to 10 do
      for h' = 1 to 10 do
	let a = float_of_int a'
	and h = float_of_int h' in
	let r = a *. h /. sqrt (a *. a +. 4.0 *. h *. h) in
	  if abs_float (r -. x) < 1e-6 then (
	    Printf.printf "%d %d\n" a' h';
	    exit 0
	  )
      done
    done

let _ =
    let main () =
        let f a =
            Printf.printf "%.f\n" (6. *. a *. (a -. 1.) +. 1.)
        in
        Scanf.sscanf (read_line ()) "%f" f
    in
    main ()

open Int64

let rec solve n accm =
  if n < one
  then accm
  else solve (sub n one) (add accm (mul (of_int 12) (pred n)))

let solve n = solve (of_int n) one

let _ =
  Printf.printf "%s\n" (to_string (Scanf.scanf " %d" solve))

let _ =
  let sb = Scanf.Scanning.stdib in
  let a = Scanf.bscanf sb "%ld " (fun s -> s) in
  let res = Int32.add (Int32.mul 6l (Int32.mul a (Int32.sub a 1l))) 1l in
      Printf.printf "%ld\n" res;

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

let s = String.lowercase (read_string())
let k = read_int();;
for i=0 to (String.length s)-1 do
	s.[i] <- if int_of_char s.[i] < k + 97 then Char.uppercase s.[i] else Char.lowercase s.[i]
done;
Printf.printf "%s\n" s

let _ =
  let sb = Scanf.Scanning.stdib in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = String.lowercase s in
  let res = String.make (String.length s) '\000' in
  let n = ref n in
  let pos = ref 0 in
    for i = 0 to String.length s - 1 do
      let c = s.[i] in
	if Char.code c < !n + 97
	then res.[!pos] <- Char.uppercase c
	else res.[!pos] <- c;
	incr pos
    done;
    Printf.printf "%s\n" res

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

let s = String.lowercase (read_string())

let k = read_int()
;;

for i=0 to (String.length s)-1 do
  let c = s.[i] in
  let c =  if int_of_char c < k + 97 then
      Char.uppercase c else Char.lowercase c in
    s.[i] <- c
done;

Printf.printf "%s\n" s

let which f =
    let plop = ref [] in
    for a = 0 to 1 do
    for b = 0 to 1 do
    for c = 0 to 1 do
    for d = 0 to 1 do
        plop := (f (a=1) (b=1) (c=1) (d=1))::!plop
    done;
    done;
    done;
    done;
    !plop

let l =
    let olol = ref [] in
        for a = 0 to 1 do
        let b = 1 - a in
        for d = 0 to 1 do
            olol := (fun i j ->
                match i,j with
                    | true,true -> a=0
                    | true,false -> b=0
                    | false,true -> b=0
                    | false,false -> d=0
                    )::!olol;
        done;
        done;
        !olol

let prod_cart a b =
    List.(flatten (map (fun i -> map (fun j -> i,j) b) a))

let beuleu = prod_cart l (prod_cart l l)

let hamza = List.map (fun (i,(j,k)) -> i,j,k) beuleu

let f (triang,triang_sub,sqr) a b c d =
    let x = triang a b in
    let y = triang_sub c d in
    let z = sqr b c in
    let zz = triang a d in
    let dx = sqr x y in
    let dy = triang_sub z zz in
    let final = triang dx dy in
        final;;

let all_f = List.map f hamza;;

let finally = List.filter (fun i -> false = i false true true false) all_f;;

let rec no_doub = function
    | (a,b)::(c,_)::l when a = c -> no_doub ((a,b)::l)
    | x::l -> x::(no_doub l)
    | l -> l;;

let compare (a,_) (c,_) = compare a c;;

let with_sig = no_doub (List.sort compare (List.map (fun h -> which h, h) finally));;

let finnnnal = List.map snd with_sig;;

let _ =
    let kikou = Scanf.scanf "%d %d %d %d" (fun a b c d -> (List.hd finnnnal) (a=1) (b=1) (c=1) (d=1)) in
        if kikou then print_int 1 else print_int 0

let l = [(0,1,1,0),0;(0,0,0,0),0]

let rec ok x = function
    | (a,b)::l when a = x -> b
    | _::l -> ok x l
    | _ -> -1;;

let _ =
    let (a,b,c,d) = Scanf.scanf "%d %d %d %d" (fun a b c d -> (a,b,c,d)) in
    let ans = ok (a,b,c,d) l in
        if ans = -1
        then if a = 0 then print_int (1/0) else print_int 2
        else print_int ans

let rec pow x n =
    if n = 0 then 1 else x * pow x (n-1)

let olol n =
    if n = 3 then 27 else 2

let _ =
    let n = Scanf.scanf "%d" (fun n -> n) in
        print_int (olol n)

let l = [(0,1,1,0),0;(0,0,0,0),0;(1,0,0,0),1;(0,1,0,0),0;(1,1,0,0),0]

let rec ok x = function
    | (a,b)::l when a = x -> b
    | _::l -> ok x l
    | _ -> -1;;

let _ =
    let (a,b,c,d) = Scanf.scanf "%d %d %d %d" (fun a b c d -> (a,b,c,d)) in
    let ans = ok (a,b,c,d) l in
        if ans = -1
        then if b = 0 then print_int (1/0) else print_int 2
        else print_int ans

let which f =
    let plop = ref [] in
    for a = 0 to 1 do
    for b = 0 to 1 do
    for c = 0 to 1 do
    for d = 0 to 1 do
        plop := (f (a=1) (b=1) (c=1) (d=1))::!plop
    done;
    done;
    done;
    done;
    !plop

let l =
    let olol = ref [] in
        for a = 0 to 1 do
        let b = 1 - a in
        for d = 0 to 1 do
            olol := (fun i j ->
                match i,j with
                    | true,true -> a=0
                    | true,false -> b=0
                    | false,true -> b=0
                    | false,false -> d=0
                    )::!olol;
        done;
        done;
        !olol

let prod_cart a b =
    List.(flatten (map (fun i -> map (fun j -> i,j) b) a))

let beuleu = prod_cart l (prod_cart l l)

let hamza = List.map (fun (i,(j,k)) -> i,j,k) beuleu

let f (triang,triang_sub,sqr) a b c d =
    let x = triang a b in
    let y = triang_sub c d in
    let z = sqr b c in
    let zz = triang a d in
    let dx = sqr x y in
    let dy = triang_sub z zz in
    let final = triang dx dy in
        final;;

let all_f = List.map f hamza;;

let finally = List.filter (fun i -> false = i false true true false) all_f;;

let rec no_doub = function
    | (a,b)::(c,_)::l when a = c -> no_doub ((a,b)::l)
    | x::l -> x::(no_doub l)
    | l -> l;;

let compare (a,_) (c,_) = compare a c;;

let with_sig = no_doub (List.sort compare (List.map (fun h -> which h, h) finally));;

let finnnnal = List.map snd with_sig;;

let _ =
    let kikou = Scanf.scanf "%d %d %d %d" (fun a b c d -> (List.nth finnnnal 2) (a=1) (b=1) (c=1) (d=1)) in
        if kikou then print_int 1 else print_int 0

let l = [(0,1,1,0),0;(0,0,0,0),0;(1,0,0,0),0]

let rec ok x = function
    | (a,b)::l when a = x -> b
    | _::l -> ok x l
    | _ -> -1;;

let _ =
    let (a,b,c,d) = Scanf.scanf "%d %d %d %d" (fun a b c d -> (a,b,c,d)) in
    let ans = ok (a,b,c,d) l in
        if ans = -1
        then if a = 0 then print_int (1/0) else print_int 2
        else print_int ans

let l = [(0,1,1,0),0;(0,0,0,0),0;(1,0,0,0),1;(0,1,0,0),0]

let rec ok x = function
    | (a,b)::l when a = x -> b
    | _::l -> ok x l
    | _ -> -1;;

let _ =
    let (a,b,c,d) = Scanf.scanf "%d %d %d %d" (fun a b c d -> (a,b,c,d)) in
    let ans = ok (a,b,c,d) l in
        if ans = -1
        then if b = 0 then print_int (1/0) else print_int 2
        else print_int ans

let l =
    let olol = ref [] in
        for a = 0 to 1 do
        let b = 1 - a in
        for d = 0 to 1 do
            olol := (fun i j ->
                match i,j with
                    | true,true -> a=0
                    | true,false -> b=0
                    | false,true -> b=0
                    | false,false -> d=0
                    )::!olol;
        done;
        done;
        !olol

let prod_cart a b =
    List.(flatten (map (fun i -> map (fun j -> i,j) b) a))

let beuleu = prod_cart l (prod_cart l l)

let hamza = List.map (fun (i,(j,k)) -> i,j,k) beuleu

let f (triang,triang_sub,sqr) a b c d =
    let x = triang a b in
    let y = triang_sub c d in
    let z = sqr b c in
    let zz = triang a d in
    let dx = sqr x y in
    let dy = triang_sub z zz in
    let final = triang dx dy in
        final

let all_f = List.map f hamza

let finally = List.filter (fun i -> false = i false true true false) all_f

let _ =
    let kikou = Scanf.scanf "%d %d %d %d" (fun a b c d -> (List.hd finally) (a=1) (b=1) (c=1) (d=1)) in
        if kikou then print_int 1 else print_int 0

let rec solve n accm =
  if n < 1 then accm else solve (n - 1) (accm + 12*(n-1))

let solve n = solve n 1

let _ =
  Printf.printf "%d\n" (Scanf.scanf " %d" solve)

let l = [(0,1,1,0),0;(0,0,0,0),0;(1,0,0,0),1]

let rec ok x = function
    | (a,b)::l when a = x -> b
    | _::l -> ok x l
    | _ -> -1;;

let _ =
    let (a,b,c,d) = Scanf.scanf "%d %d %d %d" (fun a b c d -> (a,b,c,d)) in
    let ans = ok (a,b,c,d) l in
        if ans = -1
        then if b = 0 then print_int (1/0) else print_int 2
        else print_int ans

let which f =
    let plop = ref [] in
    for a = 0 to 1 do
    for b = 0 to 1 do
    for c = 0 to 1 do
    for d = 0 to 1 do
        plop := (f (a=1) (b=1) (c=1) (d=1))::!plop
    done;
    done;
    done;
    done;
    !plop

let l =
    let olol = ref [] in
        for a = 0 to 1 do
        let b = 1 - a in
        for d = 0 to 1 do
            olol := (fun i j ->
                match i,j with
                    | true,true -> a=0
                    | true,false -> b=0
                    | false,true -> b=0
                    | false,false -> d=0
                    )::!olol;
        done;
        done;
        !olol

let prod_cart a b =
    List.(flatten (map (fun i -> map (fun j -> i,j) b) a))

let beuleu = prod_cart l (prod_cart l l)

let hamza = List.map (fun (i,(j,k)) -> i,j,k) beuleu

let f (triang,triang_sub,sqr) a b c d =
    let x = triang a b in
    let y = triang_sub c d in
    let z = sqr b c in
    let zz = triang a d in
    let dx = sqr x y in
    let dy = triang_sub z zz in
    let final = triang dx dy in
        final;;

let all_f = List.map f hamza;;

let finally = List.filter (fun i -> false = i false true true false) all_f;;

let rec no_doub = function
    | (a,b)::(c,_)::l when a = c -> no_doub ((a,b)::l)
    | x::l -> x::(no_doub l)
    | l -> l;;

let compare (a,_) (c,_) = compare a c;;

let with_sig = no_doub (List.sort compare (List.map (fun h -> which h, h) finally));;

let finnnnal = List.map snd with_sig;;

let _ =
    let kikou = Scanf.scanf "%d %d %d %d" (fun a b c d -> (List.nth finnnnal 1) (a=1) (b=1) (c=1) (d=1)) in
        if kikou then print_int 1 else print_int 0

let rec pow x n =
    if n = 0 then 1 else x * pow x (n-1)

let olol n =
    2

let _ =
    let n = Scanf.scanf "%d" (fun n -> n) in
        print_int (olol n)


You are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits.Some keys on the keypad have fingerprints. You believe the correct code is the longest not necessarily contiguous subsequence of the sequence you have that only contains digits with fingerprints on the corresponding keys. Find such code.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10$$$) representing the number of digits in the sequence you have and the number of keys on the keypad that have fingerprints. The next line contains $$$n$$$ distinct space-separated integers $$$x_1, x_2, \ldots, x_n$$$ ($$$0 \le x_i \le 9$$$) representing the sequence. The next line contains $$$m$$$ distinct space-separated integers $$$y_1, y_2, \ldots, y_m$$$ ($$$0 \le y_i \le 9$$$) — the keys with fingerprints.

In a single line print a space-separated sequence of integers representing the code. If the resulting sequence is empty, both printing nothing and printing a single line break is acceptable.

NoteIn the first example, the only digits with fingerprints are $$$1$$$, $$$2$$$ and $$$7$$$. All three of them appear in the sequence you know, $$$7$$$ first, then $$$1$$$ and then $$$2$$$. Therefore the output is 7 1 2. Note that the order is important, and shall be the same as the order in the original sequence.In the second example digits $$$0$$$, $$$1$$$, $$$7$$$ and $$$9$$$ have fingerprints, however only $$$0$$$ and $$$1$$$ appear in the original sequence. $$$1$$$ appears earlier, so the output is 1 0. Again, the order is important.

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

let _ = read_line () in
let seq = read_line () |> split_on_char ' ' |> List.map int_of_string in
let keys = read_line () |> split_on_char ' ' |> List.map int_of_string in
List.iter (fun x -> if List.mem x keys then Printf.printf "%i " x) seq;
print_newline ();

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

let _ = read_line () in
let seq = read_line () |> split_on_char ' ' |> List.map int_of_string in
let keys = read_line () |> split_on_char ' ' |> List.map int_of_string in
let longest = ref [] in
let rec loop l = function
      | [] -> ()
      | h::t ->
        if List.mem h keys then loop (h::l) t
        else begin
          if List.length l > List.length !longest then longest := l;
          loop [] t
        end
in
loop [] seq;
List.rev !longest |> List.iter (fun i -> Printf.printf "%i " i);
print_newline ();


Vasilisa the Wise from the Kingdom of Far Far Away got a magic box with a secret as a present from her friend Hellawisa the Wise from the Kingdom of A Little Closer. However, Vasilisa the Wise does not know what the box's secret is, since she cannot open it again. She hopes that you will help her one more time with that.The box's lock looks as follows: it contains 4 identical deepenings for gems as a 2 × 2 square, and some integer numbers are written at the lock's edge near the deepenings. The example of a lock is given on the picture below.  The box is accompanied with 9 gems. Their shapes match the deepenings' shapes and each gem contains one number from 1 to 9 (each number is written on exactly one gem). The box will only open after it is decorated with gems correctly: that is, each deepening in the lock should be filled with exactly one gem. Also, the sums of numbers in the square's rows, columns and two diagonals of the square should match the numbers written at the lock's edge. For example, the above lock will open if we fill the deepenings with gems with numbers as is shown on the picture below.  Now Vasilisa the Wise wants to define, given the numbers on the box's lock, which gems she should put in the deepenings to open the box. Help Vasilisa to solve this challenging task.

The input contains numbers written on the edges of the lock of the box. The first line contains space-separated integers r1 and r2 that define the required sums of numbers in the rows of the square. The second line contains space-separated integers c1 and c2 that define the required sums of numbers in the columns of the square. The third line contains space-separated integers d1 and d2 that define the required sums of numbers on the main and on the side diagonals of the square (1 ≤ r1, r2, c1, c2, d1, d2 ≤ 20). Correspondence between the above 6 variables and places where they are written is shown on the picture below. For more clarifications please look at the second sample test that demonstrates the example given in the problem statement.

Print the scheme of decorating the box with stones: two lines containing two space-separated integers from 1 to 9. The numbers should be pairwise different. If there is no solution for the given lock, then print the single number "-1" (without the quotes). If there are several solutions, output any.

NotePay attention to the last test from the statement: it is impossible to open the box because for that Vasilisa the Wise would need 4 identical gems containing number "5". However, Vasilisa only has one gem with each number from 1 to 9.

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

let () =
  let r1 = read_int() in
  let r2 = read_int() in
  let c1 = read_int() in
  let c2 = read_int() in
  let d1 = read_int() in
  let d2 = read_int() in

    for i1 = 1 to 9 do
      for i2 = 1 to 9 do
	for i3 = 1 to 9 do
	  for i4 = 1 to 9 do
	    if r1=i1+i2 && r2=i3+i4 && c1=i1+i3 && c2=i2+i4 && d1=i1+i4 && d2=i2+i3
	      && i1<>i2 && i1<>i3 && i1<>i4 && i2<>i3 && i2<>i4 && i3<>i4
	    then (
	      Printf.printf "%d %d\n%d %d\n" i1 i2 i3 i4;
	      exit 1
	    )
	  done
	done
      done
    done;

    Printf.printf "-1\n"

let r1, r2 = Scanf.scanf "%d %d\n" (fun x y -> (x,y));;
let c1, c2 = Scanf.scanf "%d %d\n" (fun x y -> (x,y));;
let d1, d2 = Scanf.scanf "%d %d\n" (fun x y -> (x,y));;

for p00 = 1 to 9 do begin
	let p01 = r1 - p00
	and p10 = c1 - p00
	and p11 = d1 - p00 in
	if (p00 <> p01) && (p00 <> p10) && (p00 <> p11) &&
		(p01 <> p10) && (p01 <> p11) && (p10 <> p11)
		&& (p10 + p11 = r2) && (p01 + p11 = c2) && (p10 + p01 = d2) then begin
		print_int p00; print_char ' '; print_int p01; print_string "\n";
		print_int p10; print_char ' '; print_int p11;
		exit 0;
	end
end done;;

print_int (0-1);;

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

let () =
  let r1 = read_int() in
  let r2 = read_int() in
  let c1 = read_int() in
  let c2 = read_int() in
  let d1 = read_int() in
  let d2 = read_int() in

    for i1 = 0 to 9 do
      for i2 = 0 to 9 do
	for i3 = 0 to 9 do
	  for i4 = 0 to 9 do
	    if r1=i1+i2 && r2=i3+i4 && c1=i1+i3 && c2=i2+i4 && d1=i1+i4 && d2=i2+i3
	      && i1<>i2 && i1<>i3 && i1<>i4 && i2<>i3 && i2<>i4 && i3<>i4
	    then (
	      Printf.printf "%d %d\n%d %d\n" i1 i2 i3 i4;
	      exit 1
	    )
	  done
	done
      done
    done;

    Printf.printf "-1\n"

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

let () =
  let r1 = read_int() in
  let r2 = read_int() in
  let c1 = read_int() in
  let c2 = read_int() in
  let d1 = read_int() in
  let d2 = read_int() in

    for i1 = 0 to 9 do
      for i2 = 0 to 9 do
	for i3 = 0 to 9 do
	  for i4 = 0 to 9 do
	    if r1=i1+i2 && r2=i3+i4 && c1=i1+i3 && c2=i2+i4 && d1=i1+i4 && d2=i2+i3 then (
	      Printf.printf "%d %d\n%d %d\n" i1 i2 i3 i4;
	      exit 1
	    )
	  done
	done
      done
    done;

    Printf.printf "-1\n"


Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob.Right now, Limak and Bob weigh a and b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight.Limak eats a lot and his weight is tripled after every year, while Bob's weight is doubled after every year.After how many full years will Limak become strictly larger (strictly heavier) than Bob?

The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 10) — the weight of Limak and the weight of Bob respectively.

Print one integer, denoting the integer number of years after which Limak will become strictly larger than Bob.

NoteIn the first sample, Limak weighs 4 and Bob weighs 7 initially. After one year their weights are 4·3 = 12 and 7·2 = 14 respectively (one weight is tripled while the other one is doubled). Limak isn't larger than Bob yet. After the second year weights are 36 and 28, so the first weight is greater than the second one. Limak became larger than Bob after two years so you should print 2.In the second sample, Limak's and Bob's weights in next years are: 12 and 18, then 36 and 36, and finally 108 and 72 (after three years). The answer is 3. Remember that Limak wants to be larger than Bob and he won't be satisfied with equal weights.In the third sample, Limak becomes larger than Bob after the first year. Their weights will be 3 and 2 then.

(*#load "str.cma"*)
open Printf;;
let [a; b] = List.map (ref) (List.map (int_of_string) (Str.split (Str.regexp " ") (read_line ())));;
let ans = ref 0;;
while (!a <= !b) do
	a := !a * 3;
	b := !b * 2;
	ans := !ans + 1;
done;;
printf "%d\n" !ans;;

    let x, y = Scanf.scanf "%d %d" (fun x y -> float_of_int x, float_of_int y)

    let _ =
        print_int (int_of_float (ceil (log(y /. x) /. log(1.5) +. 0.00001)))

let input () = Scanf.scanf "%d %d" (fun a b -> (a, b))

let solve (a, b) =
    let rec loop i a_mass b_mass =
        if a_mass > b_mass
        then
            i
        else
            loop (i + 1) (a_mass * 3) (b_mass * 2)
    in loop 0 a b

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

let main () =
  let gr () = Scanf.scanf " %d" (fun i -> i) in
  let a = gr () in
  let b = gr() in
  let rec f x y ans =
      let newx = 3 * x in
      let newy = 2 * y in
      if newx > newy then (ans + 1) else f newx newy (ans + 1)
  in
  Printf.printf "%d\n"  (f a b 0)
  ;;
let _ = main();;

let get_int () =
  Scanf.scanf " %d" (fun x -> x)
;;
let () =
  let x = get_int () in
  let y = get_int () in
  let rec f x y t =
    if x > y then t
    else f (3 * x) (2 * y) (t + 1) in
  Printf.printf "%d\n" (f x y 0)
;;

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

let rec kiedy a b cnt =
    let roznica = (a > b) in
        match roznica with
        | true -> cnt
        | false -> (kiedy (a*3) (b*2) (cnt+1))
;;

let main () =
    let a = (wczytaj ()) in
        let b = (wczytaj ()) in
            (kiedy a b 0)
;;

let _ = Printf.printf "%d" (main ())

let min_time a b =
    let rec helper a b acc =
        if a > b then
            acc
        else helper (3 * a) (2 * b) (acc + 1)
    in helper a b 0
in

let a = Scanf.scanf " %d" (fun x -> x) in
let b = Scanf.scanf " %d" (fun x -> x) in
Printf.printf "%d\n" (min_time a b);

open Printf
open Scanf

let read_pair _ = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let (a,b) = read_pair () in

  let rec loop a b count = if a > b then count else loop (3*a) (2*b) (count+1) in

  printf "%d\n" (loop a b 0)

let x, y = Scanf.scanf "%d %d" (fun x y -> float_of_int x, float_of_int y)

let _ =
    print_int (int_of_float (ceil (log(y /. x) /. log(1.5))))


One day, the Grasshopper was jumping on the lawn and found a piece of paper with a string. Grasshopper became interested what is the minimum jump ability he should have in order to be able to reach the far end of the string, jumping only on vowels of the English alphabet. Jump ability is the maximum possible length of his jump. Formally, consider that at the begginning the Grasshopper is located directly in front of the leftmost character of the string. His goal is to reach the position right after the rightmost character of the string. In one jump the Grasshopper could jump to the right any distance from 1 to the value of his jump ability.    The picture corresponds to the first example. The following letters are vowels: 'A', 'E', 'I', 'O', 'U' and 'Y'.

The first line contains non-empty string consisting of capital English letters. It is guaranteed that the length of the string does not exceed 100.

Print single integer a — the minimum jump ability of the Grasshopper (in the number of symbols) that is needed to overcome the given string, jumping only on vowels.

let input () = Scanf.scanf "%s\n" (fun s -> s)
let print result = Printf.printf "%d\n" result

let is_vovel char =
    match char with
        | 'A' | 'E' | 'I' | 'O' | 'U' | 'Y' -> true
        | _ -> false

let solve str =
    let l = String.length str in
    let rec loop i prev max =
        if i < l
        then
            let char = str.[i] in
            if is_vovel char
            then
                let hop = Pervasives.max (i - prev) max in
                loop (i + 1) i hop
            else
                loop (i + 1) prev max
        else
            Pervasives.max (l - prev) max
    in loop 0 (-1) 0

let () = print (solve (input ()))

let solve s =
    let n = String.length s in
    let rec solve last_pos i acc =
        if i <= n then
            if s.[i-1] = 'A' || s.[i-1] = 'E' || s.[i-1] = 'I' ||
                s.[i-1] = 'O' || s.[i-1] = 'U' || s.[i-1] = 'Y' then
                solve i (i+1) (max acc (i - last_pos))
            else
                solve last_pos (i+1) acc
        else
            max acc (i - last_pos)
    in
    solve 0 1 0
;;

let main () =
    let s = read_line () in
    let () = print_int (solve s) in
    print_string "\n"
;;

main ()

let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let is_vowel c =
  List.mem c ['A'; 'E'; 'I'; 'O'; 'U'; 'Y']

let () =
  let s = read_string () in
  let len = String.length s in
  let rec loop i count max_sum =
    if i >= len then max (count + 1) max_sum
    else if is_vowel s.[i] then
      loop (i + 1) 0 (max (count + 1) max_sum)
    else loop (i + 1) (count + 1) max_sum
  in
  loop 0 0 0
  |> Printf.printf "%d"

let is_vowel c =
  List.mem c ['A'; 'E'; 'I'; 'O'; 'U'; 'Y']

let () =
  let s = read_line () in
  let rec loop ability jump i =
    if i = String.length s then
      max jump ability
    else if is_vowel s.[i] then
      loop (max jump ability) 1 (i + 1)
    else
      loop ability (jump + 1) (i + 1) in
  loop 1 1 0 |> string_of_int |> print_endline

let min_jump s =
  let len = String.length s in
  let rec jump i jumped m =
    if i = len then
      max m jumped
    else
      match s.[i] with
        'A' | 'E' | 'I' | 'O' | 'U' | 'Y' ->
        jump (i+1) 1 (max m jumped)
      | _ ->
        jump (i+1) (jumped+1) m
  in jump 0 1 0 ;;

let s = read_line ()
in Printf.printf "%d\n" (min_jump s) ;;

let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let is_vowel c =
  List.mem c ['A'; 'E'; 'I'; 'O'; 'U'; 'Y']

let () =
  let s = read_string () in
  let len = String.length s in
  let rec loop i count max_sum =
    if i >= len then max count max_sum
    else if is_vowel s.[i] then
      loop (i + 1) 0 (max (count + 1) max_sum)
    else loop (i + 1) (count + 1) max_sum
  in
  loop 0 0 0
  |> Printf.printf "%d"

let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let is_vowel c =
  List.mem c ['A'; 'E'; 'I'; 'O'; 'U'; 'Y']

let () =
  let s = read_string () in
  let len = String.length s in
  let rec loop i count max_sum =
    if i >= len then max_sum
    else if is_vowel s.[i] then
      loop (i + 1) 0 (max (count + 1) max_sum)
    else loop (i + 1) (count + 1) max_sum
  in
  loop 0 0 0
  |> Printf.printf "%d"

let is_vowel c =
  List.mem c ['A'; 'E'; 'I'; 'O'; 'U'; 'Y']

let () =
  let s = read_line () in
  let rec loop ability jump i =
    if i = String.length s then
      ability
    else if is_vowel s.[i] then
      loop (max jump ability) 1 (i + 1)
    else
      loop ability (jump + 1) (i + 1) in
  loop 1 1 0 |> string_of_int |> print_endline

let min_jump s =
  let len = String.length s in
  let rec jump i jumped m =
    if i = len then
      max m jumped
    else
      match s.[i] with
        'A' | 'E' | 'I' | 'O' | 'U' | 'Y' ->
        jump (i+1) 1 (max m jumped)
      | _ ->
        jump (i+1) (jumped+1) m
  in jump 0 0 0 ;;

let s = read_line ()
in Printf.printf "%d\n" (min_jump s) ;;