You are given an array of positive integers $$$a = [a_0, a_1, \dots, a_{n - 1}]$$$ ($$$n \ge 2$$$).In one step, the array $$$a$$$ is replaced with another array of length $$$n$$$, in which each element is the greatest common divisor (GCD) of two neighboring elements (the element itself and its right neighbor; consider that the right neighbor of the $$$(n - 1)$$$-th element is the $$$0$$$-th element).Formally speaking, a new array $$$b = [b_0, b_1, \dots, b_{n - 1}]$$$ is being built from array $$$a = [a_0, a_1, \dots, a_{n - 1}]$$$ such that $$$b_i$$$ $$$= \gcd(a_i, a_{(i + 1) \mod n})$$$, where $$$\gcd(x, y)$$$ is the greatest common divisor of $$$x$$$ and $$$y$$$, and $$$x \mod y$$$ is the remainder of $$$x$$$ dividing by $$$y$$$. In one step the array $$$b$$$ is built and then the array $$$a$$$ is replaced with $$$b$$$ (that is, the assignment $$$a$$$ := $$$b$$$ is taking place).For example, if $$$a = [16, 24, 10, 5]$$$ then $$$b = [\gcd(16, 24)$$$, $$$\gcd(24, 10)$$$, $$$\gcd(10, 5)$$$, $$$\gcd(5, 16)]$$$ $$$= [8, 2, 5, 1]$$$. Thus, after one step the array $$$a = [16, 24, 10, 5]$$$ will be equal to $$$[8, 2, 5, 1]$$$.For a given array $$$a$$$, find the minimum number of steps after which all values $$$a_i$$$ become equal (that is, $$$a_0 = a_1 = \dots = a_{n - 1}$$$). If the original array $$$a$$$ consists of identical elements then consider the number of steps is equal to $$$0$$$.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. Each test case contains two lines. The first line contains an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_0, a_1, \dots, a_{n - 1}$$$ ($$$1 \le a_i \le 10^6$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Print $$$t$$$ numbers — answers for each test case.

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func getGcd(a, b int) int {
	if a == 0 {
		return b
	}
	return getGcd(b%a, a)
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.NextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.NextInt()
		arr := sc.NextInts(N)
		arr = append(arr, arr...)

		ans := 0
		for i := 0; i < N; i++ {
			x := arr[i]
			y := arr[i+1]
			gap := 0
			for x != y {
				gap++
				x = getGcd(x, arr[i+gap])
				y = getGcd(y, arr[i+1+gap])
			}
			ans = Max(ans, gap)
		}
		fmt.Fprintln(wr, ans)

	}

}

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}
func (s *Scanner) Next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}
func (s *Scanner) NextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt()
	}
	return res
}
func (s *Scanner) NextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt64()
	}
	return res
}
func Min(num int, nums ...int) int {
	ans := num
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}
func Max(num int, nums ...int) int {
	ans := num
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func InitInts1D(n int, val int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = val
	}
	return res
}
func InitInts2D(n, m int, val int) [][]int {
	res := make([][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts1D(m, val)
	}
	return res
}
func InitInts3D(n, m, k int, val int) [][][]int {
	res := make([][][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts2D(m, k, val)
	}
	return res
}

type StepState struct {
	step, dist int
}
type PriorityQueue []*StepState

func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
	if pq[i].step != pq[j].step {
		return pq[i].step < pq[j].step
	}
	return pq[i].dist < pq[j].dist
}
func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Push(x interface{}) {
	item := x.(*StepState)
	*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

package main

import (
	"bufio"
	//	"container/heap" for heap.Init()
	"fmt"
	"os"
	"runtime"
)

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

const (
	INIT  = -1
	MAX_A = 1000002
)

var (
	n      int
	arr    []int
	sieve  []int
	primes []int
	exists = struct{}{}
)

func pre() {
	sieve = make([]int, MAX_A)
	sieve[1] = 1
	var i, j int64 // int32 on win32
	for i = 2; i < MAX_A; i++ {
		if sieve[i] == 0 {
			sieve[i] = int(i)
			primes = append(primes, int(i))

			for j = i; i*j < MAX_A; j++ {
				sieve[i*j] = int(i)
			}
		}
	}

	// for i := 1; i < 50; i++ {
	// 	fmt.Printf("sieve[%d] %d\n", i, sieve[i])
	// }
}

func gcd(a, b int) int {
	for a > 0 {
		tmp := a
		a = b % a
		b = tmp
	}
	return b
}

func solve() int {
	pf := make([]map[int]struct{}, n)
	for i := 0; i < n; i++ {
		pf[i] = make(map[int]struct{})
		t := arr[i]
		for t > 1 {
			pf[i][sieve[t]] = exists
			t /= sieve[t]
		}
	}

	ans := 0
	for i := 0; i < n; i++ {
		for p := range pf[i] {

			cnt := 1
			j := (n + i - 1) % n
			for {
				if _, ok := pf[j][p]; ok {
					delete(pf[j], p)
					cnt++
					j = (n + j - 1) % n
				} else {
					break
				}
			}

			j = (i + 1) % n
			for {
				if _, ok := pf[j][p]; ok {
					delete(pf[j], p)
					cnt++
					j = (j + 1) % n
				} else {
					break
				}
			}

			delete(pf[i], p)

			if ans < cnt {
				ans = cnt
			}
		}
	}

	return ans
}

func main() {
	pre()

	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var tc int
	fmt.Fscan(r, &tc)

	for ; tc > 0; tc-- {
		fmt.Fscan(r, &n)

		arr = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &arr[i])
		}

		com := arr[0]
		for i := 1; i < n; i++ {
			com = gcd(com, arr[i])
		}

		for i := 0; i < n; i++ {
			arr[i] /= com
		}

		fmt.Println(solve())
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := sint64s(n)
		printf("%d\n", sol(n, a))
	}
}

func sol(n int64, a []int64) int64 {
	tmp := append(a, a...)
	st := BuildSparseTable(tmp, gcd)
	ans := n * 2
	l, r := int64(1), n*2
	for l <= r {
		m := (l + r) / 2
		// printf("l=%d r=%d m=%d\n", l, r, m)
		if possible(st, n*2, m) {
			ans = min(ans, m-1)
			r = m - 1
		} else {
			l = m + 1
		}
	}
	return ans
}

func possible(st SparseTable, n, m int64) bool {
	target := st.Query(0, m-1)
	for i := int64(1); i+m-1 < n; i++ {
		if st.Query(i, i+m-1) != target {
			return false
		}
	}
	return true
}

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	if a < 0 {
		a *= -1
	}
	return a
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

type SparseTable struct {
	vals [][]int64
	agg  func(int64, int64) int64
}

func BuildSparseTable(a []int64, agg func(int64, int64) int64) SparseTable {
	st := SparseTable{
		vals: [][]int64{a},
		agg:  agg,
	}
	prevRow := a
	for pow2 := 1 << 1; pow2 < len(a); pow2 <<= 1 {
		row := make([]int64, len(a)-pow2+1)
		for i := range row {
			row[i] = st.agg(prevRow[i], prevRow[i+pow2/2])
		}
		st.vals = append(st.vals, row)
		prevRow = row
	}
	return st
}

func (st SparseTable) Query(l, r int64) int64 {
	row := 0
	size := int64(1)
	for size<<1 < r-l+1 {
		row++
		size <<= 1
	}
	return st.agg(st.vals[row][l], st.vals[row][r-size+1])
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1547F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		ans := 0
		for i, x := range a {
			y := a[(i+1)%n]
			j := i
			for ; x != y; j++ {
				x = gcd(x, a[(j+1)%n])
				y = gcd(y, a[(j+2)%n])
			}
			if j-i > ans {
				ans = j - i
			}
		}
		Fprintln(out, ans)
	}
}

func main() { CF1547F(os.Stdin, os.Stdout) }

func gcd(a, b int) int {
	for a != 0 {
		a, b = b%a, a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := sint64s(n)
		printf("%d\n", sol(n, a))
	}
}

func sol(n int64, a []int64) int64 {
	p := int64(0)
	maxSoFar := int64(0)
	for p < n {
		curr := a[p]
		for offset := int64(1); offset < n; offset++ {
			nxtp := (p + offset) % n
			if curr == a[nxtp] {
				maxSoFar = max(maxSoFar, offset-1)
				p += offset
				break
			}
			if offset == n-1 {
				return n - 1
			}
			if curr == 1 {
				p++
				break
			}
			curr = gcd(curr, a[nxtp])
		}
	}
	return maxSoFar
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	if a < 0 {
		a *= -1
	}
	return a
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}


3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or aliveNEKO#ΦωΦ has just got a new maze game on her PC!The game's main puzzle is a maze, in the forms of a $$$2 \times n$$$ rectangle grid. NEKO's task is to lead a Nekomimi girl from cell $$$(1, 1)$$$ to the gate at $$$(2, n)$$$ and escape the maze. The girl can only move between cells sharing a common side.However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.After hours of streaming, NEKO finally figured out there are only $$$q$$$ such moments: the $$$i$$$-th moment toggles the state of cell $$$(r_i, c_i)$$$ (either from ground to lava or vice versa).Knowing this, NEKO wonders, after each of the $$$q$$$ moments, whether it is still possible to move from cell $$$(1, 1)$$$ to cell $$$(2, n)$$$ without going through any lava cells.Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?

The first line contains integers $$$n$$$, $$$q$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le q \le 10^5$$$). The $$$i$$$-th of $$$q$$$ following lines contains two integers $$$r_i$$$, $$$c_i$$$ ($$$1 \le r_i \le 2$$$, $$$1 \le c_i \le n$$$), denoting the coordinates of the cell to be flipped at the $$$i$$$-th moment. It is guaranteed that cells $$$(1, 1)$$$ and $$$(2, n)$$$ never appear in the query list.

For each moment, if it is possible to travel from cell $$$(1, 1)$$$ to cell $$$(2, n)$$$, print "Yes", otherwise print "No". There should be exactly $$$q$$$ answers, one after every update. You can print the words in any case (either lowercase, uppercase or mixed).

NoteWe'll crack down the example test here:  After the first query, the girl still able to reach the goal. One of the shortest path ways should be: $$$(1,1) \to (1,2) \to (1,3) \to (1,4) \to (1,5) \to (2,5)$$$.  After the second query, it's impossible to move to the goal, since the farthest cell she could reach is $$$(1, 3)$$$.  After the fourth query, the $$$(2, 3)$$$ is not blocked, but now all the $$$4$$$-th column is blocked, so she still can't reach the goal.  After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.

package main

import (
	"bufio"
	"fmt"
	"os"
)

func checkOpenRoute(x int, y int) bool {
	var prefix int
	if check[x][y] {
		prefix = 1
	} else {
		prefix = -1
	}
	sum += (getCellParams(1 - x, y - 1) + getCellParams(1 - x, y) + getCellParams(1 - x, y + 1)) * prefix
	check[x][y] = !check[x][y]
	if sum >= 0 {
		return true
	} else {
		return false
	}
}

func getCellParams(x int, y int) int {
	if y < 0 { return 0 }
	if y == n { return 0 }
	if check[x][y] {
		return 1
	} else {
		return 0
	}
}

var check = make([][]bool, 2)
var sum = 0
var n, k int
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var x, y int
	fmt.Fscan(in, &n, &k)
	check[0] = make([]bool, n)
	check[1] = make([]bool, n)
	for i :=0; i < k; i++ {
		fmt.Fscan(in, &x, &y)
		if checkOpenRoute(x - 1, y - 1) {
			fmt.Fprintln(out, "Yes")
		} else {
			fmt.Fprintln(out, "No")
		}
	}
}

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

//=====I/O=====

var (
	sc = bufio.NewScanner(os.Stdin)
	wr = bufio.NewWriter(os.Stdout)
)

func scanInt() int {
	sc.Scan()
	a,_ := strconv.Atoi(sc.Text())
	return a
}

func scanInt64() int64 {
	sc.Scan()
	a,_ := strconv.ParseInt(sc.Text(),10,64)
	return a
}

func scanFloat64() float64 {
	sc.Scan()
	a,_ := strconv.Atoi(sc.Text())
	return float64(a)
}

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ { res[i] = scanInt() }
	return res
}

func scanText() string {
	sc.Scan()
	return sc.Text()
}

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e)
		if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr)
	wr.Flush()
}

//•*¨*•.¸¸♪func•*¨*•.¸¸♪

//•*¨*•.¸¸♪main•*¨*•.¸¸♪

func main() {
	sc.Split(bufio.ScanWords)
	// sc.Buffer(make([]byte, 10000), 100000000)

	n := scanInt()
	q := scanInt()

	// g := make([][]int, 2)
	// g[0] = make([]int, n)
	// g[1] = make([]int, n)

	f := make([][]bool, 2)
	f[0] = make([]bool, n)
	f[1] = make([]bool, n)

	cnt := 0
	for i := 0; i < q; i++ {
		r := scanInt()-1
		c := scanInt()-1

		if f[r][c] {
			ch := 0
			if f[1-r][c] {
				ch++
			}
			if c>0&&f[1-r][c-1] {
				ch++
			}
			if c<n-1&&f[1-r][c+1] {
				ch++
			}

			cnt-=ch

			f[r][c] = false
		} else {
			ch := 0
			if f[1-r][c] {
				ch++
			}
			if c>0&&f[1-r][c-1] {
				ch++
			}
			if c<n-1&&f[1-r][c+1] {
				ch++
			}

			cnt+=ch

			// g[r][c] = c

			f[r][c] = true
		}

		if cnt==0 {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}

		// fmt.Println(f[0])
		// fmt.Println(f[1])
		// fmt.Println(cnt)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type points struct {
	y1 int
	x1 int
	y2 int
	x2 int
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n int
	var q int
	fmt.Fscan(r, &n, &q)
	grid := make([][]int, 2)
	grid[0] = make([]int, n)
	grid[1] = make([]int, n)
	blockers := make(map[points]bool)
	for ; q > 0; q-- {
		var y int
		var x int
		fmt.Fscan(r, &y, &x)
		y--
		x--
		grid[y][x] = 1 - grid[y][x]
		ny := 1 - y
		for dx := -1; dx <= 1; dx++ {
			nx := x + dx
			if ny < 0 || nx < 0 || ny >= 2 || nx >= n || grid[ny][nx] == 0 {
				continue
			}
			switch grid[y][x] {
			case 1:
				blockers[points{y, x, ny, nx}] = true
			case 0:
				delete(blockers, points{y, x, ny, nx})
				delete(blockers, points{ny, nx, y, x})
			}
		}
		switch len(blockers) {
		case 0:
			fmt.Fprintln(w, "Yes")
		default:
			fmt.Fprintln(w, "No")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n int
	var q int
	fmt.Fscan(r, &n, &q)
	grid := make([][]int, 2)
	grid[0] = make([]int, n)
	grid[1] = make([]int, n)
	di := []int{-1, -1, -1, 1, 1, 1}
	dj := []int{-1, 0, 1, -1, 0, 1}
	blocked := 0
	for ; q > 0; q-- {
		var i int
		var j int
		fmt.Fscan(r, &i, &j)
		i--
		j--
		grid[i][j] = 1 - grid[i][j]
		for d := 0; d < 6; d++ {
			ni := i + di[d]
			nj := j + dj[d]
			if ni < 0 || nj < 0 || ni >= 2 || nj >= n || grid[ni][nj] == 0 {
				continue
			}
			switch grid[i][j] {
			case 1:
				blocked++
			case 0:
				blocked--
			}
		}
		switch blocked {
		case 0:
			fmt.Fprintln(w, "Yes")
		default:
			fmt.Fprintln(w, "No")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n int
	var q int
	fmt.Fscan(r, &n, &q)
	grid := make([][]int, 2)
	grid[0] = make([]int, n)
	grid[1] = make([]int, n)
	blocked := 0
	for ; q > 0; q-- {
		var y int
		var x int
		fmt.Fscan(r, &y, &x)
		y--
		x--
		grid[y][x] = 1 - grid[y][x]
		ny := 1 - y
		for dx := -1; dx <= 1; dx++ {
			nx := x + dx
			if ny < 0 || nx < 0 || ny >= 2 || nx >= n || grid[ny][nx] == 0 {
				continue
			}
			switch grid[y][x] {
			case 1:
				blocked++
			case 0:
				blocked--
			}
		}
		switch blocked {
		case 0:
			fmt.Fprintln(w, "Yes")
		default:
			fmt.Fprintln(w, "No")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader=bufio.NewReader(os.Stdin)
var writer *bufio.Writer=bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}){fmt.Fprintf(writer,f,a...)}
func scanf(f string,a...interface{}){fmt.Fscanf(reader,f,a...)}

func main(){
	defer writer.Flush()
	var n,q,r,c int
	var state [2][100010]bool
	var block [100010]bool
	var conn [100010]int
	scanf("%d%d\n",&n,&q)
	cnt:=0
	for ;q>0;q--{
		scanf("%d%d\n",&r,&c)
		if r==1{
			if state[0][c] {
				if block[c]{
					block[c]=false
					cnt--
				}
			}else{
				if conn[c]>0 {
					if(!block[c]){
						block[c]=true
						cnt++
					}
				}
			}
			state[0][c]=!state[0][c]
		}else{
			if state[1][c]{
				for i:=-1;i<=1;i++{
					if c+i>0 && c+i<=n {
						conn[c+i]--
						if conn[c+i]==0 && block[c+i]{
							block[c+i]=false
							cnt--
						}
					}
				}
			}else{
				for i:=-1;i<=1;i++{
					if c+i>0 && c+i<=n {
						conn[c+i]++
						if conn[c+i]==1 && state[0][c+i] && !block[c+i]{
							block[c+i]=true
							cnt++
						}
					}
				}
			}
			state[1][c]=!state[1][c]
		}
		if cnt>0 {printf("No\n")}else{printf("Yes\n")}
	}
}

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

//=====I/O=====

var (
	sc = bufio.NewScanner(os.Stdin)
	wr = bufio.NewWriter(os.Stdout)
)

func scanInt() int {
	sc.Scan()
	a,_ := strconv.Atoi(sc.Text())
	return a
}

func scanInt64() int64 {
	sc.Scan()
	a,_ := strconv.ParseInt(sc.Text(),10,64)
	return a
}

func scanFloat64() float64 {
	sc.Scan()
	a,_ := strconv.Atoi(sc.Text())
	return float64(a)
}

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ { res[i] = scanInt() }
	return res
}

func scanText() string {
	sc.Scan()
	return sc.Text()
}

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e)
		if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr)
	wr.Flush()
}

//•*¨*•.¸¸♪func•*¨*•.¸¸♪

//•*¨*•.¸¸♪main•*¨*•.¸¸♪

func main() {
	sc.Split(bufio.ScanWords)
	// sc.Buffer(make([]byte, 10000), 100000000)

	n := scanInt()
	q := scanInt()

	// g := make([][]int, 2)
	// g[0] = make([]int, n)
	// g[1] = make([]int, n)

	f := make([][]bool, 2)
	f[0] = make([]bool, n)
	f[1] = make([]bool, n)

	cnt := 0
	for i := 0; i < q; i++ {
		r := scanInt()-1
		c := scanInt()-1

		if f[r][c] {
			ch := 0
			if f[1-r][c] {
				ch++
			}
			if c>0&&f[1-r][c-1] {
				ch++
			}
			if c<n-1&&f[1-r][c+1] {
				ch++
			}
			if ch>0 {
				cnt--
			}

			f[r][c] = false
		} else {
			ch := 0
			if f[1-r][c] {
				ch++
			}
			if c>0&&f[1-r][c-1] {
				ch++
			}
			if c<n-1&&f[1-r][c+1] {
				ch++
			}
			if ch>0 {
				cnt++
			}

			// g[r][c] = c

			f[r][c] = true
		}

		if cnt==0 {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}

		// fmt.Println(f[0])
		// fmt.Println(f[1])
		// fmt.Println(cnt)
	}
}


Innokentiy decides to change the password in the social net "Contact!", but he is too lazy to invent a new password by himself. That is why he needs your help. Innokentiy decides that new password should satisfy the following conditions:  the length of the password must be equal to n,  the password should consist only of lowercase Latin letters,  the number of distinct symbols in the password must be equal to k,  any two consecutive symbols in the password must be distinct. Your task is to help Innokentiy and to invent a new password which will satisfy all given conditions.

The first line contains two positive integers n and k (2 ≤ n ≤ 100, 2 ≤ k ≤ min(n, 26)) — the length of the password and the number of distinct symbols in it.  Pay attention that a desired new password always exists.

Print any password which satisfies all conditions given by Innokentiy.

NoteIn the first test there is one of the appropriate new passwords — java, because its length is equal to 4 and 3 distinct lowercase letters a, j and v are used in it.In the second test there is one of the appropriate new passwords — python, because its length is equal to 6 and it consists of 6 distinct lowercase letters.In the third test there is one of the appropriate new passwords — phphp, because its length is equal to 5 and 2 distinct lowercase letters p and h are used in it.Pay attention the condition that no two identical symbols are consecutive is correct for all appropriate passwords in tests.

package main

import "fmt"

func main() {
	var n, k uint8
	var b []byte
	var i uint8

	fmt.Scanf("%d %d", &n, &k)

	b = make([]byte, n)

	for i = uint8(0); i < k; i++ {
		b[i] = i + 97
	}
	for ; i < n; i++ {
		b[i] = b[i%k]
	}

	fmt.Println(string(b))

}

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	var n, k uint8
	var b []byte
	var i uint8
	var r *rand.Rand
	var seedIndex int
	var tmp []int
	var before byte

	fmt.Scanf("%d %d", &n, &k)

	b = make([]byte, n)
	tmp = make([]int, 26)

	r = rand.New(rand.NewSource(time.Now().UnixNano()))

	i = uint8(0)

	for i < k {
		seedIndex = r.Intn(26)
		if tmp[seedIndex] == 1 {
			continue
		}
		tmp[seedIndex] = 1
		b[i] = byte(seedIndex) + 97
		i++
	}
	before = b[k-1]
	for i < n {
		seedIndex = r.Intn(int(k))
		// fmt.Println(i, seedIndex)
		if b[seedIndex%int(k)] == before {
			continue
		}
		b[i] = b[seedIndex%int(k)]
		before = b[i]
		i++
	}

	fmt.Println(string(b))

}

package main

import "fmt"

func main() {
	var n, k int
	fmt.Scanf("%d %d\n", &n, &k)
	var pass string
	start := 97
	l := 0
	for i := 0; i < n; i++ {
		if l == k {
			l = 0
		}
		pass += string(rune(start + l))
		l++
	}
	fmt.Println(pass)
}

package main

import (
	"fmt"
)

func main() {
	var alphabet = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
	var n, k int
	fmt.Scanln(&n, &k)

	letters := alphabet[:k]

	var ans string

	for i := 0; i < n; i++ {
		tempIndex := i
		for {
			if tempIndex < len(letters) {
				ans += letters[tempIndex]
				break
			} else {
				tempIndex -= len(letters)
			}
		}
	}

	fmt.Println(ans)
}

package main
import (
	"fmt"
	"strings"
)
func main() {
	var n, k int
	fmt.Scan(&n,&k)
	var str strings.Builder
	for i := 0; i < n; i++{
		str.WriteString(string((i%k)+97))
	}
	fmt.Println(str.String()[0:n])
}

package main
import (
	"fmt"
	"strings"
)
func main() {
	var n, k int
	fmt.Scan(&n,&k)
	var str strings.Builder
	for i := 0; i < n; i++{
		if i < k {
			str.WriteString(string((i)+97))
		}else{
			str.WriteString(str.String()[0:k])
		}
	}
	fmt.Println(str.String()[0:n])
}

package main

import (
  "fmt"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d\n", &n, &k)
	baseCharCode := 97
	for i := 0; i < n; i++ {
		fmt.Print(string(baseCharCode + (i % k)))
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, k int
	dict := "abcdefghijklmnopqrstuvwxyz"
	fmt.Scan(&n, &k)
	newDict := dict[:k]
	result := ""
	j := 0
	for i := 0; i < n; i++ {
		if j >= k {
			j = 0
		}
		result += string(newDict[j])
		j++
	}
	fmt.Println(result)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	var n, k int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	fmt.Fscan(reader, &k)
	var arr = [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
	for index := 0; index < k; index++ {
		fmt.Print(arr[index])
	}
	wordlength := k
	for index := 1; index < n-k+1; index++ {
		fmt.Print(arr[index-1])
		wordlength++
		if index == k {
			index = 0

		}
		if wordlength == n {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n, k int
	fmt.Fscan(r, &n, &k)
	a := make([]byte, n)
	for i := 0; i < n;{
		for j := 0; j < k && i < n; j, i = j + 1, i + 1 {
			a[i] = 'a' + byte(j)
		}
	}
	fmt.Fprintln(w, string(a))
}

package main

import "fmt"

func main() {
	var n, k int
	fmt.Scanf("%d %d\n", &n, &k)
	var pass string
	start := 'a'
	l := k
	for i := 0; i < n; i++ {
		if l == 0 {
			l = k
		}
		pass += string(start + rune(l))
		l--
	}
	fmt.Println(pass)
}

package main
import (
	"fmt"
	"strings"
    "math/rand"
)
func main() {
	var n, k int
	fmt.Scan(&n,&k)
	var str strings.Builder
	for j := 0; j < n; j++{
		str.WriteString(string(rand.Intn(k)+97))
	}
	fmt.Println(str.String())
}

package main
import (
	"fmt"
	"strings"
)
func main() {
	var n, k int
	fmt.Scan(&n,&k)
	var str strings.Builder
	var ss string
	for i := 0; i < n; i++{
		if i < k {
			str.WriteString(string((i)+97))
			ss = string((i)+97)
		}else{
			str.WriteString(ss)
		}
	}
	fmt.Println(str.String())
}

package main
import (
	"fmt"
	"strings"
)
func main() {
	var n, k int
	fmt.Scan(&n,&k)
	var str strings.Builder
	for i := 0; i < n; i++{
		if i < k {
			str.WriteString(string((i)+97))
		}else{
			str.WriteString(string(str.String()[0]))
		}
	}
	fmt.Println(str.String())
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	var n, k int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	fmt.Fscan(reader, &k)
	var arr = [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "z", "x", "c", "v", "b", "n", "m"}
	for index := 0; index < k; index++ {
		fmt.Print(arr[index])
	}
	wordlength := k
	for index := 1; index < n-k+1; index++ {
		fmt.Print(arr[index-1])
		wordlength++
		if index == k {
			index = 0

		}
		if wordlength == n {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	var n, k int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	fmt.Fscan(reader, &k)
	var arr = [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "z", "x", "c", "v", "b", "n", "m"}
	for index := 0; index < k; index++ {
		fmt.Print(arr[index])
	}
	wordlength := k
	for index := 1; index < n-k; index++ {
		fmt.Print(arr[index-1])
		wordlength++
		if index == k {
			index = 0

		}
		if wordlength == n {
			break
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	var n, k int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	fmt.Fscan(reader, &k)
	var arr = [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "z", "x", "c", "v", "b", "n", "m"}
	for index := 0; index < k; index++ {
		fmt.Print(arr[index])
	}
	for index := 0; index < n-k; index++ {
		fmt.Print(arr[index])
	}
}


You are given $$$n$$$ colored segments on the number line. Each segment is either colored red or blue. The $$$i$$$-th segment can be represented by a tuple $$$(c_i, l_i, r_i)$$$. The segment contains all the points in the range $$$[l_i, r_i]$$$, inclusive, and its color denoted by $$$c_i$$$:   if $$$c_i = 0$$$, it is a red segment;  if $$$c_i = 1$$$, it is a blue segment. We say that two segments of different colors are connected, if they share at least one common point. Two segments belong to the same group, if they are either connected directly, or through a sequence of directly connected segments. Find the number of groups of segments.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of segments.  Each of the next $$$n$$$ lines contains three integers $$$c_i, l_i, r_i$$$ ($$$0 \leq c_i \leq 1, 0 \leq l_i \leq r_i \leq 10^9$$$), describing the $$$i$$$-th segment.  It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print a single integer $$$k$$$, the number of groups of segments.

NoteIn the first example there are $$$5$$$ segments. The segments $$$1$$$ and $$$2$$$ are connected, because they are of different colors and share a point. Also, the segments $$$2$$$ and $$$3$$$ are connected, and so are segments $$$4$$$ and $$$5$$$. Thus, there are two groups: one containing segments $$$\{1, 2, 3\}$$$, and the other one containing segments $$$\{4, 5\}$$$.

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	type ival struct { c,l,r,idx int }
	type event struct { typ,t,idx int }
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); iarr := make([]ival,0,N); for i:=0;i<N;i++ { iarr = append(iarr,ival{gi(),gi(),gi(),i} ) }
		earr := make([]event,0,2*N)
		for i,ii := range iarr { earr = append(earr,event{1,ii.l,i},event{-1,ii.r,i})}
		sort.Slice(earr,func(i,j int) bool { return earr[i].t < earr[j].t || earr[i].t == earr[j].t && earr[i].typ == 1 && earr[j].typ == -1 })
		redgrp := make(map[int]bool); bluegrp := make(map[int]bool); redsolo := make(map[int]bool); bluesolo := make(map[int]bool)
		ans := 0
		for _,e := range earr {
			if e.typ == 1 {
				if iarr[e.idx].c == 0 { // Red case
					if len(bluegrp) > 0 || len(bluesolo) > 0 {
						redgrp[e.idx] = true
						for k := range bluesolo { delete(bluesolo,k); bluegrp[k] = true }
					} else {
						redsolo[e.idx] = true
					}
				} else {
					if len(redgrp) > 0 || len(redsolo) > 0 {
						bluegrp[e.idx] = true
						for k := range redsolo { delete(redsolo,k); redgrp[k] = true }
					} else {
						bluesolo[e.idx] = true
					}
				}
			} else {
				if redsolo[e.idx] {
					delete(redsolo,e.idx); ans++
				} else if bluesolo[e.idx] {
					delete(bluesolo,e.idx); ans++
				} else if redgrp[e.idx] {
					delete(redgrp,e.idx); if len(redgrp) == 0 && len(bluegrp) == 0 { ans++ }
				} else if bluegrp[e.idx] {
					delete(bluegrp,e.idx); if len(redgrp) == 0 && len(bluegrp) == 0 { ans++ }
				}
			}
		}
		fmt.Fprintln(wrtr,ans)
	}
}


You are given two positive integer numbers x and y. An array F is called an y-factorization of x iff the following conditions are met:  There are y elements in F, and all of them are integer numbers;  . You have to count the number of pairwise distinct arrays that are y-factorizations of x. Two arrays A and B are considered different iff there exists at least one index i (1 ≤ i ≤ y) such that Ai ≠ Bi. Since the answer can be very large, print it modulo 109 + 7.

The first line contains one integer q (1 ≤ q ≤ 105) — the number of testcases to solve. Then q lines follow, each containing two integers xi and yi (1 ≤ xi, yi ≤ 106). Each of these lines represents a testcase.

Print q integers. i-th integer has to be equal to the number of yi-factorizations of xi modulo 109 + 7.

NoteIn the second testcase of the example there are six y-factorizations:  { - 4,  - 1};  { - 2,  - 2};  { - 1,  - 4};  {1, 4};  {2, 2};  {4, 1}.

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF893E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mx = 1 << 20
	lpf := [mx + 1]int{1: 1}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}
	const mod int64 = 1e9 + 7
	F := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		F[i] = F[i-1] * int64(i) % mod
	}
	pow := func(x, n int64) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	invF := [...]int64{mx: pow(F[mx], mod-2)}
	for i := mx; i > 0; i-- {
		invF[i-1] = invF[i] * int64(i) % mod
	}
	C := func(n, k int) int64 { return F[n] * invF[k] % mod * invF[n-k] % mod }
	pow2 := [mx + 1]int64{1}
	for i := 1; i <= mx; i++ {
		pow2[i] = pow2[i-1] << 1 % mod
	}

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		ans := int64(1)
		Fscan(in, &n, &m)
		for n > 1 {
			p, e := lpf[n], 1
			for n /= p; lpf[n] == p; n /= p {
				e++
			}
			ans = ans * C(e+m-1, e) % mod
		}
		Fprintln(out, ans*pow2[m-1]%mod)
	}
}

func main() { CF893E(os.Stdin, os.Stdout) }


HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≤ fi ≤ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th.It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time.

The first line contains a positive integer n (1 ≤ n ≤ 2·105) — the number of fragments. The second line contains n different integers fi (1 ≤ fi ≤ n) — the number of the fragment written in the i-th sector.

Print the only integer — the number of time units needed to read the file.

NoteIn the second example the head moves in the following way:  1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units  2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units  3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units  4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10.

package main

import (
         "fmt"
         "bufio"
         "os"
       )

func main() {
    stin := bufio.NewReader(os.Stdin)
    var n int
    fmt.Fscanln(stin, &n)
    var f [200000]int
    var p [200000]int64
    for i := 0; i < n; i++ {
        fmt.Fscan(stin, &f[i])
    }

    for i := 0; i < n; i++ {
        p[f[i]-1] = int64(i)
    }
    s := int64(0)
    for i := 1; i < n; i++ {
        //s += abs(p[i] - p[i-1])

        q := p[i] - p[i-1]
        if q > 0 {
            s += q
        } else {
            s -= q
        }
    }
    fmt.Printf("%v", s)
}

package main

import (
	"fmt"
	"bufio"
	"os"
	"strings"
	"strconv"
	// "sort"
	// "math"
)

//N _
const (
	N = 2*1e5 + 5
)

func inputter(inputReader *bufio.Reader,strs chan *string){
	for {
		input , _ := inputReader.ReadString('\n')
		input = strings.TrimSpace(input)
		if len(input) > 0 {
			inputs := strings.Split(input, " ")
			for i := range inputs{
				strs <- &inputs[i]
			}
		}
	}
}

func abs(a int64) int64 {
	if a > 0{
		return a
	}
	return 0 - a
}

func main(){
	rs := bufio.NewReader(os.Stdin)
	ws := bufio.NewWriter(os.Stdout)

	strs := make(chan *string, 100)

	go inputter(rs, strs)

	n , _ := strconv.ParseInt(*<-strs, 10, 64)

	a := make([]int64, n + 1)

	for i := int64(1); i < n + 1; i++ {
		x, _ := strconv.ParseInt(*<-strs, 10, 64)
		a[x] = i
	}

	tot := int64(0)

	for i := int64(1); i < n; i++ {
		tot += abs(a[i] - a[i + 1])
	}

	ws.WriteString(fmt.Sprint(tot))

	ws.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func Abs(x int64) int64 {
	switch {
	case x < 0:
		return -x
	case x == 0:
		return 0 // return correctly abs(-0)
	}
	return x
}

func Scan(args ...interface{}) (e error)             { _, e = fmt.Fscan(in, args...); return }
func Scanf(ft string, args ...interface{}) (e error) { _, e = fmt.Fscanf(in, ft, args...); return }
func Scanln(args ...interface{}) (e error)           { _, e = fmt.Fscanln(in, args...); return }
func Print(args ...interface{})                      { out.WriteString(fmt.Sprint(args...)) }
func Printf(ft string, args ...interface{})          { out.WriteString(fmt.Sprintf(ft, args...)) }
func Println(args ...interface{})                    { out.WriteString(fmt.Sprintln(args...)) }

var (
	dataIn = "data.in"
	in     = bufio.NewReader(os.Stdin)
	//in, _ = os.Open(dataIn)
	out   = bufio.NewWriter(os.Stdout)
)

var n int
var loc []int

func read() {
	loc = make([]int, n)
	var sec int
	for i := 0; i < n; i++ {
		Scan(&sec)
		loc[sec-1] = i
	}
}

func solve() {
	ret := int64(0)
	for i := 0; i < n-1; i++ {
		ret += Abs(int64(loc[i] - loc[i+1]))
	}
	Println(ret)
}

func main() {
	defer out.Flush()
	for Scan(&n) != io.EOF {
		read()
		solve()
	}
	return
}

package main

import (
  "fmt"
  "bufio"
  "os"
)

func abs(x int) int {
	if x < 0 {
		return -x
	} else {
		return x
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanln(in, &n)
	pos := map[int]int{}
	for i := 0; i < n; i++ {
		var part int
		fmt.Fscan(in, &part)
		pos[part] = i
	}
	ans := int64(0)
	for part := 2; part <= n; part++ {
		ans += int64(abs(pos[part] - pos[part - 1]))
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func Abs(x int) int {
	switch {
	case x < 0:
		return -x
	case x == 0:
		return 0 // return correctly abs(-0)
	}
	return x
}

func Scan(args ...interface{}) (e error)             { _, e = fmt.Fscan(in, args...); return }
func Scanf(ft string, args ...interface{}) (e error) { _, e = fmt.Fscanf(in, ft, args...); return }
func Scanln(args ...interface{}) (e error)           { _, e = fmt.Fscanln(in, args...); return }
func Print(args ...interface{})                      { out.WriteString(fmt.Sprint(args...)) }
func Printf(ft string, args ...interface{})          { out.WriteString(fmt.Sprintf(ft, args...)) }
func Println(args ...interface{})                    { out.WriteString(fmt.Sprintln(args...)) }

var (
	dataIn = "data.in"
	in     = bufio.NewReader(os.Stdin)
	//in, _ = os.Open(dataIn)
	out   = bufio.NewWriter(os.Stdout)
)

var n int
var loc []int

func read() {
	loc = make([]int, n)
	var sec int
	for i := 0; i < n; i++ {
		Scan(&sec)
		loc[sec-1] = i
	}
}

func solve() {
	ret := 0
	for i := 0; i < n-1; i++ {
		ret += Abs(loc[i] - loc[i+1])
	}
	Println(ret)
}

func main() {
	defer out.Flush()
	for Scan(&n) != io.EOF {
		read()
		solve()
	}
	return
}

package main

import (
  "fmt"
  "bufio"
  "os"
)

var in = bufio.NewReader(os.Stdin)

func abs(x int) int {
	if x < 0 {
		return -x
	} else {
		return x
	}
}

func main() {
	var n int
	fmt.Fscanln(in, &n)
	pos := map[int]int{}
	for i := 0; i < n; i++ {
		var part int
		fmt.Fscan(in, &part)
		pos[part] = i
	}
	ans := 0
	for part := 2; part <= n; part++ {
		ans += abs(pos[part] - pos[part - 1])
	}
	fmt.Println(ans)
}


You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, and an array $$$b$$$, with length $$$n$$$. Initially $$$b_i=0$$$ for each $$$1 \leq i \leq n$$$.In one move you can choose an integer $$$i$$$ ($$$1 \leq i \leq n$$$), and add $$$a_i$$$ to $$$b_i$$$ or subtract $$$a_i$$$ from $$$b_i$$$. What is the minimum number of moves needed to make $$$b$$$ increasing (that is, every element is strictly greater than every element before it)?

The first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 5000$$$). The second line contains $$$n$$$ integers, $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array $$$a$$$.

Print a single integer, the minimum number of moves to make $$$b$$$ increasing.

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}

	tot := int64(math.MaxInt64)
	for i := 0; i < n; i++ {
		var sum int64
		var last int64
		for j := i - 1; j >= 0; j-- {
			d := (last / arr[j]) + 1
			sum += d
			last = d * arr[j]
		}

		last = 0
		for j := i + 1; j < n; j++ {
			d := (last / arr[j]) + 1
			sum += d
			last = d * arr[j]
		}
		if sum < tot {
			tot = sum
		}
	}
	fmt.Fprintln(out, tot)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			d := (last / arr[i]) + 1
			tot += d
			last = d * arr[i]
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			d := (last / arr[i]) + 1
			tot += d
			last = d * arr[i]
		}

		return tot
	}

	tot := score(0)
	for i := 1; i < n; i++ {
		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

import (
	"bufio"
	. "fmt"
	"math"
	"os"
)

const (
	INF = 3e5 + 10
	L0  = int64(0)
	L1  = int64(1)
)

var ans int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	Fscan(in, &n)

	a := make([]int64, n)
	for i := 0; i < n; i++ {
		Fscan(in, &a[i])

	}
	ans = math.MaxInt64
	for i := 0; i < n; i++ {
		Cal(i, n, a)
	}
	Fprintln(out, ans)

}

func Cal(x int, n int, a []int64) int64 {
	res := L0
	cur := L0
	for i := x + 1; i < n; i++ {
		k := (cur + a[i]) / a[i]
		res += k
		cur = a[i] * k
	}

	cur = 0
	for i := x - 1; i >= 0; i-- {
		k := (cur + a[i]) / a[i]
		res += k
		cur = a[i] * k
	}
	if res < ans {
		ans = res
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	nums := make([]int64, t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &nums[i])
	}
	var ans int64 = -1
	for i := 0; i < t; i++ {
		temp := int64(0)
		var last int64
		for j := i - 1; j >= 0; j-- {
			temp += last/nums[j] + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		last = 0
		for j := i + 1; j < t; j++ {
			temp += last/nums[j] + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		if temp < ans || ans == -1 {
			ans = temp
		}
	}
	fmt.Fprintln(out, ans)
	//for ii := 0; ii < t; ii++ {
	//	var n int
	//	var m int64
	//	fmt.Fscan(in, &n, &m)
	//	nums := make([]int64, n)
	//	for i := 0; i < n; i++ {
	//		fmt.Fscan(in, &nums[i])
	//	}
	//	sort.Slice(nums, func(i, j int) bool {
	//		return nums[i] < nums[j]
	//	})
	//	used := 1 + nums[n-1]
	//	for i := n - 2; i >= 0; i-- {
	//		if i == 0 {
	//			used += nums[n-1] + 1
	//		} else {
	//			used += nums[i] + 1
	//		}
	//	}
	//	if used <= m {
	//		fmt.Fprintln(out, "YES")
	//	} else {
	//		fmt.Fprintln(out, "NO")
	//	}
	//}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	c1668(os.Stdin, os.Stdout)
}

func c1668(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var x, n int64
	Fscan(in, &n)

	row := []int64{}
	for i := int64(0); i < n; i++ {
		Fscan(in, &x)
		row = append(row, x)
	}

	ans := int64(1<<63 - 1)
	for z := int64(0); z < n; z++ {
		pre, t := int64(0), int64(0)
		for i := z - 1; i >= 0; i-- {
			t += pre/row[i] + 1
			pre = (pre/row[i] + 1) * row[i]
		}
		pre = 0
		for i := z + 1; i < n; i++ {
			t += pre/row[i] + 1
			pre = (pre/row[i] + 1) * row[i]
		}
		if t < ans {
			ans = t
		}
	}

	Fprintln(out, ans)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
6
2 5 4 2 3 1
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	const maxInt = int64(^uint64(0) >> 1)
	minCnt := maxInt

	for i := 0; i < n; i++ {
		cnt := int64(0)

		prev := int64(0)
		for j := i + 1; j < n; j++ {
			k := prev/int64(a[j]) + 1
			cnt += k
			prev = k * int64(a[j])
		}

		prev = int64(0)
		for j := i - 1; j >= 0; j-- {
			k := prev/int64(a[j]) + 1
			cnt += k
			prev = k * int64(a[j])
		}

		minCnt = min(minCnt, cnt)
	}

	fmt.Fprintln(writer, minCnt)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	//scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnhandledErrorResult
func factorize(num int) []int {
	factors := []int{1}

	sqrtA := int(math.Sqrt(float64(num)))

	for ftr := 2; ftr <= sqrtA; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > sqrtA {
		factors = append(factors, num)
	}

	return factors
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	CF1667A(os.Stdin, os.Stdout)
}

func CF1667A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()

	var n int64
	var ans int64 = 1e16
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}

	for pos := int64(0); pos < n; pos++ { //枚举0所在的位置
		var sum int64
		//分别处理[0, pos) (pos, n)
		pre := int64(0)
		for left := pos - 1; left >= 0; left-- {
			pre--
			k := cal(-pre, a[left])
			sum += k
			pre = -(k * a[left])
		}
		pre = int64(0)
		for right := pos + 1; right < n; right++ {
			pre++
			k := cal(pre, a[right])
			sum += k
			pre = k * a[right]
		}
		if sum < ans {
			ans = sum
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	//CF1667A(os.Stdin, os.Stdout)
	cf1667A(os.Stdin, os.Stdout)
}

func cf1667A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	fmt.Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	ans := int64(1e18)
	for i := range a {
		cur := int64(0)
		for j, pre := i+1, int64(0); j < n; j++ {
			cnt := pre/a[j] + 1
			cur += cnt
			pre = a[j] * cnt
		}
		for j, pre := i-1, int64(0); j >= 0; j-- {
			cnt := pre/a[j] + 1
			cur += cnt
			pre = a[j] * cnt
		}
		if ans > cur {
			ans = cur
		}
	}
	fmt.Fprintln(out, ans)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	//notAdding()
	makeItIncreasing()
}

func makeItIncreasing() {
	n := 0
	fmt.Scan(&n)
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&arr[i])
	}
	var ans int64 = math.MaxInt64
	for i := 0; i < n; i++ {
		var cnt int64 = 0
		var cur int64 = 0
		for j := i + 1; j < n; j++ {
			d := cur/arr[j] + 1
			cur = arr[j] * d
			cnt += d
		}
		cur = 0
		for j := i - 1; j >= 0; j-- {
			d := cur/(-arr[j]) + 1
			cur = -arr[j] * d
			cnt += d
		}
		ans = min(ans, cnt)
	}
	fmt.Println(ans)
}

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var n int
	fmt.Fscan(in, &n)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	res := int64(1e18)
	for i := 0; i < n; i++ {
		cur := int64(0)
		pr := int64(0)

		for j := i + 1; j < n; j++ {
			pr = (pr/a[j] + 1) * a[j]
			cur += pr / int64(a[j])
		}
		pr = 0
		for j := i - 1; j >= 0; j-- {
			pr = (pr/a[j] + 1) * a[j]
			cur += pr / int64(a[j])
		}
		if cur < res {
			res = cur
		}
	}
	fmt.Fprintln(out, res)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

var (
	a [5005]int64
)

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int64) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int64(b&15)
		}
		return
	}
	n := read()
	var i int64
	for i = 1; i <= n; i++ {
		a[i] = read()
	}
	var ans int64 = 1e18
	for i = 1; i <= n; i++ {
		var tmp, pre int64
		for j := i - 1; j >= 1; j-- {
			x := pre/a[j] + 1
			tmp += x
			pre = x * a[j]
		}
		pre = 0
		for j := i + 1; j <= n; j++ {
			x := pre/a[j] + 1
			tmp += x
			pre = x * a[j]
		}
		if ans > tmp {
			ans = tmp
		}
	}
	Fprint(out, ans)
}

func main() {
	run(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1667A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	Fscan(in, &n)
	if n == 2 {
		Fprint(out, 1)
		return
	}
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	ans := int64(1e18)
	for i := 1; i < n-1; i++ {
		c := int64(2)
		for j, pre := i-2, a[i-1]; j >= 0; j-- {
			v := pre/a[j] + 1
			c += v
			pre = a[j] * v
		}
		for j, pre := i+2, a[i+1]; j < n; j++ {
			v := pre/a[j] + 1
			c += v
			pre = a[j] * v
		}
		if c < ans {
			ans = c
		}
	}
	Fprintln(out, ans)
}

func main() { CF1667A(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}

	tot := int64(math.MaxInt64)
	for i := 0; i < n; i++ {
		var sum int64
		var last int64
		for j := i - 1; j >= 0; j-- {
			d := (last / arr[j]) + 1
			sum += d
			last = d * arr[j]
		}

		last = 0
		for j := i + 1; j < n; j++ {
			d := (last / arr[j]) + 1
			tot += d
			last = d * arr[j]
		}
		if sum < tot {
			tot = sum
		}
	}
	fmt.Fprintln(out, tot)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			d := (last / arr[i]) + 1
			tot += d
			last = d * arr[i]
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			d := (last / arr[i]) + 1
			tot += d
			last = d * arr[i]
		}

		return tot
	}

	tot := score(0)
	for i := 1; i < n; i++ {
		d := score(i)
		if d > tot {
			break
		}
		tot = d
	}
	fmt.Fprintln(out, tot)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func abs(n int64) int64 {
	if n < 0 {
		return n * -1
	}
	return n
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}

	max := int64(0)
	m := make([]int64, n)
	for i := 1; i < n-1; i++ {
		m[i] = abs(arr[i-1]-arr[i]) + abs(arr[i+1]-arr[i])
		if m[i] > max {
			max = m[i]
		}
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			var v int64
			for v >= last {
				v -= arr[i]
				tot++
			}

			last = v
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			var v int64
			for v <= last {
				v += arr[i]
				tot++
			}
			last = v
		}

		return tot
	}

	tot := score(0)
	for i, v := range m {
		if v != max {
			continue
		}

		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func abs(n int64) int64 {
	if n < 0 {
		return n * -1
	}
	return n
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}

	max := int64(0)
	m := make([]int64, n)
	for i := 1; i < n-1; i++ {
		m[i] = abs(arr[i-1]-arr[i]) + abs(arr[i+1]-arr[i])
		if m[i] > max {
			max = m[i]
		}
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			var v int64
			for v >= last {
				v -= arr[i]
				tot++
			}

			last = v
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			var v int64
			for v <= last {
				v += arr[i]
				tot++
			}
			last = v
		}

		return tot
	}

	tot := int64(math.MaxInt64)
	for i, v := range m {
		if v != max {
			continue
		}

		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}

	max := int64(0)
	min := int64(math.MaxInt64)
	for i := 1; i < n; i++ {
		if arr[i] > max {
			max = arr[i]
		}

		if arr[i] < min {
			min = arr[i]
		}
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			var v int64
			for v >= last {
				v -= arr[i]
				tot++
			}

			last = v
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			var v int64
			for v <= last {
				v += arr[i]
				tot++
			}
			last = v
		}

		return tot
	}

	tot := int64(math.MaxInt64)
	for i := 0; i < n; i++ {
		if arr[i] != max && arr[i] != min {
			continue
		}

		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)

	arr := make([]int64, n)
	max := int64(0)
	min := int64(math.MaxInt32)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
		if arr[i] > max {
			max = arr[i]
		}

		if arr[i] < min {
			min = arr[i]
		}
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			var v int64
			for v >= last {
				v -= arr[i]
				tot++
			}

			last = v
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			var v int64
			for v <= last {
				v += arr[i]
				tot++
			}
			last = v
		}

		return tot
	}

	tot := int64(math.MaxInt32)
	for i := 0; i < n; i++ {
		if arr[i] != max && arr[i] != min {
			continue
		}

		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

import (
	"bufio"
	. "fmt"
	"math"
	"os"
)

const (
	INF = 3e5 + 10
	L0  = int64(0)
	L1  = int64(1)
)

var ans int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	Fscan(in, &n)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		Fscan(in, &a[i])

	}
	ans = math.MaxInt64
	for i := 0; i < n; i++ {
		Cal(i, n, a)
	}
	Fprintln(out, ans)

}

func Cal(x int, n int, a []int) int64 {
	res := L0
	cur := 0
	for i := x + 1; i < n; i++ {
		k := (cur + a[i]) / a[i]
		res += int64(k)
		cur = a[i] * k
	}

	cur = 0
	for i := x - 1; i >= 0; i-- {
		k := (cur + a[i]) / a[i]
		res += int64(k)
		cur = a[i] * k
	}
	if res < ans {
		ans = res
	}
	return res
}

package main

import (
	"bufio"
	. "fmt"
	"math"
	"os"
)

const (
	INF = 3e5 + 10
	L0  = int64(0)
	L1  = int64(1)
)

var ans int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	Fscan(in, &n)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		Fscan(in, &a[i])

	}
	ans = math.MaxInt
	for i := 0; i < n; i++ {
		Cal(i, n, a)
	}
	Fprintln(out, ans)

}

func Cal(x int, n int, a []int) int {
	res := 0
	cur := 0
	for i := x + 1; i < n; i++ {
		k := (cur + a[i]) / a[i]
		res += k
		cur = a[i] * k
	}

	cur = 0
	for i := x - 1; i >= 0; i-- {
		k := (cur + a[i]) / a[i]
		res += k
		cur = a[i] * k
	}
	if res < ans {
		ans = res
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	nums := make([]int64, t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &nums[i])
	}
	ans := int64(t * t)
	for i := 0; i < t; i++ {
		temp := int64(0)
		var last int64
		for j := i - 1; j >= 0; j-- {
			temp += last/nums[j] + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		last = 0
		for j := i + 1; j < t; j++ {
			temp += last/nums[j] + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		if temp < ans {
			ans = temp
		}
	}
	fmt.Fprintln(out, ans)
	//for ii := 0; ii < t; ii++ {
	//	var n int
	//	var m int64
	//	fmt.Fscan(in, &n, &m)
	//	nums := make([]int64, n)
	//	for i := 0; i < n; i++ {
	//		fmt.Fscan(in, &nums[i])
	//	}
	//	sort.Slice(nums, func(i, j int) bool {
	//		return nums[i] < nums[j]
	//	})
	//	used := 1 + nums[n-1]
	//	for i := n - 2; i >= 0; i-- {
	//		if i == 0 {
	//			used += nums[n-1] + 1
	//		} else {
	//			used += nums[i] + 1
	//		}
	//	}
	//	if used <= m {
	//		fmt.Fprintln(out, "YES")
	//	} else {
	//		fmt.Fprintln(out, "NO")
	//	}
	//}
}

//4444444444
//4444444444
//4444444444
//4444444444
//4444444444

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	nums := make([]int64, t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &nums[i])
	}
	ans := t * (t - 1) / 2
	for i := 0; i < t; i++ {
		temp := 0
		var last int64
		for j := i - 1; j >= 0; j-- {
			temp += int(last/nums[j]) + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		last = 0
		for j := i + 1; j < t; j++ {
			temp += int(last/nums[j]) + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		if temp < ans {
			ans = temp
		}
	}
	fmt.Fprintln(out, ans)
	//for ii := 0; ii < t; ii++ {
	//	var n int
	//	var m int64
	//	fmt.Fscan(in, &n, &m)
	//	nums := make([]int64, n)
	//	for i := 0; i < n; i++ {
	//		fmt.Fscan(in, &nums[i])
	//	}
	//	sort.Slice(nums, func(i, j int) bool {
	//		return nums[i] < nums[j]
	//	})
	//	used := 1 + nums[n-1]
	//	for i := n - 2; i >= 0; i-- {
	//		if i == 0 {
	//			used += nums[n-1] + 1
	//		} else {
	//			used += nums[i] + 1
	//		}
	//	}
	//	if used <= m {
	//		fmt.Fprintln(out, "YES")
	//	} else {
	//		fmt.Fprintln(out, "NO")
	//	}
	//}
}

//4444444444
//4444444444
//4444444444
//4444444444
//4444444444

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	CF1667A(os.Stdin, os.Stdout)
}

func CF1667A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	ans := int64(1e15)
	for k := int64(0); k <= n; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := (b[i-1] + 1) / (-a[i])
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	CF1667A(os.Stdin, os.Stdout)
}

func CF1667A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 10
	ans := int64(1e13)
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := -(b[i-1] + 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	CF1667A(os.Stdin, os.Stdout)
}

func CF1667A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 1
	ans := int64(1e13)
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := -(b[i-1] + 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			if tmp == 12989782 {
				Println(b[0], b[1], b[2])
			}
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	CF1667A(os.Stdin, os.Stdout)
}

func CF1667A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 1
	ans := int64(1e13)
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := -(b[i-1]+1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	CF1667A(os.Stdin, os.Stdout)
}

func CF1667A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 10
	ans := int64(1e18)
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := ((-b[i-1]) - 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	CF1667A(os.Stdin, os.Stdout)
}

func CF1667A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 10
	ans := cnt
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := ((-b[i-1]) - 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	CF1667A(os.Stdin, os.Stdout)
}

func CF1667A(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()

	n := 0
	Fscan(in, &n)

	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 10
	ans := cnt
	for k := 0; k <= cnt; k++ {
		b := make([]int, n)
		b[0] = -a[0] * k
		tmp := k
		for i := 1; i < n; i++ {
			if b[i-1]+1 < 0 {
				m := ((-b[i-1]) - 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int) int {
	if x % y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	//CF1667A(os.Stdin, os.Stdout)
	cf1667A(os.Stdin, os.Stdout)
}

func cf1667A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	ans := int64(1e18)
	for i := range a {
		cur := int64(0)
		for j, pre := i+1, 0; j < n; j++ {
			cnt := pre/a[j] + 1
			cur += int64(cnt)
			pre = a[j] * cnt
		}
		for j, pre := i-1, 0; j >= 0; j-- {
			cnt := pre/a[j] + 1
			cur += int64(cnt)
			pre = a[j] * cnt
		}
		if ans > cur {
			ans = cur
		}
	}
	fmt.Fprintln(out, ans)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	//notAdding()
	makeItIncreasing()
}

func makeItIncreasing() {
	n := 0
	fmt.Scan(&n)
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&arr[i])
	}
	ans := math.MaxInt
	for i := 0; i < n; i++ {
		cnt := 0
		cur := 0
		for j := i + 1; j < n; j++ {
			d := cur/arr[j] + 1
			cur = arr[j] * d
			cnt += d
		}
		cur = 0
		for j := i - 1; j >= 0; j-- {
			d := cur/(-arr[j]) + 1
			cur = -arr[j] * d
			cnt += d
		}
		ans = min(ans, cnt)
	}
	fmt.Println(ans)
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}


The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!There are $$$n$$$ snacks flavors, numbered with integers $$$1, 2, \ldots, n$$$. Bessie has $$$n$$$ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:  First, Bessie will line up the guests in some way.  Then in this order, guests will approach the snacks one by one.  Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.  Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.

The first line contains integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le 10^5$$$), the number of snacks and the number of guests.  The $$$i$$$-th of the following $$$k$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$, $$$x_i \ne y_i$$$), favorite snack flavors of the $$$i$$$-th guest.

Output one integer, the smallest possible number of sad guests.

NoteIn the first example, Bessie can order the guests like this: $$$3, 1, 2, 4$$$. Guest $$$3$$$ goes first and eats snacks $$$1$$$ and $$$4$$$. Then the guest $$$1$$$ goes and eats the snack $$$2$$$ only, because the snack $$$1$$$ has already been eaten. Similarly, the guest $$$2$$$ goes up and eats the snack $$$3$$$ only. All the snacks are gone, so the guest $$$4$$$ will be sad. In the second example, one optimal ordering is $$$2, 1, 3, 5, 4$$$. All the guests will be satisfied.

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func Sol1209D(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()
	var fa []int
	initFa := func(n int) {
		fa = make([]int, n+1)
		for i := range fa {
			fa[i] = i
		}
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}
	merge := func(from, to int) { fa[find(from)] = find(to) }
	same := func(x, y int) bool { return find(x) == find(y) }

	var n, m, v, w, ans int
	Fscan(in, &n, &m)
	initFa(n)
	for ; m > 0; m-- {
		Fscan(in, &v, &w)
		v--
		w--
		if same(v, w) {
			ans++
		} else {
			merge(v, w)
		}
	}
	Fprint(out, ans)
}

func main() {
	Sol1209D(os.Stdin, os.Stdout)
}


Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers.To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets.Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options?

The first line contains a single integer n — the length of the sequence of games (1 ≤ n ≤ 105). The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record.

In the first line print a single number k — the number of options for numbers s and t. In each of the following k lines print two integers si and ti — the option for numbers s and t. Print the options in the order of increasing si, and for equal si — in the order of increasing ti.

package main

import (
	"fmt"
	"sort"
)

type Combo struct {
	s int
	t int
}

type Result []Combo

func (c Result) Len() int {
	return len(c)
}

func (c Result) Less(i, j int) bool {
	if c[i].s != c[j].s {
		return c[i].s < c[j].s
	} else {
		return c[i].t < c[j].t
	}
}

func (c Result) Swap(i, j int) {
	c[i], c[j] = c[j], c[i]
}

var (
	n      int
	match  []int
	a      []int
	b      []int
	m      [][2]int
	result Result
)

func in() {
	fmt.Scanln(&n)
	match = make([]int, n)
	a = make([]int, 2*n)
	b = make([]int, 2*n)
	for i := range a {
		a[i] = -1
		b[i] = -1
	}
	a[0] = 0
	b[0] = 0

	m = make([][2]int, n+1)
	m[0][0] = 0
	m[0][1] = 0
	var ai, bi int = 0, 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &match[i])
		if match[i] == 1 {
			ai++
			a[ai] = i
		} else {
			bi++
			b[bi] = i
		}
		m[i+1][0] = ai
		m[i+1][1] = bi
	}
}

func out() {
	sort.Sort(result)
	fmt.Println(len(result))
	for i := 0; i < len(result); i++ {
		fmt.Printf("%d %d\n", result[i].s, result[i].t)
	}
}

func run() {
	for t := 1; t <= n; t++ {
		a_win := 0
		b_win := 0
		valid := true

		var last_win_by_a bool = false

		for start_idx := 0; start_idx < n; {
			prev_a_score := m[start_idx][0]
			prev_b_score := m[start_idx][1]
			next_a_score := prev_a_score + t
			next_b_score := prev_b_score + t
			next_a_idx := a[next_a_score]
			next_b_idx := b[next_b_score]
			// fmt.Println(start_idx, ": ", prev_a_score, prev_b_score, next_a_score, next_b_score, next_a_idx, next_b_idx)
			if next_a_idx == -1 && next_b_idx == -1 {
				valid = false
				break
			}
			if next_b_idx == -1 || (next_a_idx < next_b_idx && next_a_idx != -1) { // a win
				start_idx = next_a_idx + 1
				a_win++
				last_win_by_a = true
			} else { // b win
				start_idx = next_b_idx + 1
				b_win++
				last_win_by_a = false
			}
		}

		if valid {
			if a_win > b_win && last_win_by_a {
				result = append(result, Combo{a_win, t})
			} else if b_win > a_win && !last_win_by_a {
				result = append(result, Combo{b_win, t})
			}
		}
		// fmt.Println(valid, s, t)
	}
}

func main() {
	in()
	run()
	out()
}


This is the hard version of the problem. The difference is the constraints on $$$n$$$, $$$m$$$ and $$$t$$$. You can make hacks only if all versions of the problem are solved.Alice and Bob are given the numbers $$$n$$$, $$$m$$$ and $$$k$$$, and play a game as follows:The game has a score that Alice tries to maximize, and Bob tries to minimize. The score is initially $$$0$$$. The game consists of $$$n$$$ turns. Each turn, Alice picks a real number from $$$0$$$ to $$$k$$$ (inclusive) which Bob either adds to or subtracts from the score of the game. But throughout the game, Bob has to choose to add at least $$$m$$$ out of the $$$n$$$ turns.Bob gets to know which number Alice picked before deciding whether to add or subtract the number from the score, and Alice gets to know whether Bob added or subtracted the number for the previous turn before picking the number for the current turn (except on the first turn since there was no previous turn).If Alice and Bob play optimally, what will the final score of the game be?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. The description of test cases follows. Each test case consists of a single line containing the three integers, $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le m \le n \le 10^6, 0 \le k < 10^9 + 7$$$) — the number of turns, how many of those turns Bob has to add, and the biggest number Alice can choose, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.

For each test case output a single integer number — the score of the optimal game modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.

NoteIn the first test case, the entire game has $$$3$$$ turns, and since $$$m = 3$$$, Bob has to add in each of them. Therefore Alice should pick the biggest number she can, which is $$$k = 2$$$, every turn.In the third test case, Alice has a strategy to guarantee a score of $$$\frac{75}{8} \equiv 375000012 \pmod{10^9 + 7}$$$.In the fourth test case, Alice has a strategy to guarantee a score of $$$\frac{45}{2} \equiv 500000026 \pmod{10^9 + 7}$$$.

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...); out.Flush() }

const MOD = int(1e9 + 7)

func ExtendedGcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := ExtendedGcd(b, a%b)
	return d, y, (x - (a/b)*y)
}

func Add(a, b int) int {
	return (a + b) % MOD
}

func Mul(a, b int) int {
	return int((int64(a) * int64(b)) % int64(MOD))
}

func Neg(a int) int {
	return (MOD - a) % MOD
}

func Inv(a int) int {
	_, x, _ := ExtendedGcd(a, MOD)
	// x*a + y*MOD = 1
	return ((x % MOD) + MOD) % MOD
}

const TAM = int(1e6 + 100)

var fact [TAM]int
var p2inv [TAM]int

func comb(h, w int) int {
	if h < w {
		return 0
	}
	return Mul(fact[h], Mul(Inv(fact[w]), Inv(fact[h-w])))
}

func solveCase() int {
	var n, m, k int
	scan(&n, &m, &k)

	if m == n {
		return Mul(n, k)
	}

	var r = 0
	for i := 1; i <= n && i <= m; i++ {
		paths := comb(n-i-1, m-i)
		toAdd := Mul(i, Mul(paths, p2inv[n-i]))
		r = Add(r, toAdd)
	}

	return Mul(r, k)
}

func main() {
	defer out.Flush()

	p2inv[0] = 1
	p2inv[1] = Inv(2)
	fact[0] = 1
	for i := 1; i < TAM; i++ {
		p2inv[i] = Mul(p2inv[i-1], p2inv[1])
		fact[i] = Mul(i, fact[i-1])
	}

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		printf("%d\n", solveCase())
	}
}