You are given n points on the plane. You need to delete exactly k of them (k < n) so that the diameter of the set of the remaining n - k points were as small as possible. The diameter of a set of points is the maximum pairwise distance between the points of the set. The diameter of a one point set equals zero.

The first input line contains a pair of integers n, k (2 ≤ n ≤ 1000, 1 ≤ k ≤ 30, k < n) — the numbers of points on the plane and the number of points to delete, correspondingly. Next n lines describe the points, one per line. Each description consists of a pair of integers xi, yi (0 ≤ xi, yi ≤ 32000) — the coordinates of the i-th point. The given points can coincide.

Print k different space-separated integers from 1 to n — the numbers of points to delete. The points are numbered in the order, in which they are given in the input from 1 to n. You can print the numbers in any order. If there are multiple solutions, print any of them.

exception Found

let debug = false

let _ =
  let sqr x = x * x in
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make n (0, 0) in
  let es = Array.make (n * n) (0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%d " (fun s -> s) in
      let y = Scanf.bscanf sb "%d " (fun s -> s) in
	p.(i) <- (x, y)
    done;
    for i = 0 to n - 1 do
      let (x1, y1) = p.(i) in
	for j = i + 1 to n - 1 do
	  let (x2, y2) = p.(j) in
	  let d =
	    Int32.add
	      (Int32.of_int (sqr (x2 - x1)))
	      (Int32.of_int (sqr (y2 - y1)))
	  in
	    es.(i * n + j) <- (d, i, j)
	done
    done;
    let cmp (x, _, _) (y, _, _) = Int32.compare y x in
      Array.sort cmp es
  in
  let res = Array.make n 0 in
  let solve d =
    if debug then Printf.printf "solve %ld\n" d;
    let m =
      let m = ref 0 in
	while !m < n * n &&
	  (let (d', _, _) = es.(!m) in d' > d )
	do
	  incr m
	done;
	!m
    in
    let es' = Array.make n [] in
    let () =
      for i = 0 to n - 1 do
	res.(i) <- 0
      done;
      for i = 0 to m - 1 do
	let (_d, u, v) = es.(i) in
if debug then Printf.printf "edge %d %d  %ld\n" u v _d;
	  es'.(u) <- v :: es'.(u);
	  es'.(v) <- u :: es'.(v)
      done
    in
    let es = Array.map Array.of_list es' in
    let vs = ref [] in
    let k = ref k in
    let m = ref m in
    let () =
      for i = 0 to n - 1 do
	if Array.length es.(i) = 0
	then ()
	else if Array.length es.(i) = 1 && Array.length es.(es.(i).(0)) = 1
	then (
	  if i < es.(i).(0) then (
    (*Printf.printf "remove %d-%d\n" i es.(i).(0);*)
	    res.(i) <- 1;
	    decr k;
	    decr m;
	  )
	) else if Array.length es.(i) = 1
	then ()
	else vs := i :: !vs
      done
    in
    let vs = Array.of_list !vs in
(*Printf.printf "asd2 %ld %d\n" d (Array.length vs);*)
    let rec bf p k m =
      if debug then Printf.printf "bf %d %d %d\n" p k m;
      assert (m >= 0);
      if k >= 0 then (
	if m = 0 then (
	  let k = ref k in
	    if !k = 0
	    then raise Found;
	    for i = 0 to n - 1 do
	      if res.(i) = 0 then (
		res.(i) <- 1;
		decr k;
		if !k = 0
		then raise Found
	      )
	    done;
	    assert false
	) else if p < Array.length vs then (
	  let v = vs.(p) in
if debug then Printf.printf "asd4 %d\n" v;
	    if res.(v) = 0 then (
	      (let m' = ref m in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
if debug then Printf.printf "rem %d %d\n" v u;
		       m' := !m' - 1
		     );
		 done;
		 res.(v) <- 1;
		 if !m' < m - 1
		 then bf (p + 1) (k - 1) !m';
		 res.(v) <- 0
	      );
(*Printf.printf "asd45\n";*)
	      (let m' = ref m in
	       let k' = ref k in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
		       decr k';
		       for i = 0 to Array.length es.(u) - 1 do
			 let w = es.(u).(i) in
			   if res.(w) = 0 then (
if debug then Printf.printf "rem2 %d %d %d\n" v u w;
			     m' := !m' - 1
			   );
		       done;
		     );
		     res.(u) <- res.(u) + 1
		 done;
		 bf (p + 1) !k' !m';
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     res.(u) <- res.(u) - 1
		 done;
	      )
	    ) else bf (p + 1) k m
	)
      )
(*;Printf.printf "asd5\n";*)
    in
      try
	bf 0 !k !m;
	false
      with
	| Found ->
	    true
  in
  let l = ref 0l in
  let r = ref Int32.max_int in
    while !l < Int32.sub !r 1l do
      let m = Int32.div (Int32.add !l !r) 2l in
	if solve m
	then r := m
	else l := m
    done;
    if debug then Printf.printf "QWE %ld\n" !r;
    ignore (solve !r);
    for i = 0 to n - 1 do
      if res.(i) > 0
      then Printf.printf "%d " (i + 1)
    done;
    Printf.printf "\n"

exception Found

let debug = false

let _ =
  let sqr x = x * x in
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make n (0, 0) in
  let es = Array.make (n * n) (0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%d " (fun s -> s) in
      let y = Scanf.bscanf sb "%d " (fun s -> s) in
	p.(i) <- (x, y)
    done;
    for i = 0 to n - 1 do
      let (x1, y1) = p.(i) in
	for j = i + 1 to n - 1 do
	  let (x2, y2) = p.(j) in
	  let d =
	    Int32.add
	      (Int32.of_int (sqr (x2 - x1)))
	      (Int32.of_int (sqr (y2 - y1)))
	  in
	    es.(i * n + j) <- (d, i, j)
	done
    done;
    let cmp (x, _, _) (y, _, _) = Int32.compare y x in
      Array.sort cmp es
  in
  let res = Array.make n 0 in
  let solve d =
    if debug then Printf.printf "solve %ld\n" d;
    let m =
      let m = ref 0 in
	while !m < n * n &&
	  (let (d', _, _) = es.(!m) in d' > d )
	do
	  incr m
	done;
	!m
    in
    let es' = Array.make n [] in
    let () =
      for i = 0 to n - 1 do
	res.(i) <- 0
      done;
      for i = 0 to m - 1 do
	let (_d, u, v) = es.(i) in
if debug then Printf.printf "edge %d %d  %ld\n" u v _d;
	  es'.(u) <- v :: es'.(u);
	  es'.(v) <- u :: es'.(v)
      done
    in
    let es = Array.map Array.of_list es' in
    let vs = ref [] in
    let k = ref k in
    let m = ref m in
    let () =
      for i = 0 to n - 1 do
	if Array.length es.(i) = 0
	then ()
	else if Array.length es.(i) = 1 && Array.length es.(es.(i).(0)) = 1
	then (
	  if i < es.(i).(0) then (
    (*Printf.printf "remove %d-%d\n" i es.(i).(0);*)
	    res.(i) <- 1;
	    decr k;
	    decr m;
	  )
	) else if Array.length es.(i) = 1
	then ()
	else vs := i :: !vs
      done
    in
    let vs = Array.of_list !vs in
(*Printf.printf "asd2 %ld %d\n" d (Array.length vs);*)
    let rec bf p k m =
      if debug then Printf.printf "bf %d %d %d\n" p k m;
      assert (m >= 0);
      if k >= 0 then (
	if m = 0 then (
	  let k = ref k in
	    if !k = 0
	    then raise Found;
	    for i = 0 to n - 1 do
	      if res.(i) = 0 then (
		res.(i) <- 1;
		decr k;
		if !k = 0
		then raise Found
	      )
	    done;
	    assert false
	) else if p < Array.length vs then (
	  let v = vs.(p) in
if debug then Printf.printf "asd4 %d\n" v;
	    if res.(v) = 0 then (
	      (let m' = ref m in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     m' := !m' - 1;
		     if res.(u) = 0 then (
if debug then Printf.printf "rem %d %d\n" v u;
		     );
		 done;
		 res.(v) <- 1;
		 bf (p + 1) (k - 1) !m';
		 res.(v) <- 0
	      );
(*Printf.printf "asd45\n";*)
	      (let m' = ref m in
	       let k' = ref k in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
		       decr k';
		       for i = 0 to Array.length es.(u) - 1 do
			 let w = es.(u).(i) in
			   m' := !m' - 1;
			   if res.(w) = 0 then (
if debug then Printf.printf "rem2 %d %d %d\n" v u w;
			   );
		       done;
		     );
		     res.(u) <- res.(u) + 1
		 done;
		 bf (p + 1) !k' !m';
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     res.(u) <- res.(u) - 1
		 done;
	      )
	    ) else bf (p + 1) k m
	)
      )
(*;Printf.printf "asd5\n";*)
    in
      try
	bf 0 !k (2 * !m);
	false
      with
	| Found ->
	    true
  in
  let l = ref 0l in
  let r = ref Int32.max_int in
    while !l < Int32.sub !r 1l do
      let m = Int32.div (Int32.add !l !r) 2l in
	if solve m
	then r := m
	else l := m
    done;
    if debug then Printf.printf "QWE %ld\n" !r;
    ignore (solve !r);
    for i = 0 to n - 1 do
      if res.(i) > 0
      then Printf.printf "%d " (i + 1)
    done;
    Printf.printf "\n"

exception Found

let debug = false

let _ =
  let sqr x = x * x in
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make n (0, 0) in
  let es = Array.make (n * n) (0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%d " (fun s -> s) in
      let y = Scanf.bscanf sb "%d " (fun s -> s) in
	p.(i) <- (x, y)
    done;
    for i = 0 to n - 1 do
      let (x1, y1) = p.(i) in
	for j = i + 1 to n - 1 do
	  let (x2, y2) = p.(j) in
	  let d =
	    Int32.add
	      (Int32.of_int (sqr (x2 - x1)))
	      (Int32.of_int (sqr (y2 - y1)))
	  in
	    es.(i * n + j) <- (d, i, j)
	done
    done;
    let cmp (x, _, _) (y, _, _) = Int32.compare y x in
      Array.sort cmp es
  in
  let res = Array.make n 0 in
  let solve d =
    if debug then Printf.printf "solve %ld\n" d;
    let m =
      let m = ref 0 in
	while !m < n * n &&
	  (let (d', _, _) = es.(!m) in d' > d )
	do
	  incr m
	done;
	!m
    in
    let es' = Array.make n [] in
    let () =
      for i = 0 to n - 1 do
	res.(i) <- 0
      done;
      for i = 0 to m - 1 do
	let (_d, u, v) = es.(i) in
if debug then Printf.printf "edge %d %d  %ld\n" u v _d;
	  es'.(u) <- v :: es'.(u);
	  es'.(v) <- u :: es'.(v)
      done
    in
    let es = Array.map Array.of_list es' in
    let vs = ref [] in
    let k = ref k in
    let m = ref m in
    let () =
      for i = 0 to n - 1 do
	if Array.length es.(i) = 0
	then ()
	else if Array.length es.(i) = 1 && Array.length es.(es.(i).(0)) = 1
	then (
	  if i < es.(i).(0) then (
    (*Printf.printf "remove %d-%d\n" i es.(i).(0);*)
	    res.(i) <- 1;
	    decr k;
	    decr m;
	  )
	) else if Array.length es.(i) = 1
	then ()
	else vs := i :: !vs
      done
    in
    let vs = Array.of_list !vs in
(*Printf.printf "asd2 %ld %d\n" d (Array.length vs);*)
    let rec bf p k m =
      if debug then Printf.printf "bf %d %d %d\n" p k m;
      assert (m >= 0);
      if k >= 0 then (
	if m = 0 then (
	  let k = ref k in
	    if !k = 0
	    then raise Found;
	    for i = 0 to n - 1 do
	      if res.(i) = 0 then (
		res.(i) <- 1;
		decr k;
		if !k = 0
		then raise Found
	      )
	    done;
	    assert false
	) else if p < Array.length vs then (
	  let v = vs.(p) in
if debug then Printf.printf "asd4 %d\n" v;
	    if res.(v) = 0 then (
	      (let m' = ref m in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
if debug then Printf.printf "rem %d %d\n" v u;
		       m' := !m' - 1
		     );
		 done;
		 res.(v) <- 1;
		 bf (p + 1) (k - 1) !m';
		 res.(v) <- 0
	      );
(*Printf.printf "asd45\n";*)
	      (let m' = ref m in
	       let k' = ref k in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
		       decr k';
		       for i = 0 to Array.length es.(u) - 1 do
			 let w = es.(u).(i) in
			   if res.(w) = 0 then (
if debug then Printf.printf "rem2 %d %d %d\n" v u w;
			     m' := !m' - 1
			   );
		       done;
		     );
		     res.(u) <- res.(u) + 1
		 done;
		 if !k' <= k - 1
		 then bf (p + 1) !k' !m';
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     res.(u) <- res.(u) - 1
		 done;
	      )
	    ) else bf (p + 1) k m
	)
      )
(*;Printf.printf "asd5\n";*)
    in
      try
	bf 0 !k !m;
	false
      with
	| Found ->
	    true
  in
  let l = ref 0l in
  let r = ref Int32.max_int in
    while !l < Int32.sub !r 1l do
      let m = Int32.div (Int32.add !l !r) 2l in
	if solve m
	then r := m
	else l := m
    done;
    if debug then Printf.printf "QWE %ld\n" !r;
    ignore (solve !r);
    for i = 0 to n - 1 do
      if res.(i) > 0
      then Printf.printf "%d " (i + 1)
    done;
    Printf.printf "\n"

exception Found

let debug = false

let _ =
  let sqr x = x * x in
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make n (0, 0) in
  let es = Array.make (n * n) (0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%d " (fun s -> s) in
      let y = Scanf.bscanf sb "%d " (fun s -> s) in
	p.(i) <- (x, y)
    done;
    for i = 0 to n - 1 do
      let (x1, y1) = p.(i) in
	for j = i + 1 to n - 1 do
	  let (x2, y2) = p.(j) in
	  let d =
	    Int32.add
	      (Int32.of_int (sqr (x2 - x1)))
	      (Int32.of_int (sqr (y2 - y1)))
	  in
	    es.(i * n + j) <- (d, i, j)
	done
    done;
    let cmp (x, _, _) (y, _, _) = Int32.compare y x in
      Array.sort cmp es
  in
  let res = Array.make n 0 in
  let solve d =
    if debug then Printf.printf "solve %ld\n" d;
    let m =
      let m = ref 0 in
	while !m < n * n &&
	  (let (d', _, _) = es.(!m) in d' > d )
	do
	  incr m
	done;
	!m
    in
    let es' = Array.make n [] in
    let () =
      for i = 0 to n - 1 do
	res.(i) <- 0
      done;
      for i = 0 to m - 1 do
	let (_d, u, v) = es.(i) in
if debug then Printf.printf "edge %d %d  %ld\n" u v _d;
	  es'.(u) <- v :: es'.(u);
	  es'.(v) <- u :: es'.(v)
      done
    in
    let es = Array.map Array.of_list es' in
    let vs = ref [] in
    let k = ref k in
    let m = ref m in
    let () =
      for i = 0 to n - 1 do
	if Array.length es.(i) = 0
	then ()
	else if Array.length es.(i) = 1 && Array.length es.(es.(i).(0)) = 1
	then (
	  if i < es.(i).(0) then (
    (*Printf.printf "remove %d-%d\n" i es.(i).(0);*)
	    res.(i) <- 1;
	    decr k;
	    decr m;
	  )
	) else if Array.length es.(i) = 1
	then ()
	else vs := i :: !vs
      done
    in
    let vs = Array.of_list !vs in
(*Printf.printf "asd2 %ld %d\n" d (Array.length vs);*)
    let rec bf p k m =
      if debug then Printf.printf "bf %d %d %d\n" p k m;
      if k >= 0 then (
	if m = 0 then (
	  let k = ref k in
	    if !k = 0
	    then raise Found;
	    for i = 0 to n - 1 do
	      if res.(i) = 0 then (
		res.(i) <- 1;
		decr k;
		if !k = 0
		then raise Found
	      )
	    done;
	    assert false
	) else if p < Array.length vs then (
	  let v = vs.(p) in
if debug then Printf.printf "asd4 %d\n" v;
	    if res.(v) = 0 then (
	      (let m' = ref m in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
if debug then Printf.printf "rem %d %d\n" v u;
		       m' := !m' - 1
		     );
		 done;
		 res.(v) <- 1;
		 bf (p + 1) (k - 1) !m';
		 res.(v) <- 0
	      );
(*Printf.printf "asd45\n";*)
	      (let m' = ref m in
	       let k' = ref k in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
		       decr k';
		       for i = 0 to Array.length es.(u) - 1 do
			 let w = es.(u).(i) in
			   if res.(w) = 0 then (
if debug then Printf.printf "rem2 %d %d %d\n" v u w;
			     m' := !m' - 1
			   );
		       done;
		     );
		     res.(u) <- res.(u) + 1
		 done;
		 bf (p + 1) !k' !m';
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     res.(u) <- res.(u) - 1
		 done;
	      )
	    )
	)
      )
(*;Printf.printf "asd5\n";*)
    in
      try
	bf 0 !k !m;
	false
      with
	| Found ->
	    true
  in
  let l = ref 0l in
  let r = ref Int32.max_int in
    while !l < Int32.sub !r 1l do
      let m = Int32.div (Int32.add !l !r) 2l in
	if solve m
	then r := m
	else l := m
    done;
    if debug then Printf.printf "QWE %ld\n" !r;
    ignore (solve !r);
    for i = 0 to n - 1 do
      if res.(i) > 0
      then Printf.printf "%d " (i + 1)
    done;
    Printf.printf "\n"

exception Found

let debug = false

let _ =
  let sqr x = x * x in
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make n (0, 0) in
  let es = Array.make (n * n) (0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%d " (fun s -> s) in
      let y = Scanf.bscanf sb "%d " (fun s -> s) in
	p.(i) <- (x, y)
    done;
    for i = 0 to n - 1 do
      let (x1, y1) = p.(i) in
	for j = i + 1 to n - 1 do
	  let (x2, y2) = p.(j) in
	  let d =
	    Int32.add
	      (Int32.of_int (sqr (x2 - x1)))
	      (Int32.of_int (sqr (y2 - y1)))
	  in
	    es.(i * n + j) <- (d, i, j)
	done
    done;
    let cmp (x, _, _) (y, _, _) = Int32.compare y x in
      Array.sort cmp es
  in
  let res = Array.make n 0 in
  let solve d =
    if debug then Printf.printf "solve %ld\n" d;
    let m =
      let m = ref 0 in
	while !m < n * n &&
	  (let (d', _, _) = es.(!m) in d' > d )
	do
	  incr m
	done;
	!m
    in
    let es' = Array.make n [] in
    let () =
      for i = 0 to n - 1 do
	res.(i) <- 0
      done;
      for i = 0 to m - 1 do
	let (_d, u, v) = es.(i) in
if debug then Printf.printf "edge %d %d  %ld\n" u v _d;
	  es'.(u) <- v :: es'.(u);
	  es'.(v) <- u :: es'.(v)
      done
    in
    let es = Array.map Array.of_list es' in
    let vs = ref [] in
    let k = ref k in
    let m = ref m in
    let () =
      for i = 0 to n - 1 do
	if Array.length es.(i) = 0
	then ()
	else if Array.length es.(i) = 1 && Array.length es.(es.(i).(0)) = 1
	then (
	  if i < es.(i).(0) then (
    (*Printf.printf "remove %d-%d\n" i es.(i).(0);*)
	    res.(i) <- 1;
	    decr k;
	    decr m;
	  )
	) else if Array.length es.(i) = 1
	then ()
	else vs := i :: !vs
      done
    in
    let vs = Array.of_list !vs in
(*Printf.printf "asd2 %ld %d\n" d (Array.length vs);*)
    let rec bf p k m =
      if debug then Printf.printf "bf %d %d %d\n" p k m;
      assert (m >= 0);
      if k >= 0 then (
	if m = 0 then (
	  let k = ref k in
	    if !k = 0
	    then raise Found;
	    for i = 0 to n - 1 do
	      if res.(i) = 0 then (
		res.(i) <- 1;
		decr k;
		if !k = 0
		then raise Found
	      )
	    done;
	    assert false
	) else if p < Array.length vs then (
	  let v = vs.(p) in
if debug then Printf.printf "asd4 %d\n" v;
	    if res.(v) = 0 then (
	      (let m' = ref m in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
if debug then Printf.printf "rem %d %d\n" v u;
		       m' := !m' - 1
		     );
		 done;
		 res.(v) <- 1;
		 bf (p + 1) (k - 1) !m';
		 res.(v) <- 0
	      );
(*Printf.printf "asd45\n";*)
	      (let m' = ref m in
	       let k' = ref k in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
		       decr k';
		       for i = 0 to Array.length es.(u) - 1 do
			 let w = es.(u).(i) in
			   if res.(w) = 0 then (
if debug then Printf.printf "rem2 %d %d %d\n" v u w;
			     m' := !m' - 1
			   );
		       done;
		     );
		     res.(u) <- res.(u) + 1
		 done;
		 if !k' <= k - 2
		 then bf (p + 1) !k' !m';
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     res.(u) <- res.(u) - 1
		 done;
	      )
	    ) else bf (p + 1) k m
	)
      )
(*;Printf.printf "asd5\n";*)
    in
      try
	bf 0 !k !m;
	false
      with
	| Found ->
	    true
  in
  let l = ref 0l in
  let r = ref Int32.max_int in
    while !l < Int32.sub !r 1l do
      let m = Int32.div (Int32.add !l !r) 2l in
	if solve m
	then r := m
	else l := m
    done;
    if debug then Printf.printf "QWE %ld\n" !r;
    ignore (solve !r);
    for i = 0 to n - 1 do
      if res.(i) > 0
      then Printf.printf "%d " (i + 1)
    done;
    Printf.printf "\n"

exception Found

let debug = false

let _ =
  let sqr x = x * x in
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make n (0, 0) in
  let es = Array.make (n * n) (0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%d " (fun s -> s) in
      let y = Scanf.bscanf sb "%d " (fun s -> s) in
	p.(i) <- (x, y)
    done;
    for i = 0 to n - 1 do
      let (x1, y1) = p.(i) in
	for j = i + 1 to n - 1 do
	  let (x2, y2) = p.(j) in
	  let d =
	    Int32.add
	      (Int32.of_int (sqr (x2 - x1)))
	      (Int32.of_int (sqr (y2 - y1)))
	  in
	    es.(i * n + j) <- (d, i, j)
	done
    done;
    let cmp (x, _, _) (y, _, _) = Int32.compare y x in
      Array.sort cmp es
  in
  let res = Array.make n 0 in
  let solve d =
    if debug then Printf.printf "solve %ld\n" d;
    let m =
      let m = ref 0 in
	while !m < n * n &&
	  (let (d', _, _) = es.(!m) in d' > d )
	do
	  incr m
	done;
	!m
    in
    let es' = Array.make n [] in
    let () =
      for i = 0 to n - 1 do
	res.(i) <- 0
      done;
      for i = 0 to m - 1 do
	let (_d, u, v) = es.(i) in
if debug then Printf.printf "edge %d %d  %ld\n" u v _d;
	  es'.(u) <- v :: es'.(u);
	  es'.(v) <- u :: es'.(v)
      done
    in
    let es = Array.map Array.of_list es' in
    let vs = ref [] in
    let k = ref k in
    let m = ref m in
    let () =
      for i = 0 to n - 1 do
	if Array.length es.(i) = 0
	then ()
	else if Array.length es.(i) = 1 && Array.length es.(es.(i).(0)) = 1
	then (
	  if i < es.(i).(0) then (
    (*Printf.printf "remove %d-%d\n" i es.(i).(0);*)
	    res.(i) <- 1;
	    decr k;
	    decr m;
	  )
	) else if Array.length es.(i) = 1
	then ()
	else vs := i :: !vs
      done
    in
    let vs = Array.of_list !vs in
(*Printf.printf "asd2 %ld %d\n" d (Array.length vs);*)
    let rec bf p k m =
      if debug then Printf.printf "bf %d %d %d\n" p k m;
      assert (m >= 0);
      if k >= 0 then (
	if m = 0 then (
	  let k = ref k in
	    if !k = 0
	    then raise Found;
	    for i = 0 to n - 1 do
	      if res.(i) = 0 then (
		res.(i) <- 1;
		decr k;
		if !k = 0
		then raise Found
	      )
	    done;
	    assert false
	) else if p < Array.length vs then (
	  let v = vs.(p) in
if debug then Printf.printf "asd4 %d\n" v;
	    if res.(v) = 0 then (
	      (let m' = ref m in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
if debug then Printf.printf "rem %d %d\n" v u;
		       m' := !m' - 1
		     );
		 done;
		 res.(v) <- 1;
		 bf (p + 1) (k - 1) !m';
		 res.(v) <- 0
	      );
(*Printf.printf "asd45\n";*)
	      (let m' = ref m in
	       let k' = ref k in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
		       decr k';
		       for i = 0 to Array.length es.(u) - 1 do
			 let w = es.(u).(i) in
			   if res.(w) = 0 then (
if debug then Printf.printf "rem2 %d %d %d\n" v u w;
			     m' := !m' - 1
			   );
		       done;
		     );
		     res.(u) <- res.(u) + 1
		 done;
		 bf (p + 1) !k' !m';
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     res.(u) <- res.(u) - 1
		 done;
	      )
	    )
	)
      )
(*;Printf.printf "asd5\n";*)
    in
      try
	bf 0 !k !m;
	false
      with
	| Found ->
	    true
  in
  let l = ref 0l in
  let r = ref Int32.max_int in
    while !l < Int32.sub !r 1l do
      let m = Int32.div (Int32.add !l !r) 2l in
	if solve m
	then r := m
	else l := m
    done;
    if debug then Printf.printf "QWE %ld\n" !r;
    ignore (solve !r);
    for i = 0 to n - 1 do
      if res.(i) > 0
      then Printf.printf "%d " (i + 1)
    done;
    Printf.printf "\n"

exception Found

let _ =
  let sqr x = x * x in
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make n (0, 0) in
  let es = Array.make (n * n) (0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%d " (fun s -> s) in
      let y = Scanf.bscanf sb "%d " (fun s -> s) in
	p.(i) <- (x, y)
    done;
    for i = 0 to n - 1 do
      let (x1, y1) = p.(i) in
	for j = i + 1 to n - 1 do
	  let (x2, y2) = p.(j) in
	  let d =
	    Int32.add
	      (Int32.of_int (sqr (x2 - x1)))
	      (Int32.of_int (sqr (y2 - y1)))
	  in
	    es.(i * n + j) <- (d, i, j)
	done
    done;
    let cmp (x, _, _) (y, _, _) = Int32.compare y x in
      Array.sort cmp es
  in
  let res = Array.make n 0 in
  let solve d =
    (*Printf.printf "solve %ld\n" d;*)
    let m =
      let m = ref 0 in
	while !m < n &&
	  (let (d', _, _) = es.(!m) in d' > d )
	do
	  incr m
	done;
	!m
    in
    let es' = Array.make n [] in
    let () =
      for i = 0 to n - 1 do
	res.(i) <- 0
      done;
      for i = 0 to m - 1 do
	let (_, u, v) = es.(i) in
    (*Printf.printf "edge %d %d\n" u v;*)
	  es'.(u) <- v :: es'.(u);
	  es'.(v) <- u :: es'.(v)
      done
    in
    let es = Array.map Array.of_list es' in
    let vs = ref [] in
    let k = ref k in
    let m = ref m in
    let () =
      for i = 0 to n - 1 do
	if Array.length es.(i) = 0
	then ()
	else if Array.length es.(i) = 1 && Array.length es.(es.(i).(0)) = 1
	then (
	  if i < es.(i).(0) then (
    (*Printf.printf "remove %d-%d\n" i es.(i).(0);*)
	    res.(i) <- 1;
	    decr k;
	    decr m;
	  )
	) else if Array.length es.(i) = 1
	then ()
	else vs := i :: !vs
      done
    in
    let vs = Array.of_list !vs in
(*Printf.printf "asd2 %ld %d\n" d (Array.length vs);*)
    let rec bf p k m =
(*Printf.printf "bf %d %d %d\n" p k m;*)
      if k >= 0 then (
	if m = 0 then (
	  let k = ref k in
	    if !k = 0
	    then raise Found;
	    for i = 0 to n - 1 do
	      if res.(i) = 0 then (
		res.(i) <- 1;
		decr k;
		if !k = 0
		then raise Found
	      )
	    done;
	    assert false
	) else if p < Array.length vs then (
	  let v = vs.(p) in
(*Printf.printf "asd4 %d\n" v;*)
	    if res.(v) = 0 then (
	      (let m' = ref m in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
(*Printf.printf "rem %d %d\n" v u;*)
		       m' := !m' - 1
		     );
		 done;
		 res.(v) <- 1;
		 bf (p + 1) (k - 1) !m';
		 res.(v) <- 0
	      );
(*Printf.printf "asd45\n";*)
	      (let m' = ref m in
		for i = 0 to Array.length es.(v) - 1 do
		  let u = es.(v).(i) in
		    if res.(u) = 0 then (
		      for i = 0 to Array.length es.(u) - 1 do
			let w = es.(u).(i) in
			  if res.(w) = 0 then (
(*Printf.printf "rem2 %d %d %d\n" v u w;*)
			    m' := !m' - 1
			  );
		      done;
		    );
		    res.(u) <- res.(u) + 1
		done;
		bf (p + 1) (k - Array.length es.(v)) !m';
		for i = 0 to Array.length es.(v) - 1 do
		  let u = es.(v).(i) in
		    res.(u) <- res.(u) - 1
		done;
	      )
	    )
	)
      )
(*;Printf.printf "asd5\n";*)
    in
      try
	bf 0 !k !m;
	false
      with
	| Found ->
	    true
  in
  let l = ref 0l in
  let r = ref Int32.max_int in
    while !l < Int32.sub !r 1l do
      let m = Int32.div (Int32.add !l !r) 2l in
	if solve m
	then r := m
	else l := m
    done;
(*Printf.printf "QWE %ld\n" !r;*)
    ignore (solve !r);
    for i = 0 to n - 1 do
      if res.(i) > 0
      then Printf.printf "%d " (i + 1)
    done;
    Printf.printf "\n"

exception Found

let debug = false

let _ =
  let sqr x = x * x in
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let p = Array.make n (0, 0) in
  let es = Array.make (n * n) (0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let x = Scanf.bscanf sb "%d " (fun s -> s) in
      let y = Scanf.bscanf sb "%d " (fun s -> s) in
	p.(i) <- (x, y)
    done;
    for i = 0 to n - 1 do
      let (x1, y1) = p.(i) in
	for j = i + 1 to n - 1 do
	  let (x2, y2) = p.(j) in
	  let d =
	    Int32.add
	      (Int32.of_int (sqr (x2 - x1)))
	      (Int32.of_int (sqr (y2 - y1)))
	  in
	    es.(i * n + j) <- (d, i, j)
	done
    done;
    let cmp (x, _, _) (y, _, _) = Int32.compare y x in
      Array.sort cmp es
  in
  let res = Array.make n 0 in
  let solve d =
    if debug then Printf.printf "solve %ld\n" d;
    let m =
      let m = ref 0 in
	while !m < n * n &&
	  (let (d', _, _) = es.(!m) in d' > d )
	do
	  incr m
	done;
	!m
    in
    let es' = Array.make n [] in
    let () =
      for i = 0 to n - 1 do
	res.(i) <- 0
      done;
      for i = 0 to m - 1 do
	let (_d, u, v) = es.(i) in
if debug then Printf.printf "edge %d %d  %ld\n" u v _d;
	  es'.(u) <- v :: es'.(u);
	  es'.(v) <- u :: es'.(v)
      done
    in
    let es = Array.map Array.of_list es' in
    let vs = ref [] in
    let k = ref k in
    let m = ref m in
    let () =
      for i = 0 to n - 1 do
	if Array.length es.(i) = 0
	then ()
	else if Array.length es.(i) = 1 && Array.length es.(es.(i).(0)) = 1
	then (
	  if i < es.(i).(0) then (
    (*Printf.printf "remove %d-%d\n" i es.(i).(0);*)
	    res.(i) <- 1;
	    decr k;
	    decr m;
	  )
	) else if Array.length es.(i) = 1
	then ()
	else vs := i :: !vs
      done
    in
    let vs = Array.of_list !vs in
(*Printf.printf "asd2 %ld %d\n" d (Array.length vs);*)
    let rec bf p k m =
      if debug then Printf.printf "bf %d %d %d\n" p k m;
      assert (m >= 0);
      if k >= 0 then (
	if m = 0 then (
	  let k = ref k in
	    if !k = 0
	    then raise Found;
	    for i = 0 to n - 1 do
	      if res.(i) = 0 then (
		res.(i) <- 1;
		decr k;
		if !k = 0
		then raise Found
	      )
	    done;
	    assert false
	) else if p < Array.length vs then (
	  let v = vs.(p) in
if debug then Printf.printf "asd4 %d\n" v;
	    if res.(v) = 0 then (
	      (let m' = ref m in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
if debug then Printf.printf "rem %d %d\n" v u;
		       m' := !m' - 1
		     );
		 done;
		 res.(v) <- 1;
		 bf (p + 1) (k - 1) !m';
		 res.(v) <- 0
	      );
(*Printf.printf "asd45\n";*)
	      (let m' = ref m in
	       let k' = ref k in
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     if res.(u) = 0 then (
		       decr k';
		       for i = 0 to Array.length es.(u) - 1 do
			 let w = es.(u).(i) in
			   if res.(w) = 0 then (
if debug then Printf.printf "rem2 %d %d %d\n" v u w;
			     m' := !m' - 1
			   );
		       done;
		     );
		     res.(u) <- res.(u) + 1
		 done;
		 if !m' <= m - 2
		 then bf (p + 1) !k' !m';
		 for i = 0 to Array.length es.(v) - 1 do
		   let u = es.(v).(i) in
		     res.(u) <- res.(u) - 1
		 done;
	      )
	    ) else bf (p + 1) k m
	)
      )
(*;Printf.printf "asd5\n";*)
    in
      try
	bf 0 !k !m;
	false
      with
	| Found ->
	    true
  in
  let l = ref 0l in
  let r = ref Int32.max_int in
    while !l < Int32.sub !r 1l do
      let m = Int32.div (Int32.add !l !r) 2l in
	if solve m
	then r := m
	else l := m
    done;
    if debug then Printf.printf "QWE %ld\n" !r;
    ignore (solve !r);
    for i = 0 to n - 1 do
      if res.(i) > 0
      then Printf.printf "%d " (i + 1)
    done;
    Printf.printf "\n"


You are given a bracket sequence consisting of $$$n$$$ characters '(' and/or )'. You perform several operations with it.During one operation, you choose the shortest prefix of this string (some amount of first characters of the string) that is good and remove it from the string.The prefix is considered good if one of the following two conditions is satisfied:  this prefix is a regular bracket sequence;  this prefix is a palindrome of length at least two. A bracket sequence is called regular if it is possible to obtain a correct arithmetic expression by inserting characters '+' and '1' into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not.The bracket sequence is called palindrome if it reads the same back and forth. For example, the bracket sequences )), (( and )(() are palindromes, while bracket sequences (), )( and ))( are not palindromes.You stop performing the operations when it's not possible to find a good prefix. Your task is to find the number of operations you will perform on the given string and the number of remaining characters in the string.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the length of the bracket sequence. The second line of the test case contains $$$n$$$ characters '(' and/or ')' — the bracket sequence itself. It is guaranteed that the sum of $$$n$$$ over all test cases do not exceed $$$5 \cdot 10^5$$$ ($$$\sum n \le 5 \cdot 10^5$$$).

For each test case, print two integers $$$c$$$ and $$$r$$$ — the number of operations you will perform on the given bracket sequence and the number of characters that remain in the string after performing all operations.

exception Breakloop ;;

let isPalin tab i j =
  try
    for k = 0 to (j-i)/2 do
      if tab.(i+k) <> tab.(j-k) then (raise Breakloop)
    done;
    true
  with Breakloop -> false

;;
let isReg tab k count =
  if tab.(k) = '(' then
    incr count ;
  if tab.(k) = ')' then
    decr count ;
  !count
;;

let findShor tab i =
  let pretoto = (isReg tab i (ref 0)) in
  let rec aux tab i j count canReg= match j with
    |x when x >= (Array.length tab) -> prerr_endline "tooHigh" ; -1
    |_ when canReg && (isReg tab j (ref count)) = 0 -> prerr_endline "isReg" ; j
    |_ when isPalin tab i j -> prerr_endline "isPalin" ; j
    |_ -> let toto = isReg tab j (ref count) in let temp = if toto<0 then false else true in
        aux tab i (j+1) toto temp
  in aux tab i (i+1) (if pretoto = -1 then -5 else 1) true
;;

let finish tab =
  let rec aux tab ind count = match (findShor tab ind) with
    | -1 -> (count,Array.length tab - ind)
    |x when x = Array.length tab -1 -> (count+1,0)
    |x -> aux tab (x+1) (count+1)
  in aux tab 0 0
;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let len = int_of_string (input_line stdin) in
  let str = input_line stdin in
  let tab = Array.make len '(' in
  for j = 0 to len -1 do
    tab.(j)<- str.[j]
  done;
  let a,b = finish tab in
  print_endline ((string_of_int a)^" "^(string_of_int b))
done;
;;

let isPalin tab i j =
  let toto = ref true in
  for k = 0 to j-i do
    if tab.(i+k) <> tab.(j-k) then toto := false
  done;
  !toto
;;
let isReg tab i j =
  let toto = ref true in
  let count = ref 0 in
  for k = i to j do
    if !toto && tab.(k) = '(' then
      incr count ;
    if !toto && tab.(k) = ')' then
      decr count ;
    if !count < 0 then toto := false
  done ;
  if !count = 0 then !toto else false
;;

let findShor tab i =
  let rec aux tab i j = match j with
    |x when x >= (Array.length tab) -> -1
    |_-> if isPalin tab i j || (tab.(i) = '(' && j-i mod 2 =1 && isReg tab i j) then j else aux tab i (j+1)
  in aux tab i (i+1)
;;

let finish tab =
  let rec aux tab ind count = match (findShor tab ind) with
    | -1 -> (count,Array.length tab - ind)
    |x when x = Array.length tab -1 -> (count+1,0)
    |x -> aux tab (x+1) (count+1)
  in aux tab 0 0
;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let len = int_of_string (input_line stdin) in
  let str = input_line stdin in
  let tab = Array.make len '(' in
  for j = 0 to len -1 do
    tab.(j)<- str.[j]
  done;
  let a,b = finish tab in
  print_endline ((string_of_int a)^" "^(string_of_int b))
done;
;;

let isPalin tab i j =
  let toto = ref true in
  for k = 0 to j-i do
    if tab.(i+k) <> tab.(j-k) then toto := false
  done;
  !toto
;;
let isReg tab i j =
  let toto = ref true in
  let count = ref 0 in
  for k = i to j do
    if !toto && tab.(k) = '(' then
      incr count ;
    if !toto && tab.(k) = ')' then
      decr count ;
    if !count < 0 then toto := false
  done ;
  if !count = 0 then !toto else false
;;

let findShor tab i =
  let rec aux tab i j = match j with
    |x when x >= (Array.length tab) -> -1
    |_-> if isPalin tab i j || (tab.(i) = '(' && isReg tab i j) then j else aux tab i (j+2)
  in aux tab i (i+2)
;;

let finish tab =
  let rec aux tab ind count = match (findShor tab ind) with
    | -1 -> (count,Array.length tab - ind)
    |x when x = Array.length tab -1 -> (count+1,0)
    |x -> aux tab (x+1) (count+1)
  in aux tab 0 0
;;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let len = int_of_string (input_line stdin) in
  let str = input_line stdin in
  let tab = Array.make len '(' in
  for j = 0 to len -1 do
    tab.(j)<- str.[j]
  done;
  let a,b = finish tab in
  print_endline ((string_of_int a)^" "^(string_of_int b))
done;
;;


A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.Determine if it is possible for the islanders to arrange the statues in the desired order.

The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands. The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently placed on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct. The third line contains n space-separated integers bi (0 ≤ bi ≤ n - 1) — the desired statues of the ith island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.

Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.

NoteIn the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.In the second sample, the islanders can simply move statue 1 from island 1 to island 2.In the third sample, no sequence of movements results in the desired position.

let print_tab tab w =
   print_endline w

let main () =
   let n = Scanf.scanf "%d" (fun i -> i) in
   let tab = Array.make (n-1) 0 in
   let tab' = Array.make (n-1) 0 in
      let j = ref 0 in
      for i = 1 to n do
         let a = Scanf.scanf " %d" (fun i -> i) in
            if a <> 0 then (tab.(!j) <- a;incr j);
      done;
      let j = ref 0 in
      for i = 1 to n do
         let a = Scanf.scanf " %d" (fun i -> i) in
            if a <> 0 then (tab'.(!j) <- a;incr j);
      done;
   let i = ref (-1) and y = tab.(0) in
   let flag = ref true in
      Array.iteri (fun j x -> if x = y then i := j) tab';
      for j = 0 to n-2 do
         flag := !flag && (tab'.((!i + j) mod (n-1)) = tab.(j));
      done;
      print_string (if !flag then "YES" else "NO");;

main ()

open Printf
open Scanf

let rec forall i j f = (i>j) || ((f i) && forall (i+1) j f)

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

  let fill_x a =
    let x = Array.make (n-1) 0 in
    let rec loop i j = if i=n then () else
	if a.(i) = 0 then loop (i+1) j
	else (
	  x.(j) <- a.(i);
	  loop (i+1) (j+1)
	)
    in
    loop 0 0;
    let rec find1 i = if x.(i) = 1 then i else find1 (i+1) in
    (x, find1 0)
  in

  let (ax,ai) = fill_x a in
  let (bx,bi) = fill_x b in

  let ans = forall 0 (n-2) (fun i -> ax.((ai+i) mod (n-1)) = bx.((bi+i) mod (n-1))) in

  if ans then printf "YES\n" else printf "NO\n"


You are given two strings $$$s$$$ and $$$t$$$, both consisting only of lowercase Latin letters.The substring $$$s[l..r]$$$ is the string which is obtained by taking characters $$$s_l, s_{l + 1}, \dots, s_r$$$ without changing the order.Each of the occurrences of string $$$a$$$ in a string $$$b$$$ is a position $$$i$$$ ($$$1 \le i \le |b| - |a| + 1$$$) such that $$$b[i..i + |a| - 1] = a$$$ ($$$|a|$$$ is the length of string $$$a$$$).You are asked $$$q$$$ queries: for the $$$i$$$-th query you are required to calculate the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.

The first line contains three integer numbers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n, m \le 10^3$$$, $$$1 \le q \le 10^5$$$) — the length of string $$$s$$$, the length of string $$$t$$$ and the number of queries, respectively. The second line is a string $$$s$$$ ($$$|s| = n$$$), consisting only of lowercase Latin letters. The third line is a string $$$t$$$ ($$$|t| = m$$$), consisting only of lowercase Latin letters. Each of the next $$$q$$$ lines contains two integer numbers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the arguments for the $$$i$$$-th query.

Print $$$q$$$ lines — the $$$i$$$-th line should contain the answer to the $$$i$$$-th query, that is the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.

NoteIn the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.

module type StringView = sig
  type t
  val length: t -> int
  val get: t -> int -> char
end

module MakeStringViewUtil(S: StringView) = struct
  let iteri (f: int -> char -> unit) (src: S.t): unit =
    for i = 0 to (S.length src - 1) do f i (S.get src i) done
end

module MakeStringViewEqual(A: StringView)(B: StringView) = struct
  let equal (a: A.t) (b: B.t) =
    if A.length a != B.length b then false
    else
      let rec f i =
        if i = A.length a then true
        else if A.get a i != B.get b i then false
        else f (i + 1)
      in f 0
end

module MakeStringTruncated(S: StringView) = struct
  type t = S.t * int * int
  let create src l r = (src, l, r)
  let length (_, l, r) = r - l
  let get (src, l, _) i = S.get src (l + i)
end

module StringTruncated = MakeStringTruncated(String)

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

module MatchSet = Set.Make(Match)

module MakeStringFindMultiple(S: StringView) = struct

  let do_find (callback: int -> unit) (term: string) (src: S.t): unit =
    let tlen = String.length term in

    let module STruncated = MakeStringTruncated(S) in
    let module EqStrSTrunc = MakeStringViewEqual(String)(STruncated) in
    let module SUtil = MakeStringViewUtil(S) in

    SUtil.iteri (fun i c ->
        if i <= S.length src - tlen &&
           c = String.get term 0 then
          if EqStrSTrunc.equal term (STruncated.create src i (i + tlen))
          then callback i
      ) src

  let count (term: string) (src: S.t): int =
    let ret = ref 0 in
    do_find (fun _ -> ret := !ret + 1) term src;
    !ret

  let all_matches (term: string) (src: S.t): MatchSet.t =
    let ret = ref MatchSet.empty in
    do_find (fun i -> ret := MatchSet.add i !ret) term src;
    !ret
end

module StringFindMultiple = MakeStringFindMultiple(String)
module StringTruncatedFindMultiple = MakeStringFindMultiple(StringTruncated)

let find_opt (tbl: ('k, 'v) Hashtbl.t) (key: 'k): 'v option =
  let ret = Hashtbl.find_all tbl key in
  match ret with
  | [a] -> Some a
  | _ -> None

module MakeSetBipart(S: Set.S) = struct
  let bipart ((l, r): S.elt * S.elt) (src: S.t): S.t =
    let (_, present, ge) = S.split l src in
    let ge = if present then S.add l ge else ge in
    let (le, _, _) = S.split r ge in
    le
end

module type OrderedContainer = sig
  type t
  type elt
  val length: t -> int
  val get: int -> t -> elt
end

module MakeBisearch(C: OrderedContainer) = struct
  let find_first_index_satisfying (callback: C.elt -> bool) (cont: C.t): int option =
    let rec f l r =
      if l >= r
      then if l = r && C.get l cont |> callback then Some l else None
      else
        let m = (l + r) / 2 in
        if C.get m cont |> callback then f l m else f (m + 1) r
    in f 0 (C.length cont - 1)
end

module MakeSetToOrderedArray(S: Set.S)(E: Set.OrderedType with type t = S.elt) = struct
  let convert (src: S.t): E.t array =
    let l = S.elements src in
    let ret = Array.of_list l in
    Array.sort E.compare ret;
    ret
end

module MatchArray = struct
  type elt = Match.t
  type t = elt array
  let length = Array.length
  let get i a = Array.get a i
end

module MatchSetBipart = MakeSetBipart(MatchSet)
module MatchSetToOrderedArray = MakeSetToOrderedArray(MatchSet)(Match)
module MatchArrayBisearch = MakeBisearch(MatchArray)

let option_get ~(default: 'a) (x: 'a option): 'a =
  match x with | Some x -> x | None -> default

let _ =
  let n, m, q = Scanf.scanf " %d %d %d" (fun x y z -> x, y, z) in
  let s, t = Scanf.scanf " %s %s" (fun x y -> x, y) in
  let matches_set = StringFindMultiple.all_matches t s in
  let matches = MatchSetToOrderedArray.convert matches_set in
  (* let cache = Hashtbl.create 102539 in *)
  for i = 1 to q do
    let entry = Scanf.scanf " %d %d" (fun x y -> x, y) in
    let l, r = entry in
    let lb = matches |>
             MatchArrayBisearch.find_first_index_satisfying (fun x -> x >= l - 1) |>
             option_get ~default:(Array.length matches) in
    let rb = matches |>
             MatchArrayBisearch.find_first_index_satisfying (fun x -> x > r - String.length t) |>
             option_get ~default:(Array.length matches) in
    (* Printf.printf "-:%d ::%d\n" lb rb; *)
    rb - lb |> max 0 |> string_of_int |> print_endline

    (* matches |>
     * MatchSetBipart.bipart (l - 1, r - String.length t + 1) |>
     * MatchSet.cardinal |>
     * string_of_int |> print_endline *)
    (* let strunc = StringTruncated.create s (l - 1) r in
     * StringTruncatedFindMultiple.count t strunc
     * |> string_of_int |> print_endline *)
  done

module type StringView = sig
  type t
  val length: t -> int
  val get: t -> int -> char
end

module MakeStringViewUtil(S: StringView) = struct
  let iteri (f: int -> char -> unit) (src: S.t): unit =
    for i = 0 to (S.length src - 1) do f i (S.get src i) done
end

module MakeStringViewEqual(A: StringView)(B: StringView) = struct
  let equal (a: A.t) (b: B.t) =
    if A.length a != B.length b then false
    else
      let rec f i =
        if i = A.length a then true
        else if A.get a i != B.get b i then false
        else f (i + 1)
      in f 0
end

module MakeStringTruncated(S: StringView) = struct
  type t = S.t * int * int
  let create src l r = (src, l, r)
  let length (_, l, r) = r - l
  let get (src, l, _) i = S.get src (l + i)
end

module StringTruncated = MakeStringTruncated(String)

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

module MatchSet = Set.Make(Match)

module MakeStringFindMultiple(S: StringView) = struct

  let do_find (callback: int -> unit) (term: string) (src: S.t): unit =
    let tlen = String.length term in

    let module STruncated = MakeStringTruncated(S) in
    let module EqStrSTrunc = MakeStringViewEqual(String)(STruncated) in
    let module SUtil = MakeStringViewUtil(S) in

    SUtil.iteri (fun i c ->
        if i <= S.length src - tlen &&
           c = String.get term 0 then
          if EqStrSTrunc.equal term (STruncated.create src i (i + tlen))
          then callback i
      ) src

  let count (term: string) (src: S.t): int =
    let ret = ref 0 in
    do_find (fun _ -> ret := !ret + 1) term src;
    !ret

  let all_matches (term: string) (src: S.t): MatchSet.t =
    let ret = ref MatchSet.empty in
    do_find (fun i -> ret := MatchSet.add i !ret) term src;
    !ret
end

module StringFindMultiple = MakeStringFindMultiple(String)
module StringTruncatedFindMultiple = MakeStringFindMultiple(StringTruncated)

let find_opt (tbl: ('k, 'v) Hashtbl.t) (key: 'k): 'v option =
  let ret = Hashtbl.find_all tbl key in
  match ret with
  | [a] -> Some a
  | _ -> None

module MakeSetBipart(S: Set.S) = struct
  let bipart ((l, r): S.elt * S.elt) (src: S.t): S.t =
    let (_, present, ge) = S.split l src in
    let ge = if present then S.add l ge else ge in
    let (le, _, _) = S.split r ge in
    le
end

module MatchSetBipart = MakeSetBipart(MatchSet)

let _ =
  let n, m, q = Scanf.scanf " %d %d %d" (fun x y z -> x, y, z) in
  let s, t = Scanf.scanf " %s %s" (fun x y -> x, y) in
  let matches = StringFindMultiple.all_matches t s in
  (* let cache = Hashtbl.create 102539 in *)
  for i = 1 to q do
    let entry = Scanf.scanf " %d %d" (fun x y -> x, y) in
    let l, r = entry in
    matches |>
    MatchSetBipart.bipart (l - 1, r - String.length t + 1) |>
    MatchSet.cardinal |>
    string_of_int |> print_endline
    (* let strunc = StringTruncated.create s (l - 1) r in
     * StringTruncatedFindMultiple.count t strunc
     * |> string_of_int |> print_endline *)
  done

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let q = Scanf.bscanf sb "%d " (fun s -> s) in
  let s = Scanf.bscanf sb "%s " (fun s -> s) in
  let t = Scanf.bscanf sb "%s " (fun s -> s) in
  let a = Array.make (n + 1) 0 in
    for i = 0 to n - m do
      let b = ref true in
	for j = 0 to m - 1 do
	  if s.[i + j] <> t.[j]
	  then b := false
	done;
	if !b
	then a.(i + 1) <- a.(i) + 1
	else a.(i + 1) <- a.(i)
    done;
    for i = 1 to q do
      let l = Scanf.bscanf sb "%d " (fun s -> s) in
      let r = Scanf.bscanf sb "%d " (fun s -> s) in
      let res =
	let r = r - m + 1 in
	  if l <= r
	  then a.(r) - a.(l - 1)
	  else 0
      in
	Printf.printf "%d\n" res
    done;

module type StringView = sig
  type t
  val length: t -> int
  val get: t -> int -> char
end

module MakeStringViewUtil(S: StringView) = struct
  let iteri (f: int -> char -> unit) (src: S.t): unit =
    for i = 0 to (S.length src - 1) do f i (S.get src i) done
end

module MakeStringViewEqual(A: StringView)(B: StringView) = struct
  let equal (a: A.t) (b: B.t) =
    if A.length a != B.length b then false
    else
      let rec f i =
        if i = A.length a then true
        else if A.get a i != B.get b i then false
        else f (i + 1)
      in f 0
end

module MakeStringTruncated(S: StringView) = struct
  type t = S.t * int * int
  let create src l r = (src, l, r)
  let length (_, l, r) = r - l
  let get (src, l, _) i = S.get src (l + i)
end

module StringTruncated = MakeStringTruncated(String)

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

module MatchSet = Set.Make(Match)

module MakeStringFindMultiple(S: StringView) = struct

  let do_find (callback: int -> unit) (term: string) (src: S.t): unit =
    let tlen = String.length term in

    let module STruncated = MakeStringTruncated(S) in
    let module EqStrSTrunc = MakeStringViewEqual(String)(STruncated) in
    let module SUtil = MakeStringViewUtil(S) in

    SUtil.iteri (fun i c ->
        if i <= S.length src - tlen &&
           c = String.get term 0 then
          if EqStrSTrunc.equal term (STruncated.create src i (i + tlen))
          then callback i
      ) src

  let count (term: string) (src: S.t): int =
    let ret = ref 0 in
    do_find (fun _ -> ret := !ret + 1) term src;
    !ret

  let all_matches (term: string) (src: S.t): MatchSet.t =
    let ret = ref MatchSet.empty in
    do_find (fun i -> ret := MatchSet.add i !ret) term src;
    !ret
end

module StringFindMultiple = MakeStringFindMultiple(String)
module StringTruncatedFindMultiple = MakeStringFindMultiple(StringTruncated)

let find_opt (tbl: ('k, 'v) Hashtbl.t) (key: 'k): 'v option =
  let ret = Hashtbl.find_all tbl key in
  match ret with
  | [a] -> Some a
  | _ -> None

module MakeSetBipart(S: Set.S) = struct
  let bipart ((l, r): S.elt * S.elt) (src: S.t): S.t =
    let (_, present, ge) = S.split l src in
    let ge = if present then S.add l ge else ge in
    let (le, _, _) = S.split r ge in
    le
end

module type OrderedContainer = sig
  type t
  type elt
  val length: t -> int
  val get: int -> t -> elt
end

module MakeBisearch(C: OrderedContainer) = struct
  let find_first_index_satisfying (callback: C.elt -> bool) (cont: C.t): int option =
    let rec f l r =
      if l >= r
      then if l = r && C.get l cont |> callback then Some l else None
      else
        let m = (l + r) / 2 in
        if C.get m cont |> callback then f l m else f (m + 1) r
    in f 0 (C.length cont - 1)
end

module MakeSetToOrderedArray(S: Set.S)(E: Set.OrderedType with type t = S.elt) = struct
  let convert (src: S.t): E.t array =
    let l = S.elements src in
    let ret = Array.of_list l in
    Array.sort E.compare ret;
    ret
end

module MatchArray = struct
  type elt = Match.t
  type t = elt array
  let length = Array.length
  let get i a = Array.get a i
end

module MatchSetBipart = MakeSetBipart(MatchSet)
module MatchSetToOrderedArray = MakeSetToOrderedArray(MatchSet)(Match)
module MatchArrayBisearch = MakeBisearch(MatchArray)

let option_get ~(default: 'a) (x: 'a option): 'a =
  match x with | Some x -> x | None -> default

let _ =
  let n, m, q = Scanf.scanf " %d %d %d" (fun x y z -> x, y, z) in
  let s, t = Scanf.scanf " %s %s" (fun x y -> x, y) in
  let matches_set = StringFindMultiple.all_matches t s in
  let matches = MatchSetToOrderedArray.convert matches_set in
  (* let cache = Hashtbl.create 102539 in *)
  for i = 1 to q do
    let entry = Scanf.scanf " %d %d" (fun x y -> x, y) in
    let l, r = entry in
    let lb = matches |>
             MatchArrayBisearch.find_first_index_satisfying (fun x -> x >= l - 1) |>
             option_get ~default:0 in
    let rb = matches |>
             MatchArrayBisearch.find_first_index_satisfying (fun x -> x <= r - String.length t) |>
             option_get ~default:(-1) in
    (rb - lb + 1) |> max 0 |> string_of_int |> print_endline

    (* matches |>
     * MatchSetBipart.bipart (l - 1, r - String.length t + 1) |>
     * MatchSet.cardinal |>
     * string_of_int |> print_endline *)
    (* let strunc = StringTruncated.create s (l - 1) r in
     * StringTruncatedFindMultiple.count t strunc
     * |> string_of_int |> print_endline *)
  done


You are given array a with n integers and m queries. The i-th query is given with three integers li, ri, xi.For the i-th query find any position pi (li ≤ pi ≤ ri) so that api ≠ xi.

The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of elements in a and the number of queries. The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a. Each of the next m lines contains three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106) — the parameters of the i-th query.

Print m lines. On the i-th line print integer pi — the position of any number not equal to xi in segment [li, ri] or the value  - 1 if there is no such number.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
  let _ =
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
  in
  let b = Array.make n (-1) in
  let _ =
    for i = n - 2 downto 0 do
      if a.(i) = a.(i + 1) then (
	b.(i) <- b.(i + 1)
      ) else (
	b.(i) <- i + 1
      )
    done;
  in
    for t = 1 to m do
      let l = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let r = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let x = Scanf.bscanf sb "%d " (fun s -> s) in
      let res =
	if a.(l) <> x
	then l + 1
	else if b.(l) < 0 || b.(l) > r
	then -1
	else b.(l) + 1
      in
	Printf.printf "%d\n" res
    done;


You are given three integers $$$n$$$, $$$l$$$, and $$$r$$$. You need to construct an array $$$a_1,a_2,\dots,a_n$$$ ($$$l\le a_i\le r$$$) such that $$$\gcd(i,a_i)$$$ are all distinct or report there's no solution.Here $$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows. The first line contains three integers $$$n$$$, $$$l$$$, $$$r$$$ ($$$1 \le n \le 10^5$$$, $$$1\le l\le r\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, if there is no solution, print "NO" (without quotes). You can print letters in any case (upper or lower). Otherwise, print "YES" (without quotes). In the next line, print $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ — the array you construct. If there are multiple solutions, you may output any.

(* From @Darooha's code (https://codeforces.com/profile/Darooha) *)

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let long x = Int64.of_int x
let short x = Int64.to_int x

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

(* Main Code file *)

let rec sum = function
        | [] -> 0
        | h::t -> h + sum(t)

let rec all = function
        | [] -> true
        | h::t -> if h then all(t) else false

let rec any = function
        | [] -> false
        | h::t -> if h then true else any(t)

let divis a b = b mod a = 0

let gen l r i = if r / (i + 1) = (l - 1) / (i + 1) then -1 else r / (i + 1) * (i + 1)
let output_with_space x = printf "%d " x
let eqm x = x = -1

let () =
    let t = read_int() in
    for i=1 to t do
        let n = read_int() in
        let l = read_int() in
        let r = read_int() in
        let arr = Array.init n (gen l r) in
        let no = any (Array.to_list (Array.map eqm arr)) in
        if no then printf "NO\n"
        else (printf "YES\n");

        if not no then Array.iter output_with_space arr;

        print_newline()
    done;


Recently n students from city S moved to city P to attend a programming camp.They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea.i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it).

The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000). Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students. Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every  condition li - 1 ≤ li holds. The sum of n over all test cases doesn't exceed 1000. Note that in hacks you have to set t = 1.

For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea.

NoteThe example contains 2 tests:  During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second.  During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea.

(* Codeforces 920 B Tea Queue - train  *)

(* you can use either gr or rdln but not both *)
let gr () = Scanf.scanf " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let grc () = Scanf.scanf " %c" (fun i -> i);;
let rdln() = input_line stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

let rec readlist n acc = match n with
	| 0 -> acc
	| _ -> readlist (n -1) (gr() :: acc);;

let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;

type student =
	{ l : int; r : int };;

let rec read_students n = match n with
	| 0 -> []
	| _ ->
			let ll = gr () in
			let rr = gr () in
			let st = { l = ll; r = rr } in
			st :: read_students (n - 1);;

let rec tea_times tm stl = match stl with
	| [] -> []
	| st :: t ->
			if st.l > tm then
				st.l :: tea_times (st.l +1) t
			else
			if st.r < tm then
				0 :: tea_times tm t
			else
				tm :: tea_times (tm + 1) t;;

let main() =
	let t = gr () in
	for i = 1 to t do
		begin
			let n = gr () in
			let stl = read_students n in
			let tt = tea_times 0 stl in
			let _ = printlisti tt in
			print_string "\n"
		end
	done;;

main();;


Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.

The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops. Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.

Output one integer — the number of pairs of bishops which attack each other.

NoteIn the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.

open Printf
open Scanf

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

let rec sum i j f ac = if i>j then ac else sum (i+1) j f (ac ++ (f i))

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

let () =
  let n = read_int () in
  let k = 1000 in
  let diag1 = Array.make (2*k) 0 in
  let diag2 = Array.make (2*k) 0 in

  for i=1 to n do
    let (x,y) = read_pair() in
    let (x,y) = (x-1,y-1) in
    diag1.(x+y) <- diag1.(x+y) + 1;
    diag2.(x-y+k-1) <- diag2.(x-y+k-1) + 1;
  done;

  let count ar = sum 0 (2*k-1) (fun i -> ((long (ar.(i)) -- 1L) ** (long (ar.(i)))) // 2L) 0L in

  printf "%Ld\n" ((count diag1) ++ (count diag2))


You are given an array $$$a_1, a_2, \ldots, a_n$$$ of positive integers. A good pair is a pair of indices $$$(i, j)$$$ with $$$1 \leq i, j \leq n$$$ such that, for all $$$1 \leq k \leq n$$$, the following equality holds:$$$$$$ |a_i - a_k| + |a_k - a_j| = |a_i - a_j|, $$$$$$ where $$$|x|$$$ denotes the absolute value of $$$x$$$.Find a good pair. Note that $$$i$$$ can be equal to $$$j$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ for all test cases is at most $$$2 \cdot 10^5$$$.

For each test case, print a single line with two space-separated indices $$$i$$$ and $$$j$$$ which form a good pair of the array. The case $$$i=j$$$ is allowed. It can be shown that such a pair always exists. If there are multiple good pairs, print any of them.

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

let findmm tab = let min = ref (-1,0) in let max = ref (-1,0) in
  for i = 0 to Array.length tab -1 do
    if fst (!min) = -1 then (min := (i,tab.(i)) ; max := (i,tab.(i)) )
    else ( if snd (!min) > tab.(i) then (min := (i,tab.(i))) ;  if snd (!max) < tab.(i) then(max := (i,tab.(i)))  )
  done ; (fst (!min)+1, fst (!max) +1)
;;

let n = int_of_string(input_line stdin) in
for i = 0 to n-1 do
  let len = int_of_string(input_line stdin) in
  let str = (input_line stdin) in
  let tab = parse str len in
  let max,min = findmm tab in
  print_endline (string_of_int max ^" "^ string_of_int min)
done ;;


Let's define $$$S(x)$$$ to be the sum of digits of number $$$x$$$ written in decimal system. For example, $$$S(5) = 5$$$, $$$S(10) = 1$$$, $$$S(322) = 7$$$.We will call an integer $$$x$$$ interesting if $$$S(x + 1) < S(x)$$$. In each test you will be given one integer $$$n$$$. Your task is to calculate the number of integers $$$x$$$ such that $$$1 \le x \le n$$$ and $$$x$$$ is interesting.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — number of test cases. Then $$$t$$$ lines follow, the $$$i$$$-th line contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$) for the $$$i$$$-th test case.

Print $$$t$$$ integers, the $$$i$$$-th should be the answer for the $$$i$$$-th test case.

NoteThe first interesting number is equal to $$$9$$$.

for i=1 to read_int() do
print_string(let x = read_int() in string_of_int ((x + 1) / 10));
print_string("\n")
done

for i=1 to read_int() do
print_string(let x = read_int() in string_of_int ((x + 1) / 10));
done


Alice gave Bob two integers $$$a$$$ and $$$b$$$ ($$$a > 0$$$ and $$$b \ge 0$$$). Being a curious boy, Bob wrote down an array of non-negative integers with $$$\operatorname{MEX}$$$ value of all elements equal to $$$a$$$ and $$$\operatorname{XOR}$$$ value of all elements equal to $$$b$$$.What is the shortest possible length of the array Bob wrote?Recall that the $$$\operatorname{MEX}$$$ (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the $$$\operatorname{XOR}$$$ of an array is the bitwise XOR of all the elements of the array.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a \leq 3 \cdot 10^5$$$; $$$0 \leq b \leq 3 \cdot 10^5$$$) — the $$$\operatorname{MEX}$$$ and $$$\operatorname{XOR}$$$ of the array, respectively.

For each test case, output one (positive) integer — the length of the shortest array with $$$\operatorname{MEX}$$$ $$$a$$$ and $$$\operatorname{XOR}$$$ $$$b$$$. We can show that such an array always exists.

NoteIn the first test case, one of the shortest arrays with $$$\operatorname{MEX}$$$ $$$1$$$ and $$$\operatorname{XOR}$$$ $$$1$$$ is $$$[0, 2020, 2021]$$$.In the second test case, one of the shortest arrays with $$$\operatorname{MEX}$$$ $$$2$$$ and $$$\operatorname{XOR}$$$ $$$1$$$ is $$$[0, 1]$$$.It can be shown that these arrays are the shortest arrays possible.

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))
let () =
  let msize = 300001 in
  let xorsum = Array.make msize 0 in
  for i=1 to msize-1 do
    xorsum.(i) <- xorsum.(i-1) lxor i
  done;
  let cases = read_int () in
  for case = 1 to cases do
    let (mex,xor) = read_pair() in
    let natural_xor = xorsum.(mex-1) in
    let need_one = natural_xor lxor xor in
    let answer = if xor = natural_xor then mex
      else if need_one > mex then mex+1
      else if need_one = mex then mex+2
      else mex+1
    in
    printf "%d\n" answer
  done

open Printf
open Scanf

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

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))
let () =
  let cases = read_int () in
  for case = 1 to cases do
    let (mex,xor) = read_pair() in

    let answer = if xor = xor_sum 0 (mex-1) (fun i -> i) then mex else mex+1 in
    printf "%d\n" answer
  done


You are given n points on a plane. All the points are distinct and no three of them lie on the same line. Find the number of parallelograms with the vertices at the given points.

The first line of the input contains integer n (1 ≤ n ≤ 2000) — the number of points. Each of the next n lines contains two integers (xi, yi) (0 ≤ xi, yi ≤ 109) — the coordinates of the i-th point.

Print the only integer c — the number of parallelograms with the vertices at the given points.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let x = Array.make n 0 in
  let y = Array.make n 0 in
  let _ =
    for i = 0 to n - 1 do
      x.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      y.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
    done;
  in
  let ht = Hashtbl.create (n * n) in
  let add key =
    try
      let c = Hashtbl.find ht key in
	Hashtbl.replace ht key (c + 1)
    with
      | Not_found ->
	  Hashtbl.replace ht key 1
  in
  let _ =
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	if i <> j then (
	  let dx = x.(i) - x.(j) in
	  let dy = y.(i) - y.(j) in
	    add (dx, dy)
	)
      done
    done;
  in
  let res = ref 0 in
    Hashtbl.iter
      (fun _key c ->
	 res := !res + c * (c - 1) / 2
      ) ht;
    res := !res / 4;
    Printf.printf "%d\n" !res

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let x = Array.make n 0 in
  let y = Array.make n 0 in
  let _ =
    for i = 0 to n - 1 do
      x.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
      y.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
    done;
  in
  let ht = Hashtbl.create 10 in
  let add key =
    try
      let c = Hashtbl.find ht key in
	Hashtbl.replace ht key (c + 1)
    with
      | Not_found ->
	  Hashtbl.replace ht key 1
  in
  let _ =
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
	if i <> j then (
	  let dx = x.(i) - x.(j) in
	  let dy = y.(i) - y.(j) in
	    add (dx, dy)
	)
      done
    done;
  in
  let res = ref 0 in
    res := !res / 4;
    Hashtbl.iter
      (fun _key c ->
	 res := !res + c * (c - 1) / 2
      ) ht;
    Printf.printf "%d\n" !res


The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same.After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code.Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it.The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.

The first line of the input contains four integers x, y, x0, y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right. The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.

Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.

NoteIn the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: .

let xs, ys, x0, y0, s = Scanf.scanf "%d %d %d %d %s" (fun a b c d e -> (a,b,c-1,d-1,e)) ;;
let next (x,y) ch = match ch with
    | 'L' -> (x, max 0 (y-1))
    | 'R' -> (x, min (ys-1) (y+1))
    | 'U' -> (max 0 (x-1), y)
    | 'D' -> (min (xs-1) (x+1), y)
let v = Array.make_matrix xs ys false ;;
let rec f i (x,y) =
    if i < String.length s then (
        let mine =
        if not v.(x).(y) then (
            v.(x).(y) <- true;
            1
        ) else
            0
        in
        Printf.printf "%d " mine ;
        mine + ( f (i+1) (next (x,y) s.[i]) )
    ) else 0
;;
print_int( xs * ys - f 0 (x0, y0) )

let new_place (x,y) (x0,y0) l =
    let new_xy = ref (x0,y0) in
    (match l with
        | 'R' -> if y0 < y then new_xy := (x0, y0+1)
        | 'L' -> if y0 > 1 then new_xy := (x0, y0-1)
        | 'U' -> if x0 > 1 then new_xy := (x0-1,y0)
        | 'D' -> if x0 < x then new_xy := (x0+1,y0));
    !new_xy

let main () =
    let (lim,pos,w) = Scanf.scanf "%d %d %d %d %s" (fun i j k l m -> ((i,j),(ref (k,l)),m)) and mat = Array.make_matrix 555 555 true and compt = ref 0 in
    for i = 0 to String.length w - 1 do
        let (a,b) = !pos in
            Printf.printf (if mat.(a).(b) = true
                then (mat.(a).(b) <- false; incr compt; "1 ")
                else "0 ");
        pos := new_place lim !pos (w.[i]);
    done;
    Printf.printf "%d" ((fst lim) * (snd lim) - !compt);;

main ();;

(* start: 9:58 *)
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 xm = read_int () in
  let ym = read_int () in
  let x0 = read_int () in
  let y0 = read_int () in

  let s = read_string () in

  let ns = String.length s in

  let bomb = Array.make_matrix (xm+1) (ym+1) 1 in

  let rec loop x y i times_died =
    if i = ns then times_died else (
      let times_died = times_died + bomb.(x).(y) in
	printf "%d " bomb.(x).(y);
	bomb.(x).(y) <- 0;
	let (x,y) =
	  match s.[i] with
	    | 'U' -> (max 1 (x-1), y)
	    | 'D' -> (min xm (x+1), y)
	    | 'L' -> (x, max 1 (y-1))
	    | 'R' -> (x, min ym (y+1))
	    | _ -> failwith "bad char"
	in
	loop x y (i+1) times_died
    )
  in

  let times_died = loop x0 y0 0 0 in

  printf "%d\n" (xm*ym - times_died)


Fox Ciel just designed a puzzle game called "Polygon"! It is played using triangulations of a regular n-edge polygon. The goal is to transform one triangulation to another by some tricky rules.Triangulation of an n-edge poylgon is a set of n - 3 diagonals satisfying the condition that no two diagonals share a common internal point.For example, the initial state of the game may look like (a) in above figure. And your goal may look like (c). In each step you can choose a diagonal inside the polygon (but not the one of edges of the polygon) and flip this diagonal. Suppose you are going to flip a diagonal a – b. There always exist two triangles sharing a – b as a side, let's denote them as a – b – c and a – b – d. As a result of this operation, the diagonal a – b is replaced by a diagonal c – d. It can be easily proven that after flip operation resulting set of diagonals is still a triangulation of the polygon.So in order to solve above case, you may first flip diagonal 6 – 3, it will be replaced by diagonal 2 – 4. Then you flip diagonal 6 – 4 and get figure (c) as result.Ciel just proved that for any starting and destination triangulations this game has a solution. She wants you to solve it in no more than 20 000 steps for any puzzle satisfying n ≤ 1000.

The first line contain an integer n (4 ≤ n ≤ 1000), number of edges of the regular polygon.  Then follows two groups of (n - 3) lines describing the original triangulation and goal triangulation. Description of each triangulation consists of (n - 3) lines. Each line contains 2 integers ai and bi (1 ≤ ai, bi ≤ n), describing a diagonal ai – bi. It is guaranteed that both original and goal triangulations are correct (i. e. no two diagonals share a common internal point in both of these triangulations).

First, output an integer k (0 ≤ k ≤ 20, 000): number of steps. Then output k lines, each containing 2 integers ai and bi: the endpoints of a diagonal you are going to flip at step i. You may output ai and bi in any order. If there are several possible solutions, output any of them.

NoteSample test 2 is discussed above and shown on the picture.

module IntSet = Set.Make(struct
  type t = int
  let compare = compare
end)

type dir = Forward | Backward

(* normalise a triangulation to have all
 * diagonals coming from node 1 *)
let normal t s dir =
  (* try updating edge a-b to point to c *)
  let update a b c d =
    (* normalise this diagonal *)
    let (a, b) = if a > b then (b, a) else (a, b) in

    if Hashtbl.mem t (a, b) then begin
      (* if this is a diagonal, get the other triangle
       * points *)
      let (x, y) = Hashtbl.find t (a, b) in
      let old = if x = d then y else x in
      let v = if old > c then (c, old) else (old, c) in
      Hashtbl.replace t (a, b) v;

      (* this edge now has a triangle containing node 0
       * we'll flip it next time around *)
      if c = 0 then Stack.push (a, b) s
    end
  in

  let rec aux a =
    if Stack.is_empty s then a
    else begin
      let cur = Stack.pop s in
      let (l, r) = Hashtbl.find t cur in
      (* flip edge *)
      Hashtbl.remove t cur;
      Hashtbl.add t (l, r) cur;
      (* update adjacent triangles *)
      let (x, y) = cur in
      update x l r y; update x r l y;
      update y l r x; update y r l x;
      match dir with
      | Forward  -> aux (cur :: a)
      | Backward -> aux ((l, r) :: a)
    end
  in
  aux []

(* for each diagonal, find the
 * two triangles to which it belongs *)
let triang n t a s =
  let e = Hashtbl.create (Array.length t) in
  Array.iter (fun (x, y) ->
    (* get the node by following the diagonal
    * (or edge) prior to x-y and the one
    * after x-y *)
    let lx, _, gx = IntSet.split x a.(y) in
    let l = if IntSet.is_empty lx
    then IntSet.max_elt gx
    else IntSet.max_elt lx
    and r = if IntSet.is_empty gx
    then IntSet.min_elt lx
    else IntSet.min_elt gx
    in
    if l = 0 || r = 0 then Stack.push (x, y) s;
    let v = if l > r then (r, l) else (l, r) in
    Hashtbl.add e (x,y) v
  ) t;
  e

let read_diags n a =
  Array.init (n - 3) (fun _ ->
    Scanf.scanf "%d %d\n" (fun x y ->
      let x = x - 1 and y = y - 1 in
      (* increment the degree of the edges *)
      a.(x) <- IntSet.add y a.(x);
      a.(y) <- IntSet.add x a.(y);
      (* add the edge to the table with no triangles *)
      if x > y then (y, x) else (x, y)
    )
  )

let make_deg n =
  Array.init n (fun i ->
    IntSet.union
      (IntSet.singleton ((i + 1) mod n))
      (IntSet.singleton ((i - 1 + n) mod n))
  )

let () = Scanf.scanf "%d\n" (fun n ->
  let print s =
    print_string (String.concat "" (List.map (fun (x, y) -> Printf.sprintf "%d %d\n" (x+1) (y+1)) s))
  in
  (* degree of each node *)
  let odeg = make_deg n
  and tdeg = make_deg n
  in
  (* edge lists *)
  let orig = read_diags n odeg
  and targ = read_diags n tdeg in

  let s = Stack.create () in
  let otri = triang n orig odeg s in
  let fsteps = List.rev (normal otri s Forward) in

  let s = Stack.create () in
  let ttri = triang n targ tdeg s in
  let bsteps = normal ttri s Backward in
  Printf.printf "%d\n" (List.length fsteps + List.length bsteps);
  print fsteps; print bsteps;
)


Recently, Tokitsukaze found an interesting game. Tokitsukaze had $$$n$$$ items at the beginning of this game. However, she thought there were too many items, so now she wants to discard $$$m$$$ ($$$1 \le m \le n$$$) special items of them.These $$$n$$$ items are marked with indices from $$$1$$$ to $$$n$$$. In the beginning, the item with index $$$i$$$ is placed on the $$$i$$$-th position. Items are divided into several pages orderly, such that each page contains exactly $$$k$$$ positions and the last positions on the last page may be left empty.Tokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded.    Consider the first example from the statement: $$$n=10$$$, $$$m=4$$$, $$$k=5$$$, $$$p=[3, 5, 7, 10]$$$. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices $$$3$$$ and $$$5$$$. After, the first page remains to be special. It contains $$$[1, 2, 4, 6, 7]$$$, Tokitsukaze discards the special item with index $$$7$$$. After, the second page is special (since it is the first page containing a special item). It contains $$$[9, 10]$$$, Tokitsukaze discards the special item with index $$$10$$$. Tokitsukaze wants to know the number of operations she would do in total.

The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le n \le 10^{18}$$$, $$$1 \le m \le 10^5$$$, $$$1 \le m, k \le n$$$) — the number of items, the number of special items to be discarded and the number of positions in each page. The second line contains $$$m$$$ distinct integers $$$p_1, p_2, \ldots, p_m$$$ ($$$1 \le p_1 < p_2 < \ldots < p_m \le n$$$) — the indices of special items which should be discarded.

Print a single integer — the number of operations that Tokitsukaze would do in total.

NoteFor the first example:  In the first operation, Tokitsukaze would focus on the first page $$$[1, 2, 3, 4, 5]$$$ and discard items with indices $$$3$$$ and $$$5$$$;  In the second operation, Tokitsukaze would focus on the first page $$$[1, 2, 4, 6, 7]$$$ and discard item with index $$$7$$$;  In the third operation, Tokitsukaze would focus on the second page $$$[9, 10]$$$ and discard item with index $$$10$$$. For the second example, Tokitsukaze would focus on the second page $$$[6, 7, 8, 9, 10]$$$ and discard all special items at once.

let () =
    let split s =
        let rec loop i cur flag acc =
            if i = String.length s then ( if flag then cur :: acc else acc) else (
                match flag, s.[i] with
                | true,  ' ' -> loop (i + 1) "" false (cur :: acc)
                | false, ' ' -> loop (i + 1) "" false acc
                | true,  v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
                | false, v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
            )
        in
        List.map Int64.of_string (List.rev (loop 0 "" false []))
    in
    let main () =
        let n, m, k = Scanf.sscanf (read_line ()) "%Ld %d %Ld" (fun n m k -> n, m, k) in
        let p = split (read_line ()) in
        let (+@) = Int64.add in
        let (-@) = Int64.sub in
        let ( *@) = Int64.mul in
        let (/@) = Int64.div in
        let page k f f2 = (f -@ f2 -@ Int64.one) /@ k *@ k +@ f2 +@ k in
        let rec shred f2 cnt p =
            match p with
            | [] -> cnt, []
            | hd :: tl -> if Int64.compare hd f2 > 0 then cnt, p else shred f2 (cnt + 1) tl
        in
        let imin a b = if Int64.compare a b > 0 then b else a in
        let rec loop n m f2 p count =
            if m = 0 then count else
                match p with
                | [] -> count
                | f :: _ ->
                    let f2 = if Int64.compare f f2 > 0 then imin n (page k f f2) else f2 in
                    let c, p = shred f2 0 p in
                    loop n (m - c) (min n (f2 +@ Int64.of_int c)) p (count + 1)
        in
        Printf.printf "%d\n" (loop n m k p 0)
    in
    main ()


Suppose you have two points $$$p = (x_p, y_p)$$$ and $$$q = (x_q, y_q)$$$. Let's denote the Manhattan distance between them as $$$d(p, q) = |x_p - x_q| + |y_p - y_q|$$$.Let's say that three points $$$p$$$, $$$q$$$, $$$r$$$ form a bad triple if $$$d(p, r) = d(p, q) + d(q, r)$$$.Let's say that an array $$$b_1, b_2, \dots, b_m$$$ is good if it is impossible to choose three distinct indices $$$i$$$, $$$j$$$, $$$k$$$ such that the points $$$(b_i, i)$$$, $$$(b_j, j)$$$ and $$$(b_k, k)$$$ form a bad triple.You are given an array $$$a_1, a_2, \dots, a_n$$$. Calculate the number of good subarrays of $$$a$$$. A subarray of the array $$$a$$$ is the array $$$a_l, a_{l + 1}, \dots, a_r$$$ for some $$$1 \le l \le r \le n$$$.Note that, according to the definition, subarrays of length $$$1$$$ and $$$2$$$ are good.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \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, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print the number of good subarrays of array $$$a$$$.

open Scanf
open Printf

let scan_int _ = scanf " %d" (fun x -> x)

let mono [x; y; z] =
  (x <= y && y <= z) || (x >= y && y >= z)

let rec comb k xs = match k, xs with
  | 0, _  -> [[]]
  | _, [] -> []
  | k, (x :: xs) -> let cons x xs = x :: xs in
    List.map (cons x) (comb (k-1) xs) @ comb k xs

let () =
  for _ = 1 to read_int () do
    let n = scan_int () in
    let a = Array.init n scan_int in
    let bad l len =
      l + len > n || List.exists mono @@ comb 3 @@ Array.to_list @@ Array.sub a l len
    in
    let ans = ref 0 in
    for l = 0 to n-1 do
      let rec test len = if not (bad l len) then (incr ans; test (len+1)) in
      test 1
    done;
    printf "%d\n" !ans
  done


A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from $$$1$$$ to $$$9$$$ (inclusive) are round.For example, the following numbers are round: $$$4000$$$, $$$1$$$, $$$9$$$, $$$800$$$, $$$90$$$. The following numbers are not round: $$$110$$$, $$$707$$$, $$$222$$$, $$$1001$$$.You are given a positive integer $$$n$$$ ($$$1 \le n \le 10^4$$$). Represent the number $$$n$$$ as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number $$$n$$$ as a sum of the least number of terms, each of which is a round number.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is a line containing an integer $$$n$$$ ($$$1 \le n \le 10^4$$$).

Print $$$t$$$ answers to the test cases. Each answer must begin with an integer $$$k$$$ — the minimum number of summands. Next, $$$k$$$ terms must follow, each of which is a round number, and their sum is $$$n$$$. The terms can be printed in any order. If there are several answers, print any of them.

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

let rec f x =
  if x = 0 then [], 0
  else if x < 10 then [x], 1
  else let y = int_of_float (10. ** (float_of_int(int_of_float(log10 (float_of_int x))))) in
    let z = f (x - (x / y) * y) in
      (x / y) * y :: fst z, 1 + snd z
;;

open List
open Printf

let solve () =
  let n = re () in
  let m = f n in
  printf "%d \n" (snd m);
  iter (printf "%d ") (fst m);
  printf "\n"
;;

let _ =
  let t = re () in
  for i = 1 to t do
    solve ()
  done
;;

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

open Printf
open List

let run () =
  let n = scan_int () in
  let rec f x b =
    if x = 0 then []
    else begin
      let rest = f (x/10) (b * 10) in
      let tmp = (x mod 10) * b in
      if tmp > 0 then tmp :: rest
      else rest
    end in
  let lst = f n 1 in
  printf "%d\n" (List.length lst);
  List.iter (printf "%d ") lst;
  print_endline ""
;;

let () =
  let t = scan_int () in
  for i = 1 to t do
    run ()
  done
;;

(* Codeforces 1352 0 Sum of Round Numbers comp   *)

(* you can use either gr or rdln but not both *)
let gr () = Scanf.scanf " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

(* reading reversed list *)
let rec readlist n acc = match n with
	| 0 -> acc
	| _ -> readlist (n -1) (gr() :: acc);;
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printlists ls = List.iter (fun s -> (print_string s; print_string " ")) ls;;

let rec round_list acc p10 n = match n with
    | 0 -> acc
    | _ ->
			let r = n mod 10 in
			let n_acc = if r == 0 then acc else (r*p10) :: acc in
			let p10 = p10 * 10 in
			let n = n / 10 in
			round_list n_acc p10 n;;

let do_one () =
	let n = gr () in
	let lr = round_list [] 1 n in
	let _ = Printf.printf "%d\n" (List.length lr) in
	let _ = printlisti lr in
	print_string "\n";;

let do_all t =
    for i = 1 to t do
        do_one ()
    done;;

let main() =
	let t = gr () in
	do_all t;;

main();;

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

let rec f x =
  if x = 0 then []
  else if x < 10 then [x]
  else let y = int_of_float (10. ** (float_of_int(int_of_float(log10 (float_of_int x))))) in
    (x / y) * y :: f (x - (x / y) * y)
;;

open List
open Printf

let solve () =
  let n = re () in
  let m = int_of_float(log10 (float_of_int n)) + 1 in
  printf "%d \n" m;
  iter (printf "%d ") (f n);
  printf "\n"
;;

let _ =
  let t = re () in
  for i = 1 to t do
    solve ()
  done
;;

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

let rec f x =
  if x = 0 then []
  else if x < 10 then [x]
  else let y = int_of_float (10. ** (float_of_int(int_of_float(log10 (float_of_int x))))) in
    (x / y) * y :: f (x - (x / y) * y)
;;

open List
open Printf

let solve () =
  let n = re () in
  let () = List.iter (printf "%d ") (f n) in
  printf "\n"
;;

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

let rec f x =
  if x = 0 then []
  else if x < 10 then [x]
  else let y = int_of_float (10. ** (float_of_int(int_of_float(log10 (float_of_int x))))) in
    (x / y) * y :: f (x - (x / y) * y)
;;

open List
open Printf

let solve () =
  let n = 4 in
  iter (printf "%d ") (f n);
  printf "\n"
;;

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

let rec f x =
  if x = 0 then []
  else if x < 10 then [x]
  else let y = int_of_float (10. ** (float_of_int(int_of_float(log10 (float_of_int x))))) in
    (x / y) * y :: f (x - (x / y) * y)
;;

let rec print_list = function
[] -> print_string "\n"
| e::l -> print_int e ; print_string " " ; print_list l

let solve () =
  let n = re () in
  print_list (f n)
;;

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

let rec f x =
  if x = 0 then []
  else if x < 10 then [x]
  else let y = int_of_float (10. ** (float_of_int(int_of_float(log10 (float_of_int x))))) in
    (x / y) * y :: f (x - (x / y) * y)
;;

open List
open Printf

let solve () =
  let n = 4 in
  let m = int_of_float(log10 (float_of_int n)) + 1 in
  printf "%d \n" m;
  iter (printf "%d ") (f n);
  printf "\n"
;;


Nikephoros and Polycarpus play rock-paper-scissors. The loser gets pinched (not too severely!).Let us remind you the rules of this game. Rock-paper-scissors is played by two players. In each round the players choose one of three items independently from each other. They show the items with their hands: a rock, scissors or paper. The winner is determined by the following rules: the rock beats the scissors, the scissors beat the paper and the paper beats the rock. If the players choose the same item, the round finishes with a draw.Nikephoros and Polycarpus have played n rounds. In each round the winner gave the loser a friendly pinch and the loser ended up with a fresh and new red spot on his body. If the round finished in a draw, the players did nothing and just played on.Nikephoros turned out to have worked out the following strategy: before the game began, he chose some sequence of items A = (a1, a2, ..., am), and then he cyclically showed the items from this sequence, starting from the first one. Cyclically means that Nikephoros shows signs in the following order: a1, a2, ..., am, a1, a2, ..., am, a1, ... and so on. Polycarpus had a similar strategy, only he had his own sequence of items B = (b1, b2, ..., bk).Determine the number of red spots on both players after they've played n rounds of the game. You can consider that when the game began, the boys had no red spots on them.

The first line contains integer n (1 ≤ n ≤ 2·109) — the number of the game's rounds. The second line contains sequence A as a string of m characters and the third line contains sequence B as a string of k characters (1 ≤ m, k ≤ 1000). The given lines only contain characters "R", "S" and "P". Character "R" stands for the rock, character "S" represents the scissors and "P" represents the paper.

Print two space-separated integers: the numbers of red spots Nikephoros and Polycarpus have.

NoteIn the first sample the game went like this:  R - R. Draw.  P - S. Nikephoros loses.  S - P. Polycarpus loses.  R - P. Nikephoros loses.  P - R. Polycarpus loses.  S - S. Draw.  R - P. Nikephoros loses. Thus, in total Nikephoros has 3 losses (and 3 red spots), and Polycarpus only has 2.

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

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%ld " (fun s -> s) in
  let a = Scanf.bscanf sb "%s " (fun s -> s) in
  let b = Scanf.bscanf sb "%s " (fun s -> s) in
  let m = String.length a in
  let k = String.length b in
  let ares = ref 0 in
  let bres = ref 0 in
    for i = 0 to m * k - 1 do
      let ai = i mod m
      and bi = i mod k in
	match a.[ai], b.[bi] with
	  | 'R', 'S'
	  | 'S', 'P'
	  | 'P', 'R' -> incr bres
	  | 'S', 'R'
	  | 'P', 'S'
	  | 'R', 'P' -> incr ares
	  | _, _ -> ()
    done;
    let ar = Int32.mul (Int32.of_int !ares)
      (Int32.div n (Int32.of_int (m * k)))
    in
    let br = Int32.mul (Int32.of_int !bres)
      (Int32.div n (Int32.of_int (m * k)))
    in
    let ares = ref 0 in
    let bres = ref 0 in
      for i = 0 to Int32.to_int (Int32.rem n (Int32.of_int (m * k))) - 1 do
	let ai = i mod m
	and bi = i mod k in
	  match a.[ai], b.[bi] with
	    | 'R', 'S'
	    | 'S', 'P'
	    | 'P', 'R' -> incr bres
	    | 'S', 'R'
	    | 'P', 'S'
	    | 'R', 'P' -> incr ares
	    | _, _ -> ()
      done;
      let ar = Int32.add ar (Int32.of_int !ares) in
      let br = Int32.add br (Int32.of_int !bres) in
	Printf.printf "%ld %ld\n" ar br


Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \leq n \leq 100$$$; $$$1 \leq b < r \leq n$$$, $$$r+b=n$$$).

For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.

RBR
RRBRBRBRBR
RBRBRBRBRBR
RRRRRBRRRR
RRRBRRRBRR
RRRBRRRBRRR

NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let a,b,c = Scanf.sscanf (input_line stdin) "%d  %d  %d" (fun a b c -> (a,b,c)) in
   let toto = b/(c+1) in
  let titi = ref (b mod (c+1)) in
  let rep = ref "" in
  let tyty = ref (!titi > 0) in
  for i = 0 to c -1 do
    rep := !rep ^ (String.make (toto+(if !tyty then 1 else 0)) 'R');
    rep := !rep ^ (String.make 1 'B') ;
    if !tyty then titi := !titi -1 ; if !titi <= 0 then tyty := false ;
  done;
  rep := !rep ^ (String.make (toto) 'R');
  print_endline (!rep)
done;