You are given a string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters.A substring of a string is a contiguous subsequence of that string. So, string "forces" is substring of string "codeforces", but string "coder" is not.Your task is to calculate the number of ways to remove exactly one substring from this string in such a way that all remaining characters are equal (the number of distinct characters either zero or one).It is guaranteed that there is at least two different characters in $$$s$$$.Note that you can remove the whole string and it is correct. Also note that you should remove at least one character.Since the answer can be rather large (not very large though) print it modulo $$$998244353$$$.If you are Python programmer, consider using PyPy instead of Python when you submit your code.

The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. It is guaranteed that there is at least two different characters in $$$s$$$.

Print one integer — the number of ways modulo $$$998244353$$$ to remove exactly one substring from $$$s$$$ in such way that all remaining characters are equal.

NoteLet $$$s[l; r]$$$ be the substring of $$$s$$$ from the position $$$l$$$ to the position $$$r$$$ inclusive.Then in the first example you can remove the following substrings:   $$$s[1; 2]$$$;  $$$s[1; 3]$$$;  $$$s[1; 4]$$$;  $$$s[2; 2]$$$;  $$$s[2; 3]$$$;  $$$s[2; 4]$$$. In the second example you can remove the following substrings:   $$$s[1; 4]$$$;  $$$s[1; 5]$$$;  $$$s[1; 6]$$$;  $$$s[1; 7]$$$;  $$$s[2; 7]$$$;  $$$s[3; 7]$$$. In the third example you can remove the following substrings:   $$$s[1; 1]$$$;  $$$s[1; 2]$$$;  $$$s[2; 2]$$$.

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  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 (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module Int64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  let pow n k =
    let rec f a n = function
    | 0L -> a | 1L -> n*a | k when k mod 2L = 0L -> f a (n*n) (k/2L) | k -> f (a*n) (n*n) (k/2L)
    in f 1L n k
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let iia = input_i64_array
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let gi = get_i64
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let g2 = get_2_i64
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let g3 = get_3_i64
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let g4 = get_4_i64
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let g5 = get_5_i64
  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)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let slen s = String.length s
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  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 fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

let md = 998244353L
let () =
  let n = gi 0 in let n = i32 n in
  let s = scanf " %s" @@ id in
  if s.[0] = s.[n-$1] then (
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let p = i64 !c mod md in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let q = i64 !c mod md in
    printf "%Ld\n" @@ (((p+1L)*(q+1L)) mod md);
  ) else (
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d\n" !c; *)
    let p = i64 !c in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d\n" !c; *)
    let q = i64 !c in
    printf "%Ld\n" @@ ((p+1L)+(q+1L)-1L) mod md;
  )

let rec fix f x = f (fix f) x
let i64,i32 = Int64.(of_int,to_int)
let md = 998244353L
let ( **% ) p q = Int64.(rem (mul p q) md);;
Scanf.(Array.(
  let n = scanf " %d" @@ fun v -> v in
  let s = scanf " %s" @@ fun v -> v in
  let p = fix (fun f i a ->
    if s.[i] = s.[0] then f (i+1) (i::a)
    else a
  ) 0 [] |> List.length in
  let q = fix (fun f i a ->
    if s.[i] = s.[n-1] then f (i-1) (i::a)
    else a
  ) (n-1) [] |> List.length in
  if s.[0] = s.[n-1] then (
    print_int @@ i32 @@ (i64 @@ p+1) **% (i64 @@ q+1)
  ) else (
    print_int @@ i32 @@ (i64 ((p+1) + (q+1) - 1) **% 1L)
  )
))

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  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 (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module Int64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  let pow n k =
    let rec f a n = function
    | 0L -> a | 1L -> n*a | k when k mod 2L = 0L -> f a (n*n) (k/2L) | k -> f (a*n) (n*n) (k/2L)
    in f 1L n k
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let iia = input_i64_array
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let gi = get_i64
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let g2 = get_2_i64
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let g3 = get_3_i64
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let g4 = get_4_i64
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let g5 = get_5_i64
  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)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let slen s = String.length s
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  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 fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

let md = 998244353
let () =
  let n = gi 0 in let n = i32 n in
  let s = scanf " %s" @@ id in
  if s.[0] = s.[n-$1] then (
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let p = !c %$ md in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let q = !c %$ md in
    printf "%d\n" @@ (((p+$1)*$(q+$1)) %$ md);
  ) else (
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let p = !c in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let q = !c in
    printf "%d\n" @@ ((p+$1)+$(q+$1)-$1) %$ md;
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  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 (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module Int64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  let pow n k =
    let rec f a n = function
    | 0L -> a | 1L -> n*a | k when k mod 2L = 0L -> f a (n*n) (k/2L) | k -> f (a*n) (n*n) (k/2L)
    in f 1L n k
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let iia = input_i64_array
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let gi = get_i64
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let g2 = get_2_i64
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let g3 = get_3_i64
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let g4 = get_4_i64
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let g5 = get_5_i64
  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)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let slen s = String.length s
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  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 fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

let md = 998244353
let () =
  let n = gi 0 in let n = i32 n in
  let s = scanf " %s" @@ id in
  if s.[0] = s.[n-$1] then (
    (* let f,c = ref true,ref s.[0] in
    repi 0 (n-$1) (fun i -> f := !f && !c = s.[i]);
    if !f then (
      fail 0
    ) else (

    ) *)
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let p = !c in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let q = !c in
    printf "%d" @@ (((p+$1) %$ md)*$((q+$1) %$ md) %$ md);
  ) else (
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let p = !c in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let q = !c in
    printf "%d\n" @@ (p+$q+$1) %$ md;
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  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 (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module Int64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  let pow n k =
    let rec f a n = function
    | 0L -> a | 1L -> n*a | k when k mod 2L = 0L -> f a (n*n) (k/2L) | k -> f (a*n) (n*n) (k/2L)
    in f 1L n k
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let iia = input_i64_array
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let gi = get_i64
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let g2 = get_2_i64
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let g3 = get_3_i64
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let g4 = get_4_i64
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let g5 = get_5_i64
  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)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let slen s = String.length s
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  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 fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array
let allsame s =
  let f = ref true in
  String.iter (fun v -> if v <> s.[0] then f := false) s; !f
let () =
  let n = gi 0 in let n = i32 n in
  let s = scanf " %s" @@ id in
  let c = ref 0L in
  repi 0 (n) (fun i ->
    repi i (n) (fun j ->
      let p = String.sub s 0 i in
      let q = String.sub s j (n-$j) in
      let f = allsame @@ p ^ q  in
      if f then
      printf "%s ^ %s = %s :: %b\n"
        p q (p^q) f
      ;
      if f then c+=1L
    )
  ); printf "%Ld\n" !c

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  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 (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module Int64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  let pow n k =
    let rec f a n = function
    | 0L -> a | 1L -> n*a | k when k mod 2L = 0L -> f a (n*n) (k/2L) | k -> f (a*n) (n*n) (k/2L)
    in f 1L n k
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let iia = input_i64_array
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let gi = get_i64
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let g2 = get_2_i64
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let g3 = get_3_i64
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let g4 = get_4_i64
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let g5 = get_5_i64
  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)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let slen s = String.length s
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  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 fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

let () =
  let n = gi 0 in let n = i32 n in
  let s = scanf " %s" @@ id in
  if s.[0] = s.[n-$1] then (
    (* let f,c = ref true,ref s.[0] in
    repi 0 (n-$1) (fun i -> f := !f && !c = s.[i]);
    if !f then (
      fail 0
    ) else (

    ) *)
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let p = !c in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let q = !c in
    printf "%d" @@ p*$q;
  ) else (
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let p = !c in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let q = !c in
    printf "%d\n" @@ p+$q+$1;
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  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 (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module Int64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  let pow n k =
    let rec f a n = function
    | 0L -> a | 1L -> n*a | k when k mod 2L = 0L -> f a (n*n) (k/2L) | k -> f (a*n) (n*n) (k/2L)
    in f 1L n k
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v)) let iia = input_i64_array
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v) let gi = get_i64
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v) let g2 = get_2_i64
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w) let g3 = get_3_i64
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x) let g4 = get_4_i64
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y) let g5 = get_5_i64
  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)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let slen s = String.length s
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  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 fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

let () =
  let n = gi 0 in let n = i32 n in
  let s = scanf " %s" @@ id in
  if s.[0] = s.[n-$1] then (
    (* let f,c = ref true,ref s.[0] in
    repi 0 (n-$1) (fun i -> f := !f && !c = s.[i]);
    if !f then (
      fail 0
    ) else (

    ) *)
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let p = !c in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let q = !c in
    printf "%d" @@ (p+$1)*$(q+$1);
  ) else (
    let f,c = ref true,ref 0 in
    repi 0 (n-$1) (fun i ->
      if !f && s.[i] = s.[0] then (
        c := i+$1
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let p = !c in
    let f,c = ref true,ref 0 in
    repi_rev (n-$1) 0 (fun i ->
      if !f && s.[i] = s.[n-$1] then (
        c := n-$i
      ) else f := false;
    );
    (* printf "%d" !c; *)
    let q = !c in
    printf "%d\n" @@ p+$q+$1;
  )


Wilbur the pig is tinkering with arrays again. He has the array a1, a2, ..., an initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elements ai, ai + 1, ... , an or subtract 1 from all elements ai, ai + 1, ..., an. His goal is to end up with the array b1, b2, ..., bn. Of course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array ai. Initially ai = 0 for every position i, so this array is not given in the input. The second line of the input contains n integers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109).

Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.

NoteIn the first sample, Wilbur may successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes.In the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract 1.

let n = read_int ();;
let a = Array.of_list (List.map (Int64.of_string) (Str.split (Str.regexp " ") (read_line ())));;
let x = ref 0L and ans = ref 0L;;
for i = 0 to n - 1 do
	ans :=
		Int64.add !ans (Int64.abs (Int64.sub !x  a.(i)));
		x := a.(i);
done;;
print_string (Int64.to_string !ans);;

open Big_int

let rec yolo now = function
	|t::q	-> Big_int.add_big_int (Big_int.abs_big_int (Big_int.sub_big_int now t)) (yolo t q)
	| []	-> Big_int.big_int_of_int 0

let main () =
	let n = Scanf.scanf "%d" (fun i -> i) and l = ref [] in
		for j=1 to n do
			Scanf.scanf " %d" (fun i -> l := (Big_int.big_int_of_int i):: !l);
		done;
	Printf.printf "%s" (Big_int.string_of_big_int (yolo (Big_int.big_int_of_int 0) (List.rev !l)));;

main ();;

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 rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) ++ a) 0L

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

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

  let b = Array.init n (fun _ -> long (read_int())) in

  let rec loop i running_total total_ops =
    if i=n then total_ops else (
      let x = running_total -- b.(i) in
      let total_ops = total_ops ++ (Int64.abs x) in
      let running_total = running_total -- x in
      loop (i+1) running_total total_ops
    )
  in

  let answer = loop 0 0L 0L in

  printf "%Ld\n" answer


Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward).  Ivan has beads of n colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace.

The first line of the input contains a single number n (1 ≤ n ≤ 26) — the number of colors of beads. The second line contains after n positive integers ai   — the quantity of beads of i-th color. It is guaranteed that the sum of ai is at least 2 and does not exceed 100 000.

In the first line print a single number — the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace. Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point.

NoteIn the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture.In the second sample there is only one way to compose a necklace.

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) + a) 0
let rec gcd a b = if a=0 then b else gcd (b mod a) a

let n2l n = char_of_int ((int_of_char 'a') + n)

let reverse s = String.init (String.length s) (fun i -> s.[(String.length s) - 1 - i])

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

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

  let oddcount = sum 0 (n-1) (fun i -> a.(i) land 1) in
  let m =
    let rec loop i ac = if i=n then ac else
	if a.(i) land 1 = 1 then loop (i+1) ac
	else loop (i+1) (gcd ac a.(i))
    in
    loop 0 0
  in

  match oddcount with
    | 0 ->
      printf "%d\n" m;

      let slist = fold 0 (n-1) (fun i ac ->
	(String.make (a.(i)/m) (n2l i))::ac
      ) [] in

      let s = String.concat "" slist in
      let sr = reverse s in

      for i=1 to m/2 do
	printf "%s%s" s sr;
      done;
      print_newline()
    | 1 ->
      let oddindex = sum 0 (n-1) (fun i -> i * (a.(i) land 1)) in
      let oddone = a.(oddindex) in

      let rec loop q =
	let m' = gcd (oddone-q) m in
	if (m'/2) mod q = 0 then q else loop (q-2)
      in

      let q = loop oddone in

      printf "%d\n" q;
      a.(oddindex) <- oddone-q;

      let slist = fold 0 (n-1) (fun i ac ->
	(String.make (a.(i)/(2*q)) (n2l i))::ac
      ) [] in
      let s = String.concat "" slist in
      let s' = sprintf "%s%c%s" s (n2l oddindex) (reverse s) in

      for i=1 to q do
	printf "%s" s'
      done;
      print_newline()
    | _ ->
      let slist = fold 0 (n-1) (fun i ac ->
	(String.make a.(i) (n2l i))::ac
      ) [] in

      printf "0\n%s\n" (String.concat "" slist)


Vasya had two arrays consisting of non-negative integers: a of size n and b of size m. Vasya chose a positive integer k and created an n × m matrix v using the following formula:Vasya wrote down matrix v on a piece of paper and put it in the table.A year later Vasya was cleaning his table when he found a piece of paper containing an n × m matrix w. He remembered making a matrix one day by the rules given above but he was not sure if he had found the paper with the matrix v from those days. Your task is to find out if the matrix w that you've found could have been obtained by following these rules and if it could, then for what numbers k, a1, a2, ..., an, b1, b2, ..., bm it is possible.

The first line contains integers n and m (1 ≤ n, m ≤ 100), separated by a space — the number of rows and columns in the found matrix, respectively.  The i-th of the following lines contains numbers wi, 1, wi, 2, ..., wi, m (0 ≤ wi, j ≤ 109), separated by spaces — the elements of the i-th row of matrix w.

If the matrix w could not have been obtained in the manner described above, print "NO" (without quotes) in the single line of output. Otherwise, print four lines. In the first line print "YES" (without quotes). In the second line print an integer k (1 ≤ k ≤ 1018). Note that each element of table w should be in range between 0 and k - 1 inclusively. In the third line print n integers a1, a2, ..., an (0 ≤ ai ≤ 1018), separated by spaces. In the fourth line print m integers b1, b2, ..., bm (0 ≤ bi ≤ 1018), separated by spaces.

NoteBy  we denote the remainder of integer division of b by c.It is guaranteed that if there exists some set of numbers k, a1, ..., an, b1, ..., bm, that you could use to make matrix w, then there also exists a set of numbers that meets the limits 1 ≤ k ≤ 1018, 1 ≤ ai ≤ 1018, 1 ≤ bi ≤ 1018 in the output format. In other words, these upper bounds are introduced only for checking convenience purposes.

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( %% ) a b = Int64.rem a b
let long x = Int64.of_int x

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

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

  let w = Array.make_matrix n m 0L in

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

  let mink = 1L ++ maxf 0 (n-1) (fun i-> maxf 0 (m-1) (fun j -> w.(i).(j))) in

  let g = ref 0L in

  for i=0 to 0 do
    for i'=1 to n-1 do
      for j=0 to 0 do
	for j'=j+1 to m-1 do
	  let d1 = w.(i).(j) -- w.(i').(j) in
	  let d2 = w.(i).(j') -- w.(i').(j') in
	  let d3 = Int64.abs (d1 -- d2) in
	  g := gcd !g d3;

	  let d1 = w.(i).(j) -- w.(i).(j') in
	  let d2 = w.(i').(j) -- w.(i').(j') in
	  let d3 = Int64.abs (d1 -- d2) in
	  g := gcd !g d3;

	done
      done
    done
  done;

  let k = if !g = 0L then mink else !g in

  if k < mink then printf "NO\n" else (
    printf "YES\n";

    let a = Array.make n 0L in
    let b = Array.make m 0L in

    for i=1 to n-1 do
      a.(i) <- (w.(i).(0) -- w.(0).(0)) %% k;
      if a.(i) < 0L then a.(i) <- a.(i) ++ k;
    done;

    for j=0 to m-1 do
      b.(j) <- w.(0).(j) %% k;
    done;

    printf "%Ld\n" k;

    for i=0 to n-1 do
      printf "%Ld " a.(i)
    done;

    print_newline();

    for j=0 to m-1 do
      printf "%Ld " b.(j)
    done;
    print_newline();
  )


Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them.Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block.For example:  the following words have typos: "hellno", "hackcerrs" and "backtothefutttture";  the following words don't have typos: "helllllooooo", "tobeornottobe" and "oooooo". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos.Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem.

The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters.

Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them.

let rec splitstr s i pc1 pc2 cnt =
if (i < String.length s) then
(
	match s.[i] with
	|'a'|'e'|'i'|'o'|'u' as ch -> (* Printf.printf "vowel: %c\n" ch; flush(stdout); *)(String.make 1 ch)^(splitstr s (i+1) pc2 ch 0)
	|ch when (cnt < 2) -> (* Printf.printf "Conscnt<3: %c %d\n" ch cnt; flush(stdout); *)(String.make 1 ch)^(splitstr s (i+1) pc2 ch (cnt+1))
	|ch when ((ch = pc1)&&(ch=pc2)) -> (* Printf.printf "series: %c %d\n" ch cnt; flush(stdout); *)(String.make 1 ch)^(splitstr s (i+1) pc2 ch (cnt+1))
	|ch when (cnt>=2) -> (* Printf.printf "Overconso: %c %d\n" ch cnt; flush(stdout); *)(" "^(String.make 1 ch))^(splitstr s (i+1) pc2 ch 1)
	|_ -> failwith "Redundancy"
)
else "";;

let st = read_line();;

let _ =
	Printf.printf "%s\n" (splitstr st 0 '-' '-' (0));
	flush(stdout);;

let rec splitstr s i pc1 pc2 cnt =
if (i < String.length s) then
(
	match s.[i] with
	|'a'|'e'|'i'|'o'|'u' as ch -> (* Printf.printf "vowel: %c\n" ch; flush(stdout); *)(String.make 1 ch)^(splitstr s (i+1) pc2 ch 0)
	|ch when (cnt < 2) -> (* Printf.printf "Conscnt<3: %c %d\n" ch cnt; flush(stdout); *)(String.make 1 ch)^(splitstr s (i+1) pc2 ch (cnt+1))
	|ch when ((ch = pc1)&&(ch=pc2)) -> (* Printf.printf "series: %c %d\n" ch cnt; flush(stdout); *)(String.make 1 ch)^(splitstr s (i+1) pc2 ch (cnt+1))
	|ch when (cnt>=2) -> (* Printf.printf "Overconso: %c %d\n" ch cnt; flush(stdout); *)(" "^(String.make 1 ch))^(splitstr s (i+1) pc2 ch 0)
	|_ -> failwith "Redundancy"
)
else "";;

let st = read_line();;

let _ =
	Printf.printf "%s\n" (splitstr st 0 '-' '-' (0));
	flush(stdout);;


Several days ago you bought a new house and now you are planning to start a renovation. Since winters in your region can be very cold you need to decide how to heat rooms in your house.Your house has $$$n$$$ rooms. In the $$$i$$$-th room you can install at most $$$c_i$$$ heating radiators. Each radiator can have several sections, but the cost of the radiator with $$$k$$$ sections is equal to $$$k^2$$$ burles.Since rooms can have different sizes, you calculated that you need at least $$$sum_i$$$ sections in total in the $$$i$$$-th room. For each room calculate the minimum cost to install at most $$$c_i$$$ radiators with total number of sections not less than $$$sum_i$$$.

The first line contains single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of rooms. Each of the next $$$n$$$ lines contains the description of some room. The $$$i$$$-th line contains two integers $$$c_i$$$ and $$$sum_i$$$ ($$$1 \le c_i, sum_i \le 10^4$$$) — the maximum number of radiators and the minimum total number of sections in the $$$i$$$-th room, respectively.

For each room print one integer — the minimum possible cost to install at most $$$c_i$$$ radiators with total number of sections not less than $$$sum_i$$$.

NoteIn the first room, you can install only one radiator, so it's optimal to use the radiator with $$$sum_1$$$ sections. The cost of the radiator is equal to $$$(10^4)^2 = 10^8$$$.In the second room, you can install up to $$$10^4$$$ radiators, but since you need only one section in total, it's optimal to buy one radiator with one section.In the third room, there $$$7$$$ variants to install radiators: $$$[6, 0]$$$, $$$[5, 1]$$$, $$$[4, 2]$$$, $$$[3, 3]$$$, $$$[2, 4]$$$, $$$[1, 5]$$$, $$$[0, 6]$$$. The optimal variant is $$$[3, 3]$$$ and it costs $$$3^2+ 3^2 = 18$$$.

let rep f n = let rec iter i = if i < n then (f ();iter (i+1)) in iter 0

let () =
  read_int () |> rep (fun () ->
    let (a, b) = Scanf.scanf "%d %d " (fun a b -> (a,b)) in
    let k = b/a and l = b mod a in
    print_int (k*k*(a-l) + (k+1)*(k+1)*l);
    print_newline ()
    )


You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i1, j1), (i2, j2), ..., (im, jm). Each good pair (ik, jk) meets the following conditions: ik + jk is an odd number and 1 ≤ ik < jk ≤ n.In one operation you can perform a sequence of actions:   take one of the good pairs (ik, jk) and some integer v (v > 1), which divides both numbers a[ik] and a[jk];  divide both numbers by v, i. e. perform the assignments:  and . Determine the maximum number of operations you can sequentially perform on the given array. Note that one pair may be used several times in the described operations.

The first line contains two space-separated integers n, m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100). The second line contains n space-separated integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109) — the description of the array. The following m lines contain the description of good pairs. The k-th line contains two space-separated integers ik, jk (1 ≤ ik < jk ≤ n, ik + jk is an odd number). It is guaranteed that all the good pairs are distinct.

open Printf
open Scanf

(* Implementation of Dinic's algorithm for max flow. See ../spoj/PROFIT/
   for additional algorithms. *)

let infinite_c = 10000 (* "infinite" capacity *)

let maxflow s t adj cap =
  (* s and t are nodes (numbers). g is a graph (represented via an
     array of adjacency lists). c is a 1-d array of capacities.  The
     adjacency list on node v stores pairs like (w,i) where w is the
     other node, and i is the index into the capacity array for where
     this capacity is stored.  It's set up so that i lxor 1 is where the
     capacity of edge (w,v) is stored.  We compute the maximum flow in
     the same representation as the capacities.  *)

  let c = Array.copy cap in
  let n = Array.length adj in
  let alive = Array.make n [] in
  let level = Array.make n 0 in
  let queue = Array.make n 0 in
  let front = ref 0 in
  let back = ref 0 in

  let push w = queue.(!back) <- w; back := !back+1 in
  let eject() = let x = queue.(!front) in front := !front + 1; x; in

  let rec bfs () =
    let rec loop () =
      if !front = !back then false else
	let v = eject() in
	  if level.(v) = level.(t) then true else (
	    List.iter (
	      fun (w,i) -> if level.(w) < 0 && c.(i) > 0 then (
		level.(w) <- level.(v) + 1;
		push w
	      )
	    ) adj.(v);
	    loop ()
	  )
    in
      level.(s) <- 0;
      back := 0; front := 0;
      push s;
      loop ();
  in

  let rec dfs v cap =
    (* Does a DFS from v looking for a path to t.  It tries to push cap units
       of flow to t.  It returns the amount of flow actually pushed *)
    if v=t then cap else
      let rec loop total remaining li =
	  match li with
	    | [] ->
		alive.(v) <- [];
		total
	    | (w,i)::tail ->
		if level.(w) = level.(v)+1 && c.(i) > 0 then (
		  let p = dfs w (min c.(i) remaining) in
		    c.(i) <- c.(i) - p;
		    c.(i lxor 1) <- c.(i lxor 1) + p;
		    if remaining-p = 0 then (
		      alive.(v) <- li;
		      total+p
		    ) else loop (total+p) (remaining-p) tail
		) else loop total remaining tail
      in loop 0 cap alive.(v)
  in

  let rec main_loop flow_so_far =
    for i=0 to n-1 do level.(i) <- -1; done;
    Array.blit adj 0 alive 0 n;
    if bfs () then main_loop (flow_so_far + (dfs s infinite_c)) else flow_so_far
  in
    main_loop 0

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

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

let () =
  let n = read_int () in
  let m = read_int () in
  let a = Array.init n (fun _ -> read_int()) in
  let pair = Array.init m (fun _ ->
    let i = read_int() -1 in
    let j = read_int() -1 in
    if i land 1 = 0 then (i,j) else (j,i)
  ) in

  let fact = Array.init n (fun i -> factor a.(i)) in

  let primeset = Hashtbl.create 10 in  (* the set of primes that occur *)
  for i=0 to n-1 do
    List.iter (fun p -> Hashtbl.replace primeset p true) fact.(i)
  done;

  let build_graph p =
    (* build the graph for a specific prime p *)
    let s = n in
    let t = n+1 in
    let adj = Array.make (n+2) [] in
    let cap = Array.make (2*m + 2*n) 0 in

    let add_edge u v c cr ind indr =
      adj.(u) <- (v,ind)::adj.(u);
      adj.(v) <- (u,indr)::adj.(v);
      cap.(ind) <- c;
      cap.(indr) <- cr
    in

    for k = 0 to m-1 do
      let (i,j) = pair.(k) in
      add_edge i j infinite_c 0 (2*k) (2*k+1)
    done;

    for v = 0 to n-1 do
      let capacity = List.length (List.filter (fun e -> e = p) fact.(v)) in
      let edgeno = 2*m + 2*v in
      if v land 1 = 0 then (
	add_edge s v capacity 0 edgeno (edgeno+1);
      ) else (
	add_edge v t capacity 0 edgeno (edgeno+1);
      )
    done;
    (s,t,adj,cap)
  in

  let flowsum = ref 0 in

  Hashtbl.iter (fun p _ ->
    let (s,t,adj,cap) = build_graph p in
    let f = maxflow s t adj cap in
    flowsum := !flowsum + f
  ) primeset;

  printf "%d\n" !flowsum


Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities.Bear Limak was once in a city a and he wanted to go to a city b. There was no direct connection so he decided to take a long walk, visiting each city exactly once. Formally:   There is no road between a and b.  There exists a sequence (path) of n distinct cities v1, v2, ..., vn that v1 = a, vn = b and there is a road between vi and vi + 1 for . On the other day, the similar thing happened. Limak wanted to travel between a city c and a city d. There is no road between them but there exists a sequence of n distinct cities u1, u2, ..., un that u1 = c, un = d and there is a road between ui and ui + 1 for .Also, Limak thinks that there are at most k roads in Bearland. He wonders whether he remembers everything correctly.Given n, k and four distinct cities a, b, c, d, can you find possible paths (v1, ..., vn) and (u1, ..., un) to satisfy all the given conditions? Find any solution or print -1 if it's impossible.

The first line of the input contains two integers n and k (4 ≤ n ≤ 1000, n - 1 ≤ k ≤ 2n - 2) — the number of cities and the maximum allowed number of roads, respectively. The second line contains four distinct integers a, b, c and d (1 ≤ a, b, c, d ≤ n).

Print -1 if it's impossible to satisfy all the given conditions. Otherwise, print two lines with paths descriptions. The first of these two lines should contain n distinct integers v1, v2, ..., vn where v1 = a and vn = b. The second line should contain n distinct integers u1, u2, ..., un where u1 = c and un = d. Two paths generate at most 2n - 2 roads: (v1, v2), (v2, v3), ..., (vn - 1, vn), (u1, u2), (u2, u3), ..., (un - 1, un). Your answer will be considered wrong if contains more than k distinct roads or any other condition breaks. Note that (x, y) and (y, x) are the same road.

NoteIn the first sample test, there should be 7 cities and at most 11 roads. The provided sample solution generates 10 roads, as in the drawing. You can also see a simple path of length n between 2 and 4, and a path between 7 and 3.

open Printf
open Scanf

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

let () =
  let (n,k) = read_pair() in
  let (a,b) = read_pair() in
  let (c,d) = read_pair() in

  if n <= 4 || k < n+1 then
    printf "-1\n"
  else (
    let perm = Array.make (n+1) 0 in
    let used = Array.make (n+1) false in
    perm.(1) <- a;
    perm.(2) <- c;
    perm.(n) <- b;
    perm.(n-1) <- d;
    used.(a) <- true;
    used.(b) <- true;
    used.(c) <- true;
    used.(d) <- true;
    let lowused = ref 0 in
    for i=1 to n do
      if perm.(i) = 0 then (
	let rec loop j = if used.(j) then loop (j+1) else j in
	let next = loop (!lowused + 1) in
	lowused := next;
	used.(next) <- true;
	perm.(i) <- next
      )
    done;

    for i=1 to n do
      printf "%d " perm.(i)
    done;
    print_newline();
    for i=1 to n do
      let j =
	if i=1 then 2
	else if i=2 then 1
	else if i=n-1 then n
	else if i=n then n-1 else i
      in
      printf "%d " perm.(j)
    done;
    print_newline();
  )


Vlad likes to eat in cafes very much. During his life, he has visited cafes n times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research.First of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes he visited in a row, in order of visiting them. Now, Vlad wants to find such a cafe that his last visit to that cafe was before his last visits to every other cafe. In other words, he wants to find such a cafe that he hasn't been there for as long as possible. Help Vlad to find that cafe.

In first line there is one integer n (1 ≤ n ≤ 2·105) — number of cafes indices written by Vlad. In second line, n numbers a1, a2, ..., an (0 ≤ ai ≤ 2·105) are written — indices of cafes in order of being visited by Vlad. Vlad could visit some cafes more than once. Note that in numeration, some indices could be omitted.

Print one integer — index of the cafe that Vlad hasn't visited for as long as possible.

NoteIn first test, there are three cafes, and the last visits to cafes with indices 1 and 2 were after the last visit to cafe with index 3; so this cafe is the answer. In second test case, there are also three cafes, but with indices 1, 2 and 4. Cafes with indices 1 and 4 were visited after the last visit of cafe with index 2, so the answer is 2. Note that Vlad could omit some numbers while numerating the cafes.

let n = read_int () in
let h = Hashtbl.create n in
for i = 0 to n-1 do Hashtbl.replace h (Scanf.scanf " %d" (fun x -> x)) i done;
print_int @@ fst @@ Hashtbl.fold (fun a b c -> if b < snd c then (a,b) else c) h (min_int, max_int);

module IntSet = Set.Make(struct
    let compare = Pervasives.compare
    type t = int
end);;
let n = read_int () in
let a = Array.make n 0 and
s = ref IntSet.empty in
for i = 0 to n-1 do begin
    a.(i) <- Scanf.scanf " %d" (fun x -> x);
    s := IntSet.add a.(i) !s
end done;
let p = ref 0 in
for i = n-1 downto 0 do begin
    if IntSet.mem a.(i) !s then begin
        p := a.(i) ;
        s := IntSet.remove a.(i) !s
    end
end done;
print_int !p;

(* 7:22 *)

open Printf
open Scanf

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

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

  let m = 200_001 in
  let vtime = Array.make (m+1) (2*m) in

  for i=0 to n-1 do
    vtime.(a.(i)) <- i
  done;

  let cafe = minf 0 m (fun cafe -> vtime.(cafe)) in

  printf "%d\n" a.(cafe)


Bessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play! More specifically, he wants to get from $$$(0,0)$$$ to $$$(x,0)$$$ by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its $$$n$$$ favorite numbers: $$$a_1, a_2, \ldots, a_n$$$. What is the minimum number of hops Rabbit needs to get from $$$(0,0)$$$ to $$$(x,0)$$$? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination.Recall that the Euclidean distance between points $$$(x_i, y_i)$$$ and $$$(x_j, y_j)$$$ is $$$\sqrt{(x_i-x_j)^2+(y_i-y_j)^2}$$$.For example, if Rabbit has favorite numbers $$$1$$$ and $$$3$$$ he could hop from $$$(0,0)$$$ to $$$(4,0)$$$ in two hops as shown below. Note that there also exists other valid ways to hop to $$$(4,0)$$$ in $$$2$$$ hops (e.g. $$$(0,0)$$$ $$$\rightarrow$$$ $$$(2,-\sqrt{5})$$$ $$$\rightarrow$$$ $$$(4,0)$$$).    Here is a graphic for the first example. Both hops have distance $$$3$$$, one of Rabbit's favorite numbers. In other words, each time Rabbit chooses some number $$$a_i$$$ and hops with distance equal to $$$a_i$$$ in any direction he wants. The same number can be used multiple times.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Next $$$2t$$$ lines contain test cases — two lines per test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le x \le 10^9$$$)  — the number of favorite numbers and the distance Rabbit wants to travel, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$)  — Rabbit's favorite numbers. It is guaranteed that the favorite numbers are distinct. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$10^5$$$.

For each test case, print a single integer — the minimum number of hops needed.

NoteThe first test case of the sample is shown in the picture above. Rabbit can hop to $$$(2,\sqrt{5})$$$, then to $$$(4,0)$$$ for a total of two hops. Each hop has a distance of $$$3$$$, which is one of his favorite numbers.In the second test case of the sample, one way for Rabbit to hop $$$3$$$ times is: $$$(0,0)$$$ $$$\rightarrow$$$ $$$(4,0)$$$ $$$\rightarrow$$$ $$$(8,0)$$$ $$$\rightarrow$$$ $$$(12,0)$$$.In the third test case of the sample, Rabbit can hop from $$$(0,0)$$$ to $$$(5,0)$$$.In the fourth test case of the sample, Rabbit can hop: $$$(0,0)$$$ $$$\rightarrow$$$ $$$(5,10\sqrt{2})$$$ $$$\rightarrow$$$ $$$(10,0)$$$.

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

let rec read_int_seq n =
  match n with
    0 -> []
  | _ -> (get_int ()) :: (read_int_seq (n-1))
;;

let read_case () =
  let n = get_int () in
  let x = get_int () in
  let a = read_int_seq n in
  (n, x, a)
;;

let rec max xs =
  match xs with
    [x] -> x
  | x :: xs' -> let y = max xs' in
                if x > y then x else y
;;

let rec contains xs y =
  match xs with
    [] -> false
  | x :: xs' -> (x=y) || (contains xs' y)
;;

let solve (n, x, a) =
  let m = max a in
  if (x mod m)=0 then x/m else
    if x > m then (x/m)+1 else
      if (contains a x) then 1 else 2
;;

let rec solve_cases t =
  if t=0 then () else
    let case = read_case () in
    let _ = (solve case) |> string_of_int |> print_endline in
    solve_cases (t-1)
;;

let t = read_int () in
    solve_cases t

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

let rec read_int_seq n =
  match n with
    0 -> []
  | _ -> (get_int ()) :: (read_int_seq (n-1))
;;

let read_case () =
  let n = get_int () in
  let x = get_int () in
  let a = read_int_seq n in
  (n, x, a)
;;

let rec max xs =
  match xs with
    [x] -> x
  | x :: xs' -> let y = max xs' in
                if x > y then x else y
;;

let solve (n, x, a) =
  let m = max a in
  if (x mod m)=0 then x/m else
  if (x/m)=0 then 2 else (x/m)+1
;;

let rec solve_cases t =
  if t=0 then () else
    let case = read_case () in
    let _ = (solve case) |> string_of_int |> print_endline in
    solve_cases (t-1)
;;

let t = read_int () in
    solve_cases t


Let's call an undirected graph $$$G = (V, E)$$$ relatively prime if and only if for each edge $$$(v, u) \in E$$$  $$$GCD(v, u) = 1$$$ (the greatest common divisor of $$$v$$$ and $$$u$$$ is $$$1$$$). If there is no edge between some pair of vertices $$$v$$$ and $$$u$$$ then the value of $$$GCD(v, u)$$$ doesn't matter. The vertices are numbered from $$$1$$$ to $$$|V|$$$.Construct a relatively prime graph with $$$n$$$ vertices and $$$m$$$ edges such that it is connected and it contains neither self-loops nor multiple edges.If there exists no valid graph with the given number of vertices and edges then output "Impossible".If there are multiple answers then print any of them.

The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of vertices and the number of edges.

If there exists no valid graph with the given number of vertices and edges then output "Impossible". Otherwise print the answer in the following format: The first line should contain the word "Possible". The $$$i$$$-th of the next $$$m$$$ lines should contain the $$$i$$$-th edge $$$(v_i, u_i)$$$ of the resulting graph ($$$1 \le v_i, u_i \le n, v_i \neq u_i$$$). For each pair $$$(v, u)$$$ there can be no more pairs $$$(v, u)$$$ or $$$(u, v)$$$. The vertices are numbered from $$$1$$$ to $$$n$$$. If there are multiple answers then print any of them.

NoteHere is the representation of the graph from the first example:

open Printf open Scanf
module MyInt64 = struct
	(* include Int64 *)
	(* int *)
	let (+$) = (+)
	let (-$) = (-)
	let (/$) = (/)
	let ( *$) = ( * )
	(* int64 *)
	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 labs p = if p < 0L then -1L*p else p
	(* let (~|) p = Int64.of_int p *)
	let (~|) p = Int64.to_int p
	let (~~|) p = Int64.of_int p

	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 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 rec gcd m n = match m,n with
		| m,0L -> m
		| m,n -> gcd n (m mod n)

end open MyInt64

exception Hoge
exception Fuga

let () =
	let n,m = get_2_i64 ()
	in
	let ls = ref []
	in let rest = ref m
	in
	if m < n - 1L then printf "Impossible\n"
	else
	try
		for i=1 to ~|n do
			for j=i+$1 to ~|n do
				(* printf "gcd %Ld %Ld = %Ld; %b\n" ~~|i ~~|j (gcd ~~|i ~~|j) (gcd ~~|i ~~|j = 1L); *)
				if gcd ~~|i ~~|j = 1L then (
					ls := (i,j)::!ls;
					rest -= 1L;
					if !rest <= 0L then raise Hoge
				)
			done;
		done;
		raise Fuga
	with
	| Hoge -> (
		printf "Possible\n";
		List.iter (fun (u,v) -> printf "%d %d\n" u v) !ls;
	)
	| Fuga ->
		printf "Impossible\n";

open Printf open Scanf
module MyInt64 = struct
	(* include Int64 *)
	(* int *)
	let (+$) = (+)
	let (-$) = (-)
	let (/$) = (/)
	let ( *$) = ( * )
	(* int64 *)
	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 labs p = if p < 0L then -1L*p else p
	(* let (~|) p = Int64.of_int p *)
	let (~|) p = Int64.to_int p
	let (~~|) p = Int64.of_int p

	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 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 rec gcd m n = match m,n with
		| m,0L -> m
		| m,n -> gcd n (m mod n)

end open MyInt64

exception Hoge
exception Fuga

let () =
	let n,m = get_2_i64 ()
	in
	let ls = ref []
	in let rest = ref m
	in
	try
		for i=1 to ~|n do
			for j=i+$1 to ~|n do
				(* printf "gcd %Ld %Ld = %Ld; %b\n" ~~|i ~~|j (gcd ~~|i ~~|j) (gcd ~~|i ~~|j = 1L); *)
				if gcd ~~|i ~~|j = 1L then (
					ls := (i,j)::!ls;
					rest -= 1L;
					if !rest <= 0L then raise Hoge
				)
			done;
		done;
		raise Fuga
	with
	| Hoge -> (
		printf "Possible\n";
		List.iter (fun (u,v) -> printf "%d %d\n" u v) !ls;
	)
	| Fuga ->
		printf "Impossible\n";


Of course, many of you can calculate φ(n) — the number of positive integers that are less than or equal to n, that are coprime with n. But what if we need to calculate φ(φ(...φ(n))), where function φ is taken k times and n is given in the canonical decomposition into prime factors? You are given n and k, calculate the value of φ(φ(...φ(n))). Print the result in the canonical decomposition into prime factors.

The first line contains integer m (1 ≤ m ≤ 105) — the number of distinct prime divisors in the canonical representaion of n. Each of the next m lines contains a pair of space-separated integers pi, ai (2 ≤ pi ≤ 106; 1 ≤ ai ≤ 1017) — another prime divisor of number n and its power in the canonical representation. The sum of all ai doesn't exceed 1017. Prime divisors in the input follow in the strictly increasing order. The last line contains integer k (1 ≤ k ≤ 1018).

In the first line, print integer w — the number of distinct prime divisors of number φ(φ(...φ(n))), where function φ is taken k times. Each of the next w lines must contain two space-separated integers qi, bi (bi ≥ 1) — another prime divisor and its power in the canonical representaion of the result. Numbers qi must go in the strictly increasing order.

NoteYou can read about canonical representation of a positive integer here: http://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic.You can read about function φ(n) here: http://en.wikipedia.org/wiki/Euler's_totient_function.

(*
   Imagine the following graph.  There's a node for every prime, and in
   the node an exponent of that prime is stored.  There is an edge from
   prime p to each of the prime factors of p-1.  EG from 13 there are two
   edges to 2 and one edge to 3.  (2*2*3=12).

   To compute Phi of a number represented in this way, you do the following.
   For each non-zero exponent (simultaneously) decrement its exponentent
   by 1. Then for each successor of those that you decremented, you increment
   the exponent by 1.

   The problem here is to be able to iterate the Phi operator a huge
   number of times efficiently.

   Call a state "normal" if there is no pair of nodes p-->q such that
   p is non-zero while q is zero.  The beauty of a normal state is
   that if you apply the Phi operator to it, it remains normal.

   Consider what happens to some node when you hit it with the Phi
   operator in a normal state.  Exactly the same thing happens every
   time locally to it until all of its sources dry up.

   This means we can apply the Phi operator to various different nodes
   in any order we want.  So we apply it in topological order
   (i.e. largest first).  It's easy to apply the operator all k times
   to a node in O(number successors of it).

   It remains to explain how to get the system into a normal state.
   We simply iterate the Phi operator on it until it becomes normal.
   Then we reduce k by the number of times we had to apply the
   operator.  The number of times we have to do this is the length of
   the longest path in the graph.  This is at most logarithmic in the
   largest prime (a pessimistic estimate).

   (There is also an alternative algorithm that "protects" some amount
   of the exponent from being depleted.  You are effectively computing
   the number of iterations until a node becomes non-zero.  That's the
   amount of protection it gets.  This can be computed as you go along.
   Some of the other shorter solutions do it this way.)

                  Danny Sleator, March 2014
*)

open Printf
open Scanf

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

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

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

let () =
  let m = read_int () in
  let p = Array.make m 0 in
  let a = Array.make m 0L in
  for i=0 to m-1 do
    p.(i) <- read_int();
    a.(i) <- read_long()
  done;

  let k = read_long () in

  let max_p = p.(m-1) in

  let sieve = Array.make (max_p+1) 0 in
(* sieve.(n) is the smallest prime that divides n.
   sieve.(n) = n iff n is prime. *)

  for i=2 to max_p do
    if sieve.(i) = 0 then
      let rec loop j = if j <= max_p then (
	sieve.(j) <- i;
	loop (j+i);
      ) in
      loop (i)
  done;

  let e = Array.make (max_p+1) 0L in

  for i=0 to m-1 do
    e.(p.(i)) <- a.(i)
  done;

  let is_normal () =
(*    let process p =
      let rec loop r = r=1 || (e.(sieve.(r)) <> 0L && loop (r/sieve.(r))) in
      loop (p-1);
    in *)

    let process p =
      let rec loop r = if r=1 then true
	else if e.(sieve.(r)) = 0L then false else loop (r/sieve.(r))
      in
      loop (p-1);
    in

    let rec loop p = if p > max_p then true
      else if (sieve.(p) <> p || e.(p) = 0L) then loop (p+1)
      else process p && loop (p+1)
    in
    loop 2
  in

  let step () =
    let process p =
      let rec loop r = if r>1 then
	  let s = sieve.(r) in
	  e.(s) <- e.(s) ++ 1L;
	  loop (r/s)
      in
      loop (p-1);
      e.(p) <- e.(p) -- 1L;
    in

    for p=2 to max_p do
      if sieve.(p) = p && e.(p) > 0L then process p
    done;
  in

  let rec steploop k = if k=0L || is_normal() then k else (
    step();
    steploop (k--1L)
  )
  in

  let k = steploop k in

  let elim p =
    let d = min e.(p) k in
    e.(p) <- e.(p) -- d;
      (* now add d to the exponents of all the prime factors of p-1 *)
    let rec loop r = if r=1 then () else
	let s = sieve.(r) in
	e.(s) <- e.(s) ++ d;
	loop (r/s)
    in
    loop (p-1);
  in

  for p=max_p downto 2 do
    if sieve.(p) = p then elim p
  done;

  let np = sum 2 max_p (fun i -> if e.(i) <> 0L then 1 else 0) in

  printf "%d\n" np;

  for i=2 to max_p do
    if e.(i) <> 0L then printf "%d %Ld\n" i e.(i)
  done

open Printf
open Scanf

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

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

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

let () =
  let m = read_int () in
  let p = Array.make m 0 in
  let a = Array.make m 0L in
  for i=0 to m-1 do
    p.(i) <- read_int();
    a.(i) <- read_long()
  done;

  let k = read_long () in

  let max_p = p.(m-1) in

  let sieve = Array.make (max_p+1) 0 in
(* sieve.(n) is the smallest prime that divides n.
   sieve.(n) = n iff n is prime. *)

  for i=2 to max_p do
    if sieve.(i) = 0 then
      let rec loop j = if j <= max_p then (
	sieve.(j) <- i;
	loop (j+i);
      ) in
      loop (i)
  done;

  let e = Array.make (max_p+1) 0L in

  for i=0 to m-1 do
    e.(p.(i)) <- a.(i)
  done;

  let is_normal () =
    let process p =
      let rec loop r = r=1 || (e.(sieve.(r)) <> 0L && loop (r/sieve.(r))) in
      loop (p-1);
    in

    let rec loop p = if p = max_p then true
      else if (sieve.(p) <> p || e.(p) = 0L) then loop (p+1)
      else process p && loop (p+1)
    in
    loop 2
  in

  let step () =
    let process p =
      let rec loop r = if r>1 then
	  let s = sieve.(r) in
	  e.(s) <- e.(s) ++ 1L;
	  loop (r/s)
      in
      loop (p-1);
      e.(p) <- e.(p) -- 1L;
    in

    for p=2 to max_p do
      if sieve.(p) = p && e.(p) > 0L then process p
    done;
  in

  let rec steploop k = if k=0L || is_normal() then k else (
    step();
    steploop (k--1L)
  )
  in

  let k = steploop k in

  let elim p =
    let d = min e.(p) k in
    e.(p) <- e.(p) -- d;
      (* now add d to the exponents of all the prime factors of p-1 *)
    let rec loop r = if r=1 then () else
	let s = sieve.(r) in
	e.(s) <- e.(s) ++ d;
	loop (r/s)
    in
    loop (p-1);
  in

  for p=max_p downto 2 do
    if sieve.(p) = p then elim p
  done;

  let np = sum 2 max_p (fun i -> if e.(i) <> 0L then 1 else 0) in

  printf "%d\n" np;

  for i=2 to max_p do
    if e.(i) <> 0L then printf "%d %Ld\n" i e.(i)
  done


There are $$$n$$$ candies put from left to right on a table. The candies are numbered from left to right. The $$$i$$$-th candy has weight $$$w_i$$$. Alice and Bob eat candies. Alice can eat any number of candies from the left (she can't skip candies, she eats them in a row). Bob can eat any number of candies from the right (he can't skip candies, he eats them in a row). Of course, if Alice ate a candy, Bob can't eat it (and vice versa).They want to be fair. Their goal is to eat the same total weight of candies. What is the most number of candies they can eat in total?

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2\cdot10^5$$$) — the number of candies on the table. The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \leq w_i \leq 10^4$$$) — the weights of candies from left to right. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.

For each test case, print a single integer — the maximum number of candies Alice and Bob can eat in total while satisfying the condition.

NoteFor the first test case, Alice will eat one candy from the left and Bob will eat one candy from the right. There is no better way for them to eat the same total amount of weight. The answer is $$$2$$$ because they eat two candies in total.For the second test case, Alice will eat the first three candies from the left (with total weight $$$7$$$) and Bob will eat the first three candies from the right (with total weight $$$7$$$). They cannot eat more candies since all the candies have been eaten, so the answer is $$$6$$$ (because they eat six candies in total).For the third test case, there is no way Alice and Bob will eat the same non-zero weight so the answer is $$$0$$$.For the fourth test case, Alice will eat candies with weights $$$[7, 3, 20]$$$ and Bob will eat candies with weights $$$[10, 8, 11, 1]$$$, they each eat $$$30$$$ weight. There is no better partition so the answer is $$$7$$$.

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

let getList(n): int list =
  let rec getList2 ((n: int), (acc: int list)) = match n with
	  | 0 -> List.rev acc
	  | _ -> getList2 (n-1, getInt() :: acc)
  in getList2(n, [])

let getLine() = Scanf.scanf " %s" (fun i -> i);;

let prefix((arr: int list)) =
  let rec p2((arr: int list), (acc: int list), (s: int)) =
    match arr with
    | [] -> List.rev(acc)
    | x::xs -> p2(xs, (s + x)::acc, s + x)
  in p2(arr, [0], 0);;

let rec solve((p1: int list), (p2: int list), (ct: int), (n: int), (ret: int)) =
  if ct = n then ret else
  if (List.hd(List.tl(p2)) < List.hd(p1)) then solve(p1, List.tl(p2), ct + 1, n, ret)
  else if (List.hd(List.tl(p2)) = List.hd(p1))then solve(p1, List.tl(p2), ct + 1, n, ct + 1)
  else solve(List.tl(p1), p2, ct + 1, n, ret);;

let main() =
  let t = getInt() in
  for i = 1 to t do
    let n = getInt() in let arr = getList(n) in let arr2 = List.rev(arr) in
    let p1 = prefix(arr) in let p2 = prefix(arr2) in
    print_endline(string_of_int(solve(p1, p2, 0, n, 0)))

  done;;

main();;


To stay woke and attentive during classes, Karen needs some coffee!  Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe".She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste.Karen thinks that a temperature is admissible if at least k recipes recommend it.Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range?

The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≤ li ≤ ri ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive.

For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.

NoteIn the first test case, Karen knows 3 recipes.  The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.  The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.  The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it.She asks 4 questions.In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible.In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible.In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none.In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible.In the second test case, Karen knows 2 recipes.  The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree.  The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it.In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none.

Scanf.scanf "%d %d %d" (fun n k q ->
let maxn=200001 in
let add,sub = (Array.make maxn 0) ,(Array.make maxn 0) in
let presum=Array.make 211111 0 in
let cnt = ref 0 in
  for i = 0 to n-1 do
    Scanf.scanf "\n%d %d" (fun x y ->
    add.(x) <- add.(x) + 1 ;
    sub.(y) <- sub.(y) + 1 )
  done;
  for i = 1 to maxn-1 do
    cnt:=!cnt+add.(i)-sub.(i-1);
    presum.(i) <- (if !cnt>=k then 1 else 0) +presum.(i-1)
  done;
  for i= 1 to q do
    Scanf.scanf "\n%d %d" (fun l r ->
      Printf.printf "%d\n" (presum.(r)-presum.(l-1))
    )
  done)

Scanf.scanf "%d %d %d" (fun n k q ->
let qs,qt = Queue.create(), Queue.create() in
let dat = Array.make n (0,0) in
let presum=Array.make 211111 0 in
let cnt = ref 0 in
  for i = 0 to n-1 do
    Scanf.scanf "\n%d %d" (fun x y ->dat.(i) <-(x,y))
  done;
  Array.sort (fun (xh,_) (yh,_) -> xh-yh) dat;
  Array.iter (fun (h,_) -> Queue.push h qs) dat;
  Array.sort (fun (_,xt) (_,yt) -> xt-yt) dat;
  Array.iter (fun (_,t) -> Queue.push t qt) dat;
  for i = 1 to 200000 do
    (*Printf.printf "i=%d\n" i;*)
    while not (Queue.is_empty qs) && Queue.top qs <= i do

      (*Printf.printf "qs:%d\n"*)   ( Queue.pop qs );
         cnt:= !cnt +1
      done;
    while not (Queue.is_empty qt) && Queue.top qt < i do
      (*Printf.printf "qt:%d\n"*)   ( Queue.pop qt );
         cnt:= !cnt -1
    done;
    presum.(i) <- (if !cnt>=k then 1 else 0) +presum.(i-1)
  done;
  for i= 1 to q do
    Scanf.scanf "\n%d %d" (fun l r ->
      Printf.printf "%d\n" (presum.(r)-presum.(l-1))
    )
  done)


Valera runs a 24/7 fast food cafe. He magically learned that next day n people will visit his cafe. For each person we know the arrival time: the i-th person comes exactly at hi hours mi minutes. The cafe spends less than a minute to serve each client, but if a client comes in and sees that there is no free cash, than he doesn't want to wait and leaves the cafe immediately. Valera is very greedy, so he wants to serve all n customers next day (and get more profit). However, for that he needs to ensure that at each moment of time the number of working cashes is no less than the number of clients in the cafe. Help Valera count the minimum number of cashes to work at his cafe next day, so that they can serve all visitors.

The first line contains a single integer n (1 ≤ n ≤ 105), that is the number of cafe visitors. Each of the following n lines has two space-separated integers hi and mi (0 ≤ hi ≤ 23; 0 ≤ mi ≤ 59), representing the time when the i-th person comes into the cafe.  Note that the time is given in the chronological order. All time is given within one 24-hour period.

Print a single integer — the minimum number of cashes, needed to serve all clients next day.

NoteIn the first sample it is not enough one cash to serve all clients, because two visitors will come into cafe in 8:10. Therefore, if there will be one cash in cafe, then one customer will be served by it, and another one will not wait and will go away.In the second sample all visitors will come in different times, so it will be enough one cash.

let input () =
    let n = Scanf.scanf "%d " (fun n -> n) in
    let rec loop i list =
        if i < n
        then
            let str = Scanf.scanf "%s %s " (fun a b -> String.concat "" [a; b]) in
            loop (i + 1) (str :: list)
        else
            List.rev list
    in loop 0 []

let print int = Printf.printf "%d\n" int

let solve list =
    let rec loop rest prev count max =
        match rest with
        | (head :: tail) ->
            if String.compare head prev = 0
            then
                let max_upd = Pervasives.max (count + 1) max in
                loop tail head (count + 1) max_upd
            else
                loop tail head 1 max
        | [] -> max
    in loop list "start" 1 1

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

let print int = Printf.printf "%d\n" int

let same time prev =
    match time, prev with
        | (h, m), (hh, mm) ->
            if h = hh && m = mm
            then
                true
            else
                false

let () =
    let n = Scanf.scanf "%d " (fun n -> n) in
    let rec loop i prev count max  =
        if i < n
        then
            let time = Scanf.scanf "%d %d " (fun h m -> (h, m)) in
            if same time prev
            then
                let max_upd = Pervasives.max (count + 1) max in
                loop (i + 1) time (count + 1) max_upd
            else
                loop (i + 1) time 1 max
        else
            print max
    in loop 0 (99, 99) 1 1

let input () =
    let n = Scanf.scanf "%d " (fun n -> n) in
    let rec loop i list =
        if i < n
        then
            let time = Scanf.scanf "%d %d " (fun h m -> (h, m)) in
            loop (i + 1) (time :: list)
        else
            list
    in loop 0 []

let print int = Printf.printf "%d\n" int

let solve list =
    let rec loop rest prev count max =
        match rest, prev with
        | ((h, m) as head :: tail), (hh, mm) ->
            if h = hh && m = mm
            then
                let max_upd = Pervasives.max (count + 1) max in
                loop tail head (count + 1) max_upd
            else
                loop tail head 1 max
        | [], _ -> max
    in loop list (99, 99) 1 1

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

(* Codeforces 237A *)

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

let solve n =
  let rec inner n prev_time count max_count =
    if (n == 0) then max count max_count
    else
      begin
	let time = read_time() in
	if (time = prev_time)
	then inner (n-1) time (count+1) max_count
	else
	  if (count > max_count)
	  then inner (n-1) time 1 count
	  else inner (n-1) time 1 max_count
      end
  in
  inner n (-1, -1) 0 0

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

(* Codeforces 237A *)

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

let solve n =
  let rec inner n prev_time count max_count =
    if (n == 0) then max_count
    else
      begin
	let time = read_time() in
	let max_count =
	  if (max_count < count)
	  then count
	  else max_count
	in
	let count =
	  if (time = prev_time)
	  then count+1
	  else 1
	in
	inner (n-1) time count max_count
      end
  in
  inner n (-1, -1) 0 0

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