You are given two permutations $$$a$$$ and $$$b$$$, both consisting of $$$n$$$ elements. Permutation of $$$n$$$ elements is such a integer sequence that each value from $$$1$$$ to $$$n$$$ appears exactly once in it.You are asked to perform two types of queries with them:  $$$1~l_a~r_a~l_b~r_b$$$ — calculate the number of values which appear in both segment $$$[l_a; r_a]$$$ of positions in permutation $$$a$$$ and segment $$$[l_b; r_b]$$$ of positions in permutation $$$b$$$;  $$$2~x~y$$$ — swap values on positions $$$x$$$ and $$$y$$$ in permutation $$$b$$$. Print the answer for each query of the first type.It is guaranteed that there will be at least one query of the first type in the input.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le 2 \cdot 10^5$$$) — the number of elements in both permutations and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — permutation $$$a$$$. It is guaranteed that each value from $$$1$$$ to $$$n$$$ appears in $$$a$$$ exactly once. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — permutation $$$b$$$. It is guaranteed that each value from $$$1$$$ to $$$n$$$ appears in $$$b$$$ exactly once. Each of the next $$$m$$$ lines contains the description of a certain query. These are either:   $$$1~l_a~r_a~l_b~r_b$$$ ($$$1 \le l_a \le r_a \le n$$$, $$$1 \le l_b \le r_b \le n$$$);  $$$2~x~y$$$ ($$$1 \le x, y \le n$$$, $$$x \ne y$$$).

Print the answers for the queries of the first type, each answer in the new line — the number of values which appear in both segment $$$[l_a; r_a]$$$ of positions in permutation $$$a$$$ and segment $$$[l_b; r_b]$$$ of positions in permutation $$$b$$$.

NoteConsider the first query of the first example. Values on positions $$$[1; 2]$$$ of $$$a$$$ are $$$[5, 1]$$$ and values on positions $$$[4; 5]$$$ of $$$b$$$ are $$$[1, 4]$$$. Only value $$$1$$$ appears in both segments.After the first swap (the second query) permutation $$$b$$$ becomes $$$[2, 1, 3, 5, 4, 6]$$$.After the second swap (the sixth query) permutation $$$b$$$ becomes $$$[5, 1, 3, 2, 4, 6]$$$.

package main

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

type query struct {
	op int
	la, ra, lb, rb int
	x, y int
}

var BITQueryMap []map[int]bool
var BITQueryY [][]int

func prepareUpdate(x, y int) {
	n := len(BITQueryMap) - 1
	for ; x <= n; x += x & (-x) {
		BITQueryMap[x][y] = true
	}
}

func prepareGetSum(x, y int) {
	if y <= 0 {
		return
	}
	for ; x > 0; x -= x & (-x) {
		BITQueryMap[x][y] = true
	}
}

func getYPosInXRow(x, y int) int {
	if y <= 0 {
		return 0
	}
	//return y
	s := BITQueryY[x]
	res := sort.SearchInts(s, y)
	//fmt.Printf("getYPosInXRow(x %d, y %d), s %v, res = %d\n", x, y, s, res)
	return res + 1
}

type BITTree2D struct {
	n int
	s [][]int
}

func NewBITTree2D(n int) *BITTree2D {
	t := BITTree2D{n: n, s: make([][]int, n + 1)}
	for i := 1; i <= n; i++ {
		t.s[i] = make([]int, len(BITQueryY[i]) + 1)
		//t.s[i] = make([]int, n + 1)
	}
	return &t
}

func (t *BITTree2D) Update(x, y, addVal int) {
	//fmt.Printf("Update(x %d, y %d, addVal %d)\n", x, y, addVal)
	for x1 := x; x1 <= t.n; x1 += x1 & (-x1) {
		y1 := getYPosInXRow(x1, y)
		//fmt.Printf("len(t.s[%d] = %d\n", x1, len(t.s[x1]))
		for ; y1 < len(t.s[x1]); y1 += y1 & (-y1) {
			//fmt.Printf("in update x1 %d, y1 %d\n", x1, y1)
			t.s[x1][y1] += addVal
		}
	}
}

func (t *BITTree2D) GetSum(x, y int) int {
	//fmt.Printf("GetSum (x %d, y %d)\n", x, y)
	sum := 0
	for x1 := x; x1 > 0; x1 -= x1 & (-x1) {
		y1 := getYPosInXRow(x1, y)
		for ; y1 > 0; y1 -= y1 & (-y1) {
			sum += t.s[x1][y1]
			//fmt.Printf("t.s[%d][%d] = %d\n", x1, y1, t.s[x1][y1])
		}
	}
	//fmt.Printf("GetSum (x %d, y %d), sum %d\n", x, y, sum)
	return sum
}

func (t *BITTree2D) GetRangeSum(xLow, xHigh, yLow, yHigh int) int {
	//fmt.Printf("GetRangeSum(xL %d, xH %d, yL %d, yH %d\n", xLow, xHigh, yLow, yHigh)
	res := t.GetSum(xHigh, yHigh) - t.GetSum(xLow - 1, yHigh) -
		t.GetSum(xHigh, yLow - 1) + t.GetSum(xLow - 1, yLow - 1)
	return res
}

func main() {
	stdin := bufio.NewReader(os.Stdin)
	stdout := bufio.NewWriter(os.Stdout)

	var n, m int
	fmt.Fscan(stdin, &n, &m)
	a := make([]int, n + 1)
	b := make([]int, n + 1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(stdin, &a[i])
	}
	for i := 1; i <= n; i++ {
		fmt.Fscan(stdin, &b[i])
	}
	queries := make([]query, m)
	for i, _ := range queries {
		q := &queries[i]
		fmt.Fscan(stdin, &q.op)
		if q.op == 1 {
			fmt.Fscan(stdin, &q.la, &q.ra, &q.lb, &q.rb)
		} else {
			fmt.Fscan(stdin, &q.x, &q.y)
		}
	}

	posInA := make([]int, n+1)
	for i := 1; i < len(a); i++ {
		posInA[a[i]] = i
	}
	for i := 1; i < len(b); i++ {
		b[i] = posInA[b[i]]
	}
	origB := make([]int, n + 1)
	copy(origB, b)
	BITQueryMap = make([]map[int]bool, n + 1)
	for i := 1; i <= n; i++ {
		BITQueryMap[i] = make(map[int]bool)
	}
	for i := 1; i < len(b); i++ {
		prepareUpdate(i, b[i])
	}
	for _, q := range queries {
		if q.op == 1 {
			prepareGetSum(q.rb, q.ra)
			prepareGetSum(q.rb, q.la - 1)
			prepareGetSum(q.lb -1, q.ra)
			prepareGetSum(q.lb - 1, q.la - 1)
		} else {
			prepareUpdate(q.x, b[q.x])
			prepareUpdate(q.x, b[q.y])
			prepareUpdate(q.y, b[q.x])
			prepareUpdate(q.y, b[q.y])
			b[q.x], b[q.y] = b[q.y], b[q.x]
		}
	}
	copy(b, origB)

	BITQueryY = make([][]int, n + 1)
	for i, m := range BITQueryMap {
		s := make([]int, 0, len(m))
		for k, _ := range m {
			s = append(s, k)
		}
		sort.Ints(s)
		BITQueryY[i] = s
		//fmt.Printf("BITQuery[%d] = %v\n", i, BITQueryY[i])
	}

	t := NewBITTree2D(n)
	for i := 1; i <= n; i++ {
		t.Update(i, b[i], 1)
	}
	for _, q := range queries {
		if q.op == 1 {
			sum := t.GetRangeSum(q.lb, q.rb, q.la, q.ra)
			fmt.Fprintf(stdout, "%d\n", sum)
		} else {
			t.Update(q.x, b[q.x], -1)
			t.Update(q.y, b[q.y], -1)
			t.Update(q.x, b[q.y], 1)
			t.Update(q.y, b[q.x], 1)
			b[q.x], b[q.y] = b[q.y], b[q.x]
			/*
			for x, y := range b {
				fmt.Printf("b[%d] = %d\n", x, y)
			}
			*/
		}
	}
	stdout.Flush()
}


You are given a list of $$$n$$$ integers. You can perform the following operation: you choose an element $$$x$$$ from the list, erase $$$x$$$ from the list, and subtract the value of $$$x$$$ from all the remaining elements. Thus, in one operation, the length of the list is decreased by exactly $$$1$$$.Given an integer $$$k$$$ ($$$k>0$$$), find if there is some sequence of $$$n-1$$$ operations such that, after applying the operations, the only remaining element of the list is equal to $$$k$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n \leq 2\cdot 10^5$$$, $$$1 \leq k \leq 10^9$$$), the number of integers in the list, and the target value, respectively. The second line of each test case contains the $$$n$$$ integers of the list $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \leq a_i \leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases is not greater that $$$2 \cdot 10^5$$$.

For each test case, print YES if you can achieve $$$k$$$ with a sequence of $$$n-1$$$ operations. Otherwise, print NO. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer).

NoteIn the first example we have the list $$$\{4, 2, 2, 7\}$$$, and we have the target $$$k = 5$$$. One way to achieve it is the following: first we choose the third element, obtaining the list $$$\{2, 0, 5\}$$$. Next we choose the first element, obtaining the list $$$\{-2, 3\}$$$. Finally, we choose the first element, obtaining the list $$$\{5\}$$$.

package main

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

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func solve(istream *bufio.Reader) {
	tmp := readArrInt(istream)
	n := tmp[0]
	k := tmp[1]
	values := readArrInt(istream)
	sort.Ints(values)
	rbound := 0
	for i := 0; i < n; i++ {
		for rbound < n && values[i]+k > values[rbound] {
			rbound++
		}
		if rbound < n && values[i]+k == values[rbound] {
			fmt.Printf("yes\n")
			return
		}
	}
	fmt.Printf("no\n")
}

func main() {
	istream := bufio.NewReader(os.Stdin)
	t := readInt(istream)
	for i := 0; i < t; i++ {
		solve(istream)
	}
}

package main
import "fmt"
import "os"
import "bufio"
import "sort"
var (
	input = bufio.NewReader(os.Stdin)
	ouput = bufio.NewWriter(os.Stdout)
)
func main() {
	defer ouput.Flush()
	count := 0
	fmt.Fscan(input,&count)
	for count>0 {
		count --
		n := 0
		k := 0
		fmt.Fscan(input,&n)
		fmt.Fscan(input,&k)
		arr := make([]int,0,n)
		for n>0 {
			n--
			v := 0
			fmt.Fscan(input,&v)
			arr = append(arr,v)
		}
		sort.Ints(arr)
		ans := "NO"
		for l,r:=0,1;l<len(arr)&&r<len(arr); {
			if arr[l] + k == arr[r] {
				ans = "YES"
				break
			}
			if arr[l] + k < arr[r] {
				l ++
			} else {
				r ++
			}
		}
		fmt.Fprintln(ouput,ans)
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		var k int64
		fmt.Fscan(in, &n, &k)

		m := make(map[int64]bool)
		var f bool
		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)

			if m[v-k] || m[v+k] {
				f = true
			}
			m[v] = true
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

var k, t, n int

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func main() {
	ip := bufio.NewReader(os.Stdin)
	t = readInt(ip)
	for ; t > 0; t-- {
		tmp := readArrInt(ip)
		n = tmp[0]
		k = tmp[1]
		a := readArrInt(ip)
		f := false
		l, r := 0, 1
		if n == 1 {
			f = a[0] == k
			goto there
		}
		sort.Ints(a)

		for l < n && r < n {
			if a[l]+abs(k) == a[r] {
				f = true
				goto there
			} else if a[l]+abs(k) > a[r] {
				r++
			} else {
				l++
			}
		}

	there:
		if f {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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

package main

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

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

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

	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, 0, n)
		for j := 0; j < n; j++ {
			var num int
			fmt.Fscan(in, &num)
			arr = append(arr, num)
		}
		sort.Ints(arr)

		// solve
		var flag bool
		if len(arr) == 1 {
			flag = arr[0] == k
		}
		for i, j := 0, 1; i < n && j < n; {
			if arr[i] + k == arr[j] {
				flag = true
				break
			}
			if arr[i] + k < arr[j] {
				i ++
			} else {
				j ++
			}
		}
		if flag {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

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

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

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

package main
import (
	. "fmt"
	"bufio"
	"os"
)

func main(){
	read := bufio.NewReader(os.Stdin)
	write := bufio.NewWriter(os.Stdout)

	defer write.Flush()

	var tc int

	for Fscan (read, &tc); tc > 0; tc -- {
		var n, k int

		Fscan (read, &n, &k)

		mp, ans := map[int]bool{}, 0
		for i := 1; i <= n; i++ {
		    var val int
		    Fscan (read, &val)
		    mp[val] = true
		}

		for key, _ := range mp {
		    if mp[key + k] {
		        ans = 1
		    }
		}

		if ans == 1 {
		    Fprintln(write, "YES")
		} else {
		    Fprintln(write, "NO")
		}
	}
}

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)

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

	for i := 0; i < tc; i++ {
		var n int
		var k int
		_, _ = fmt.Fscan(r, &n, &k)

		numbers := make([]int, n)
		for i := range numbers {
			_, _ = fmt.Fscan(r, &numbers[i])
		}

		answer := subtractOperation(n, k, numbers)
		fmt.Println(answer)
	}
}

func subtractOperation(n int, k int, numbers []int) string {
	if n == 1 {
		if numbers[0] == k {
			return "YES"
		} else {
			return "NO"
		}
	}

	sort.Ints(numbers)

	i, j := 0, 1
	for j < n {
		if i >= j {
			j++
			continue
		}

		diff := numbers[j] - numbers[i]
		if diff == k {
			return "YES"
		}

		if diff < k {
			j++
		} else {
			i++
			j--
		}
	}

	return "NO"
}

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	for ii := 0; ii < t; ii++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		c := map[int]bool{}
		for i := 0; i < n; i++ {
			num := 0
			fmt.Fscan(in, &num)
			c[num] = true
		}
		find := false
		for num := range c {
			if c[num-k] {
				fmt.Fprintln(out, "YES")
				find = true
				break
			}
		}
		if !find {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

/*
5 4
3 1 3 2 5
---
YES

*/

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

	a := make([]int64, n)
	numToCnt := make(map[int64]int)

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

	for _, num1 := range a {
		num2 := num1 - k
		if num1 != num2 && numToCnt[num2] > 0 || num1 == num2 && numToCnt[num2] > 1 {
			fmt.Fprintln(writer, "YES")
			return
		}
	}
	fmt.Fprintln(writer, "NO")
}

//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 minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//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))
}

package main
import "fmt"
import "os"
import "bufio"
import "sort"
var (
	input = bufio.NewReader(os.Stdin)
	ouput = bufio.NewWriter(os.Stdout)
)
func main() {
	defer ouput.Flush()
	count := 0
	fmt.Fscan(input,&count)
	for count>0 {
		count --
		n := 0
		k := 0
		fmt.Fscan(input,&n)
		fmt.Fscan(input,&k)
		arr := make([]int,0,n)
		for n>0 {
			n--
			v := 0
			fmt.Fscan(input,&v)
			arr = append(arr,v)
		}
		sort.Ints(arr)
		l,r := 0,len(arr)-1
		f := "no"
		for l<r {
			if arr[l]+k == arr[r] {
				f = "yes"
				break
			}
			if arr[l]+k<arr[r] {
				l ++
			} else {
				r --
			}
		}
		fmt.Fprintln(ouput,f)
	}
}

package main
import "fmt"
import "os"
import "bufio"
import "sort"
var (
	input = bufio.NewReader(os.Stdin)
	ouput = bufio.NewWriter(os.Stdout)
)

/*
4
4 5
4 2 2 7
5 4
1 9 1 3 4
2 17
17 0
2 17
18 18
 */
func main() {
	count := 0
	fmt.Fscan(input,&count)
	for count>0 {
		count --
		n := 0
		k := 0
		fmt.Fscan(input,&n)
		fmt.Fscan(input,&k)
		arr := make([]int,0,n)
		for n>0 {
			n--
			v := 0
			fmt.Fscan(input,&v)
			arr = append(arr,v)
		}
		sort.Ints(arr)
		l,r := 0,len(arr)-1
		f := "no"
		for l<r {
			if arr[l]+k == arr[r] {
				f = "yes"
				break
			}
			if arr[l]+k<arr[r] {
				l ++
			} else {
				r --
			}
		}
		fmt.Printf("%s\n",f)
	}
}

package main
import "fmt"
import "os"
import "bufio"
import "sort"
var (
	input = bufio.NewReader(os.Stdin)
	ouput = bufio.NewWriter(os.Stdout)
)

/*
4
4 5
4 2 2 7
5 4
1 9 1 3 4
2 17
17 0
2 17
18 18
 */
func main() {
	count := 0
	fmt.Fscan(input,&count)
	for count>0 {
		count --
		n := 0
		k := 0
		fmt.Fscan(input,&n)
		fmt.Fscan(input,&k)
		arr := make([]int,0,n)
		for n>0 {
			n--
			v := 0
			fmt.Fscan(input,&v)
			arr = append(arr,v)
		}
		sort.Ints(arr)
		l,r := 0,len(arr)-1
		f := "no"
		for l<r {
			if arr[l]+k == arr[r] {
				f = "yes"
				break
			}
			if arr[l]+k<arr[r] {
				l ++
			} else {
				r --
			}
		}
		fmt.Println(f)
	}
}

package main
import "fmt"
import "os"
import "bufio"
import "sort"
var (
	input = bufio.NewReader(os.Stdin)
	ouput = bufio.NewWriter(os.Stdout)
)

/*
4
4 5
4 2 2 7
5 4
1 9 1 3 4
2 17
17 0
2 17
18 18
 */
func main() {
	count := 0
	fmt.Fscan(input,&count)
	for count>0 {
		count --
		n := 0
		k := 0
		fmt.Fscan(input,&n)
		fmt.Fscan(input,&k)
		arr := make([]int,0,n)
		for n>0 {
			n--
			v := 0
			fmt.Fscan(input,&v)
			arr = append(arr,v)
		}
		sort.Ints(arr)
		l,r := 0,len(arr)-1
		f := "no"
		for l<r {
			if arr[l]+k == arr[r] {
				f = "yes"
				break
			}
			if arr[l]+k<arr[r] {
				l ++
			} else {
				r --
			}
		}
		fmt.Fprintln(ouput,f)
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		var k int64
		fmt.Fscan(in, &n, &k)

		m := make(map[int64]bool)
		var f bool
		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)

			if (v >= k && m[v-k]) || (v < k && m[v+k]) {
				f = true
			}
			m[v] = true
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		var k int64
		fmt.Fscan(in, &n, &k)

		m := make(map[int64]bool)
		var f bool
		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)

			if (v >= k && m[v-k]) || (v < k && m[v+k]) {
				f = true
				break
			}
			m[v] = true
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		var k int64
		fmt.Fscan(in, &n, &k)

		m := make(map[int64]bool)
		var f bool
		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)

			m[v] = true
			if (v >= k && m[v-k]) || (v < k && m[v+k]) {
				f = true
				break
			}
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

var k,t,n int
func main(){
    ip:=bufio.NewScanner(os.Stdin)
    ip.Scan()
    t,_=strconv.Atoi(ip.Text())
    for ;t>0;t--{
        ip.Scan()
        n,_=strconv.Atoi(strings.Split(ip.Text()," ")[0])
        k,_=strconv.Atoi(strings.Split(ip.Text()," ")[1])
        a:=make([]int,n)
        ip.Scan()
        f:=false
        l,r:=0,1
        ss:=strings.Split(ip.Text()," ")
        for i:=range ss{a[i],_=strconv.Atoi(ss[i])}
        if n==1{
            f= a[0]==k
            goto there
        }
        sort.Ints(a)

        for l<n && r<n{
            if a[l]+abs(k)==a[r]{
                f=true
                goto there
            }else if a[l]+abs(k)>a[r]{
                r++
            }else{
                l++
            }
        }

        there:
        if f{
            fmt.Println("YES")
        }else{
            fmt.Println("NO")
        }
    }
}

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

package main

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

var k,t,n int
func main(){
    ip:=bufio.NewScanner(os.Stdin)
    ip.Scan()
    t,_=strconv.Atoi(ip.Text())
    for ;t>0;t--{
        ip.Scan()
        n,_=strconv.Atoi(strings.Split(ip.Text()," ")[0])
        k,_=strconv.Atoi(strings.Split(ip.Text()," ")[1])
        a:=make([]int,n)
        ip.Scan()
        f:=false
        i,j:=0,1
        ss:=strings.Split(ip.Text()," ")
        for i:=range ss{a[i],_=strconv.Atoi(ss[i])}
        if n==1{
            f=(a[0]==k)
            goto there
        }
        sort.Ints(a)

        for i<n && j<n{
            if a[i]+abs(k)==a[j]{
                f=true
                goto there
            }else if a[i]+abs(k)>a[j]{
                j++
            }else{
                i++
            }
        }

        there:
        if f{
            fmt.Println("YES")
        }else{
            fmt.Println("NO")
        }
    }
}

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

package main

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

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

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

	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, 0, n)
		for j := 0; j < n; j++ {
			var num int
			fmt.Fscan(in, &num)
			arr = append(arr, num)
		}
		sort.Ints(arr)
		idx := 0
		for i := 0; i < len(arr); i ++ {
			arr[idx] = arr[i]
			idx ++
			for i < len(arr)-1 && arr[i] == arr[i+1] {
				i ++
			}
		}
		arr = arr[:idx]
		// solve
		target := arr[len(arr)-1] - k
		//fmt.Fprintln(out, arr, t, target)
		presum := make([]int, len(arr))
		for k, v := range arr[:len(arr)-1] {
			presum[k+1] = presum[k] + abs(v)
		}
		sufsum := make([]int, len(arr))
		for i := len(arr)-2; i >= 0; i -- {
			sufsum[i] = sufsum[i+1] + abs(arr[i])
		}
		var flag bool
		for k := range arr[:len(arr)-1] {
			if sufsum[k] - presum[k] == target {
				flag = true
				break
			}
		}
		if flag {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

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

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

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


This is the easy version of the problem. The difference between the versions is the constraints on $$$a_i$$$. You can make hacks only if all versions of the problem are solved.Little Dormi has recently received a puzzle from his friend and needs your help to solve it. The puzzle consists of an upright board with $$$n$$$ rows and $$$m$$$ columns of cells, some empty and some filled with blocks of sand, and $$$m$$$ non-negative integers $$$a_1,a_2,\ldots,a_m$$$ ($$$0 \leq a_i \leq n$$$). In this version of the problem, $$$a_i$$$ will be equal to the number of blocks of sand in column $$$i$$$.When a cell filled with a block of sand is disturbed, the block of sand will fall from its cell to the sand counter at the bottom of the column (each column has a sand counter). While a block of sand is falling, other blocks of sand that are adjacent at any point to the falling block of sand will also be disturbed and start to fall. Specifically, a block of sand disturbed at a cell $$$(i,j)$$$ will pass through all cells below and including the cell $$$(i,j)$$$ within the column, disturbing all adjacent cells along the way. Here, the cells adjacent to a cell $$$(i,j)$$$ are defined as $$$(i-1,j)$$$, $$$(i,j-1)$$$, $$$(i+1,j)$$$, and $$$(i,j+1)$$$ (if they are within the grid). Note that the newly falling blocks can disturb other blocks.In one operation you are able to disturb any piece of sand. The puzzle is solved when there are at least $$$a_i$$$ blocks of sand counted in the $$$i$$$-th sand counter for each column from $$$1$$$ to $$$m$$$.You are now tasked with finding the minimum amount of operations in order to solve the puzzle. Note that Little Dormi will never give you a puzzle that is impossible to solve.

The first line consists of two space-separated positive integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \cdot m \leq 400\,000$$$). Each of the next $$$n$$$ lines contains $$$m$$$ characters, describing each row of the board. If a character on a line is '.', the corresponding cell is empty. If it is '#', the cell contains a block of sand. The final line contains $$$m$$$ non-negative integers $$$a_1,a_2,\ldots,a_m$$$ ($$$0 \leq a_i \leq n$$$) — the minimum amount of blocks of sand that needs to fall below the board in each column. In this version of the problem, $$$a_i$$$ will be equal to the number of blocks of sand in column $$$i$$$.

Print one non-negative integer, the minimum amount of operations needed to solve the puzzle.

NoteFor example $$$1$$$, by disturbing both blocks of sand on the first row from the top at the first and sixth columns from the left, and the block of sand on the second row from the top and the fourth column from the left, it is possible to have all the required amounts of sand fall in each column. It can be proved that this is not possible with fewer than $$$3$$$ operations, and as such the answer is $$$3$$$. Here is the puzzle from the first example.  For example $$$2$$$, by disturbing the cell on the top row and rightmost column, one can cause all of the blocks of sand in the board to fall into the counters at the bottom. Thus, the answer is $$$1$$$. Here is the puzzle from the second example.

package main

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

type cell struct {
	row int
	col int
}

var n int
var m int
var used [][]bool
var s []string
var order []cell
var X = []int{0, 1, 0, -1}
var Y = []int{1, 0, -1, 0}
var c []int

func inside(x int, y int) bool {
	return x >= 0 && x < n && y >= 0 && y < m
}
func dfs(x int, y int, postorder bool) {
	used[x][y] = true
	if s[x][y] == '#' {
		for i := 0; i < 4; i++ {
			xx := x + X[i]
			yy := y + Y[i]
			if inside(xx, yy) {
				if !used[xx][yy] {
					dfs(xx, yy, true)
				}
			}
		}
	} else {
		xx := x + 1
		yy := y
		if inside(xx, yy) {
			if !used[xx][yy] {
				dfs(xx, yy, true)
			}
		}
	}
	if postorder {
		order = append(order, cell{row: x, col: y})
	}
}
func solve(r io.Reader, w io.Writer) {
	reader := bufio.NewReader(r)
	writer := bufio.NewWriter(w)
	defer writer.Flush()
	fmt.Fscan(reader, &n)
	fmt.Fscan(reader, &m)
	used = make([][]bool, n)
	s = make([]string, n)
	for i, _ := range used {
		used[i] = make([]bool, m)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &s[i])
	}
	c = make([]int, m) // cost
	for i, _ := range c {
		fmt.Fscan(reader, &c[i])
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if !used[i][j] && s[i][j] == '#' {
				dfs(i, j, true)
			}
		}
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			used[i][j] = false
		}
	}
	res := 0
	for i := len(order) - 1; i >= 0; i-- {
		x, y := order[i].row, order[i].col
		if !used[x][y] && s[x][y] == '#' {
			if c[y] > 0 {
				res++
				dfs(x, y, false)
			}
		}
	}
	fmt.Fprint(writer, res)
}

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


You are given an undirected graph consisting of n vertices and  edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.

The first line contains two integers n and m (1 ≤ n ≤ 200000, ). Then m lines follow, each containing a pair of integers x and y (1 ≤ x, y ≤ n, x ≠ y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices.

Firstly print k — the number of connected components in this graph. Then print k integers — the sizes of components. You should output these integers in non-descending order.

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol920E(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)
		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 int
	Fscan(in, &n, &m)
	g := make([][]int, n)
	for ; m > 0; m-- {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	maxDeg0, maxDeg0V := 0, 0
	for v, edges := range g {
		if deg0 := n - 1 - len(edges); deg0 > maxDeg0 {
			maxDeg0 = deg0
			maxDeg0V = v
		}
	}
	if maxDeg0 == 0 {
		Fprintln(out, n)
		Fprintln(out, strings.Repeat("1 ", n))
		return
	}

	merge0 := func(v int, edges []int) {
		sort.Ints(edges)
		j := 0
		for i := 0; i < n; i++ {
			if i == v {
				continue
			}
			if j == len(edges) || i < edges[j] {
				merge(i, v)
			} else {
				j++
			}
		}
	}
	initFa(n)
	merge0(maxDeg0V, g[maxDeg0V])
	vs := map[int]bool{maxDeg0V: true}
	for _, w := range g[maxDeg0V] {
		vs[w] = true
	}
	for v, edges := range g {
		if same(v, maxDeg0V) {
			continue
		}
		cnt := 1
		for _, w := range edges {
			if !vs[w] {
				cnt++
			}
		}
		if cnt < n-len(g[maxDeg0V]) {
			merge(v, maxDeg0V)
		} else {
			merge0(v, edges)
		}
	}

	cnt := map[int]int{}
	for i := range fa {
		cnt[find(i)]++
	}
	values := make([]int, 0, len(cnt))
	for _, v := range cnt {
		values = append(values, v)
	}
	sort.Ints(values)
	Fprintln(out, len(values))
	for _, v := range values {
		Fprint(out, v, " ")
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol920E(reader io.Reader, writer io.Writer) {
	in := bufio.NewScanner(reader)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(writer)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b-'0')
		}
		return
	}
	var fa []int
	initFa := func(n int) {
		fa = make([]int, n)
		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) }

	n, m := read(), read()
	g := make([][]int, n)
	for ; m > 0; m-- {
		v, w := read()-1, read()-1
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	maxDeg0, maxDeg0V := 0, 0
	for v, edges := range g {
		if deg0 := n - 1 - len(edges); deg0 > maxDeg0 {
			maxDeg0 = deg0
			maxDeg0V = v
		}
	}
	if maxDeg0 == 0 {
		Fprintln(out, n)
		Fprintln(out, strings.Repeat("1 ", n))
		return
	}

	merge0 := func(v int, edges []int) {
		vs := map[int]bool{v: true}
		for _, w := range edges {
			vs[w] = true
		}
		for i := 0; i < n; i++ {
			if !vs[i] {
				merge(i, v)
			}
		}
	}
	initFa(n)
	merge0(maxDeg0V, g[maxDeg0V])
	for v, edges := range g {
		if !same(v, maxDeg0V) {
			merge0(v, edges)
		}
	}

	cnt := map[int]int{}
	for i := range fa {
		cnt[find(i)]++
	}
	values := make([]int, 0, len(cnt))
	for _, v := range cnt {
		values = append(values, v)
	}
	sort.Ints(values)
	Fprintln(out, len(values))
	for _, v := range values {
		Fprint(out, v, " ")
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol920E(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)
		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 int
	Fscan(in, &n, &m)
	g := make([][]int, n)
	for ; m > 0; m-- {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	maxDeg0, maxDeg0V := 0, 0
	for v, edges := range g {
		if deg0 := n - 1 - len(edges); deg0 > maxDeg0 {
			maxDeg0 = deg0
			maxDeg0V = v
		}
	}
	if maxDeg0 == 0 {
		Fprintln(out, 1)
		Fprintln(out, n)
		return
	}

	merge0 := func(v int, edges []int) {
		sort.Ints(edges)
		j := 0
		for i := 0; i < n; i++ {
			if i == v {
				continue
			}
			if j == len(edges) || i < edges[j] {
				merge(i, v)
			} else {
				j++
			}
		}
	}
	initFa(n)
	merge0(maxDeg0V, g[maxDeg0V])
	vs := map[int]bool{maxDeg0V: true}
	for _, w := range g[maxDeg0V] {
		vs[w] = true
	}
	for v, edges := range g {
		if same(v, maxDeg0V) {
			continue
		}
		cnt := 1
		for _, w := range edges {
			if !vs[w] {
				cnt++
			}
		}
		if cnt < n-len(g[maxDeg0V]) {
			merge(v, maxDeg0V)
		} else {
			merge0(v, edges)
		}
	}

	cnt := map[int]int{}
	for i := range fa {
		cnt[find(i)]++
	}
	values := make([]int, 0, len(cnt))
	for _, v := range cnt {
		values = append(values, v)
	}
	sort.Ints(values)
	Fprintln(out, len(values))
	for _, v := range values {
		Fprint(out, v, " ")
	}
}

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


Monocarp is planning to host a martial arts tournament. There will be three divisions based on weight: lightweight, middleweight and heavyweight. The winner of each division will be determined by a single elimination system.In particular, that implies that the number of participants in each division should be a power of two. Additionally, each division should have a non-zero amount of participants.$$$n$$$ participants have registered for the tournament so far, the $$$i$$$-th of them weighs $$$a_i$$$. To split participants into divisions, Monocarp is going to establish two integer weight boundaries $$$x$$$ and $$$y$$$ ($$$x < y$$$). All participants who weigh strictly less than $$$x$$$ will be considered lightweight. All participants who weigh greater or equal to $$$y$$$ will be considered heavyweight. The remaining participants will be considered middleweight.It's possible that the distribution doesn't make the number of participants in each division a power of two. It can also lead to empty divisions. To fix the issues, Monocarp can invite an arbitrary number of participants to each division.Note that Monocarp can't kick out any of the $$$n$$$ participants who have already registered for the tournament.However, he wants to invite as little extra participants as possible. Help Monocarp to choose $$$x$$$ and $$$y$$$ in such a way that the total amount of extra participants required is as small as possible. Output that amount.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of the registered participants. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the weights of the registered participants. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase, print a single integer — the smallest number of extra participants Monocarp is required to invite after he chooses the weight boundaries $$$x$$$ and $$$y$$$.

NoteIn the first testcase of the example, Monocarp can choose $$$x=2$$$ and $$$y=3$$$. Lightweight, middleweight and heavyweight divisions will have $$$2$$$, $$$1$$$ and $$$1$$$ participants, respectively. They all are powers of two, so no extra participants are required.In the second testcase of the example, regardless of the choice of $$$x$$$ and $$$y$$$, one division will have $$$1$$$ participant, the rest will have $$$0$$$. Thus, Monocarp will have to invite $$$1$$$ participant into both of the remaining divisions.In the third testcase of the example, Monocarp can choose $$$x=1$$$ and $$$y=2$$$. Lightweight, middleweight and heavyweight divisions will have $$$0$$$, $$$3$$$ and $$$3$$$ participants, respectively. So an extra participant is needed in each division.In the fourth testcase of the example, Monocarp can choose $$$x=8$$$ and $$$y=9$$$. Lightweight, middleweight and heavyweight divisions will have $$$8$$$, $$$0$$$ and $$$0$$$ participants, respectively. Middleweight and heavyweight division need an extra participant each.

package main

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

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

func distToPowerOf2(n int) int {
	var p2 = 1
	for p2 < n {
		p2 <<= 1
	}
	return p2 - n
}

func longestPreffix(acc []int, maxSum int) int {
	var n = len(acc) - 1
	var lo = 0
	var hi = n
	var mi = 0
	for lo < hi {
		mi = (lo + hi) / 2
		if acc[0]-acc[mi+1] <= maxSum {
			lo = mi + 1
		} else {
			hi = mi
		}
	}
	return lo
}

func nextPowerOf2(n int) int {
	var p2 = 1
	for p2 < n {
		p2 <<= 1
	}
	return p2
}

func solveCase() int {
	var n int
	fmt.Fscan(in, &n)
	var arr []int = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}
	sort.Ints(arr)
	var cnt []int
	for i := 0; i < n; i++ {
		var c = 1
		for i+1 < n && arr[i+1] == arr[i] {
			i++
			c++
		}
		cnt = append(cnt, c)
	}
	var log2n = 1
	for (1 << log2n) < n {
		log2n++
	}

	n = len(cnt)
	var acc = make([]int, n+1)
	for i := n - 1; i >= 0; i-- {
		acc[i] = acc[i+1] + cnt[i]
	}

	var dp2 = make([]int, n+1)
	dp2[n] = 2
	for i := 0; i < n; i++ {
		dp2[i] = 4 * acc[0]
		for e0 := 0; e0 <= log2n; e0++ {
			var pref = longestPreffix(acc[i:], 1<<e0)
			var x = (1 << e0) + nextPowerOf2(acc[i+pref]) - acc[i]
			if x < dp2[i] {
				dp2[i] = x
			}
		}
	}

	var ans = dp2[0] + 1
	for i := 0; i < len(cnt); i += 1 {
		var ansi = distToPowerOf2(acc[0]-acc[i+1]) + dp2[i+1]
		if ansi < ans {
			ans = ansi
		}
	}

	return ans
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fprintf(out, "%d\n", solveCase())
	}
	out.Flush()
}

package main

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

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

func distToPowerOf2(n int) int {
	var p2 = 1
	for p2 < n {
		p2 <<= 1
	}
	return p2 - n
}

func longestPreffix(acc []int, maxSum int) int {
	var n = len(acc) - 1
	var lo = 0
	var hi = n - 1
	for lo < hi {
		var mi = (lo + hi) / 2
		if acc[0]-acc[mi+2] <= maxSum {
			lo = mi + 1
		} else {
			hi = mi
		}
	}
	return lo
}

func longestSuffix(acc []int, maxSum int) int {
	var n = len(acc) - 1
	var lo = 0
	var hi = n
	for lo < hi {
		var mi = (lo + hi) / 2
		if acc[mi] <= maxSum {
			hi = mi
		} else {
			lo = mi + 1
		}
	}
	return n - lo
}

func solveCase() int {
	var n int
	fmt.Fscan(in, &n)
	var arr []int = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}
	sort.Ints(arr)
	var cnt []int
	for i := 0; i < n; i++ {
		var c = 1
		for i+1 < n && arr[i+1] == arr[i] {
			i++
			c++
		}
		cnt = append(cnt, c)
	}
	var log2n = 1
	for (1 << log2n) < n {
		log2n++
	}

	n = len(cnt)
	var acc = make([]int, n+1)
	for i := n - 1; i >= 0; i-- {
		acc[i] = acc[i+1] + cnt[i]
	}

	var dp2 = make([]int, n+1)
	dp2[n] = 2
	for i := 0; i < n; i++ {
		dp2[i] = 4 * n

		for e0 := 0; e0 <= log2n; e0++ {
			for e1 := 0; e1 <= log2n; e1++ {
				var pref = longestPreffix(acc[i:], 1<<e0)
				var suff = longestSuffix(acc[i:], 1<<e1)
				if pref+suff >= n-i {
					var x = (1 << e0) + (1 << e1) - acc[i]
					if x < dp2[i] {
						dp2[i] = x
					}
				}
			}
		}
	}

	var ans = dp2[0] + 1
	for i := 0; i < len(cnt); i += 1 {
		var ansi = distToPowerOf2(acc[0]-acc[i+1]) + dp2[i+1]
		if ansi < ans {
			ans = ansi
		}
	}

	return ans
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fprintf(out, "%d\n", solveCase())
	}
	out.Flush()
}

package main

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

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

func distToPowerOf2(n int) int {
	var p2 = 1
	for p2 < n {
		p2 <<= 1
	}
	return p2 - n
}

func longestPreffix(acc []int, maxSum int) int {
	var n = len(acc) - 1
	var lo = 0
	var hi = n
	for lo < hi {
		var mi = (lo + hi) / 2
		if acc[0]-acc[mi+1] <= maxSum {
			lo = mi + 1
		} else {
			hi = mi
		}
	}
	return lo
}

func longestSuffix(acc []int, maxSum int) int {
	var n = len(acc) - 1
	var lo = 0
	var hi = n
	for lo < hi {
		var mi = (lo + hi) / 2
		if acc[mi] <= maxSum {
			hi = mi
		} else {
			lo = mi + 1
		}
	}
	return n - lo
}

func solveCase() int {
	var n int
	fmt.Fscan(in, &n)
	var arr []int = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}
	sort.Ints(arr)
	var cnt []int
	for i := 0; i < n; i++ {
		var c = 1
		for i+1 < n && arr[i+1] == arr[i] {
			i++
			c++
		}
		cnt = append(cnt, c)
	}
	var log2n = 1
	for (1 << log2n) < n {
		log2n++
	}

	n = len(cnt)
	var acc = make([]int, n+1)
	for i := n - 1; i >= 0; i-- {
		acc[i] = acc[i+1] + cnt[i]
	}

	var dp2 = make([]int, n+1)
	dp2[n] = 2
	for i := 0; i < n; i++ {
		dp2[i] = 4 * n

		for e0 := 0; e0 <= log2n; e0++ {
			for e1 := 0; e1 <= log2n; e1++ {
				var pref = longestPreffix(acc[i:], 1<<e0)
				var suff = longestSuffix(acc[i:], 1<<e1)
				if pref+suff >= n-i {
					var x = (1 << e0) + (1 << e1) - acc[i]
					if x < dp2[i] {
						dp2[i] = x
					}
				}
			}
		}
	}

	var ans = dp2[0] + 1
	for i := 0; i < len(cnt); i += 1 {
		var ansi = distToPowerOf2(acc[0]-acc[i+1]) + dp2[i+1]
		if ansi < ans {
			ans = ansi
		}
	}

	return ans
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fprintf(out, "%d\n", solveCase())
	}
	out.Flush()
}


Hooray! Polycarp turned $$$n$$$ years old! The Technocup Team sincerely congratulates Polycarp!Polycarp celebrated all of his $$$n$$$ birthdays: from the $$$1$$$-th to the $$$n$$$-th. At the moment, he is wondering: how many times he turned beautiful number of years?According to Polycarp, a positive integer is beautiful if it consists of only one digit repeated one or more times. For example, the following numbers are beautiful: $$$1$$$, $$$77$$$, $$$777$$$, $$$44$$$ and $$$999999$$$. The following numbers are not beautiful: $$$12$$$, $$$11110$$$, $$$6969$$$ and $$$987654321$$$.Of course, Polycarpus uses the decimal numeral system (i.e. radix is 10).Help Polycarpus to find the number of numbers from $$$1$$$ to $$$n$$$ (inclusive) that are beautiful.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of one line, which contains a positive integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — how many years Polycarp has turned.

Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the test. Each answer is an integer: the number of beautiful years between $$$1$$$ and $$$n$$$, inclusive.

package main

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var n int64

func main() {
	t = ReadInt()
	for tc := 0; tc < t; tc++ {
		n = ReadInt64()

		solve()
	}
}

func solve() {
	if n < 10 {
		fmt.Println(n)
		return
	}

	d := int64(10)

	ans := int64(0)
	for n/d > 0 {
		ans += 9
		d *= 10
	}

	d /= 10
	ans += n/d - 1
	max := int64(0)
	digit := n / d
	for d > 0 {
		max += digit * d
		d /= 10
	}
	if n >= max {
		ans++
	}

	fmt.Println(ans)
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
*/

/*******************************************************************/

package main

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var n int

func main() {
	t = ReadInt()
	for tc := 0; tc < t; tc++ {
		n = ReadInt()

		solve()
	}
}

func solve() {
	if n < 10 {
		fmt.Println(n)
		return
	}

	d := 10

	ans := 0
	for n/d > 0 {
		ans += 9
		d *= 10
	}

	d /= 10
	ans += n/d - 1
	max := 0
	digit := n / d
	for d > 0 {
		max += digit * d
		d /= 10
	}
	if n >= max {
		ans++
	}

	fmt.Println(ans)
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
*/

/*******************************************************************/


Polycarp is wondering about buying a new computer, which costs $$$c$$$ tugriks. To do this, he wants to get a job as a programmer in a big company.There are $$$n$$$ positions in Polycarp's company, numbered starting from one. An employee in position $$$i$$$ earns $$$a[i]$$$ tugriks every day. The higher the position number, the more tugriks the employee receives. Initially, Polycarp gets a position with the number $$$1$$$ and has $$$0$$$ tugriks.Each day Polycarp can do one of two things:   If Polycarp is in the position of $$$x$$$, then he can earn $$$a[x]$$$ tugriks.  If Polycarp is in the position of $$$x$$$ ($$$x < n$$$) and has at least $$$b[x]$$$ tugriks, then he can spend $$$b[x]$$$ tugriks on an online course and move to the position $$$x+1$$$. For example, if $$$n=4$$$, $$$c=15$$$, $$$a=[1, 3, 10, 11]$$$, $$$b=[1, 2, 7]$$$, then Polycarp can act like this:   On the first day, Polycarp is in the $$$1$$$-st position and earns $$$1$$$ tugrik. Now he has $$$1$$$ tugrik;  On the second day, Polycarp is in the $$$1$$$-st position and move to the $$$2$$$-nd position. Now he has $$$0$$$ tugriks;  On the third day, Polycarp is in the $$$2$$$-nd position and earns $$$3$$$ tugriks. Now he has $$$3$$$ tugriks;  On the fourth day, Polycarp is in the $$$2$$$-nd position and is transferred to the $$$3$$$-rd position. Now he has $$$1$$$ tugriks;  On the fifth day, Polycarp is in the $$$3$$$-rd position and earns $$$10$$$ tugriks. Now he has $$$11$$$ tugriks;  On the sixth day, Polycarp is in the $$$3$$$-rd position and earns $$$10$$$ tugriks. Now he has $$$21$$$ tugriks;  Six days later, Polycarp can buy himself a new computer. Find the minimum number of days after which Polycarp will be able to buy himself a new computer.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$c$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le c \le 10^9$$$) — the number of positions in the company and the cost of a new computer. The second line of each test case contains $$$n$$$ integers $$$a_1 \le a_2 \le \ldots \le a_n$$$ ($$$1 \le a_i \le 10^9$$$). The third line of each test case contains $$$n - 1$$$ integer $$$b_1, b_2, \ldots, b_{n-1}$$$ ($$$1 \le b_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output the minimum number of days after which Polycarp will be able to buy a new computer.

package main

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

var reader = bufio.NewReader(os.Stdin)

func ri() int {
	var x int
	c, _ := reader.ReadByte()
	for c < '0' || c > '9' {
		c, _ = reader.ReadByte()
	}
	for '0' <= c && c <= '9' {
		x = (x << 3) + (x << 1) + int(c) - '0'
		c, _ = reader.ReadByte()
	}
	return x
}

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

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

const MAXN = 2e5 + 5

type Node struct {
	x, y int
}

func Solve() {
	n, c := ri(), int64(ri())
	var a, b []int64
	for i := 1; i <= n; i++ {
		a = append(a, int64(ri()))
	}
	for i := 1; i < n; i++ {
		b = append(b, int64(ri()))
	}
	ans := int64(math.Ceil(float64(c) / float64(a[0])))
	sum, res := int64(0), int64(0)
	for i := 1; i < n; i++ {
		sum += 1
		if res >= b[i-1] {
			res -= b[i-1]
		} else {
			b[i-1] -= res
			res = 0
			tmp := int64(math.Ceil(float64(b[i-1]) / float64(a[i-1])))
			sum += tmp
			res = tmp*a[i-1] - b[i-1]
		}
		if c >= res {
			ans = min(ans, sum+int64(math.Ceil(float64(c-res)/float64(a[i]))))
		} else {
			ans = min(ans, sum)
		}
	}
	fmt.Println(ans)
}

func main() {
	var T = 0
	if T == 0 {
		fmt.Scan(&T)
	}
	for t := 1; t <= T; t++ {
		Solve()
	}
}
//114514

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1512F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a > b {
			return b
		}
		return a
	}

	var T, n, c int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &c)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, n-1)
		for i := range b {
			Fscan(in, &b[i])
		}

		ans := int(2e9)
		cur, day := 0, 0
		for i, v := range a {
			if cur >= c {
				break
			}
			if i < n-1 {
				res := (c - cur + v - 1) / v
				ans = min(ans, res+day)
				if b[i] >= c {
					break
				}
				if cur < b[i] {
					d := (b[i] - cur + v - 1) / v
					day += d + 1
					cur += d*v - b[i]
				} else {
					cur -= b[i]
					day++
				}
			} else {
				res := (c - cur + v - 1) / v
				ans = min(ans, res+day)
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

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

	solve := func(Case int) {
		var n, c int
		Fscan(in, &n, &c)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, n-1)
		for i := range b {
			Fscan(in, &b[i])
		}

		ans := int(2e9)
		cur, day := 0, 0
		for i, v := range a {
			if cur >= c {
				break
			}
			if i < n-1 {
				res := (c - cur + v - 1) / v
				ans = min(ans, res+day)
				if b[i] >= c {
					break
				}
				if cur < b[i] {
					d := (b[i] - cur + v - 1) / v
					day += d + 1
					cur += d*v - b[i]
				} else {
					cur -= b[i]
					day++
				}
			} else {
				res := (c - cur + v - 1) / v
				ans = min(ans, res+day)
			}
		}
		Fprintln(out, ans)
	}

	var T int
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

func main() { run(os.Stdin, os.Stdout) }
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n int
		var c int64
		fmt.Fscan(in, &n, &c)
		a := make([]int64, n)
		b := make([]int64, n-1)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		for i := range b {
			fmt.Fscan(in, &b[i])
		}
		var has int64
		var days int64
		res := int64(math.MaxInt64)
		for i := 0; i < n; i++ {
			needDays := (c-has+a[i]-1)/a[i]
			if needDays < 0 {
				needDays = 0
			}
			curRes := days + needDays
			if res > curRes {
				res = curRes
			}
			if i != n-1 {
				needDays = (b[i]-has+a[i]-1)/a[i]
				if needDays < 0 {
					needDays = 0
				}
				has += needDays * a[i] - b[i]
				days += needDays + 1
			}
		}
		fmt.Println(res)
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func ri() int {
	var x int
	c, _ := reader.ReadByte()
	for c < '0' || c > '9' {
		c, _ = reader.ReadByte()
	}
	for '0' <= c && c <= '9' {
		x = (x << 3) + (x << 1) + int(c) - '0'
		c, _ = reader.ReadByte()
	}
	return x
}

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

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

const MAXN = 2e5 + 5

type Node struct {
	x, y int
}

func Solve() {
	n, c := ri(), ri()
	var a, b []int
	for i := 1; i <= n; i++ {
		a = append(a, ri())
	}
	for i := 1; i < n; i++ {
		b = append(b, ri())
	}
	ans := int(math.Ceil(float64(c) / float64(a[0])))
	sum, res := 0, 0
	for i := 1; i < n; i++ {
		sum += 1
		if res >= b[i-1] {
			res -= b[i-1]
		} else {
			b[i-1] -= res
			res = 0
			tmp := int(math.Ceil(float64(b[i-1]) / float64(a[i-1])))
			sum += tmp
			res = tmp*a[i-1] - b[i-1]
		}
		if c >= res {
			ans = min(ans, sum+int(math.Ceil(float64(c-res)/float64(a[i]))))
		} else {
			ans = min(ans, sum)
		}
	}
	fmt.Println(ans)
}

func main() {
	var T = 0
	if T == 0 {
		fmt.Scan(&T)
	}
	for t := 1; t <= T; t++ {
		Solve()
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func ri() int {
	var x int
	c, _ := reader.ReadByte()
	for c < '0' || c > '9' {
		c, _ = reader.ReadByte()
	}
	for '0' <= c && c <= '9' {
		x = (x << 3) + (x << 1) + int(c) - '0'
		c, _ = reader.ReadByte()
	}
	return x
}

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

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

const MAXN = 2e5 + 5

type Node struct {
	x, y int
}

func Solve() {
	n, c := ri(), ri()
	var a, b []int
	for i := 1; i <= n; i++ {
		a = append(a, ri())
	}
	for i := 1; i < n; i++ {
		b = append(b, ri())
	}
	ans := int(math.Ceil(float64(c) / float64(a[0])))
	sum, res := 0, 0
	for i := 1; i < n; i++ {
		sum += 1
		if res >= b[i-1] {
			res -= b[i-1]
		} else {
			b[i-1] -= res
			res = 0
			tmp := int(math.Ceil(float64(b[i-1]) / float64(a[i-1])))
			sum += tmp
			res = tmp*a[i-1] - b[i-1]
		}
		ans = min(ans, sum+int(math.Ceil(float64(c-res)/float64(a[i]))))
	}
	fmt.Println(ans)
}

func main() {
	var T = 0
	if T == 0 {
		fmt.Scan(&T)
	}
	for t := 1; t <= T; t++ {
		Solve()
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

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

	solve := func(Case int) {
		var n, c int
		Fscan(in, &n, &c)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, n-1)
		for i := range b {
			Fscan(in, &b[i])
		}

		ans := int(2e9)
		cur, day := 0, 0
		for i, v := range a {
			if i < n-1 {
				res := (c-cur-1)/v + 1
				ans = min(ans, res+day)
				if cur < b[i] {
					d := (b[i]-cur-1)/v + 1
					day += d + 1
					cur += d*v - b[i]
				} else {
					cur -= b[i]
					day++
				}
			} else {
				res := (c-cur-1)/v + 1
				ans = min(ans, res+day)
			}
		}
		Fprintln(out, ans)
	}

	var T int
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

func main() { run(os.Stdin, os.Stdout) }
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}


There used to be unrest in the Galactic Senate. Several thousand solar systems had declared their intentions to leave the Republic. But fear not! Master Heidi was able to successfully select the Jedi Knights that have restored peace in the galaxy. However, she knows that evil never sleeps and a time may come when she will need to pick another group of Jedi Knights. She wants to be sure she has enough options to do so.There are n Jedi Knights, each of them with a lightsaber of one of m colors. Given a number k, compute the number of differently colored collections of k lightsabers that some k Jedi Knights might have. Jedi Knights with lightsabers of the same color are indistinguishable (it's not the person, it's the lightsaber color that matters!), and their order does not matter; that is, we consider two collections of Jedi Knights to be different if and only if their vectors of counts of lightsabers of each color (like what you were given in the easy and the medium versions) are different. We count all subsets, not only contiguous subsegments of the input sequence. Output the answer modulo 1009.

The first line of the input contains n (1 ≤ n ≤ 2·105), m (1 ≤ m ≤ n) and k (1 ≤ k ≤ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of subsequent Jedi Knights.

Output one number: the number of differently colored collections of k lightsabers modulo 1009.

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math"
	"math/bits"
	"os"
)

type fft struct {
	n               int
	omega, omegaInv []complex128
}

func newFFT(n int) *fft {
	omega := make([]complex128, n)
	omegaInv := make([]complex128, n)
	for i := range omega {
		sin, cos := math.Sincos(2 * math.Pi * float64(i) / float64(n))
		omega[i] = complex(cos, sin)
		omegaInv[i] = complex(cos, -sin)
	}
	return &fft{n, omega, omegaInv}
}

func (f *fft) transform(a, omega []complex128) {
	for i, j := 0, 0; i < f.n; i++ {
		if i > j {
			a[i], a[j] = a[j], a[i]
		}
		for l := f.n >> 1; ; l >>= 1 {
			j ^= l
			if j >= l {
				break
			}
		}
	}
	for l := 2; l <= f.n; l <<= 1 {
		m := l >> 1
		for st := 0; st < f.n; st += l {
			p := a[st:]
			for i := 0; i < m; i++ {
				t := omega[f.n/l*i] * p[m+i]
				p[m+i] = p[i] - t
				p[i] += t
			}
		}
	}
}

func (f *fft) dft(a []complex128) {
	f.transform(a, f.omega)
}

func (f *fft) idft(a []complex128) {
	f.transform(a, f.omegaInv)
	for i := range a {
		a[i] /= complex(float64(f.n), 0)
	}
}

func convolution(a, b []int) []int {
	n, m := len(a), len(b)
	limit := 1 << uint(bits.Len(uint(n+m-1)))
	f := newFFT(limit)
	cmplxA := make([]complex128, limit)
	for i, v := range a {
		cmplxA[i] = complex(float64(v), 0)
	}
	cmplxB := make([]complex128, limit)
	for i, v := range b {
		cmplxB[i] = complex(float64(v), 0)
	}
	f.dft(cmplxA)
	f.dft(cmplxB)
	for i := range cmplxA {
		cmplxA[i] *= cmplxB[i]
	}
	f.idft(cmplxA)
	conv := make([]int, n+m-1)
	for i := range conv {
		conv[i] = int(int64(math.Round(real(cmplxA[i]))) % 1009)
	}
	return conv
}

func convolutionN(coefs [][]int) []int {
	var f func(l, r int) []int
	f = func(l, r int) []int {
		if l == r {
			return coefs[l-1]
		}
		mid := (l + r) >> 1
		return convolution(f(l, mid), f(mid+1, r))
	}
	return f(1, len(coefs))
}

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

	var n, m, k, x int
	Fscan(in, &n, &m, &k)
	coefs := make([][]int, m)
	for i := range coefs {
		coefs[i] = []int{1}
	}
	for ; n > 0; n-- {
		Fscan(in, &x)
		x--
		coefs[x] = append(coefs[x], 1)
	}
	Fprint(out, convolutionN(coefs)[k])
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math"
	"math/bits"
	"os"
)

type fft struct {
	n               int
	omega, omegaInv []complex128
}

func newFFT(n int) *fft {
	omega := make([]complex128, n)
	omegaInv := make([]complex128, n)
	for i := range omega {
		sin, cos := math.Sincos(2 * math.Pi * float64(i) / float64(n))
		omega[i] = complex(cos, sin)
		omegaInv[i] = complex(cos, -sin)
	}
	return &fft{n, omega, omegaInv}
}

func (f *fft) transform(a, omega []complex128) {
	for i, j := 0, 0; i < f.n; i++ {
		if i > j {
			a[i], a[j] = a[j], a[i]
		}
		for l := f.n >> 1; ; l >>= 1 {
			j ^= l
			if j >= l {
				break
			}
		}
	}
	for l := 2; l <= f.n; l <<= 1 {
		m := l >> 1
		for st := 0; st < f.n; st += l {
			p := a[st:]
			for i := 0; i < m; i++ {
				t := omega[f.n/l*i] * p[m+i]
				p[m+i] = p[i] - t
				p[i] += t
			}
		}
	}
}

func (f *fft) dft(a []complex128) {
	f.transform(a, f.omega)
}

func (f *fft) idft(a []complex128) {
	f.transform(a, f.omegaInv)
	for i := range a {
		a[i] /= complex(float64(f.n), 0)
	}
}

func convolution(a, b []int) []int {
	n, m := len(a), len(b)
	limit := 1 << uint(bits.Len(uint(n+m-1)))
	f := newFFT(limit)
	cmplxA := make([]complex128, limit)
	for i, v := range a {
		cmplxA[i] = complex(float64(v), 0)
	}
	cmplxB := make([]complex128, limit)
	for i, v := range b {
		cmplxB[i] = complex(float64(v), 0)
	}
	f.dft(cmplxA)
	f.dft(cmplxB)
	for i := range cmplxA {
		cmplxA[i] *= cmplxB[i]
	}
	f.idft(cmplxA)
	conv := make([]int, n+m-1)
	for i := range conv {
		conv[i] = int(math.Round(real(cmplxA[i]))) % 1009
	}
	return conv
}

func convolutionN(coefs [][]int) []int {
	var f func(l, r int) []int
	f = func(l, r int) []int {
		if l == r {
			return coefs[l-1]
		}
		mid := (l + r) >> 1
		return convolution(f(l, mid), f(mid+1, r))
	}
	return f(1, len(coefs))
}

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

	var n, m, k, x int
	Fscan(in, &n, &m, &k)
	coefs := make([][]int, m)
	for i := range coefs {
		coefs[i] = []int{1}
	}
	for ; n > 0; n-- {
		Fscan(in, &x)
		x--
		coefs[x] = append(coefs[x], 1)
	}
	Fprint(out, convolutionN(coefs)[k])
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math"
	"math/bits"
	"os"
)

type fft struct {
	n               int
	omega, omegaInv []complex128
}

func newFFT(n int) *fft {
	omega := make([]complex128, n)
	omegaInv := make([]complex128, n)
	for i := range omega {
		sin, cos := math.Sincos(2 * math.Pi * float64(i) / float64(n))
		omega[i] = complex(cos, sin)
		omegaInv[i] = complex(cos, -sin)
	}
	return &fft{n, omega, omegaInv}
}

func (f *fft) transform(a, omega []complex128) {
	for i, j := 0, 0; i < f.n; i++ {
		if i > j {
			a[i], a[j] = a[j], a[i]
		}
		for l := f.n >> 1; ; l >>= 1 {
			j ^= l
			if j >= l {
				break
			}
		}
	}
	for l := 2; l <= f.n; l <<= 1 {
		m := l >> 1
		for st := 0; st < f.n; st += l {
			p := a[st:]
			for i := 0; i < m; i++ {
				t := omega[f.n/l*i] * p[m+i]
				p[m+i] = p[i] - t
				p[i] += t
			}
		}
	}
}

func (f *fft) dft(a []complex128) {
	f.transform(a, f.omega)
}

func (f *fft) idft(a []complex128) {
	f.transform(a, f.omegaInv)
	for i := range a {
		a[i] /= complex(float64(f.n), 0)
	}
}

func convolution(a, b []int) []int {
	n, m := len(a), len(b)
	limit := 1 << uint(bits.Len(uint(n+m-1)))
	f := newFFT(limit)
	cmplxA := make([]complex128, limit)
	for i, v := range a {
		cmplxA[i] = complex(float64(v), 0)
	}
	cmplxB := make([]complex128, limit)
	for i, v := range b {
		cmplxB[i] = complex(float64(v), 0)
	}
	f.dft(cmplxA)
	f.dft(cmplxB)
	for i := range cmplxA {
		cmplxA[i] *= cmplxB[i]
	}
	f.idft(cmplxA)
	conv := make([]int, n+m-1)
	for i := range conv {
		conv[i] = int(math.Round(real(cmplxA[i])))
	}
	return conv
}

func convolutionN(coefs [][]int) []int {
	var f func(l, r int) []int
	f = func(l, r int) []int {
		if l == r {
			return coefs[l-1]
		}
		mid := (l + r) >> 1
		return convolution(f(l, mid), f(mid+1, r))
	}
	return f(1, len(coefs))
}

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

	var n, m, k, x int
	Fscan(in, &n, &m, &k)
	coefs := make([][]int, m)
	for i := range coefs {
		coefs[i] = []int{1}
	}
	for ; n > 0; n-- {
		Fscan(in, &x)
		x--
		coefs[x] = append(coefs[x], 1)
	}
	Fprint(out, convolutionN(coefs)[k])
}

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