You are given a set of n vectors on a plane. For each vector you are allowed to multiply any of its coordinates by -1. Thus, each vector vi = (xi, yi) can be transformed into one of the following four vectors:  vi1 = (xi, yi),  vi2 = ( - xi, yi),  vi3 = (xi,  - yi),  vi4 = ( - xi,  - yi).  You should find two vectors from the set and determine which of their coordinates should be multiplied by -1 so that the absolute value of the sum of the resulting vectors was minimally possible. More formally, you should choose two vectors vi, vj (1 ≤ i, j ≤ n, i ≠ j) and two numbers k1, k2 (1 ≤ k1, k2 ≤ 4), so that the value of the expression |vik1 + vjk2| were minimum.

The first line contains a single integer n (2 ≤ n ≤ 105). Then n lines contain vectors as pairs of integers "xi yi" ( - 10000 ≤ xi, yi ≤ 10000), one pair per line.

Print on the first line four space-separated numbers "i k1 j k2" — the answer to the problem. If there are several variants the absolute value of whose sums is minimum, you can print any of them.

NoteA sum of two vectors v = (xv, yv) and u = (xu, yu) is vector s = v + u = (xv + xu, yv + yu).An absolute value of vector v = (x, y) is number . In the second sample there are several valid answers, such as:(3 1 4 2), (3 1 4 4), (3 4 4 1), (3 4 4 3), (4 1 3 2), (4 1 3 4), (4 2 3 1).

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  (*let s = 1000003 in
  let ht = Array.make s 0 in*)
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    let ht = Hashtbl.create n in
    (*let () =
      for i = 0 to s - 1 do
	ht.(i) <- 0
      done
    in
    let hf x y =
      let r = (x * 20000 + y) mod s in
	if r < 0
	then r + s
	else r
    in*)
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let key = (xi, yi) in
	(*let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1*)
	  try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1
      done
    in
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false*)
		  if Hashtbl.mem ht key
		  then remove := false
	      ) else (
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false*)
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
(*if n = 100000 then*)
if n < 100 && Array.length ps = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m; exit 0);
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  Random.init 666666;
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (dup, di, dj) =
    let dup = ref false in
    let di = ref 0 in
    let dj = ref 0 in
    let ht = Hashtbl.create n in
      for i = 0 to n - 1 do
	if Hashtbl.mem ht a.(i) then (
	  dup := true;
	  di := i;
	  dj := Hashtbl.find ht a.(i)
	);
	Hashtbl.replace ht a.(i) i
      done;
      (!dup, !di, !dj)
  in
  let (_, i, j) =
    if dup
    then (0.0, di, dj)
    else cp2d a
  in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let s = 1000003 in
  let ht = Array.make s 0 in
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    let ht = Hashtbl.create n in
    (*let () =
      for i = 0 to s - 1 do
	ht.(i) <- 0
      done
    in*)
    let hf x y =
      let r = (x * 20000 + y) mod s in
	if r < 0
	then r + s
	else r
    in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let key = (xi, yi) in
	(*let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1*)
	  try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1
      done
    in
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false*)
		  if Hashtbl.mem ht key
		  then remove := false
	      ) else (
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false*)
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
(*if n = 100000 then*)
if n < 100 && Array.length ps = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m; exit 0);
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  Random.init 666666;
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (dup, di, dj) =
    let dup = ref false in
    let di = ref 0 in
    let dj = ref 0 in
    let ht = Hashtbl.create n in
      for i = 0 to n - 1 do
	if Hashtbl.mem ht a.(i) then (
	  dup := true;
	  di := i;
	  dj := Hashtbl.find ht a.(i)
	);
	Hashtbl.replace ht a.(i) i
      done;
      (!dup, !di, !dj)
  in
  let (_, i, j) =
    if dup
    then (0.0, di, dj)
    else cp2d a
  in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let ht = Array.make 1000003 0 in
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    (*let ht = Hashtbl.create n in*)
    let () =
      for i = 0 to 1000003 - 1 do
	ht.(i) <- 0
      done
    in
    let hf x y =
      let r = (x * 20000 + y) mod 1000003 in
	if r < 0
	then r + 1000003
	else r
    in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	(*let key = (xi, yi) in*)
	let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1
	  (*try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1*)
      done
    in
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false
		  (*if Hashtbl.mem ht key
		  then remove := false*)
	      ) else (
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false
		(*let key = xi * 20000 + yi in
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false*)
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m; exit 0);
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  Random.init 666666;
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (_, i, j) = cp2d a in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let rec estimate ps =
    let n = Array.length ps in
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    let ht = Hashtbl.create n in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	  try
	    let c = Hashtbl.find ht (xi, yi) in
	      Hashtbl.replace ht (xi, yi) (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht (xi, yi) 1
      done
    in
    let rs = ref [] in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		  if Hashtbl.mem ht (xi, yi)
		  then remove := false
	      ) else (
		let c = Hashtbl.find ht (xi, yi) in
		  if c <> 1
		  then remove := false
	      )
	    done
	  done;
	  if not !remove
	  then rs := ps.(i) :: !rs
      done;
      if !rs = []
      then d
      else estimate (Array.of_list !rs)
  in
  let n = Array.length ps in
  let b = estimate ps in
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	Hashtbl.add ht (xi, yi) i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    let rs = Hashtbl.find_all ht (xi, yi) in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let _ =
  let sb = Scanf.Scanning.stdib in
  let sb = Scanf.Scanning.from_file "input.txt" in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (_, i, j) =
    if n < 100000 then
    cp2d a
    else (0.0, 0, 0)
  in
  let fd = open_out "output.txt" in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let s = 1000003 in
  let ht = Array.make s 0 in
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    let ht = Hashtbl.create n in
    (*let () =
      for i = 0 to s - 1 do
	ht.(i) <- 0
      done
    in*)
    let hf x y =
      let r = (x * 20000 + y) mod s in
	if r < 0
	then r + s
	else r
    in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let key = (xi, yi) in
	(*let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1*)
	  try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1
      done
    in
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false*)
		  if Hashtbl.mem ht key
		  then remove := false
	      ) else (
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false*)
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
(*if n = 100000 then*)
if n < 100000 && Array.length ps = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m; exit 0);
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  Random.init 666666;
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (_, i, j) = cp2d a in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let s = 1000003 in
  let ht = Array.make s 0 in
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    let ht = Hashtbl.create n in
    (*let () =
      for i = 0 to s - 1 do
	ht.(i) <- 0
      done
    in*)
    let hf x y =
      let r = (x * 20000 + y) mod s in
	if r < 0
	then r + s
	else r
    in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let key = (xi, yi) in
	(*let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1*)
	  try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1
      done
    in
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false*)
		  if Hashtbl.mem ht key
		  then remove := false
	      ) else (
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false*)
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
(*if n = 100000 then*)
if Array.length ps = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m);
if n < 100 && Array.length ps = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m; exit 0);
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  Random.init 666666;
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (_, i, j) = cp2d a in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let ht = Array.make 1000003 0 in
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    (*let ht = Hashtbl.create n in*)
    let () =
      for i = 0 to 1000003 - 1 do
	ht.(i) <- 0
      done
    in
    let hf x y =
      let r = (x * 20000 + y) mod 1000003 in
	if r < 0
	then r + 1000003
	else r
    in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	(*let key = (xi, yi) in*)
	let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1
	  (*try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1*)
      done
    in
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false
		  (*if Hashtbl.mem ht key
		  then remove := false*)
	      ) else (
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false
		(*let key = xi * 20000 + yi in
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false*)
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m; exit 0);
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (_, i, j) = cp2d a in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let s = 4000037 in
  let ht = Array.make s 0 in
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    (*let ht = Hashtbl.create n in*)
    let () =
      for i = 0 to s - 1 do
	ht.(i) <- 0
      done
    in
    let hf x y =
      let r = (x * 20000 + y) mod s in
	if r < 0
	then r + s
	else r
    in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	(*let key = (xi, yi) in*)
	let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1
	  (*try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1*)
      done
    in
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false
		  (*if Hashtbl.mem ht key
		  then remove := false*)
	      ) else (
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false
		(*let key = xi * 20000 + yi in
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false*)
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m; exit 0);
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  Random.init 666666;
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (_, i, j) = cp2d a in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let s = 4000037 in
  let ht = Array.make s 0 in
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    (*let ht = Hashtbl.create n in*)
    let () =
      for i = 0 to s - 1 do
	ht.(i) <- 0
      done
    in
    let hf x y =
      let r = (x * 20000 + y) mod s in
	if r < 0
	then r + s
	else r
    in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	(*let key = (xi, yi) in*)
	let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1
	  (*try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1*)
      done
    in
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false
		  (*if Hashtbl.mem ht key
		  then remove := false*)
	      ) else (
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false
		(*let key = xi * 20000 + yi in
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false*)
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
(*if n = 100000 then*)
if n < 100000 && Array.length ps = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m; exit 0);
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  Random.init 666666;
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (_, i, j) = cp2d a in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let ht = Array.make 1000003 0 in
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    (*let ht = Hashtbl.create n in*)
    let () =
      for i = 0 to 1000003 - 1 do
	ht.(i) <- 0
      done
    in
    let hf x y =
      let r = (x * 20000 + y) mod 1000003 in
	if r < 0
	then r + 1000003
	else r
    in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	(*let key = (xi, yi) in*)
	let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1
	  (*try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1*)
      done
    in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false
		  (*if Hashtbl.mem ht key
		  then remove := false*)
	      ) else (
		(*let key = (xi, yi) in*)
		let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false
		(*let key = xi * 20000 + yi in
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false*)
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (_, i, j) = cp2d a in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))

let cp2d ps =
  let sqr x = x *. x in
  let dist (x1, y1) (x2, y2) =
    sqrt (sqr (x2 -. x1) +. sqr (y2 -. y1))
  in
  let s = 1000003 in
  let ht = Array.make s 0 in
  let rec estimate ps n rs =
    let idx = Random.int n in
    let d = ref infinity in
    let () =
      for i = 0 to n - 1 do
	if i <> idx
	then d := min !d (dist ps.(idx) ps.(i))
      done
    in
    let d = !d in
    let b = d /. 3.0 in
    let ht = Hashtbl.create n in
    (*let () =
      for i = 0 to s - 1 do
	ht.(i) <- 0
      done
    in*)
    let hf x y =
      let r = (x * 20000 + y) mod s in
	if r < 0
	then r + s
	else r
    in
    let () =
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let key = (xi, yi) in
	(*let key = hf xi yi in
	  ht.(key) <- ht.(key) + 1*)
	  try
	    let c = Hashtbl.find ht key in
	      Hashtbl.replace ht key (c + 1)
	  with
	    | Not_found ->
		Hashtbl.replace ht key 1
      done
    in
    let m = ref 0 in
      for i = 0 to n - 1 do
	let (x, y) = ps.(i) in
	let xi = int_of_float (x /. b) in
	let yi = int_of_float (y /. b) in
	let remove = ref true in
	  for xd = -1 to 1 do
	    for yd = -1 to 1 do
	      if not (xd = 0 && yd = 0) then (
		let xi = xi + xd in
		let yi = yi + yd in
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) > 0
		  then remove := false*)
		  if Hashtbl.mem ht key
		  then remove := false
	      ) else (
		let key = (xi, yi) in
		(*let key = hf xi yi in
		  if ht.(key) <> 1
		  then remove := false*)
		let c = Hashtbl.find ht key in
		  if c <> 1
		  then remove := false
	      )
	    done
	  done;
	  if not !remove then (
	    rs.(!m) <- ps.(i);
	    incr m
	  )
      done;
(*if n = 100000 then*)
if n < 100 && Array.length ps = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f %d\n%!" b !m; exit 0);
      if !m = 0
      then d
      else estimate rs !m ps
  in
  let n = Array.length ps in
  let b = estimate (Array.copy ps) n (Array.copy ps) in
if n = 100000 then
(Printf.fprintf (open_out "output.txt") "est %f\n%!" b; exit 0);
  let ht = Hashtbl.create n in
  let () =
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
      (*let key = (xi, yi) in*)
      let key = xi * 20000 + yi in
	Hashtbl.add ht key i
    done
  in
  let d = ref infinity in
  let di = ref (-1) in
  let dj = ref (-1) in
    for i = 0 to n - 1 do
      let (x, y) = ps.(i) in
      let xi = int_of_float (x /. b) in
      let yi = int_of_float (y /. b) in
	for xd = -1 to 1 do
	  for yd = -1 to 1 do
	    let xi = xi + xd in
	    let yi = yi + yd in
	    (*let key = (xi, yi) in*)
	    let key = xi * 20000 + yi in
	    let rs = Hashtbl.find_all ht key in
	      List.iter
		(fun j ->
		   if i <> j then (
		     let dd = dist ps.(i) ps.(j) in
		       if dd < !d then (
			 d := dd;
			 di := i;
			 dj := j;
		       )
		   )
		) rs
	  done
	done;
    done;
    (!d, !di, !dj)

let debug = not true

let _ =
  Random.init 666666;
  let sb =
    if debug
    then Scanf.Scanning.stdib
    else Scanf.Scanning.from_file "input.txt"
  in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0.0, 0.0) in
  let b = Array.make n 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
      let (x, y, r) =
	if x < 0 && y < 0
	then (-x, -y, 4)
	else if x < 0
	then (-x, y, 2)
	else if y < 0
	then (x, -y, 3)
	else (x, y, 1)
      in
	a.(i) <- (float_of_int x, float_of_int y);
	b.(i) <- r
    done;
  in
  let rot r =
    match r with
      | 1 -> 4
      | 2 -> 3
      | 3 -> 2
      | 4 -> 1
      | _ -> assert false
  in
  let (_, i, j) = cp2d a in
  let fd =
    if debug
    then stdout
    else open_out "output.txt"
  in
    Printf.fprintf fd "%d %d %d %d\n" (i + 1) b.(i) (j + 1) (rot b.(j))


Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \leq n, x \leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$).

For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \leq i \leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.

NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\ldots,101$$$.

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_int_pair () = let x = scan_int() in let y = scan_int() in (x,y);;
let scan_int_array_of_size n = Array.init n (fun _ -> scan_int());;
let show_int n = print_int n; print_newline ();;

let t = scan_int ();;
for x = 1 to t do
	let n,x = scan_int_pair () in
	let a = scan_int_array_of_size n in
	let deja_vu = Array.make 205 false in
	for i = 0 to n-1 do deja_vu.(a.(i)) <- true done;
	let i = ref 1 and count = ref 0 in
	while !count < x do
		if not deja_vu.(!i) then incr count;
		incr i
	done;
	while deja_vu.(!i) do incr i done;
	show_int (!i-1)
done;;


Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number.Petya can ask questions like: "Is the unknown number divisible by number y?".The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of.Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about.

A single line contains number n (1 ≤ n ≤ 103).

Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.

NoteThe sequence from the answer to the first sample test is actually correct.If the unknown number is not divisible by one of the sequence numbers, it is equal to 1.If the unknown number is divisible by 4, it is 4.If the unknown number is divisible by 3, then the unknown number is 3.Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.

let rec is_pow powered n =
	(n = powered) || ((powered mod n = 0) && (is_pow (powered / n) n));;

let f n l =
	let tab = Array.make (n+1) true and compt = ref 0 in
		tab.(0) <- false;
		tab.(1) <- false;
		for i = 2 to n do
			if tab.(i)
				then 	(incr compt;
					l := i:: !l;
					for j = 2 to (n/i) do
						tab.(i*j) <- false;
						if is_pow (i*j) i
							then (l := (i*j):: !l; incr compt);
					done);
		done;
		!compt;;

let main () =
	let l = ref [] in
		Scanf.scanf "%d" (fun i -> Printf.printf "%d\n" (f i l));
		List.iter (Printf.printf "%d ") !l;;

main ();;

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

let is_prime n =
  let rec go d =
    if d * d > n then true else
      if n mod d = 0 then false else
        go (d+1)
  in
  go 2

let rec gen_primes n ans =
  if n <= 1 then ans else
    if is_prime n then gen_primes (n-1) (n::ans) else
      gen_primes (n-1) ans

let () =
  let n = read_int () in
  let rec f p a b = if b > n then a else (f p (b::a) (b*p)) in
  let g a p = f p a p in
  let ans = List.fold_left g [] (gen_primes n []) in
  Printf.printf "%d\n" (List.length ans);
  List.iter (Printf.printf "%d ") ans;;


When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the i-th book.Valera decided to choose an arbitrary book with number i and read the books one by one, starting from this book. In other words, he will first read book number i, then book number i + 1, then book number i + 2 and so on. He continues the process until he either runs out of the free time or finishes reading the n-th book. Valera reads each book up to the end, that is, he doesn't start reading the book if he doesn't have enough free time to finish reading it. Print the maximum number of books Valera can read.

The first line contains two integers n and t (1 ≤ n ≤ 105; 1 ≤ t ≤ 109) — the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.

Print a single integer — the maximum number of books Valera can read.

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

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

let n , t = read_pair ();;

let data = Array.init (n+1) (fun i -> if i = 0 then 0 else read_int ());;

let sum = Array.init 100005 (fun i -> 0);;

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

let left , right = ( ref 1 , ref n );;

let binary_search len =
        let isok = ref false in
        for i = 0 to n-len do
                isok := !isok || (sum.(i+len) - sum.(i) <= t)
        done;
        !isok;;

while !left <= !right do
        let mid = (!left + !right)/2 in
        if binary_search mid then left := mid + 1
        else right := mid - 1
done;;

Printf.printf "%d\n" !right;;

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

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

(*let read_pair () = let a = read_int () in (a , read_int ());;*)

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

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 n , t = read_pair ();;

let data = Array.init (n+1) (fun i -> if i = 0 then Int64.of_int 0 else read_long ());;

let sum = Array.init 100005 (fun i -> Int64.of_int 0);;

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

let left , right = ( ref 1 , ref n );;

let binary_search len =
        let isok = ref false in
        for i = 0 to n-len do
                isok := !isok || (sum.(i+len) -- sum.(i) <= Int64.of_int t)
        done;
        !isok;;

while !left <= !right do
        let mid = (!left + !right)/2 in
        if binary_search mid then left := mid + 1
        else right := mid - 1
done;;

Printf.printf "%d\n" !right;;

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

let () =
  let n = read_int 0 in
  let t = read_int 0 in
  let a = Array.init n read_int in

(*
  let () = for i=1 to n-1 do
    a.(i) <- a.(i-1) + a.(i)
  done in
    (* so now a.(i) = the time needed to read books a.(0)...a.(i) *)
    (* so to read books i to j requires time a.(j) - a.(i-1) *)
*)

  let rec loop i j sum ac = if j=n-1 then ac else
    (* sum = total time of the books from i to j inclusive *)
    (* ac is the biggest number of book found so far that I can read *)
    if sum + a.(j+1) > t
      then loop (i+1) j (sum - a.(i)) ac
      else loop i (j+1) (sum + a.(j+1)) (max ac ((j+1)-i+1))
  in

  let answer = loop 0 0 a.(0) (if a.(0) > t then 0 else 1) in
    Printf.printf "%d\n" answer

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

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

(*let read_pair () = let a = read_int () in (a , read_int ());;*)

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

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 n , t = read_pair ();;

let data = Array.init (n+1) (fun i -> if i = 0 then Int64.of_int 0 else read_long ());;

let sum = Array.init 100005 (fun i -> Int64.of_int 0);;

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

let left , right = ( ref 1 , ref n );;

let binary_search len =
        let isok = ref true in
        for i = 0 to n-len do
                isok := !isok || (sum.(i+len) -- sum.(i) <= Int64.of_int t)
        done;
        !isok;;

while !left <= !right do
        let mid = (!left + !right)/2 in
        if binary_search mid then left := mid + 1
        else right := mid - 1
done;;

Printf.printf "%d\n" !right;;


An array $$$a_1, a_2, \ldots, a_n$$$ is good if and only if for every subsegment $$$1 \leq l \leq r \leq n$$$, the following holds: $$$a_l + a_{l + 1} + \ldots + a_r = \frac{1}{2}(a_l + a_r) \cdot (r - l + 1)$$$. You are given an array of integers $$$a_1, a_2, \ldots, a_n$$$. In one operation, you can replace any one element of this array with any real number. Find the minimum number of operations you need to make this array good.

The first line of input contains one integer $$$t$$$ ($$$1 \leq t \leq 100$$$): the number of test cases. Each of the next $$$t$$$ lines contains the description of a test case. In the first line you are given one integer $$$n$$$ ($$$1 \leq n \leq 70$$$): the number of integers in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-100 \leq a_i \leq 100$$$): the initial array.

For each test case, print one integer: the minimum number of elements that you need to replace to make the given array good.

NoteIn the first test case, the array is good already.In the second test case, one of the possible good arrays is $$$[1, 1, \underline{1}, \underline{1}]$$$ (replaced elements are underlined).In the third test case, the array is good already.In the fourth test case, one of the possible good arrays is $$$[\underline{-2.5}, -2, \underline{-1.5}, -1, \underline{-0.5}, 0]$$$.

open Printf
open Scanf

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

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

let is_integer_ratio a b = (a/b) * b = a

let evaluate i j k num den a =
  (* extrapoloates the requred value of a.(k) for it to work *)
  (* true if a.(k) has this value *)
  if k = i || k = j then true
  else if k < i then
    (is_integer_ratio ((i-k)*num) den && a.(k) + ((i-k)*num)/den  = a.(i))
  else if k < j then
    (is_integer_ratio ((k-i)*num) den && a.(i) + ((k-i)*num)/den  = a.(k))
  else
    (is_integer_ratio ((j-k)*num) den && a.(j) + ((k-j)*num)/den  = a.(k))

let () =
  let cases = read_int () in
  for case = 1 to cases do
    let n = read_int () in
    let a = Array.init n read_int in
    if n = 1 then printf "0\n" else
      let answer = minf 0 (n-2) (fun i -> minf (i+1) (n-1) (fun j ->
	let den = j - i in
	let num = a.(j) - a.(i) in
	sum 0 (n-1) (fun k -> if evaluate i j k num den a then 0 else 1)
      ))in
      printf "%d\n" answer
  done


There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At each step every psycho who has an id greater than the psycho to his right (if exists) kills his right neighbor in the line. Note that a psycho might kill and get killed at the same step. You're given the initial arrangement of the psychos in the line. Calculate how many steps are needed to the moment of time such, that nobody kills his neighbor after that moment. Look notes to understand the statement more precise.

The first line of input contains integer n denoting the number of psychos, (1 ≤ n ≤ 105). In the second line there will be a list of n space separated distinct integers each in range 1 to n, inclusive — ids of the psychos in the line from left to right.

Print the number of steps, so that the line remains the same afterward.

NoteIn the first sample line of the psychos transforms as follows: [10 9 7 8 6 5 3 4 2 1]  →  [10 8 4]  →  [10]. So, there are two steps.

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

let rec read_line line = function
  | 0 -> line
  | x -> read_line (read_int()::line) (x-1);;

type psy_info = {id : int;
		 kt : int};;

let ktime = Array.make 100000 0;;
let id_st = Stack.create ();;
let ans = ref 0;;

let dynpro psyline =
  let rec calc line n =
    match line with
      [] -> ()
    | x::l ->
      let larger = ref (Stack.top id_st) in
      while (not (Stack.is_empty id_st)) && (x > !larger.id) do
	larger := Stack.pop id_st;
	if ktime.(n) < !larger.kt then
	  ktime.(n) <- !larger.kt;
	if not (Stack.is_empty id_st) then
	  larger := Stack.top id_st;
      done;

      ktime.(n) <- ktime.(n) + 1;

      Stack.push {kt = ktime.(n); id = x} id_st;

      if ktime.(n) < 1000000 && ktime.(n) > !ans then
	ans:= ktime.(n);

      calc l (n+1) in

  ktime.(0) <- 1000000;
  Stack.push {kt=ktime.(0); id = List.hd psyline} id_st;
  calc (List.tl psyline) 1;;

let () =
  let n = read_int() in
  dynpro (List.rev (read_line [] n));
  if !ans < 1000000 then
    Printf.printf "%d\n" !ans;;

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

let rec read_line line = function
  | 0 -> line
  | x -> read_line (read_int()::line) (x-1);;

type psy_info = {id : int;
		 kt : int};;

let ktime = Array.make 100000 0;;
let id_st = Stack.create ();;
let ans = ref 0;;

let dynpro psyline =
  let rec calc line n =
    match line with
      [] -> ()
    | x::l ->
      let larger = ref (Stack.top id_st) in
      while (not (Stack.is_empty id_st)) && (x > !larger.id) do
	larger := Stack.pop id_st;
	if ktime.(n) < !larger.kt then
	  ktime.(n) <- !larger.kt;
	if not (Stack.is_empty id_st) then
	  larger := Stack.top id_st;
      done;

      ktime.(n) <- ktime.(n) + 1;

      Stack.push {kt = ktime.(n); id = x} id_st;

      if ktime.(n) > !ans then
	ans:= ktime.(n);

      calc l (n+1) in

  ktime.(0) <- 1000000;
  Stack.push {kt=ktime.(0); id = List.hd psyline} id_st;
  calc (List.tl psyline) 1;;

let () =
  let n = read_int() in
  dynpro (List.rev (read_line [] n));
  if !ans < 1000000 then
    Printf.printf "%d\n" !ans
  else
    Printf.printf "%d\n" 0;;


Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit?

The first line contains the only integer n (0 ≤ n ≤ 10100000). It is guaranteed that n doesn't contain any leading zeroes.

Print the number of times a number can be replaced by the sum of its digits until it only contains one digit.

NoteIn the first sample the number already is one-digit — Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991 → 19 → 10 → 1. After three transformations the number becomes one-digit.

let i = ref 0;;
let t = ref 0;;
exception End_of_line;;
try while true do
	let r = input_byte stdin in
	if r = 10 then raise End_of_line
	else (i := (!i + (r - 48)); incr t)
done with _ -> ();;

if !t = 1 then t := 0 else t := 1;;

while !i mod 10 <> !i do
	let m = ref (!i) in
	(i := 0;
	while !m <> 0 do
		i := (!i + (!m mod 10));
		m := (!m / 10)
	done;
	incr t)
done;;

print_int (!t);;

let i = ref 0;;
let t = ref 0;;
exception End_of_line;;
try while true do
	let r = input_byte stdin in
	if r = 10 then raise End_of_line
	else (i := (!i + r - 48); incr t)
done with _ -> ();;

if !t = 1 then t := 0 else t := 1;;

while !i / 10 <> 0 do
	let m = ref (!i) in
	(i := 0;
	while !m mod 10 <> 0 do
		i := (!i + (!m mod 10));
		m := (!m / 10)
	done;
	incr t)
done;;

print_int (!t);;


As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads.The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities).It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company.

The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n).

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

let () =
  let n = read_int 0 in
  let e = Array.make n [] in
  for i = 0 to n-2 do
    let x = read_int 0 - 1 in
    let y = read_int 0 - 1 in
    e.(x) <- (y,i)::e.(x);
    e.(y) <- (x,n-1+i)::e.(y)
  done;

  let dp = Array.make (2*(n-1)) (-1,-1) in
  let ans = ref 0 in
  let rec f u v i =
    if fst dp.(i) < 0 then (
      let rec go x y opt = function
        | [] ->
          x, max opt (x+y)
        | (w,j)::es ->
            if w = u then
              go x y opt es
            else
              let z, opt' = f v w j in
              let z = z+1 in
              if z > x then
                go z x (max opt opt') es
              else if z > y then
                go x z (max opt opt') es
              else
                go x y (max opt opt') es
      in
      dp.(i) <- go 0 0 0 e.(v)
    );
    dp.(i)
  in
  for x = 0 to n-1 do
    List.iter (fun (y,i) ->
      if i < n-1 then
        ans := (f x y i |> snd) * (f y x (n-1+i) |> snd) |> max !ans;
    ) e.(x)
  done;
  Printf.printf "%d\n" !ans

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

let () =
  let n = read_int 0 in
  let e = Array.make n [] in
  for i = 0 to n-2 do
    let x = read_int 0 - 1 in
    let y = read_int 0 - 1 in
    e.(x) <- (y,i)::e.(x);
    e.(y) <- (x,n-1+i)::e.(y)
  done;

  let dp = Array.make (2*(n-1)) (-1,-1) in
  let ans = ref 0 in
  let rec f u v i =
    if fst dp.(i) < 0 then (
      let rec go x y opt = function
        | [] -> x, max opt (x+y)
        | (w,j)::es ->
            if w = u then
              go x y opt es
            else
              let z, opt' = f v w j in
              let z = z+1 in
              if z > x then
                go z y (max opt opt') es
              else if z > y then
                go x z (max opt opt') es
              else
                go x y opt es
      in
      dp.(i) <- go 0 0 0 e.(v)
      (*Printf.printf "f %d %d %d %d,%d\n" u v i (fst dp.(i)) (snd dp.(i));*)
    );
    dp.(i)
  in
  for x = 0 to n-1 do
    List.iter (fun (y,i) ->
      if i < n-1 then
        ans := (f x y i |> snd) * (f y x (n-1+i) |> snd) |> max !ans;
    ) e.(x)
  done;
  Printf.printf "%d\n" !ans


Initially Ildar has an empty array. He performs $$$n$$$ steps. On each step he takes a subset of integers already added to the array and appends the mex of this subset to the array. The mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset $$$[0, 2, 3]$$$ is $$$1$$$, while the mex of the multiset $$$[1, 2, 1]$$$ is $$$0$$$.More formally, on the step $$$m$$$, when Ildar already has an array $$$a_1, a_2, \ldots, a_{m-1}$$$, he chooses some subset of indices $$$1 \leq i_1 < i_2 < \ldots < i_k < m$$$ (possibly, empty), where $$$0 \leq k < m$$$, and appends the $$$mex(a_{i_1}, a_{i_2}, \ldots a_{i_k})$$$ to the end of the array.After performing all the steps Ildar thinks that he might have made a mistake somewhere. He asks you to determine for a given array $$$a_1, a_2, \ldots, a_n$$$ the minimum step $$$t$$$ such that he has definitely made a mistake on at least one of the steps $$$1, 2, \ldots, t$$$, or determine that he could have obtained this array without mistakes.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100\,000$$$) — the number of steps Ildar made. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 10^9$$$) — the array Ildar obtained.

If Ildar could have chosen the subsets on each step in such a way that the resulting array is $$$a_1, a_2, \ldots, a_n$$$, print $$$-1$$$. Otherwise print a single integer $$$t$$$ — the smallest index of a step such that a mistake was made on at least one step among steps $$$1, 2, \ldots, t$$$.

NoteIn the first example it is possible that Ildar made no mistakes. Here is the process he could have followed.  $$$1$$$-st step. The initial array is empty. He can choose an empty subset and obtain $$$0$$$, because the mex of an empty set is $$$0$$$. Appending this value to the end he gets the array $$$[0]$$$.  $$$2$$$-nd step. The current array is $$$[0]$$$. He can choose a subset $$$[0]$$$ and obtain an integer $$$1$$$, because $$$mex(0) = 1$$$. Appending this value to the end he gets the array $$$[0,1]$$$.  $$$3$$$-rd step. The current array is $$$[0,1]$$$. He can choose a subset $$$[0,1]$$$ and obtain an integer $$$2$$$, because $$$mex(0,1) = 2$$$. Appending this value to the end he gets the array $$$[0,1,2]$$$.  $$$4$$$-th step. The current array is $$$[0,1,2]$$$. He can choose a subset $$$[0]$$$ and obtain an integer $$$1$$$, because $$$mex(0) = 1$$$. Appending this value to the end he gets the array $$$[0,1,2,1]$$$. Thus, he can get the array without mistakes, so the answer is $$$-1$$$.In the second example he has definitely made a mistake on the very first step, because he could not have obtained anything different from $$$0$$$.In the third example he could have obtained $$$[0, 1, 2]$$$ without mistakes, but $$$239$$$ is definitely wrong.

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

let rec sol min i = function
  | [] -> print_int (-1)
  | h::t -> let x = int_of_string h in
    if x > min then print_int i
    else sol (max (x+1) min) (i+1) t
;;

let _n = Scanf.sscanf (read_line ()) "%i" (fun n -> n) in
let a = split_on_char ' ' (read_line ()) in
sol 0 1 a;
print_newline ()


You are given a positive integer $$$n$$$ greater or equal to $$$2$$$. For every pair of integers $$$a$$$ and $$$b$$$ ($$$2 \le |a|, |b| \le n$$$), you can transform $$$a$$$ into $$$b$$$ if and only if there exists an integer $$$x$$$ such that $$$1 < |x|$$$ and ($$$a \cdot x = b$$$ or $$$b \cdot x = a$$$), where $$$|x|$$$ denotes the absolute value of $$$x$$$.After such a transformation, your score increases by $$$|x|$$$ points and you are not allowed to transform $$$a$$$ into $$$b$$$ nor $$$b$$$ into $$$a$$$ anymore.Initially, you have a score of $$$0$$$. You can start at any integer and transform it as many times as you like. What is the maximum score you can achieve?

A single line contains a single integer $$$n$$$ ($$$2 \le n \le 100\,000$$$) — the given integer described above.

Print an only integer — the maximum score that can be achieved with the transformations. If it is not possible to perform even a single transformation for all possible starting integers, print $$$0$$$.

NoteIn the first example, the transformations are $$$2 \rightarrow 4 \rightarrow (-2) \rightarrow (-4) \rightarrow 2$$$.In the third example, it is impossible to perform even a single transformation.

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

let factors n =
  let m,r = ref n,ref [] in
  rep 2L (n/2L) (fun i ->
    if !m mod i = 0L then (
      let k = ref 0L in
      while !m mod i = 0L do
        k += 1L; m /= i;
      done;
      r := (i,!k) :: !r));
  if !m>1L then (!m,1L)::!r else !r
let () =
  let n = get_i64 0 in
  repm 2L n 0L (fun u i ->
    let k,s = ref 2L,ref 0L in
    while i * !k <= n do
      s += !k; k += 1L
    done; `Ok (u + !s)
  )
  |> ( * ) 4L |> printf "%Ld\n"


Nastya owns too many arrays now, so she wants to delete the least important of them. However, she discovered that this array is magic! Nastya now knows that the array has the following properties:  In one second we can add an arbitrary (possibly negative) integer to all elements of the array that are not equal to zero.  When all elements of the array become equal to zero, the array explodes. Nastya is always busy, so she wants to explode the array as fast as possible. Compute the minimum time in which the array can be exploded.

The first line contains a single integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains n integers a1, a2, ..., an ( - 105 ≤ ai ≤ 105) — the elements of the array.

Print a single integer — the minimum number of seconds needed to make all elements of the array equal to zero.

NoteIn the first example you can add  - 1 to all non-zero elements in one second and make them equal to zero.In the second example you can add  - 2 on the first second, then the array becomes equal to [0, 0,  - 3]. On the second second you can add 3 to the third (the only non-zero) element.

open Printf
open Scanf

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

  let h = Hashtbl.create 10 in
  let increment t =
    let c = try Hashtbl.find h t with Not_found -> 0 in
    Hashtbl.replace h t (c+1)
  in

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

  let answer = (Hashtbl.length h) - (if Hashtbl.mem h 0 then 1 else 0) in

  printf "%d\n" answer


You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears:  Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one;  Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[\frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[\frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows:  Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$;  then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$;  then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$;  then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$;  and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique.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. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique.

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

let comp a b =
   let (da, ta) = a in
   let (db, tb) = b in

   if ta = tb then
      da - db
   else
      tb - ta
in

let nbTests = read () in
for iTest = 1 to nbTests do
   let taille = read () in

   let tableau = Array.make taille 0 in

   let rec ajoute_inters d t l =
      if t = 0 then
         l
      else if t = 1 then
         (d, t) :: l
      else
         let tg = (t - 1) / 2 in
         let td = t - tg - 1 in
         (d, t) :: ajoute_inters d tg (ajoute_inters (d + tg + 1) td l)
   in

   let inters = List.sort comp (ajoute_inters 0 taille []) in

   List.iteri (fun i a -> let (da, ta) = a in tableau.(da + ((ta - 1) / 2)) <- i + 1; ()) inters;

   for i = 0 to (taille - 1) do
      print_int tableau.(i);
      print_string " "
   done;
   print_newline ()
done;


Petya got an array $$$a$$$ of numbers from $$$1$$$ to $$$n$$$, where $$$a[i]=i$$$.He performed $$$n$$$ operations sequentially. In the end, he received a new state of the $$$a$$$ array.At the $$$i$$$-th operation, Petya chose the first $$$i$$$ elements of the array and cyclically shifted them to the right an arbitrary number of times (elements with indexes $$$i+1$$$ and more remain in their places). One cyclic shift to the right is such a transformation that the array $$$a=[a_1, a_2, \dots, a_n]$$$ becomes equal to the array $$$a = [a_i, a_1, a_2, \dots, a_{i-2}, a_{i-1}, a_{i+1}, a_{i+2}, \dots, a_n]$$$.For example, if $$$a = [5,4,2,1,3]$$$ and $$$i=3$$$ (that is, this is the third operation), then as a result of this operation, he could get any of these three arrays:  $$$a = [5,4,2,1,3]$$$ (makes $$$0$$$ cyclic shifts, or any number that is divisible by $$$3$$$);  $$$a = [2,5,4,1,3]$$$ (makes $$$1$$$ cyclic shift, or any number that has a remainder of $$$1$$$ when divided by $$$3$$$);  $$$a = [4,2,5,1,3]$$$ (makes $$$2$$$ cyclic shifts, or any number that has a remainder of $$$2$$$ when divided by $$$3$$$). Let's look at an example. Let $$$n=6$$$, i.e. initially $$$a=[1,2,3,4,5,6]$$$. A possible scenario is described below.  $$$i=1$$$: no matter how many cyclic shifts Petya makes, the array $$$a$$$ does not change.  $$$i=2$$$: let's say Petya decided to make a $$$1$$$ cyclic shift, then the array will look like $$$a = [\textbf{2}, \textbf{1}, 3, 4, 5, 6]$$$.  $$$i=3$$$: let's say Petya decided to make $$$1$$$ cyclic shift, then the array will look like $$$a = [\textbf{3}, \textbf{2}, \textbf{1}, 4, 5, 6]$$$.  $$$i=4$$$: let's say Petya decided to make $$$2$$$ cyclic shifts, the original array will look like $$$a = [\textbf{1}, \textbf{4}, \textbf{3}, \textbf{2}, 5, 6]$$$.  $$$i=5$$$: let's say Petya decided to make $$$0$$$ cyclic shifts, then the array won't change.  $$$i=6$$$: let's say Petya decided to make $$$4$$$ cyclic shifts, the array will look like $$$a = [\textbf{3}, \textbf{2}, \textbf{5}, \textbf{6}, \textbf{1}, \textbf{4}]$$$. You are given a final array state $$$a$$$ after all $$$n$$$ operations. Determine if there is a way to perform the operation that produces this result. In this case, if an answer exists, print the numbers of cyclical shifts that occurred during each of the $$$n$$$ operations.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of the description of each test case contains one integer $$$n$$$ ($$$2 \le n \le 2\cdot10^3$$$) — the length of the array $$$a$$$. The next line contains the final state of the array $$$a$$$: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) are written. All $$$a_i$$$ are distinct. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$2\cdot10^3$$$.

For each test case, print the answer on a separate line. Print -1 if the given final value $$$a$$$ cannot be obtained by performing an arbitrary number of cyclic shifts on each operation. Otherwise, print $$$n$$$ non-negative integers $$$d_1, d_2, \dots, d_n$$$ ($$$d_i \ge 0$$$), where $$$d_i$$$ means that during the $$$i$$$-th operation the first $$$i$$$ elements of the array were cyclic shifted to the right $$$d_i$$$ times. If there are several possible answers, print the one where the total number of shifts is minimal (that is, the sum of $$$d_i$$$ values is the smallest). If there are several such answers, print any of them.

NoteThe first test case matches the example from the statement.The second set of input data is simple. Note that the answer $$$[3, 2, 1]$$$ also gives the same permutation, but since the total number of shifts $$$3+2+1$$$ is greater than $$$0+0+1$$$, this answer is not correct.

let plus a b n = (a + b + n ) mod n ;;

(*pour x entre 1 et n-2 ; donne le rep.(x)*)
let pourMil len t x = let a = t.(x) in let b = t.(x+1) in
  let count = ref 0 in
  for i = x+2 to len-1 do
    if (a<b && t.(i)>a && t.(i)<b) || a>b && (t.(i)>a ||t.(i)<b) then incr count ;
  done;
  let test = plus (-a) b len - (!count) -1 in
  plus (-test) (x+1) (x+1)
;;

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

let final len t = let rep = ref "" in
  rep := "0 ";
  for i = 1 to len - 2 do
    rep := !rep^(string_of_int(pourMil len t i))^" " ;
  done;
  rep := !rep^(string_of_int(plus (-len+1) t.(len-1) len));
  !rep
;;

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 t = parse len str in
  print_endline (final len t)
done;
;;


Harry Potter and He-Who-Must-Not-Be-Named engaged in a fight to the death once again. This time they are located at opposite ends of the corridor of length l. Two opponents simultaneously charge a deadly spell in the enemy. We know that the impulse of Harry's magic spell flies at a speed of p meters per second, and the impulse of You-Know-Who's magic spell flies at a speed of q meters per second.The impulses are moving through the corridor toward each other, and at the time of the collision they turn round and fly back to those who cast them without changing their original speeds. Then, as soon as the impulse gets back to it's caster, the wizard reflects it and sends again towards the enemy, without changing the original speed of the impulse.Since Harry has perfectly mastered the basics of magic, he knows that after the second collision both impulses will disappear, and a powerful explosion will occur exactly in the place of their collision. However, the young wizard isn't good at math, so he asks you to calculate the distance from his position to the place of the second meeting of the spell impulses, provided that the opponents do not change positions during the whole fight.

The first line of the input contains a single integer l (1 ≤ l ≤ 1 000) — the length of the corridor where the fight takes place. The second line contains integer p, the third line contains integer q (1 ≤ p, q ≤ 500) — the speeds of magical impulses for Harry Potter and He-Who-Must-Not-Be-Named, respectively.

Print a single real number — the distance from the end of the corridor, where Harry is located, to the place of the second meeting of the spell impulses. Your answer will be considered correct if its absolute or relative error will not exceed 10 - 4.  Namely: let's assume that your answer equals a, and the answer of the jury is b. The checker program will consider your answer correct if .

NoteIn the first sample the speeds of the impulses are equal, so both of their meetings occur exactly in the middle of the corridor.

let () = Scanf.scanf "%f\n%f\n%f" (fun l p q ->
    Printf.printf "%f\n" (l /. (q /. p +. 1.)))

open Printf
open Scanf

let () =
  let l = float_of_int (read_int ()) in
  let p = float_of_int (read_int ()) in
  let q = float_of_int (read_int ()) in
  let res = p *. (l /. (p +. q)) in
  printf "%f\n%!" res

open Printf
open Scanf

let read_float () = bscanf Scanning.stdib " %f " (fun x -> x)

let () =
  let l = read_float () in
  let p = read_float () in
  let q = read_float () in

  let answer = (p *. l) /. (p +. q) in

  printf "%.5f\n" answer


When Adam gets a rooted tree (connected non-directed graph without cycles), he immediately starts coloring it. More formally, he assigns a color to each edge of the tree so that it meets the following two conditions:   There is no vertex that has more than two incident edges painted the same color.  For any two vertexes that have incident edges painted the same color (say, c), the path between them consists of the edges of the color c. Not all tree paintings are equally good for Adam. Let's consider the path from some vertex to the root. Let's call the number of distinct colors on this path the cost of the vertex. The cost of the tree's coloring will be the maximum cost among all the vertexes. Help Adam determine the minimum possible cost of painting the tree. Initially, Adam's tree consists of a single vertex that has number one and is the root. In one move Adam adds a new vertex to the already existing one, the new vertex gets the number equal to the minimum positive available integer. After each operation you need to calculate the minimum cost of coloring the resulting tree.

The first line contains integer n (1 ≤ n ≤ 106) — the number of times a new vertex is added. The second line contains n numbers pi (1 ≤ pi ≤ i) — the numbers of the vertexes to which we add another vertex.

Print n integers — the minimum costs of the tree painting after each addition.

NoteThe figure below shows one of the possible variants to paint a tree from the sample at the last moment. The cost of the vertexes with numbers 11 and 12 equals 3.

let (|>) x f = f x
let (@@) f x = f x

type vertex =
  { mutable s : int * bool * int;
    p : vertex option }

let () =
  let vertex p = { s = -1, false, 1; p } in
  let vertex p =
    let rec update =
      function
      | { p = None } -> ()
      | { s = _, _, c; p = Some ({ s = mcc, mcf, pc } as p) } ->
        let sgn a = if a = 0 then 0 else a / abs a in
        match sgn (c - mcc), mcf with
        | 1, _ ->
          p.s <- c, false, c;
          if pc < c then update p
        | 0, false ->
          p.s <- mcc, true, c + 1;
          update p
        | _ -> ()
    in
    let v = vertex p in
    update v;
    v
  in
  let n = read_int () in
  let open Array in
  let vs = make (n + 1) (vertex None) in
     init n (fun _ -> Scanf.scanf " %d" @@ fun x -> x - 1)
  |> iteri
       (fun i pi ->
       vs.(i + 1) <- vertex (Some vs.(pi));
       let mcc, _, _ = vs.(0).s in
       print_int mcc;
       if i < n - 1 then print_char ' ');
  print_newline ()

let (|>) x f = f x
let (@@) f x = f x

let () =
  let add, get, iter =
    let n = read_int () in
    let mk v = Array.make (n + 1) v in
    let p, mcc, mcf, cc = mk ~-1, mk ~-1, mk false, mk 1 in
    let set i c inc =
      let set a v = Array.set a i v in
      let r = cc.(i) in
      if inc then (set mcc c; set mcf false; set cc c)
             else (set mcf true; set cc (succ c));
      r < cc.(i)
    in
    let rec update i =
      let pi, c = p.(i), cc.(i) in
      let set = set pi c in
      let sgn a = if a = 0 then 0 else a / abs a in
      if (match pi, lazy (sgn (c - mcc.(pi)), mcf.(pi)) with
          | -1, _ -> false
          | _, lazy (1, _) -> set true
          | _, lazy (0, false) -> set false
          | _ -> false)
      then update pi
    in
    let k = ref 0 in
    (fun i -> incr k; p.(!k) <- i - 1; update !k),
    (fun () -> mcc.(0)),
    fun f -> for i = 1 to n do f (i < n) done
  in
  iter @@ fun s ->
   Scanf.scanf " %d" add;
   print_int (get ());
   print_char (if s then ' ' else '\n')

let (|>) x f = f x
let (@@) f x = f x

let (%) f g x = f (g x)
let (%>) f g x = g (f x)

module Max : sig
  type t
  val mk : int -> t
  val cmp : int -> t -> [`Lt | `Gt | `Eq]
  val int : t -> int
end = struct
  type t = int
  let mk v = v
  let cmp a =
    function
    | b when a < b -> `Lt
    | b when a = b -> `Eq
    | _ -> `Gt
  let int m = m
end

type vertex =
  { mutable s : Max.t * bool * int;
    p : vertex option }

let () =
  let vertex p = { s = Max.mk ~-1, false, 1; p } in
  let vertex p =
    let rec update =
      function
      | { p = None } -> ()
      | { s = _, _, c; p = Some ({ s = mcc, mcf, pc } as p) } ->
        match Max.cmp c mcc, mcf with
        | `Gt, _ ->
          p.s <- Max.mk c, false, c;
          if pc < c then update p
        | `Eq, false ->
          p.s <- mcc, true, c + 1;
          update p
        | `Eq, true | `Lt, _ -> ()
    in
    let v = vertex p in
    update v;
    v
  in
  let n = read_int () in
  let open Array in
  let vs = make (n + 1) (vertex None) in
     init n (fun _ -> Scanf.scanf " %d" @@ fun x -> x - 1)
  |> mapi (fun i pi ->
       vs.(i + 1) <- vertex (Some vs.(pi));
       let mcc, _, _ = vs.(0).s in Max.int mcc)
  |> map string_of_int
  |> to_list
  |> String.concat " "
  |> print_endline

let (|>) x f = f x
let (@@) f x = f x

let (%) f g x = f (g x)
let (%>) f g x = g (f x)

module List = struct
  include List
  let map f =
    let rec loop acc =
      function
      | [] -> rev acc
      | x :: xs -> loop (f x :: acc) xs
    in
    loop []
  let mapi f =
    map (let c = ref ~-1 in fun e -> incr c; f !c e)
end

module Max : sig
  type t
  val mk : int -> t
  val cmp : int -> t -> [`Lt | `Gt | `Eq]
  val int : t -> int
end = struct
  type t = int
  let mk v = v
  let cmp a =
    function
    | b when a < b -> `Lt
    | b when a = b -> `Eq
    | _ -> `Gt
  let int m = m
end

type vertex =
  { mutable s : Max.t * bool * int;
    p : vertex option }

let () =
  let vertex p = { s = Max.mk ~-1, false, 1; p } in
  let vertex p =
    let rec update =
      function
      | { p = None } -> ()
      | { s = _, _, c; p = Some ({ s = mcc, mcf, pc } as p) } ->
        match Max.cmp c mcc, mcf with
        | `Gt, _ ->
          p.s <- Max.mk c, false, c;
          if pc < c then update p
        | `Eq, false ->
          p.s <- mcc, true, c + 1;
          update p
        | `Eq, true | `Lt, _ -> ()
    in
    let v = vertex p in
    update v;
    v
  in
  let n = read_int () in
  let vs = Array.make (n + 1) (vertex None) in
     Array.init n (fun _ -> Scanf.scanf " %d" @@ fun x -> x - 1)
  |> Array.mapi (fun i pi ->
          vs.(i + 1) <- vertex (Some vs.(pi));
          let { s = mcc, _, _ } = vs.(0) in Max.int mcc)
  |> Array.map string_of_int
  |> Array.to_list
  |> String.concat " "
  |> print_endline

let (|>) x f = f x
let (@@) f x = f x

let () =
  let add, get, iter =
    let n = read_int () in
    let mk v = Array.make (n + 1) v in
    let p, mcc, cc = mk ~-1, mk ~-1, mk 1 in
    let rec update i =
      let pi, c = p.(i), cc.(i) in
      if (match lazy (c - mcc.(pi), cc.(pi) > mcc.(pi), cc.(pi)) with
          | _ when pi = -1 -> false
          | lazy (d, _, pc) when d > 0 ->
            mcc.(pi) <- c;
            cc.(pi) <- c;
            pc < c
          | lazy (0, false, _) ->
            cc.(pi) <- c + 1;
            true
          | _ -> false)
      then update pi
    in
    let k = ref 0 in
    (fun i -> incr k; p.(!k) <- i - 1; update !k),
    (fun () -> mcc.(0)),
    fun f -> for i = 1 to n do f (i < n) done
  in
  iter @@ fun s ->
   Scanf.scanf " %d" add;
   print_int (get ());
   print_char (if s then ' ' else '\n')

let (|>) x f = f x
let (@@) f x = f x

let (%) f g x = f (g x)
let (%>) f g x = g (f x)

type vertex =
  { mutable mcc : int;
    mutable c : int;
    p : vertex option }

let () =
  let n = read_int () in
  let vs = Array.make (n + 1) None in
  let vertex p =
    let rec update =
      let cmp a =
        function
        | b when a < b -> `Lt
        | b when a = b -> `Eq
        | _ -> `Gt
      in
      function
      | { p = None } -> ()
      | { c; p = Some p } ->
        match cmp c p.mcc with
        | `Gt -> p.mcc <- c
        | `Eq -> p.c <- p.c + 1; update p
        | `Lt -> ()
    in
    let v = { mcc = -1; c = 0; p } in
    update v;
    v
  in
  let r = { mcc = -1; c = 0; p = None } in
  vs.(0) <- Some r;
    read_line ()
  |> Str.(split (regexp_string " "))
  |> List.mapi (fun i ->
        pred % int_of_string
     %> fun pi ->
          vs.(i + 1) <- Some (vertex vs.(pi));
          r.mcc + 1)
  |> List.map string_of_int
  |> String.concat " "
  |> print_endline

let (|>) x f = f x
let (@@) f x = f x

type vertex =
  { mutable s : (int * bool) * int;
    p : vertex option }

let () =
  let vertex p = { s = (-1, false), 1; p } in
  let vertex p =
    let rec update =
      function
      | { p = None } -> ()
      | { s = _, c; p = Some ({ s = (mcc, mcf), pc } as p) } ->
        if c > mcc then (
          p.s <- (c, false), c;
          if pc < c then update p
        ) else if not mcf then (
          p.s <- (mcc, true), c + 1;
          update p
        )
    in
    let v = vertex p in
    update v;
    v
  in
  let n = read_int () in
  let open Array in
  let vs = make (n + 1) (vertex None) in
     init n (fun _ -> Scanf.scanf " %d" @@ fun x -> x - 1)
  |> iteri
       (fun i pi ->
       vs.(i + 1) <- vertex (Some vs.(pi));
       let (mcc, _), _ = vs.(0).s in
       print_int mcc;
       if i < n - 1 then print_char ' ');
  print_newline ()