«Next please», — the princess called and cast an estimating glance at the next groom.The princess intends to choose the most worthy groom, this is, the richest one. Whenever she sees a groom who is more rich than each of the previous ones, she says a measured «Oh...». Whenever the groom is richer than all previous ones added together, she exclaims «Wow!» (no «Oh...» in this case). At the sight of the first groom the princess stays calm and says nothing.The fortune of each groom is described with an integer between 1 and 50000. You know that during the day the princess saw n grooms, said «Oh...» exactly a times and exclaimed «Wow!» exactly b times. Your task is to output a sequence of n integers t1, t2, ..., tn, where ti describes the fortune of i-th groom. If several sequences are possible, output any of them. If no sequence exists that would satisfy all the requirements, output a single number -1.

The only line of input data contains three integer numbers n, a and b (1 ≤ n ≤ 100, 0 ≤ a, b ≤ 15, n > a + b), separated with single spaces.

Output any sequence of integers t1, t2, ..., tn, where ti (1 ≤ ti ≤ 50000) is the fortune of i-th groom, that satisfies the given constraints. If no sequence exists that would satisfy all the requirements, output a single number -1.

NoteLet's have a closer look at the answer for the first sample test.   The princess said «Oh...» (highlighted in bold): 5 1 3 6 16 35 46 4 200 99.  The princess exclaimed «Wow!» (highlighted in bold): 5 1 3 6 16 35 46 4 200 99.

let rec print_list lst =
  match lst with
    | [] -> ()
    | h :: [] -> print_int h
    | h :: t -> Printf.printf "%s " (string_of_int h); print_list t
in
let make_ah_ogo a b =
  (* Sum of list *)
  let rec lsum lst acc =
    match lst with
	[] -> acc
      | h :: tail -> lsum tail (acc + h)
  in
  let rec ogo lst b =
    let cur_sum = lsum lst 0 in
    match b with
	0 -> lst
      | _ -> (*Printf.printf "Cur ogo sum:%d\n" cur_sum;*) ogo (cur_sum + 1 :: lst) (b - 1)
  in
  let rec ah lst a =
    match a with
	0 -> lst
      | _ -> match lst with
	  [] -> ah [1] (a-1)
	  | x :: [] -> ah (x :: x :: []) a
	  | h :: t -> (*Printf.printf "Incr ah %d\n" h;*) ah (h + 1 :: h :: t) (a - 1)
  in
  let logo = ogo [1] b in (* Ogo! list *)
  ah logo a (* Ogo! + Ah! list *)
in
let create inp =
  let parse_input x =
    let rx = Str.regexp "\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" in
    if Str.string_match rx x 0 then
      let i ii = int_of_string (Str.matched_group ii x) in
      Some (i 1, i 2, i 3)
    else
      None
  in
  let n, a, b = match parse_input inp with
      None -> raise Not_found
    | Some (x1, x2, x3) -> x1, x2, x3
  in
  (*Printf.printf "n=%d a=%d b=%d\n" n a b;*)
  let result = make_ah_ogo a b in
  let create_remain todo =
    let rec rem list todo =
      match todo with
	  x when x <= 0 -> list
	| _ -> rem (1 :: list) (todo - 1)
    in
    rem [] todo
  in
  let len_remain = n - (List.length result) in
  (*Printf.printf "Remain to %d length: %d; done %d\n" n len_remain (List.length result);*)
  let result = (List.rev result) @ (create_remain len_remain) in
  let len = List.length result in
  if len <= n then
    result
  else begin
    [-1]
  end
in
let init_data = read_line () in
let init_list = create init_data in
let result_list = init_list in
print_list result_list

(* codeforces 105.  Danny Sleator *)
let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
  let a = read_int() in
  let b = read_int() in

  let m = 50000 in

(* i = seen already.  current = value of last one seen.  sum=sum of all previous*)
  let rec loop i sum current a b print =
    if print then Printf.printf "%d " current;
    if current > m then false
    else if i = n then a+b=0
    else (
      if b>0 then loop (i+1) (sum+(2*current)) (2*current) a (b-1) print
      else if a>0 then (
	if (current+1 > sum) then loop (i+1) (sum+current) current a b print else loop (i+1) (sum+current+1) (current+1) (a-1) b print
      ) else loop (i+1) (sum+current) current a b print
    )
  in
    if not (loop 1 1 1 a b false) then Printf.printf "-1"
    else ignore (loop 1 1 1 a b true);
    print_newline()

let rec print_list lst =
  match lst with
    | [] -> ()
    | h :: [] -> print_int h
    | h :: t -> Printf.printf "%s " (string_of_int h); print_list t
in
let make_ah_ogo a b =
  (* Sum of list *)
  let rec lsum lst acc =
    match lst with
	[] -> acc
      | h :: tail -> lsum tail (acc + h)
  in
  let rec ogo lst b =
    let cur_sum = lsum lst 0 in
    match b with
	0 -> lst
      | _ -> (*Printf.printf "Cur ogo sum:%d\n" cur_sum;*) ogo (cur_sum + 1 :: lst) (b - 1)
  in
  let rec ah lst a =
    match a with
	0 -> lst
      | _ -> match lst with
	  [] -> ah [1] (a-1)
	  | h :: t -> (*Printf.printf "Incr ah %d\n" h;*) ah (h + 1 :: h :: t) (a - 1)
  in
  let logo = ogo [1] b in (* Ogo! list *)
  ah logo a (* Ogo! + Ah! list *)
in
let create inp =
  let parse_input x =
    let rx = Str.regexp "\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" in
    if Str.string_match rx x 0 then
      let i ii = int_of_string (Str.matched_group ii x) in
      Some (i 1, i 2, i 3)
    else
      None
  in
  let n, a, b = match parse_input inp with
      None -> raise Not_found
    | Some (x1, x2, x3) -> x1, x2, x3
  in
  (*Printf.printf "n=%d a=%d b=%d\n" n a b;*)
  let result = make_ah_ogo a b in
  let create_remain todo =
    let rec rem list todo =
      match todo with
	  x when x <= 0 -> list
	| _ -> rem (1 :: list) (todo - 1)
    in
    rem [] todo
  in
  let len_remain = n - (List.length result) in
  (*Printf.printf "Remain to %d length: %d; done %d\n" n len_remain (List.length result);*)
  let result = (List.rev result) @ (create_remain len_remain) in
  let len = List.length result in
  if len <= n then
    result
  else begin
    [-1]
  end
in
let init_data = read_line () in
let init_list = create init_data in
let result_list = init_list in
print_list result_list

let rec print_list lst =
  match lst with
    | [] -> ()
    | h :: [] -> print_int h
    | h :: t -> Printf.printf "%s " (string_of_int h); print_list t
in
let make_ah_ogo a b =
  (* Sum of list *)
  let rec lsum lst acc =
    match lst with
	[] -> acc
      | h :: tail -> lsum tail (acc + h)
  in
  let rec ogo lst b =
    let cur_sum = lsum lst 0 in
    match b with
	0 -> lst
      | _ -> (*Printf.printf "Cur ogo sum:%d\n" cur_sum;*) ogo (cur_sum + 1 :: lst) (b - 1)
  in
  let rec ah lst a =
    match a with
	0 -> lst
      | _ -> match lst with
	  [] -> ah [2] (a - 1)
	  | 1 :: [] -> ah (1 :: 1 :: []) (a - 1)
	  | h :: t -> (*Printf.printf "Incr ah %d\n" h;*) ah (h + 1 :: h :: t) (a - 1)
  in
  let logo = ogo [] b in (* Ogo! list *)
  ah logo a (* Ogo! + Ah! list *)
in
let create inp =
  let parse_input x =
    let rx = Str.regexp "\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" in
    if Str.string_match rx x 0 then
      let i ii = int_of_string (Str.matched_group ii x) in
      Some (i 1, i 2, i 3)
    else
      None
  in
  let n, a, b = match parse_input inp with
      None -> raise Not_found
    | Some (x1, x2, x3) -> x1, x2, x3
  in
  (*Printf.printf "n=%d a=%d b=%d\n" n a b;*)
  let result = make_ah_ogo a b in
  let create_remain todo =
    let rec rem list todo =
      match todo with
	  x when x <= 0 -> list
	| _ -> rem (1 :: list) (todo - 1)
    in
    rem [] todo
  in
  let len_remain = n - (List.length result) in
  (*Printf.printf "Remain to %d length: %d; done %d\n" n len_remain (List.length result);*)
  let result = (List.rev result) @ (create_remain len_remain) in
  let len = List.length result in
  if len <= n then
    result
  else begin
    [-1]
  end
in
let init_data = read_line () in
let init_list = create init_data in
let result_list = init_list in
print_list result_list

let rec print_list lst =
  match lst with
    | [] -> ()
    | h :: [] -> print_int h
    | h :: t -> Printf.printf "%s " (string_of_int h); print_list t
in
let make_ah_ogo a b =
  (* Sum of list *)
  let rec lsum lst acc =
    match lst with
	[] -> acc
      | h :: tail -> lsum tail (acc + h)
  in
  let rec ogo lst b =
    let cur_sum = lsum lst 0 in
    match b with
	0 -> lst
      | _ -> (*Printf.printf "Cur ogo sum:%d\n" cur_sum;*)
	if lst = [] then
	  ogo [1] b
	else
	  ogo (cur_sum + 1 :: lst) (b - 1)
  in
  let rec ah lst a =
    match a with
	0 -> lst
      | _ -> match lst with
	  [] -> ah [2] (a - 1)
	  | 1 :: [] -> ah (1 :: 1 :: []) (a - 1)
	  | h :: t -> (*Printf.printf "Incr ah %d\n" h;*) ah (h + 1 :: h :: t) (a - 1)
  in
  let logo = ogo [] b in (* Ogo! list *)
  ah logo a (* Ogo! + Ah! list *)
in
let create inp =
  let parse_input x =
    let rx = Str.regexp "\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" in
    if Str.string_match rx x 0 then
      let i ii = int_of_string (Str.matched_group ii x) in
      Some (i 1, i 2, i 3)
    else
      None
  in
  let n, a, b = match parse_input inp with
      None -> raise Not_found
    | Some (x1, x2, x3) -> x1, x2, x3
  in
  (*Printf.printf "n=%d a=%d b=%d\n" n a b;*)
  let result = make_ah_ogo a b in
  let create_remain todo =
    let rec rem list todo =
      match todo with
	  x when x <= 0 -> list
	| _ -> rem (1 :: list) (todo - 1)
    in
    rem [] todo
  in
  let len_remain = n - (List.length result) in
  (*Printf.printf "Remain to %d length: %d; done %d\n" n len_remain (List.length result);*)
  let result = (List.rev result) @ (create_remain len_remain) in
  let len = List.length result in
  if len <= n then
    result
  else begin
    [-1]
  end
in
let init_data = read_line () in
let init_list = create init_data in
let result_list = init_list in
print_list result_list

let rec print_list lst =
  match lst with
    | [] -> ()
    | h :: [] -> print_int h
    | h :: t -> Printf.printf "%s " (string_of_int h); print_list t
in
let make_ah_ogo a b =
  (* Sum of list *)
  let rec lsum lst acc =
    match lst with
	[] -> acc
      | h :: tail -> lsum tail (acc + h)
  in
  let rec ogo lst b =
    let cur_sum = lsum lst 0 in
    match b with
	0 -> lst
      | _ -> (*Printf.printf "Cur ogo sum:%d\n" cur_sum;*)
	if lst = [] then
	  ogo [1] b
	else
	  ogo (cur_sum + 1 :: lst) (b - 1)
  in
  let rec ah lst a =
    match a with
	0 -> lst
      | _ -> match lst with
	  [] -> ah [2] a
	  | 1 :: [] -> ah (1 :: 1 :: []) (a - 1)
	  | h :: t -> (*Printf.printf "Incr ah %d\n" h;*) ah (h + 1 :: h :: t) (a - 1)
  in
  let logo = ogo [] b in (* Ogo! list *)
  ah logo a (* Ogo! + Ah! list *)
in
let create inp =
  let parse_input x =
    let rx = Str.regexp "\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" in
    if Str.string_match rx x 0 then
      let i ii = int_of_string (Str.matched_group ii x) in
      Some (i 1, i 2, i 3)
    else
      None
  in
  let n, a, b = match parse_input inp with
      None -> raise Not_found
    | Some (x1, x2, x3) -> x1, x2, x3
  in
  (*Printf.printf "n=%d a=%d b=%d\n" n a b;*)
  let result = make_ah_ogo a b in
  let create_remain todo =
    let rec rem list todo =
      match todo with
	  x when x <= 0 -> list
	| _ -> rem (1 :: list) (todo - 1)
    in
    rem [] todo
  in
  let len_remain = n - (List.length result) in
  (*Printf.printf "Remain to %d length: %d; done %d\n" n len_remain (List.length result);*)
  let result = (List.rev result) @ (create_remain len_remain) in
  let len = List.length result in
  if len <= n then
    result
  else begin
    [-1]
  end
in
let init_data = read_line () in
let init_list = create init_data in
let result_list = init_list in
print_list result_list

let rec print_list lst =
  match lst with
    | [] -> ()
    | h :: [] -> print_int h
    | h :: t -> Printf.printf "%s " (string_of_int h); print_list t
in
let make_ah_ogo a b =
  (* Sum of list *)
  let rec lsum lst acc =
    match lst with
	[] -> acc
      | h :: tail -> lsum tail (acc + h)
  in
  let rec ogo lst b =
    let cur_sum = lsum lst 0 in
    match b with
	0 -> lst
      | _ -> (*Printf.printf "Cur ogo sum:%d\n" cur_sum;*) ogo (cur_sum + 1 :: lst) (b - 1)
  in
  let rec ah lst a =
    match a with
	0 -> lst
      | _ -> match lst with
	  [] -> ah [1] (a - 1)
	  | 1 :: [] -> ah (1 :: 1 :: []) (a - 1)
	  | h :: t -> (*Printf.printf "Incr ah %d\n" h;*) ah (h + 1 :: h :: t) (a - 1)
  in
  let logo = ogo [1] b in (* Ogo! list *)
  ah logo a (* Ogo! + Ah! list *)
in
let create inp =
  let parse_input x =
    let rx = Str.regexp "\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" in
    if Str.string_match rx x 0 then
      let i ii = int_of_string (Str.matched_group ii x) in
      Some (i 1, i 2, i 3)
    else
      None
  in
  let n, a, b = match parse_input inp with
      None -> raise Not_found
    | Some (x1, x2, x3) -> x1, x2, x3
  in
  (*Printf.printf "n=%d a=%d b=%d\n" n a b;*)
  let result = make_ah_ogo a b in
  let create_remain todo =
    let rec rem list todo =
      match todo with
	  x when x <= 0 -> list
	| _ -> rem (1 :: list) (todo - 1)
    in
    rem [] todo
  in
  let len_remain = n - (List.length result) in
  (*Printf.printf "Remain to %d length: %d; done %d\n" n len_remain (List.length result);*)
  let result = (List.rev result) @ (create_remain len_remain) in
  let len = List.length result in
  if len <= n then
    result
  else begin
    [-1]
  end
in
let init_data = read_line () in
let init_list = create init_data in
let result_list = init_list in
print_list result_list

let rec print_list lst =
  match lst with
    | [] -> ()
    | h :: [] -> print_int h
    | h :: t -> Printf.printf "%s " (string_of_int h); print_list t
in
let make_ah_ogo a b =
  (* Sum of list *)
  let rec lsum lst acc =
    match lst with
	[] -> acc
      | h :: tail -> lsum tail (acc + h)
  in
  let rec ogo lst b =
    let cur_sum = lsum lst 0 in
    if lst = [] then
      ogo [1] b
    else
      match b with
	  0 -> lst
	| _ -> (*Printf.printf "Cur ogo sum:%d\n" cur_sum;*) ogo (cur_sum + 1 :: lst) (b - 1)
  in
  let rec ah lst a =
    match a with
	0 -> lst
      | _ -> match lst with
	  [] -> ah [2] (a - 1)
	  | 1 :: [] -> ah (1 :: 1 :: []) (a - 1)
	  | h :: t -> (*Printf.printf "Incr ah %d\n" h;*) ah (h + 1 :: h :: t) (a - 1)
  in
  let logo = ogo [] b in (* Ogo! list *)
  ah logo a (* Ogo! + Ah! list *)
in
let create inp =
  let parse_input x =
    let rx = Str.regexp "\\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)" in
    if Str.string_match rx x 0 then
      let i ii = int_of_string (Str.matched_group ii x) in
      Some (i 1, i 2, i 3)
    else
      None
  in
  let n, a, b = match parse_input inp with
      None -> raise Not_found
    | Some (x1, x2, x3) -> x1, x2, x3
  in
  (*Printf.printf "n=%d a=%d b=%d\n" n a b;*)
  let result = make_ah_ogo a b in
  let create_remain todo =
    let rec rem list todo =
      match todo with
	  x when x <= 0 -> list
	| _ -> rem (1 :: list) (todo - 1)
    in
    rem [] todo
  in
  let len_remain = n - (List.length result) in
  (*Printf.printf "Remain to %d length: %d; done %d\n" n len_remain (List.length result);*)
  let result = (List.rev result) @ (create_remain len_remain) in
  let len = List.length result in
  if len <= n then
    result
  else begin
    [-1]
  end
in
let init_data = read_line () in
let init_list = create init_data in
let result_list = init_list in
print_list result_list

(* codeforces 105.  Danny Sleator *)
let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
  let a = read_int() in
  let b = read_int() in

  let m = 50000 in

(* i = seen already.  current = value of last one seen *)
  let rec loop i current a b print =
    if print then Printf.printf "%d " current;
    if current > m then false
    else if i = n then true
    else (
      if b>0 then loop (i+1) (2*current) a (b-1) print
      else if a>0 then loop (i+1) (current+1) (a-1) b print
      else loop (i+1) current a b print
    )
  in
    if loop 1 1 a b false
    then ignore (loop 1 1 a b true)
    else Printf.printf "-1";
    print_newline()

(* codeforces 105.  Danny Sleator *)
let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
  let a = read_int() in
  let b = read_int() in

  let m = 50000 in

(* i = seen already.  current = value of last one seen *)
  let rec loop i current a b print =
    if print then Printf.printf "%d " current;
    if current > m then false
    else if i = n then true
    else (
      if b>0 then loop (i+1) (2*current) a (b-1) print
      else if a>0 then loop (i+1) (current+1) (a-1) b print
      else loop (i+1) current a b print
    )
  in
    if a=n-1 || (not (loop 1 1 a b false)) then Printf.printf "-1"
    else ignore (loop 1 1 a b true);
    print_newline()

(* codeforces 105.  Danny Sleator *)
let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
  let a = read_int() in
  let b = read_int() in

  let m = 50000 in

(* i = seen already.  current = value of last one seen *)
  let rec loop i current a b print =
    if print then Printf.printf "%d " current;
    if current > m then false
    else if i = n then true
    else (
      if a>0 then loop (i+1) (2*current) (a-1) b print
      else if b>0 then loop (i+1) (current+1) a (b-1) print
      else loop (i+1) current a b print
    )
  in
    if loop 1 1 a b false
    then ignore (loop 1 1 a b true)
    else Printf.printf "-1";
    print_newline()

(* codeforces 105.  Danny Sleator *)
let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
  let a = read_int() in
  let b = read_int() in

  let m = 50000 in

(* i = seen already.  current = value of last one seen *)
  let rec loop i current a b print =
    if print then Printf.printf "%d " current;
    if current > m then false
    else if i = n then true
    else (
      if b>0 then loop (i+1) (2*current) a (b-1) print
      else if a>0 then loop (i+1) (current+1) (a-1) b print
      else loop (i+1) current a b print
    )
  in
    if (a=n-1 && n>1) || (not (loop 1 1 a b false)) then Printf.printf "-1"
    else ignore (loop 1 1 a b true);
    print_newline()


Haha, try to solve this, SelectorUnlimited!— antontrygubO_oYour friends Alice and Bob practice fortune telling.Fortune telling is performed as follows. There is a well-known array $$$a$$$ of $$$n$$$ non-negative integers indexed from $$$1$$$ to $$$n$$$. The tellee starts with some non-negative number $$$d$$$ and performs one of the two operations for each $$$i = 1, 2, \ldots, n$$$, in the increasing order of $$$i$$$. The possible operations are: replace their current number $$$d$$$ with $$$d + a_i$$$ replace their current number $$$d$$$ with $$$d \oplus a_i$$$ (hereinafter $$$\oplus$$$ denotes the bitwise XOR operation)Notice that the chosen operation may be different for different $$$i$$$ and for different tellees.One time, Alice decided to start with $$$d = x$$$ and Bob started with $$$d = x + 3$$$. Each of them performed fortune telling and got a particular number in the end. Notice that the friends chose operations independently of each other, that is, they could apply different operations for the same $$$i$$$.You learnt that either Alice or Bob ended up with number $$$y$$$ in the end, but you don't know whose of the two it was. Given the numbers Alice and Bob started with and $$$y$$$, find out who (Alice or Bob) could get the number $$$y$$$ after performing the operations. It is guaranteed that on the jury tests, exactly one of your friends could have actually gotten that number.HacksYou cannot make hacks in this problem.

On the first line of the input, you are given one number $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The following $$$2 \cdot t$$$ lines contain test cases. The first line of each test case contains three numbers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le x \le 10^9$$$, $$$0 \le y \le 10^{15}$$$) — the length of array $$$a$$$, Alice's initial number (Bob's initial number is therefore $$$x+3$$$), and the number that one of the two friends got in the end. The second line of each test case contains $$$n$$$ numbers — the array $$$a$$$ ($$$0 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print the name of the friend who could get the number $$$y$$$: "Alice" or "Bob".

NoteIn the first test case, Alice could get $$$9$$$ using the following operations: $$$7 + 2 = 9$$$.In the second test case, Alice could get $$$2$$$ using this operations: $$$(0 + 1) \oplus 3 = 2$$$.In the third test case, Bob started with $$$x+3 = 0+3=3$$$ and could get $$$1$$$ this way: $$$(((3 + 1) + 2) \oplus 3) \oplus 4 = 1$$$.

Scanf.scanf "%d" (fun t ->
    let and64 = Int64.logand in
    for i = 1 to t do
        Scanf.scanf " %d %Ld %Ld" (fun n x y ->
            let x = Int64.to_int (and64 x 1L) in
            let y = Int64.to_int (and64 y 1L) in
            let a = Array.init n (fun _ -> Scanf.scanf " %Ld" (fun a -> Int64.to_int (and64 a 1L))) in
            let rec loop i acc =
                if i = n then acc else loop (i + 1) (acc lxor a.(i))
            in
            print_endline @@ if loop 0 x = y then "Alice" else "Bob"
        )
    done
)


You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \le i \le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation:  Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \le i \le n - 1$$$ and $$$1 \le x \le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$

For each test case, print the minimum number of operations needed.

NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$.

let parse len str = let rep = Array.make len (-1) in
  let temp = ref "" in
  let j = ref 0 in
  for i = 0 to String.length str -1 do
    if str.[i] = ' ' then
      (rep.(!j) <- (int_of_string !temp) ; incr j ; temp:=""  )
    else
      (temp:= !temp ^ (String.make 1 str.[i]))
  done; rep.(!j) <- (int_of_string !temp) ;rep
;;

let n = int_of_string (input_line stdin) in
for j = 0 to n-1 do
  let len = int_of_string (input_line stdin) in
  let str = (input_line stdin) in
  let tab = parse len str in
  let prem = ref (-1) in
  let dern = ref (-1) in
  for i = 0 to len -2 do
    if tab.(i) = tab.(i+1) then begin
      dern := i ;
      if !prem = -1 then prem := i ;
    end
  done;
  let dist = ref ( !dern - !prem) in
  if !dist >= 2 then decr dist ;
  print_endline (string_of_int !dist)
done;;


You are given a sequence $$$s$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$.You have to divide it into at least two segments (segment — is a consecutive sequence of elements) (in other words, you have to place separators between some digits of the sequence) in such a way that each element belongs to exactly one segment and if the resulting division will be represented as an integer numbers sequence then each next element of this sequence will be strictly greater than the previous one.More formally: if the resulting division of the sequence is $$$t_1, t_2, \dots, t_k$$$, where $$$k$$$ is the number of element in a division, then for each $$$i$$$ from $$$1$$$ to $$$k-1$$$ the condition $$$t_{i} < t_{i + 1}$$$ (using numerical comparing, it means that the integer representations of strings are compared) should be satisfied.For example, if $$$s=654$$$ then you can divide it into parts $$$[6, 54]$$$ and it will be suitable division. But if you will divide it into parts $$$[65, 4]$$$ then it will be bad division because $$$65 > 4$$$. If $$$s=123$$$ then you can divide it into parts $$$[1, 23]$$$, $$$[1, 2, 3]$$$ but not into parts $$$[12, 3]$$$.Your task is to find any suitable division for each of the $$$q$$$ independent queries.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 300$$$) — the number of queries. The first line of the $$$i$$$-th query contains one integer number $$$n_i$$$ ($$$2 \le n_i \le 300$$$) — the number of digits in the $$$i$$$-th query. The second line of the $$$i$$$-th query contains one string $$$s_i$$$ of length $$$n_i$$$ consisting only of digits from $$$1$$$ to $$$9$$$.

If the sequence of digits in the $$$i$$$-th query cannot be divided into at least two parts in a way described in the problem statement, print the single line "NO" for this query. Otherwise in the first line of the answer to this query print "YES", on the second line print $$$k_i$$$ — the number of parts in your division of the $$$i$$$-th query sequence and in the third line print $$$k_i$$$ strings $$$t_{i, 1}, t_{i, 2}, \dots, t_{i, k_i}$$$ — your division. Parts should be printed in order of the initial string digits. It means that if you write the parts one after another without changing their order then you'll get the string $$$s_i$$$. See examples for better understanding.

Scanf.(Array.(scanf " %d" @@ fun q ->
  init q (fun _ -> scanf " %d %s" @@ fun _ s ->
    Printf.(
      if String.length s = 2 && s.[0] >= s.[1] then printf "NO\n"
      else printf "YES\n2\n%c %s\n" s.[0] @@ String.sub s 1 (String.length s - 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_lim,min_i64_lim = Int64.(max_int,min_int)
  let max_i64,min_i64 = 2000000000000000000L,-2000000000000000000L
  (* 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 q = gi 0 in
  rep 1L q (fun _ ->
    let n = gi 0 in
    let s = scanf " %s" @@ id in
    if String.length s = 2 && s.[0] >= s.[1] then (
      printf "NO\n"
    ) else (
      printf "YES\n2\n%c %s\n" s.[0] @@ String.sub s 1 (String.length s-$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_lim,min_i64_lim = Int64.(max_int,min_int)
  let max_i64,min_i64 = 2000000000000000000L,-2000000000000000000L
  (* 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 q = gi 0 in
  rep 1L q (fun _ ->
    let n = gi 0 in
    let s = scanf " %s" @@ id in
    let f = ref true in
    String.iter (fun c -> if c <> s.[0] then f := false) s;
    if !f && String.length s = 2 then (
      printf "NO\n"
    ) else (
      printf "YES\n2\n%c %s\n" s.[0] @@ String.sub s 1 (String.length s-$1)
    )
    (* let prev = ref 0L in
    let a = ref 0L in
    let ex = make 10 false in
    String.iter (fun c ->
      let p = !a * 10L + i64 (int_of_char c -$ int_of_char '0') in
      if ex.(int_of_char c) then (
        if !prev > p then (

        )
      ) else
      if !prev > p then (

      )
    ) *)
  )


You are given a string $$$s$$$, consisting only of characters '0' or '1'. Let $$$|s|$$$ be the length of $$$s$$$.You are asked to choose some integer $$$k$$$ ($$$k > 0$$$) and find a sequence $$$a$$$ of length $$$k$$$ such that:   $$$1 \le a_1 < a_2 < \dots < a_k \le |s|$$$;  $$$a_{i-1} + 1 < a_i$$$ for all $$$i$$$ from $$$2$$$ to $$$k$$$. The characters at positions $$$a_1, a_2, \dots, a_k$$$ are removed, the remaining characters are concatenated without changing the order. So, in other words, the positions in the sequence $$$a$$$ should not be adjacent.Let the resulting string be $$$s'$$$. $$$s'$$$ is called sorted if for all $$$i$$$ from $$$2$$$ to $$$|s'|$$$ $$$s'_{i-1} \le s'_i$$$.Does there exist such a sequence $$$a$$$ that the resulting string $$$s'$$$ is sorted?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The only line of each testcase contains a string $$$s$$$ ($$$2 \le |s| \le 100$$$). Each character is either '0' or '1'.

For each testcase print "YES" if there exists a sequence $$$a$$$ such that removing the characters at positions $$$a_1, a_2, \dots, a_k$$$ and concatenating the parts without changing the order produces a sorted string. Otherwise, print "NO". You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

NoteIn the first testcase you can choose a sequence $$$a=[1,3,6,9]$$$. Removing the underlined letters from "10101011011" will produce a string "0011111", which is sorted.In the second and the third testcases the sequences are already sorted.In the fourth testcase you can choose a sequence $$$a=[3]$$$. $$$s'=$$$ "11", which is sorted.In the fifth testcase there is no way to choose a sequence $$$a$$$ such that $$$s'$$$ is sorted.

let n = read_int () ;;

let translate_to str=
let len = String.length(str)  in
let rec sup_fun i l=
if i = -1 then l else (sup_fun (i-1) ((int_of_char(str.[i])-int_of_char('0')) :: l)) in
sup_fun (len-1) [];;

let rec read_fucking_sheet n =
if n = 0 then []
else (translate_to (read_line ()) :: read_fucking_sheet (n-1))
;;

let rec print_yes t =
if t = true then print_string "YES"
else print_string "NO"
;;

let rec solve l flag =
match l with
hd :: hd1 :: tl ->
	if flag = 1 && hd = 1
		then (solve (hd1 :: tl) 1)
	else if flag = 1 && hd = 0 && hd1 = 0
		then false
	else if flag = 1 && hd = 0 && hd1 = 1
		then (solve tl 1)
	else if flag = 0 && hd = 0
		then (solve (hd1 :: tl) 0)
	else if flag = 0 && hd = 1 && hd1 = 0
		then (solve tl 1) || (solve tl 0 )
	else if flag = 0 && hd = 1 && hd1 = 1
		then (solve tl 1)
        else false
|_-> true
;;

let rec print_list l =
match l with
hd:: tl -> print_int hd ; print_list tl
|_-> print_string "  " ;;

let rec flum l =
match l with
hd :: tl -> (*print_list hd ;*) print_yes (solve hd 0) ; print_string "\n" ; flum tl
|_-> () ;;

(*print_list (translate_to "111000") ;;*)

flum (List.rev(read_fucking_sheet n))

(*print_yes (solve (translate_to l ((string.length (l)) -1) ) 0) ;;*)

(*let spisok = [1;1;0;0] ;;

print_yes (solve spisok 0) ;;*)

let n = read_int ()

let rec find a b i m = if i>m-2 then -1 else if String.sub a i 2 = b then i else find a b (i+1) m ;;

for i=1 to n do
    let s = read_line () in
    let z = String.length s in
    let t = find s "11" 0 z in
    if t = -1 then print_string "YES\n" else
    if find (String.sub s t (z-t)) "00" 0 (z-t) = -1 then print_string "YES\n" else print_string "NO\n"
done

let n = read_int () ;;

let translate_to str=
let len = String.length(str)  in
let rec sup_fun i l=
if i = -1 then l else (sup_fun (i-1) ((int_of_char(str.[i])-int_of_char('0')) :: l)) in
sup_fun (len-1) [];;

let rec read_fucking_sheet n =
if n = 0 then []
else (translate_to (read_line ()) :: read_fucking_sheet (n-1))
;;

let rec print_yes t =
if t = true then print_string "YES"
else print_string "NO"
;;

let rec solve l flag =
match l with
hd :: hd1 :: tl ->
	if flag = 1 && hd = 1
		then (solve (hd1 :: tl) 1)
	else if flag = 1 && hd = 0 && hd1 = 0
		then false
	else if flag = 1 && hd = 0 && hd1 = 1
		then (solve tl 1)
	else if flag = 0 && hd = 0
		then (solve (hd1 :: tl) 0)
	else if flag = 0 && hd = 1 && hd1 = 0
		then (solve tl 1) || (solve tl 0 )
	else if flag = 0 && hd = 1 && hd1 = 1
		then (solve tl 1)
        else false
|_-> true
;;

let rec print_list l =
match l with
hd:: tl -> print_int hd ; print_list tl
|_-> print_string "  " ;;

let rec flum l =
match l with
hd :: tl -> (*print_list hd ;*) print_yes (solve hd 0) ; print_string "\n" ; flum tl
|_-> () ;;

(*print_list (translate_to "111000") ;;*)

let rec revers l =
match l with
hd::tl -> hd :: (revers tl)
|_-> []
;;

flum (revers(read_fucking_sheet n))

(*print_yes (solve (translate_to l ((string.length (l)) -1) ) 0) ;;*)

(*let spisok = [1;1;0;0] ;;

print_yes (solve spisok 0) ;;*)

let n = read_int () ;;

let translate_to str=
let len = String.length(str)  in
let rec sup_fun i l=
if i = -1 then l else (sup_fun (i-1) ((int_of_char(str.[i])-int_of_char('0')) :: l)) in
sup_fun (len-1) [];;

let rec read_fucking_sheet n =
if n = 0 then []
else (translate_to (read_line ()) :: read_fucking_sheet (n-1))
;;

let rec print_yes t =
if t = true then print_string "YES"
else print_string "NO"
;;

let rec solve l flag =
match l with
hd :: hd1 :: tl ->
	if flag = 1 && hd = 1
		then (solve (hd1 :: tl) 1)
	else if flag = 1 && hd = 0 && hd1 = 0
		then false
	else if flag = 1 && hd = 0 && hd1 = 1
		then (solve tl 1)
	else if flag = 0 && hd = 0
		then (solve (hd1 :: tl) 0)
	else if flag = 0 && hd = 1 && hd1 = 0
		then (solve tl 1) || (solve tl 0 )
	else if flag = 0 && hd = 1 && hd1 = 1
		then (solve tl 1)
        else false
|_-> true
;;

let rec print_list l =
match l with
hd:: tl -> print_int hd ; print_list tl
|_-> print_string "  " ;;

let rec flum l =
match l with
hd :: tl -> (*print_list hd ;*) print_yes (solve hd 0) ; print_string "\n" ; flum tl
|_-> () ;;

(*print_list (translate_to "111000") ;;*)

flum (read_fucking_sheet n)

(*print_yes (solve (translate_to l ((string.length (l)) -1) ) 0) ;;*)

(*let spisok = [1;1;0;0] ;;

print_yes (solve spisok 0) ;;*)

let n = read_int () ;;

let translate_to str=
let len = String.length(str)  in
let rec sup_fun i l=
if i = len then [] else (sup_fun (i+1) (int_of_char(str.[i]) :: l)) in
sup_fun 0 [];;

let rec read_fucking_sheet n =
if n = 0 then []
else (translate_to (read_line ()))

let rec print_yes t =
if t = true then print_string "YES"
else print_string "NO"
;;

let rec solve l flag =
match l with
hd :: hd1 :: tl ->
	if flag = 1 && hd = 1
		then (solve (hd1 :: tl) flag)
	else if flag = 1 && hd = 0 && hd1 = 0
		then false
	else if flag = 1 && hd = 0 && hd1 = 1
		then (solve tl flag)
	else if flag = 0 && hd = 0
		then (solve (hd1 :: tl) flag)
	else if flag = 0 && hd = 1 && hd1 = 0
		then (solve tl 1) || (solve tl flag )
	else if flag = 0 && hd = 1 && hd1 = 1
		then (solve tl 1)
        else false
|_-> true
;;

let rec flum l =
match l with
hd :: tl -> print_yes (solve hd 0) ; flum tl
|_-> () ;;

flum (read_fucking_sheet n)

(*print_yes (solve (translate_to l ((string.length (l)) -1) ) 0) ;;*)

(*let spisok = [1;0;0;1;1;1;0;0;1] ;;

print_yes (solve spisok 0) ;;*)


Find out if it is possible to partition the first $$$n$$$ positive integers into two non-empty disjoint sets $$$S_1$$$ and $$$S_2$$$ such that:$$$\mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) > 1$$$ Here $$$\mathrm{sum}(S)$$$ denotes the sum of all elements present in set $$$S$$$ and $$$\mathrm{gcd}$$$ means thegreatest common divisor.Every integer number from $$$1$$$ to $$$n$$$ should be present in exactly one of $$$S_1$$$ or $$$S_2$$$.

The only line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 45\,000$$$)

If such partition doesn't exist, print "No" (quotes for clarity). Otherwise, print "Yes" (quotes for clarity), followed by two lines, describing $$$S_1$$$ and $$$S_2$$$ respectively. Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty. If there are multiple possible partitions — print any of them.

NoteIn the first example, there is no way to partition a single number into two non-empty sets, hence the answer is "No".In the second example, the sums of the sets are $$$2$$$ and $$$4$$$ respectively. The $$$\mathrm{gcd}(2, 4) = 2 > 1$$$, hence that is one of the possible answers.

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

let sum a = Array.fold_left (+) 0L a

let () =
	let n = get_i64 0 in
	let a = Array.init (~|n) (fun v -> 1L + (~~|v))
	in
	if n<=2L then (
		printf "No\n"
	)
	else (
		let p = (n-1L) / 2L
		in
		(* in let u,v = sum a - (a@p), a@p *)
		(* in let q = gcd u v in assert (q <> 1L); *)
		(* printf "%Ld, %Ld --> %Ld\n" u v q *)
		printf "Yes\n1 %Ld\n%Ld " (a@p) (n-1L);
		(
			let v = (~| (a@p)) in
			for i=1 to (~|n) do
				if i <> v then printf "%d " i
			done
		)
			(* (a |> Array.to_list |> string_of_list ~separator:" " Int64.to_string) *)
			(* (Array.init (~|n -$ 1) (fun v -> if v < (~|p) then (1+$v) else (2+$v))
			|> Array.to_list |> string_of_list ~separator:" " (string_of_int)) *)
	)

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

let sum a = Array.fold_left (+) 0L a

let () =
	let n = get_i64 0 in
	let a = Array.init (~|n) (fun v -> 1L + (~~|v))
	in
	if n<=2L then (
		printf "NO\n"
	)
	else (
		let p = (n-1L) / 2L
		in let u,v = sum a - (a@p), a@p
		in let q = gcd u v in assert (q <> 1L);
		(* printf "%Ld, %Ld --> %Ld\n" u v q *)
		printf "YES\n1 %Ld\n%Ld %s\n" (a@p) (n-1L)
			(* (a |> Array.to_list |> string_of_list ~separator:" " Int64.to_string) *)
			(Array.init (~|n -$ 1) (fun v -> if v < (~|p) then (1+$v) else (2+$v))
			|> Array.to_list |> string_of_list ~separator:" " (string_of_int))
	)


Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1, a2, ..., an of positive integers:  Initially, x = 1 and y = 0. If, after any step, x ≤ 0 or x > n, the program immediately terminates.  The program increases both x and y by a value equal to ax simultaneously.  The program now increases y by ax while decreasing x by ax.  The program executes steps 2 and 3 (first step 2, then step 3) repeatedly until it terminates (it may never terminate). So, the sequence of executed steps may start with: step 2, step 3, step 2, step 3, step 2 and so on. The cows are not very good at arithmetic though, and they want to see how the program works. Please help them!You are given the sequence a2, a3, ..., an. Suppose for each i (1 ≤ i ≤ n - 1) we run the program on the sequence i, a2, a3, ..., an. For each such run output the final value of y if the program terminates or -1 if it does not terminate.

The first line contains a single integer, n (2 ≤ n ≤ 2·105). The next line contains n - 1 space separated integers, a2, a3, ..., an (1 ≤ ai ≤ 109).

Output n - 1 lines. On the i-th line, print the requested value when the program is run on the sequence i, a2, a3, ...an. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

NoteIn the first sample   For i = 1,  x becomes  and y becomes 1 + 2 = 3.  For i = 2,  x becomes  and y becomes 2 + 4 = 6.  For i = 3,  x becomes  and y becomes 3 + 1 + 4 = 8.

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

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

let () =
  let n = read_int 0 in
  let a = Array.init (n+1) (fun i -> if i<=1 then 0 else read_int 0) in
  let outcome = Array.make_matrix (n+1) 2 0 in

  let next_state (x,s) = (* s=0 means the initial state of the process, before "step 2" *)
    if s=0 then (x+a.(x), (1-s)) else (x-a.(x), (1-s))
  in

  let rec fill_table (x,s) =
    if x<1 || x>n then 4
    else if x = 1 then 2+s (* 2 or 3 *)
    else if outcome.(x).(s) = 0 then (
      outcome.(x).(s) <- 5;
      outcome.(x).(s) <- fill_table (next_state (x,s));
      outcome.(x).(s)
    )
    else if outcome.(x).(s) = 5 then (
      outcome.(x).(s) <- 1;
      1
    )
    else outcome.(x).(s)
  in

  let () = for i=2 to n do
    ignore (fill_table (i,0));
    ignore (fill_table (i,1))
  done in

  let cycles i =
    let a = outcome.(i+1).(1) in
    let b = outcome.(i+1).(0) in
      a = 1 || a = 2 || (a = 3 && (b = 1 || b = 2 || b = 3))
  in

  let yval = Array.make_matrix (n+1) 2 (-1L) in

  let rec fill_in_y i (x,s) =
    if x<1 || x>n then 0L
    else if (x,s) = (1,1) then Int64.of_int i
    else if yval.(x).(s) >= 0L then yval.(x).(s)
    else (
      yval.(x).(s) <- (Int64.of_int a.(x)) ++ (fill_in_y i (next_state (x,s)));
      yval.(x).(s)
    )
  in

    for i=1 to n-1 do
      if cycles i then print_string ("-1\n")
      else Printf.printf "%Ld\n" ((Int64.of_int i)++(fill_in_y i (i+1,1)))
    done

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

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

let () =
  let n = read_int 0 in
  let a = Array.init (n+1) (fun i -> if i<=1 then 0 else read_int 0) in
  let outcome = Array.make_matrix (n+1) 2 0 in
    (* this table has the following meaning:
       0 unknown
       1 cyclic
       2 reaches (1,0)
       3 reaches (1,1)
       4 termates
       5 current search *)

  let next_state (x,s) = (* s=0 means the initial state of the process, before "step 2" *)
    if s=0 then (x+a.(x), (1-s)) else (x-a.(x), (1-s))
  in

  let rec fill_table (x,s) =
    if x<1 || x>n then 4
    else if x = 1 then 2+s (* 2 or 3 *)
    else if outcome.(x).(s) = 0 then (
      outcome.(x).(s) <- 5;
      outcome.(x).(s) <- fill_table (next_state (x,s));
      outcome.(x).(s)
    )
    else if outcome.(x).(s) = 5 then (
      outcome.(x).(s) <- 1;
      1
    )
    else outcome.(x).(s)
  in

  let () = for i=2 to n do
    ignore (fill_table (i,0));
    ignore (fill_table (i,1))
  done in

  let cycles i =
    let a = outcome.(i+1).(1) in
    let b = outcome.(i+1).(0) in
      a = 1 || a = 2 || (a = 3 && (b = 1 || b = 2 || b = 3))
  in

  let yval = Array.make_matrix (n+1) 2 (-1L) in

  let rec fill_in_y i (x,s) =
    if x<1 || x>n then 0L
    else if (x,s) = (1,1) then Int64.of_int i
    else if yval.(x).(s) >= 0L then yval.(x).(s)
    else (
      yval.(x).(s) <- (Int64.of_int a.(x)) ++ (fill_in_y i (next_state (x,s)));
      yval.(x).(s)
    )
  in

    for i=1 to n-1 do
      if cycles i then print_string ("-1\n")
      else Printf.printf "%Ld\n" ((Int64.of_int i)++(fill_in_y i (i+1,1)))
    done

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

let () =
  let n = read_int 0 in
  let a = Array.init (n+1) (fun i -> if i<=1 then 0 else read_int 0) in
  let outcome = Array.make_matrix (n+1) 2 0 in
(* this table has the following meaning:
   0 unknown
   1 cyclic
   2 reaches (1,0)
   3 reaches (1,1)
   4 termates
   5 current search *)

  let next_state (x,s) = (* s=0 means the initial state of the process, before "step 2" *)
    if s=0 then (x+a.(x), (1-s)) else (x-a.(x), (1-s))
  in

  let rec find_path (x,s) =
    if x<1 || x>n then 4
    else if x = 1 then 2+s (* 2 or 3 *)
    else if outcome.(x).(s) <> 0 then outcome.(x).(s)
    else (
      outcome.(x).(s) <- 5;
      find_path (next_state(x,s))
    )
  in

  let rec label_path (x,s) l =
    if x<1 || x>n then ()
    else if outcome.(x).(s) = 5 then (
      outcome.(x).(s) <- l;
      label_path (next_state(x,s)) l
    )
  in

  let fill_table (x,s) =
    let l = find_path(x,s) in
      label_path (x,s) (if l=5 then 1 else l)
  in

  let () = for i=1 to n-1 do
    fill_table (i+1,1)
  done in

  let cycles i =
    outcome.(i+1).(1) = 1 || outcome.(i+1).(1) = 2
  in

  let yval = Array.make_matrix (n+1) 2 (-1) in

  let rec testoverflow i = if i=0 then 0 else i+(testoverflow (i-1)) in

    Printf.printf "testoverflow 10 = %d\n" (testoverflow 1000000);

  let rec fill_in_y i (x,s) =
    if x<1 || x>n then 0
    else if (x,s) = (1,1) then i
    else if yval.(x).(s) >= 0 then yval.(x).(s)
    else (
      yval.(x).(s) <- a.(x) + fill_in_y i (next_state (x,s));
      yval.(x).(s)
    )
  in

    for i=1 to n-1 do
      if cycles i then print_string ("-1\n")
      else Printf.printf "%d\n" (i+(fill_in_y i (i+1,1)))
    done;

    Printf.printf "testoverflow 10 = %d\n" (testoverflow 1000000)


A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop.There are m queens on a square n × n chessboard. You know each queen's positions, the i-th queen is positioned in the square (ri, ci), where ri is the board row number (numbered from the top to the bottom from 1 to n), and ci is the board's column number (numbered from the left to the right from 1 to n). No two queens share the same position.For each queen one can count w — the number of other queens that the given queen threatens (attacks). For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack. Obviously, for any queen w is between 0 and 8, inclusive.Print the sequence t0, t1, ..., t8, where ti is the number of queens that threaten exactly i other queens, i.e. the number of queens that their w equals i.

The first line of the input contains a pair of integers n, m (1 ≤ n, m ≤ 105), where n is the size of the board and m is the number of queens on the board. Then m following lines contain positions of the queens, one per line. Each line contains a pair of integers ri, ci (1 ≤ ri, ci ≤ n) — the queen's position. No two queens stand on the same square.

Print the required sequence t0, t1, ..., t8, separating the numbers with spaces.

(* row : [(row_number,(min_column,max_column));...]
 * col : [(col_number,(min_row,max_row));...]
 * diags : [(d, ((minRow,minCol),(maxRow,maxCol)))]
 * where d = Row-Col || d = Row + Col
 * Implemented with hash tables
 *)

let solve =
  let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) in
  let readPos() = Scanf.scanf "%d %d\n" (fun x y -> (x,y)) in
  let rowsHash = Hashtbl.create n in
  let colsHash = Hashtbl.create n in
  let diagsHash = Hashtbl.create n in
  let diagsInvHash = Hashtbl.create n in
  let rec readBoard i acc =
    match i with
      | 0 -> acc
      | i -> let (row,col) = readPos() in
          readBoard (i-1) ((row,col)::acc)
  in
  let rec createRow queens =
    match queens with
      | []      -> ()
      | (row,col) :: xs -> (match (try Some (Hashtbl.find rowsHash row) with Not_found -> None) with
                              | None -> Hashtbl.add rowsHash row (col,col); createRow xs;
                              | Some (minCol,maxCol) ->
                                  if (col < minCol)
                                  then
                                    (Hashtbl.replace rowsHash row (col,maxCol); createRow xs)
                                  else if (col > maxCol) then
                                    (Hashtbl.replace rowsHash row (minCol,col); createRow xs)
                                  else
                                    createRow xs
        )
  in
  let rec createCol queens =
    match queens with
      | []      -> ()
      | (row,col) :: xs -> (match (try Some (Hashtbl.find colsHash col) with Not_found -> None) with
                              | None -> Hashtbl.add colsHash col (row,row); createCol xs;
                              | Some (minRow,maxRow) ->
                                  if (row < minRow)
                                  then
                                    (Hashtbl.replace colsHash col (row,maxRow); createCol xs)
                                  else if (row > maxRow) then
                                    (Hashtbl.replace colsHash col (minRow,row); createCol xs)
                                  else
                                    createCol xs
        )
  in
  let createDiag queens f dHash =
    let rec aux queens =
      match queens with
        | []    -> ()
        | (row,col) :: xs -> let d = f row col in
            (match (try Some (Hashtbl.find dHash d ) with Not_found -> None) with
               | None -> Hashtbl.add dHash d ((row,col),(row,col)); aux xs;
               | Some ((minRow,minCol),(maxRow,maxCol)) ->
                   if (row < minRow)
                   then
                     (Hashtbl.replace dHash d ((row,col),(maxRow,maxCol)); aux xs)
                   else if (row > maxRow) then
                     (Hashtbl.replace dHash d ((minRow,minCol),(row,col)); aux xs)
                   else
                     aux xs
            )
    in
      aux queens
  in
  let check queens =
    let rec checkAux queens t =
    let checkRow (row,col) =
      match (try Some (Hashtbl.find rowsHash row) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minCol,maxCol) ->
            if (col > minCol) then
              begin
                if (col < maxCol) then 2 else 1
              end
            else
              if (col < maxCol) then 1 else 0
    in
    let checkCol (row,col) =
      match (try Some (Hashtbl.find colsHash col) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minRow,maxRow) ->
            if (row > minRow) then
              begin
                if (row < maxRow) then 2 else 1
              end
            else
              if (row < maxRow) then 1 else 0
    in
    let checkDiag (row,col) dHash f =
      let d = f row col in
        match (try Some (Hashtbl.find dHash d) with Not_found -> None) with
          | None -> failwith "if nasa launched sattelites with bugs then
              I am allowed to fuck this up no strings attached!"
          | Some ((minRow,minCol),(maxRow,maxCol)) ->
              if (row > minRow) then
                begin
                  if (row < maxRow) then 2 else 1
                end
              else
                if (row < maxRow) then 1 else 0
    in
      match queens with
        | [] -> t
        | (row,col) :: xs -> let threats = checkRow (row,col) + checkCol (row,col) +
                                           (checkDiag (row,col) diagsHash ( - )) +
                                           (checkDiag (row,col) diagsInvHash ( + ))
          in
            (match (try Some (List.assoc threats t) with Not_found -> None) with
               | None -> checkAux xs ((threats,1)::t)
               | Some prevThreats -> checkAux xs ((threats,prevThreats + 1)::(List.remove_assoc threats t)))
    in
      checkAux queens []
  in
  let board = readBoard m [] in
  let _  = createRow  board in
  let _  = createCol  board in
  let _ = createDiag board ( - ) diagsHash in
  let _ = createDiag board ( + ) diagsInvHash in
  let tSeq = check board in
  let print lst =
    let rec printAux i =
      match i with
        | 9 -> Printf.printf "\n"
        | i ->
            (match (try Some (List.assoc i lst) with Not_found -> None) with
               | None -> Printf.printf "0 "; printAux (i+1)
               | Some v -> Printf.printf "%d " v; printAux (i+1))
    in
      printAux 0
  in
    print tSeq
;;

(* row : [(row_number,(min_column,max_column));...]
 * col : [(col_number,(min_row,max_row));...]
 * diags : [(d, ((minRow,minCol),(maxRow,maxCol)))]
 * where d = Row-Col || d = Row + Col
 *)

let solve =
  let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) in
  let readPos() = Scanf.scanf "%d %d\n" (fun x y -> (x,y)) in
  let rowsHash = Hashtbl.create n in
  let colsHash = Hashtbl.create n in
  let diagsHash = Hashtbl.create n in
  let diagsInvHash = Hashtbl.create n in
  let rec readBoard i acc =
    match i with
      | 0 -> acc
      | i -> let (row,col) = readPos() in
          readBoard (i-1) ((row,col)::acc)
  in
  let rec createRow queens =
    match queens with
      | []      -> ()
      | (row,col) :: xs -> (match (try Some (Hashtbl.find rowsHash row) with Not_found -> None) with
                              | None -> Hashtbl.add rowsHash row (col,col); createRow xs;
                              | Some (minCol,maxCol) ->
                                  if (col < minCol)
                                  then
                                    (Hashtbl.replace rowsHash row (col,maxCol); createRow xs)
                                  else if (col > maxCol) then
                                    (Hashtbl.replace rowsHash row (minCol,col); createRow xs)
                                  else
                                    createRow xs
        )
  in
  let rec createCol queens =
    match queens with
      | []      -> ()
      | (row,col) :: xs -> (match (try Some (Hashtbl.find colsHash col) with Not_found -> None) with
                              | None -> Hashtbl.add colsHash col (row,row); createCol xs;
                              | Some (minRow,maxRow) ->
                                  if (row < minRow)
                                  then
                                    (Hashtbl.replace colsHash col (row,maxRow); createCol xs)
                                  else if (row > maxRow) then
                                    (Hashtbl.replace colsHash col (minRow,row); createCol xs)
                                  else
                                    createCol xs
        )
  in
  let createDiag queens f dHash =
    let rec aux queens =
      match queens with
        | []    -> ()
        | (row,col) :: xs -> let d = f row col in
            (match (try Some (Hashtbl.find dHash d ) with Not_found -> None) with
               | None -> Hashtbl.add dHash d ((row,col),(row,col)); aux xs;
               | Some ((minRow,minCol),(maxRow,maxCol)) ->
                   if (row < minRow)
                   then
                     (Hashtbl.replace dHash d ((row,col),(maxRow,maxCol)); aux xs)
                   else if (row > maxRow) then
                     (Hashtbl.replace dHash d ((minRow,minCol),(row,col)); aux xs)
                   else
                     aux xs
            )
    in
      aux queens
  in
  let rec check queens rows cols diag1 diag2 t =
    let checkRow (row,col) =
      match (try Some (Hashtbl.find rowsHash row) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minCol,maxCol) ->
            if (col > minCol) then
              begin
                if (col < maxCol) then 2 else 1
              end
            else
              if (col < maxCol) then 1 else 0
    in
    let checkCol (row,col) =
      match (try Some (Hashtbl.find colsHash col) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minRow,maxRow) ->
            if (row > minRow) then
              begin
                if (row < maxRow) then 2 else 1
              end
            else
              if (row < maxRow) then 1 else 0
    in
    let checkDiag (row,col) dHash f =
      let d = f row col in
        match (try Some (Hashtbl.find dHash d) with Not_found -> None) with
          | None -> failwith "if nasa launched sattelites with bugs then
              I am allowed to fuck this up no strings attached!"
          | Some ((minRow,minCol),(maxRow,maxCol)) ->
              if (row > minRow) then
                begin
                  if (row < maxRow) then 2 else 1
                end
              else
                if (row < maxRow) then 1 else 0
    in
      match queens with
        | [] -> t
        | (row,col) :: xs -> let threats = checkRow (row,col) + checkCol (row,col) +
                                           (checkDiag (row,col) diagsHash ( - )) +
                                           (checkDiag (row,col) diagsInvHash ( + ))
          in
            (match (try Some (List.assoc threats t) with Not_found -> None) with
               | None -> check xs rows cols diag1 diag2 ((threats,1)::t)
               | Some prevThreats -> check xs rows cols diag1 diag2 ((threats,prevThreats + 1)::(List.remove_assoc threats t)))
  in
  let board = readBoard m [] in
  let rows  = createRow  board in
  let cols  = createCol  board in
  let diag1 = createDiag board ( - ) diagsHash in
  let diag2 = createDiag board ( + ) diagsInvHash in
  let tSeq = check board rows cols diag1 diag2 [] in
  let print lst =
    let rec aux i =
      match i with
        | 9 -> Printf.printf "\n"
        | i ->
            (match (try Some (List.assoc i lst) with Not_found -> None) with
               | None -> Printf.printf "0 "; aux (i+1)
               | Some v -> Printf.printf "%d " v; aux (i+1))
    in
      aux 0
  in
    print tSeq
;;

(* row : [(row_number,(min_column,max_column));...]
 * col : [(col_number,(min_row,max_row));...]
 * diags : [(d, ((minRow,minCol),(maxRow,maxCol)))]
 * where d = Row-Col || d = Row + Col
 *)

let solve =
  let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) in
  let readPos() = Scanf.scanf "%d %d\n" (fun x y -> (x,y)) in
  let rowsHash = Hashtbl.create n in
  let colsHash = Hashtbl.create n in
  let diagsHash = Hashtbl.create n in
  let diagsInvHash = Hashtbl.create n in
  let rec readBoard i acc =
    match i with
      | 0 -> acc
      | i -> let (row,col) = readPos() in
          readBoard (i-1) ((row,col)::acc)
  in
  let rec createRow queens =
    match queens with
      | []      -> ()
      | (row,col) :: xs -> (match (try Some (Hashtbl.find rowsHash row) with Not_found -> None) with
                              | None -> Hashtbl.add rowsHash row (col,col); createRow xs;
                              | Some (minCol,maxCol) ->
                                  if (col < minCol)
                                  then
                                    (Hashtbl.replace rowsHash row (col,maxCol); createRow xs)
                                  else if (col > maxCol) then
                                    (Hashtbl.replace rowsHash row (minCol,col); createRow xs)
                                  else
                                    createRow xs
        )
  in
  let rec createCol queens =
    match queens with
      | []      -> ()
      | (row,col) :: xs -> (match (try Some (Hashtbl.find colsHash col) with Not_found -> None) with
                              | None -> Hashtbl.add colsHash col (row,row); createCol xs;
                              | Some (minRow,maxRow) ->
                                  if (row < minRow)
                                  then
                                    (Hashtbl.replace colsHash col (row,maxRow); createCol xs)
                                  else if (row > maxRow) then
                                    (Hashtbl.replace colsHash col (minRow,row); createCol xs)
                                  else
                                    createCol xs
        )
  in
  let createDiag queens f dHash =
    let rec aux queens =
      match queens with
        | []    -> ()
        | (row,col) :: xs -> let d = f row col in
            (match (try Some (Hashtbl.find dHash d ) with Not_found -> None) with
               | None -> Hashtbl.add dHash d ((row,col),(row,col)); aux xs;
               | Some ((minRow,minCol),(maxRow,maxCol)) ->
                   if (row < minRow)
                   then
                     (Hashtbl.replace dHash d ((row,col),(maxRow,maxCol)); aux xs)
                   else if (row > maxRow) then
                     (Hashtbl.replace dHash d ((minRow,minCol),(row,col)); aux xs)
                   else
                     aux xs
            )
    in
      aux queens
  in
  let check queens rows cols diag1 diag2 =
    let rec checkAux queens t =
    let checkRow (row,col) =
      match (try Some (Hashtbl.find rowsHash row) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minCol,maxCol) ->
            if (col > minCol) then
              begin
                if (col < maxCol) then 2 else 1
              end
            else
              if (col < maxCol) then 1 else 0
    in
    let checkCol (row,col) =
      match (try Some (Hashtbl.find colsHash col) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minRow,maxRow) ->
            if (row > minRow) then
              begin
                if (row < maxRow) then 2 else 1
              end
            else
              if (row < maxRow) then 1 else 0
    in
    let checkDiag (row,col) dHash f =
      let d = f row col in
        match (try Some (Hashtbl.find dHash d) with Not_found -> None) with
          | None -> failwith "if nasa launched sattelites with bugs then
              I am allowed to fuck this up no strings attached!"
          | Some ((minRow,minCol),(maxRow,maxCol)) ->
              if (row > minRow) then
                begin
                  if (row < maxRow) then 2 else 1
                end
              else
                if (row < maxRow) then 1 else 0
    in
      match queens with
        | [] -> t
        | (row,col) :: xs -> let threats = checkRow (row,col) + checkCol (row,col) +
                                           (checkDiag (row,col) diagsHash ( - )) +
                                           (checkDiag (row,col) diagsInvHash ( + ))
          in
            (match (try Some (List.assoc threats t) with Not_found -> None) with
               | None -> checkAux xs ((threats,1)::t)
               | Some prevThreats -> checkAux xs ((threats,prevThreats + 1)::(List.remove_assoc threats t)))
    in
      checkAux queens []
  in
  let board = readBoard m [] in
  let rows  = createRow  board in
  let cols  = createCol  board in
  let diag1 = createDiag board ( - ) diagsHash in
  let diag2 = createDiag board ( + ) diagsInvHash in
  let tSeq = check board rows cols diag1 diag2 in
  let print lst =
    let rec printAux i =
      match i with
        | 9 -> Printf.printf "\n"
        | i ->
            (match (try Some (List.assoc i lst) with Not_found -> None) with
               | None -> Printf.printf "0 "; printAux (i+1)
               | Some v -> Printf.printf "%d " v; printAux (i+1))
    in
      printAux 0
  in
    print tSeq
;;

(* row : [(row_number,(min_column,max_column));...]
 * col : [(col_number,(min_row,max_row));...]
 * diags : [(d, ((minRow,minCol),(maxRow,maxCol)))]
 * where d = Row-Col || d = Row + Col
 *)

let solve =
  let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) in
  let readPos() = Scanf.scanf "%d %d\n" (fun x y -> (x,y)) in
  let rowsHash = Hashtbl.create n in
  let colsHash = Hashtbl.create n in
  let diagsHash = Hashtbl.create n in
  let diagsInvHash = Hashtbl.create n in
  let rec readBoard i acc =
    match i with
      | 0 -> acc
      | i -> let (row,col) = readPos() in
          readBoard (i-1) ((row,col)::acc)
  in
  let rec createLists queens  =
    match queens with
      | [] -> ()
      | (row,col) :: xs ->   let createRow () =
                                match (try Some (Hashtbl.find rowsHash row) with Not_found -> None) with
                                   | None -> Hashtbl.add rowsHash row (col,col);
                                   | Some (minCol,maxCol) ->
                                       if (col < minCol)
                                       then
                                         Hashtbl.replace rowsHash row (col,maxCol)
                                       else if (col > maxCol) then
                                         Hashtbl.replace rowsHash row (minCol,col)
                                       else
                                         ()
                              in
                              let createCol () =
                                match (try Some (Hashtbl.find colsHash col) with Not_found -> None) with
                                   | None -> Hashtbl.add colsHash col (row,row);
                                   | Some (minRow,maxRow) ->
                                       if (row < minRow)
                                       then
                                         Hashtbl.replace colsHash col (row,maxRow)
                                       else if (row > maxRow) then
                                         Hashtbl.replace colsHash col (minRow,row)
                                       else
                                         ()
                              in
                              let createDiag  dHash f =
                                let d = f row col in
                                  match (try Some (Hashtbl.find dHash d ) with Not_found -> None) with
                                     | None -> Hashtbl.add dHash d ((row,col),(row,col));
                                     | Some ((minRow,minCol),(maxRow,maxCol)) ->
                                         if (row < minRow)
                                         then
                                           Hashtbl.replace dHash d ((row,col),(maxRow,maxCol))
                                         else if (row > maxRow) then
                                           Hashtbl.replace dHash d ((minRow,minCol),(row,col))
                                         else
                                           ()
                              in
                                createRow ();
                                createCol ();
                                createDiag diagsHash ( - );
                                createDiag diagsInvHash ( + );
                                createLists xs
  in
  let check queens =
    let rec checkAux queens t =
    let checkRow (row,col) =
      match (try Some (Hashtbl.find rowsHash row) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minCol,maxCol) ->
            if (col > minCol) then
              begin
                if (col < maxCol) then 2 else 1
              end
            else
              if (col < maxCol) then 1 else 0
    in
    let checkCol (row,col) =
      match (try Some (Hashtbl.find colsHash col) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minRow,maxRow) ->
            if (row > minRow) then
              begin
                if (row < maxRow) then 2 else 1
              end
            else
              if (row < maxRow) then 1 else 0
    in
    let checkDiag (row,col) dHash f =
      let d = f row col in
        match (try Some (Hashtbl.find dHash d) with Not_found -> None) with
          | None -> failwith "if nasa launched sattelites with bugs then
              I am allowed to fuck this up no strings attached!"
          | Some ((minRow,minCol),(maxRow,maxCol)) ->
              if (row > minRow) then
                begin
                  if (row < maxRow) then 2 else 1
                end
              else
                if (row < maxRow) then 1 else 0
    in
      match queens with
        | [] -> t
        | (row,col) :: xs -> let threats = checkRow (row,col) + checkCol (row,col) +
                                           (checkDiag (row,col) diagsHash ( - )) +
                                           (checkDiag (row,col) diagsInvHash ( + ))
          in
            (match (try Some (List.assoc threats t) with Not_found -> None) with
               | None -> checkAux xs ((threats,1)::t)
               | Some prevThreats -> checkAux xs ((threats,prevThreats + 1)::(List.remove_assoc threats t)))
    in
      checkAux queens []
  in
  let board = readBoard m [] in
  let _ = createLists board in
  let tSeq = check board in
  let print lst =
    let rec aux i =
      match i with
        | 9 -> Printf.printf "\n"
        | i ->
            (match (try Some (List.assoc i lst) with Not_found -> None) with
               | None -> Printf.printf "0 "; aux (i+1)
               | Some v -> Printf.printf "%d " v; aux (i+1))
    in
      aux 0
  in
    print tSeq
;;

let read_stuff () =
  let m = Scanf.scanf " %d %d" (fun _ a -> a)
  and l = ref [] in
    for i = 1 to m do
      let pair = Scanf.scanf " %d %d" (fun a b -> (a, b, 0)) in
      l := pair :: !l;
    done;
    !l

let postprocess queens =
  let res = Array.create 9 0
  in List.iter (fun (_, _, sz) -> (res.(sz) <- (res.(sz) + 1))) queens; res

let print_res res =
  Array.iter (fun el -> Printf.printf "%d " el) res;
  print_endline ""

let cmp a b =
  if a == b
  then 0
  else if a > b
  then 1
  else -1

let solve_one queens pred =
  let proc2 q r =
    let (x, y, v) = q in
      if abs(pred q r) == 2
      then (x, y, v + 1)
      else q
  in let proc = function
    | h :: t ->
        snd (List.fold_left
          (* Sprawdz wszystko oprocz glowy *)
          (fun (last, acc) h ->
             (h, (proc2 h last)::acc))
          (h, [h])
          t)
    | foo -> foo
  and sorted = List.sort pred queens
  in proc (proc (sorted))

let rec solve queens preds =
  match preds with
    | [] -> queens
    | p0 :: ps -> solve (solve_one queens p0) ps

let oq a b =
  if a == 0 then 2 * b else a
;;

let predicates =
  [
    (fun (x1, y1, _) (x2, y2, _) -> oq (cmp x1 x2) (cmp y1 y2));
    (fun (x1, y1, _) (x2, y2, _) -> oq (cmp y1 y2) (cmp x1 x2));
    (fun (x1, y1, _) (x2, y2, _) -> oq (cmp (x1 - y1) (x2 - y2)) (cmp (x1 + y1) (x2 + y2)));
    (fun (x1, y1, _) (x2, y2, _) -> oq (cmp (x1 + y1) (x2 + y2)) (cmp (x1 - y1) (x2 - y2)))
  ]
in
  print_res (postprocess (solve (read_stuff()) predicates))

(* row : [(row_number,(min_column,max_column));...]
 * col : [(col_number,(min_row,max_row));...]
 * diags : [(d, ((minRow,minCol),(maxRow,maxCol)))]
 * where d = Row-Col || d = Row + Col
 *)

let solve =
  let n,m = Scanf.scanf "%d %d\n" (fun n m -> (n,m)) in
  let readPos() = Scanf.scanf "%d %d\n" (fun x y -> (x,y)) in
  let rec readBoard i acc =
    match i with
      | 0 -> List.rev acc
      | i -> let (row,col) = readPos() in
          readBoard (i-1) ((row,col)::acc)
  in
  let rec createRow queens acc =
    match queens with
      | []      -> List.rev acc
      | (row,col) :: xs -> (match (try Some (List.assoc row acc) with Not_found -> None) with
                              | None -> createRow xs ((row,(col,col)) :: acc)
                              | Some (minCol,maxCol) ->
                                  if (col < minCol)
                                  then
                                    createRow xs ((row,(col,maxCol))::(List.remove_assoc row acc))
                                  else if (col > maxCol) then
                                    createRow xs ((row,(minCol,col))::(List.remove_assoc row acc))
                                  else
                                    createRow xs acc
        )
  in
  let rec createCol queens acc =
    match queens with
      | []      -> List.rev acc
      | (row,col) :: xs -> (match (try Some (List.assoc col acc) with Not_found -> None) with
                              | None -> createCol xs ((col,(row,row)) :: acc)
                              | Some (minRow,maxRow) ->
                                  if (row < minRow)
                                  then
                                    createCol xs ((col,(row,maxRow))::(List.remove_assoc col acc))
                                  else if (row > maxRow) then
                                    createCol xs ((col,(minRow,row))::(List.remove_assoc col acc))
                                  else
                                    createCol xs acc
        )
  in
  let rec createDiag queens acc f =
    match queens with
      | []    -> List.rev acc
      | (row,col) :: xs -> let d = f row col in
          (match (try Some (List.assoc d acc) with Not_found -> None) with
             | None -> createDiag xs ((d,( (row,col), (row,col) )) :: acc) f
             | Some ((minRow,minCol),(maxRow,maxCol)) ->
                 if (row < minRow)
                 then
                   createDiag xs ((d, ((row,col),(maxRow,maxCol))) :: (List.remove_assoc d acc)) f
                 else if (row > maxRow) then
                   createDiag xs ((d, ((minRow,minCol),(row,col))) :: (List.remove_assoc d acc)) f
                 else
                   createDiag xs acc f
          )
  in
  let rec check queens rows cols diag1 diag2 t =
    let checkRow (row,col) =
      match (try Some (List.assoc row rows) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minCol,maxCol) ->
            if (col > minCol) then
              begin
                if (col < maxCol) then 2 else 1
              end
            else
              if (col < maxCol) then 1 else 0
    in
    let checkCol (row,col) =
      match (try Some (List.assoc col cols) with Not_found -> None) with
        | None -> failwith "nothing to see here, move on"
        | Some (minRow,maxRow) ->
            if (row > minRow) then
              begin
                if (row < maxRow) then 2 else 1
              end
            else
              if (row < maxRow) then 1 else 0
    in
    let checkDiag (row,col) diag f =
      let d = f row col in
        match (try Some (List.assoc d diag) with Not_found -> None) with
          | None -> failwith "if nasa launched sattelites with bugs then
              I am allowed to fuck this up no strings attached!"
          | Some ((minRow,minCol),(maxRow,maxCol)) ->
              if (row > minRow) then
                begin
                  if (row < maxRow) then 2 else 1
                end
              else
                if (row < maxRow) then 1 else 0
    in
      match queens with
        | [] -> t
        | (row,col) :: xs -> let threats = checkRow (row,col) + checkCol (row,col) +
                                           (checkDiag (row,col) diag1 ( - )) +
                                           (checkDiag (row,col) diag2 ( + ))
          in
            (match (try Some (List.assoc threats t) with Not_found -> None) with
               | None -> check xs rows cols diag1 diag2 ((threats,1)::t)
               | Some prevThreats -> check xs rows cols diag1 diag2 ((threats,prevThreats + 1)::(List.remove_assoc threats t)))
  in
  let board = readBoard m [] in
  let rows  = createRow  board [] in
  let cols  = createCol  board [] in
  let diag1 = createDiag board [] ( - ) in
  let diag2 = createDiag board [] ( + ) in
  let tSeq = check board rows cols diag1 diag2 [] in
  let normalize lst =
    let rec aux lst i acc =
      match i,lst with
        | 9,_ -> List.rev acc
        | i,[] -> aux [] (i+1) ((i,0)::acc)
        | i,((j,v)::t) -> if (i = j) then aux t (i+1) ((j,v) :: acc)
          else
            aux ((j,v)::t) (i+1) ((i,0) :: acc)
    in
      aux lst 0 []
  in
  let tOut = normalize tSeq in
    List.iter (fun (_,v2) -> Printf.printf "%d " v2) tOut;
    Printf.printf "\n"
;;

let read_stuff () =
  let m = Scanf.scanf " %d %d" (fun _ a -> a)
  and l = ref [] in
    for i = 1 to m do
      let pair = Scanf.scanf " %d %d" (fun a b -> (a, b, 0)) in
      l := pair :: !l;
    done;
    !l

let postprocess queens =
  let res = Array.create 10 0
  in List.iter (fun (_, _, sz) -> (res.(sz) <- (res.(sz) + 1))) queens; res

let print_res res =
  Array.iter (fun el -> Printf.printf "%d " el) res;
  print_endline ""

let cmp a b =
  if a == b
  then 0
  else if a > b
  then 1
  else -1

let solve_one queens pred =
  let proc2 q r =
    let (x, y, v) = q in
      if abs(pred q r) == 2
      then (x, y, v + 1)
      else q
  in let proc = function
    | h :: t ->
        snd (List.fold_left
          (* Sprawdz wszystko oprocz glowy *)
          (fun (last, acc) h ->
             (h, (proc2 h last)::acc))
          (h, [h])
          t)
    | foo -> foo
  and sorted = List.sort pred queens
  in proc (proc (sorted))

let rec solve queens preds =
  match preds with
    | [] -> queens
    | p0 :: ps -> solve (solve_one queens p0) ps

let oq a b =
  if a == 0 then 2 * b else a
;;

let predicates =
  [
    (fun (x1, y1, _) (x2, y2, _) -> oq (cmp x1 x2) (cmp y1 y2));
    (fun (x1, y1, _) (x2, y2, _) -> oq (cmp y1 y2) (cmp x1 x2));
    (fun (x1, y1, _) (x2, y2, _) -> oq (cmp (x1 - y1) (x2 - y2)) (cmp (x1 + y1) (x2 + y2)));
    (fun (x1, y1, _) (x2, y2, _) -> oq (cmp (x1 + y1) (x2 + y2)) (cmp (x1 - y1) (x2 - y2)))
  ]
in
  print_res (postprocess (solve (read_stuff()) predicates))


Consider a table of size $$$n \times m$$$, initially fully white. Rows are numbered $$$1$$$ through $$$n$$$ from top to bottom, columns $$$1$$$ through $$$m$$$ from left to right. Some square inside the table with odd side length was painted black. Find the center of this square.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 115$$$) — the number of rows and the number of columns in the table. The $$$i$$$-th of the next $$$n$$$ lines contains a string of $$$m$$$ characters $$$s_{i1} s_{i2} \ldots s_{im}$$$ ($$$s_{ij}$$$ is 'W' for white cells and 'B' for black cells), describing the $$$i$$$-th row of the table.

Output two integers $$$r$$$ and $$$c$$$ ($$$1 \le r \le n$$$, $$$1 \le c \le m$$$) separated by a space — the row and column numbers of the center of the black square.

open Printf
open Scanf

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

  let xtot = ref 0 in
  let ytot = ref 0 in
  let tot = ref 0 in

  for i=0 to n-1 do
    for j=0 to m-1 do
      if board.(i).[j] = 'B' then (
	tot := !tot + 1;
	xtot := !xtot + i + 1;
	ytot := !ytot + j + 1
      )
    done
  done;

  printf "%d %d\n" (!xtot / !tot) (!ytot / !tot)


Valera is a little boy. Yesterday he got a huge Math hometask at school, so Valera didn't have enough time to properly learn the English alphabet for his English lesson. Unfortunately, the English teacher decided to have a test on alphabet today. At the test Valera got a square piece of squared paper. The length of the side equals n squares (n is an odd number) and each unit square contains some small letter of the English alphabet.Valera needs to know if the letters written on the square piece of paper form letter "X". Valera's teacher thinks that the letters on the piece of paper form an "X", if:  on both diagonals of the square paper all letters are the same;  all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals. Help Valera, write the program that completes the described task for him.

The first line contains integer n (3 ≤ n < 300; n is odd). Each of the next n lines contains n small English letters — the description of Valera's paper.

Print string "YES", if the letters on the paper form letter "X". Otherwise, print string "NO". Print the strings without quotes.

let n=read_int();;

let tab=Array.make n " ";;

for i=0 to n-1 do
   tab.(i)<-read_line()
done;;

let a=tab.(0).[0];;
let v=tab.(0).[1];;
let b=ref true;;

for i=0 to n-1 do
   for j=0 to n-1 do
      if (i=j)||(i+j=n-1) then b:= !b&&(tab.(i).[j]=a)
      else b:= !b&&(tab.(i).[j]=v)
   done
done;;

if a=v then b:=false;;

if !b then print_string "YES" else print_string "NO";;

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

let tab = Array.init n (fun _ -> Scanf.scanf "%s\n" (fun x -> x));;

let valide = ref (tab.(0).[0] <> tab.(0).[1]);;

for i = 0 to n-1 do
for j = 0 to n-1 do
  if i = j
    then (if tab.(i).[j] <> tab.(0).[0] then valide := false)
  else if n-j-1 = i
    then (if tab.(i).[j] <> tab.(0).[0] then valide := false)
  else
    if tab.(i).[j] <> tab.(0).[1] then valide := false
done;
done;

if !valide
  then print_endline "YES"
  else print_endline "NO"

let n=read_int();;

let tab=Array.make n " ";;

for i=0 to n-1 do
   tab.(i)<-read_line()
done;;

let a=tab.(0).[0];;
let v=tab.(0).[1];;
let b=ref true;;

for i=0 to n-1 do
   for j=0 to n-1 do
      if (i=j)||(i+j=n-1) then b:= !b&&(tab.(i).[j]=a)
      else b:= !b&&(tab.(i).[j]=v)
   done
done;;

if !b then print_string "YES" else print_string "NO";;

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

let tab = Array.init n (fun _ -> Scanf.scanf "%s\n" (fun x -> x));;

let valide = ref true;;

for i = 0 to n-1 do
for j = 0 to n-1 do
  if i = j
    then (if tab.(i).[j] <> tab.(0).[0] then valide := false)
  else if n-j-1 = i
    then (if tab.(i).[j] <> tab.(0).[0] then valide := false)
  else
    if tab.(i).[j] <> tab.(0).[1] then valide := false
done;
done;

if !valide
  then print_endline "YES"
  else print_endline "NO"