Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \ne p_i$$$ holds for all $$$i$$$ ($$$3 \le i \le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 48$$$) — the number of test cases.  The single line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$).

For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem.

package main

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

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
		fmt.Fscan(in, &n)

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

		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		for _, v := range arr {
			fmt.Fprintf(out, "%d ", v)
		}
		fmt.Fprintln(out)

		for i := 0; i < n-1; i++ {
			arr[i], arr[i+1] = arr[i+1], arr[i]
			for _, v := range arr {
				fmt.Fprintf(out, "%d ", v)
			}
			fmt.Fprintln(out)

			arr[i], arr[i+1] = arr[i+1], arr[i]
		}
	}
}

// Auto generated using https://github.com/njkevlani/go_bundle
package main

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

func main() {
	s := NewStdinScanner()
	T := NextInt(s)
	for tt := 1; tt <= T; tt++ {
		n := NextInt(s)
		solve(n)
	}
}
func NewStdinScanner() *bufio.Scanner {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	return s
}
func NextInt(s *bufio.Scanner) int {
	return atoi(NextToken(s))
}
func atoi(str string) int {
	i, err := strconv.Atoi(str)
	if err != nil {
		panic(err)
	}
	return i
}
func NextToken(s *bufio.Scanner) string {
	if !s.Scan() {
		fmt.Println(s.Err().Error())
		panic("Could not read input")
	}
	return s.Text()
}
func solve(n int) {
	arr := make([]int, n)
	for i := 1; i <= n; i++ {
		arr[i-1] = i
	}
	var count int
	permutationRecursive(arr, &count, 0, len(arr)-1)
}
func permutationRecursive(arr []int, count *int, l, r int) {
	if l == r {
		if isAntiFibonacci(arr) {
			for _, el := range arr {
				fmt.Print(el, " ")
			}
			fmt.Println()
			*count++
			if *count == len(arr) {
				return
			}
		}
	} else {
		for i := l; i <= r; i++ {
			arr[l], arr[i] = arr[i], arr[l]
			if l < 2 || arr[l-2]+arr[l-1] != arr[l] {
				permutationRecursive(arr, count, l+1, r)
				if *count == len(arr) {
					return
				}
			}
			arr[l], arr[i] = arr[i], arr[l]
		}
	}
}
func isAntiFibonacci(arr []int) bool {
	for i := 2; i < len(arr); i++ {
		if arr[i-2]+arr[i-1] == arr[i] {
			return false
		}
	}
	return true
}

//nolint
package main

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

//{
var reader = bufio.NewReader(os.Stdin)
var 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 print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

func Main() {
	var t int
	scan(&t)
	for ; t > 0; t-- {
		Solve()
	}
}

//}

func Solve() {
	var n int
	scan(&n)
	arr := [][]int{{3, 2, 1}, {3, 1, 2}, {2, 3, 1}}
	for i := 4; i <= n; i++ {
		for j, l := range arr {
			arr[j] = append([]int{i}, l...)
		}
		tp := append([]int{}, arr[0]...)
		tp[0], tp[1] = tp[1], tp[0]
		arr = append(arr, tp)
	}
	printResult(arr)
}

func printResult(a [][]int) {
	for _, x := range a {
		for _, y := range x {
			print(y, " ")
		}
		println()
	}
}

package main

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

func contains(s []int, e int) bool {
	for i := 0; i < len(s); i++ {
		if e == s[i] {
			return true
		}
	}
	return false
}

func build(s []int, p []int, length int, results int) int {
	//fmt.Println(s, p, length)
	if results == length {
		return results
	}
	if len(p) == length {
		s := strings.Trim(fmt.Sprint(p), "[]")
		fmt.Println(s)
		results += 1
		return results
	}
	for i := 0; i < len(s); i++ {
		if contains(p, s[i]) {
			continue
		}
		if len(p) >= 2 {
			if p[len(p)-1]+p[len(p)-2] == s[i] {
				continue
			}

		}
		//fmt.Println(p)
		results = build(s, append(p, s[i]), length, results)
	}
	return results
}

func main() {
	var tests int
	var err error

	scanner := bufio.NewScanner(os.Stdin)
	if scanner.Scan() {
		text := scanner.Text()
		tests, err = strconv.Atoi(text)
		if err != nil {
			log.Fatalln("Error not an integral number of tests")
		}
		if tests < 1 || tests > 48 {
			log.Fatalln("Error test number out of range")
		}
	} else {
		log.Fatalln("Error reading number of tests")
	}
	for i := 0; i < tests; i++ {
		var length int
		if scanner.Scan() {
			text := scanner.Text()
			length, err = strconv.Atoi(text)
			if err != nil {
				log.Fatalln("Error not an integral number for permutation")
			}
			if length < 3 || length > 50 {
				log.Fatalln("Error test case out of range")
			}
		} else {
			log.Fatalln("Error reading permutation")
		}
		s := make([]int, length, length)
		p := make([]int, 0, length)
		for i := 0; i < length; i++ {
			s[i] = i + 1
		}
		build(s, p, length, 0)
	}
}

package main

import "fmt"

func solver() {
	var t int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)
		for i1 := 0; i1 < n; i1++ {
			for j := n; j > 1; j-- {
				if j-1 == i1 {
					fmt.Printf("1 ")
				}
				fmt.Printf("%v ", j)

				if j == 2 && i1 == 0 {
					fmt.Print("1")
				}
			}
			fmt.Println()
		}
	}
}

func main() {
	solver()
}

package main

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

type CF1644B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1644B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1644B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1644B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1644B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1644B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1644B
Date: 16/03/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1644/B
Problem: CF1644B
**/
func (in *CF1644B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		var sb strings.Builder
		if n == 3 {
			sb.WriteString("3 2 1\n1 3 2\n3 1 2\n")
		} else {
			arr := make([]int, n)
			mapa := make(map[int]bool)
			for i := 0; i < n; i++ {
				mapa[i+1] = true
			}
			ws := true

			for ws {
				ws = false
				for i := 0; i < n; i++ {
					if i == 0 {
						arr[i] = 1
					} else if i == 1 {
						arr[i] = 2
					} else {
						arr[i] = Next(mapa, arr[i-1]+arr[i-2])
					}
					if arr[i] == -1 {
						ws = true
						for i := 0; i < n; i++ {
							mapa[i+1] = true
						}
						break
					}
					delete(mapa, arr[i])
				}

			}

			for i := 0; i < n; i++ {
				for j := 0; j < n; j++ {
					sb.WriteString(fmt.Sprintf("%d ", arr[(i+j)%n]))
				}
				sb.WriteString("\n")
			}
		}
		fmt.Print(sb.String())

	}
}

func Next(mapa map[int]bool, num int) int {
	for k, v := range mapa {
		if v && k != num {
			return k
		}
	}
	return -1
}

func NewCF1644B(r *bufio.Reader) *CF1644B {
	return &CF1644B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1644B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"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 readString(in *bufio.Reader) string {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	return nStr
}

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(in int) [][]int {
	var out [][]int

	out = make([][]int, in)

	for i := range out {
		out[i] = make([]int, in)
		fixIndex := 0

		for j := range out[i] {
			if i < j {
				out[i][j] = in - j
			} else {
				out[i][j] = in - (j + 1)
			}

			if i == j {
				out[i][j] = in
			}

			if j > 1 && out[i][j] == (out[i][j-1]+out[i][j-2]) {
				tmp := out[i][j]
				out[i][j] = out[i][fixIndex]
				out[i][fixIndex] = tmp
				fixIndex++
			}
		}
	}

	return out
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n := readInt(in)

	for i := 0; i < n; i++ {
		input := readInt(in)

		res := Solve(input)

		for _, row := range res {
			for j, col := range row {
				if j < len(row)-1 {
					fmt.Printf("%d ", col)
				} else {
					fmt.Printf("%d\n", col)
				}
			}
		}
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o := make([]int, n)
		for j, k := n-1, 0; j >= 0; j-- {
			o[k], k = j+1, k+1
		}
		k, q := 0, make([]int, n)
		copy(q, o)
		for j := 0; j < n; j++ {
			if k > 0 {
				o[k-1], o[k] = o[k], o[k-1]
			}
			for l := 0; l < n; l++ {
				fmt.Fprint(w, o[l], " ")
			}
			k++
			copy(o, q)
			fmt.Fprintln(w, "")
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		list:= make([]int,n)
		for j:=n-1;j>=0;j--{
			list[n-j-1]=j+1
		}
		for j:=0;j<n;j++{
			fmt.Fprintf(out,"%v ",list[j])
		}
		fmt.Fprintln(out,)
		for j:=0;j<n-1;j++{
			for k:=0;k<n;k++{
				if k == j {
					fmt.Fprintf(out,"%v ",list[k+1])
					fmt.Fprintf(out,"%v ",list[k])
					k++
					continue
				}
				fmt.Fprintf(out,"%v ",list[k])
			}
			fmt.Fprintln(out,)
		}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1644B(_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)
		Fprint(out, 1)
		for i := n; i > 1; i-- {
			Fprint(out, " ", i)
		}
		Fprintln(out)
		for i := n; i > 1; i-- {
			j := n
			for ; j > i; j-- {
				Fprint(out, j, " ")
			}
			Fprint(out, j-1, j, " ")
			for j -= 2; j > 0; j-- {
				Fprint(out, j, " ")
			}
			Fprintln(out)
		}
	}
}

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

package main

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

func solve(scanner *bufio.Scanner) {
	scanner.Scan()
	cur, _ := strconv.Atoi(scanner.Text())
	visited := make([]bool, cur+1)
	cnt := make([]int, 1)
	dfs(cur, visited, &[]int{}, cnt)
}

func dfs(N int, visited []bool, temp *[]int, cnt []int) {
	if cnt[0] >= N {
		return
	}
	if len(*temp) >= N {
		for _, t := range *temp {
			fmt.Printf("%d ", t)
		}
		fmt.Println()
		cnt[0]++
		return
	}
	for i := 1; i <= N; i++ {
		if visited[i] {
			continue
		}
		if len(*temp) >= 2 && ((*temp)[len(*temp)-1]+(*temp)[len(*temp)-2]) == i {
			continue
		}
		visited[i] = true
		*temp = append(*temp, i)
		dfs(N, visited, temp, cnt)
		*temp = (*temp)[:len(*temp)-1]
		visited[i] = false
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		solve(scanner)
	}

}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func solve(n int) string {
	var r []string
	for i := 1; i <= n; i++ {
		x := fmt.Sprint(i)
		for j := n; j >= 1; j-- {
			if j != i {
				x += fmt.Sprintf(" %d", j)
			}
		}
		r = append(r, x)
	}
	return strings.Join(r, "\n")
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		fmt.Println(solve(n))
	}
}

package main

import "fmt"

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		solve()
	}
}
func solve() {
	var n int
	fmt.Scan(&n)
	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", i)
		for j := n; j >= 1; j-- {
			if j == i {
				continue
			}
			fmt.Printf("%d ", j)
		}
		fmt.Printf("\n")
	}
}

package main

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

/*

 */

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

	p := make([]int, n)

	for i := range p {
		p[i] = n - i
	}

	for i := n - 1; i > 0; i-- {
		for j := range p {
			fmt.Fprintf(writer, "%d ", p[j])
		}
		fmt.Fprintln(writer)

		p[i], p[i-1] = p[i-1], p[i]
	}

	for j := range p {
		fmt.Fprintf(writer, "%d ", p[j])
	}
	fmt.Fprintln(writer)
}

//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 int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

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

	return nums[idx], idx
}

//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 (
	"bufio"
	"fmt"
	"os"
	"sort"
)

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
		fmt.Fscan(in, &n)

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

		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		var i int
		for c := 0; c < n; c++ {
			for _, v := range arr {
				fmt.Fprintf(out, "%d ", v)
			}
			fmt.Fprintln(out)

			if i == n-2 {
				arr[0], arr[n-1] = arr[n-1], arr[0]
				i = 0
			} else {
				arr[i], arr[i+1] = arr[i+1], arr[i]
			}
			i++
		}
	}
}

package main

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

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
		fmt.Fscan(in, &n)

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

		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		for i := 0; i < n; i++ {
			for _, v := range arr {
				fmt.Fprintf(out, "%d ", v)
			}
			fmt.Fprintln(out)
			if i+1 < n {
				arr[i], arr[i+1] = arr[i+1], arr[i]
			}
		}
	}
}

//nolint
package main

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

//{
var reader = bufio.NewReader(os.Stdin)
var 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 print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

func Main() {
	var t int
	scan(&t)
	for ; t > 0; t-- {
		Solve()
	}
}

//}

func Solve() {
	var n int
	scan(&n)
	arr := [][]int{{2, 1, 3}, {3, 1, 2}, {1, 3, 2}}
	for i := 4; i <= n; i++ {
		for j, l := range arr {
			arr[j] = append(l, i)
		}
		arr = append(arr, append([]int{i}, arr[0][:len(arr[0])-1]...))
	}
	printResult(arr)
}

func printResult(a [][]int) {
	for _, x := range a {
		for _, y := range x {
			print(y, " ")
		}
		println()
	}
}

//nolint
package main

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

//{
var reader = bufio.NewReader(os.Stdin)
var 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 print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

func Main() {
	var t int
	scan(&t)
	for ; t > 0; t-- {
		Solve()
	}
}

//}

func Solve() {
	var n int
	scan(&n)
	arr := [][]int{{3, 2, 1}, {1, 3, 2}, {3, 1, 2}}
	for i := 4; i <= n; i++ {
		for j, l := range arr {
			arr[j] = append(l, i)
		}
		arr = append(arr, append([]int{i}, arr[0][:len(arr[0])-1]...))
	}
	printResult(arr)
}

func printResult(a [][]int) {
	for _, x := range a {
		for _, y := range x {
			print(y, " ")
		}
		println()
	}
}

package main

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

func contains(s []int, e int) bool {
	for i := 0; i < len(s); i++ {
		if e == s[i] {
			return true
		}
	}
	return false
}

func build(s []int, p []int, length int, results int) int {
	//fmt.Println(s, p, length)
	if results == length {
		return results
	}
	if len(p) == length {
		fmt.Println(p)
		results += 1
		return results
	}
	for i := 0; i < len(s); i++ {
		if contains(p, s[i]) {
			continue
		}
		if len(p) >= 2 {
			if p[len(p)-1]+p[len(p)-2] == s[i] {
				continue
			}

		}
		//fmt.Println(p)
		results = build(s, append(p, s[i]), length, results)
	}
	return results
}

func main() {
	var tests int
	var err error

	scanner := bufio.NewScanner(os.Stdin)
	if scanner.Scan() {
		text := scanner.Text()
		tests, err = strconv.Atoi(text)
		if err != nil {
			log.Fatalln("Error not an integral number of tests")
		}
		if tests < 1 || tests > 48 {
			log.Fatalln("Error test number out of range")
		}
	} else {
		log.Fatalln("Error reading number of tests")
	}
	for i := 0; i < tests; i++ {
		var length int
		if scanner.Scan() {
			text := scanner.Text()
			length, err = strconv.Atoi(text)
			if err != nil {
				log.Fatalln("Error not an integral number for permutation")
			}
			if length < 3 || length > 50 {
				log.Fatalln("Error test case out of range")
			}
		} else {
			log.Fatalln("Error reading permutation")
		}
		s := make([]int, length, length)
		p := make([]int, 0, length)
		for i := 0; i < length; i++ {
			s[i] = i
		}
		build(s, p, length, 0)
	}
}

package main

import "fmt"

func solver() {
	var t int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)
		for j := n; j >= 1; j-- {
			fmt.Printf("%v ", j)
		}
		fmt.Println()
	}
}

func main() {
	solver()
}

package main

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

type CF1644B struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1644B) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1644B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1644B) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1644B) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1644B) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1644B
Date: 16/03/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1644/B
Problem: CF1644B
**/
func (in *CF1644B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		var sb strings.Builder
		if n == 3 {
			sb.WriteString("3 2 1\n1 3 2\n3 1 2\n")
		} else {
			arr := make([]int, n)
			mapa := make(map[int]bool)
			for i := 0; i < n; i++ {
				mapa[i+1] = true
			}
			for i := 0; i < n; i++ {
				if i == 0 {
					arr[i] = 1
				} else if i == 1 {
					arr[i] = 2
				} else {
					arr[i] = Next(mapa, arr[i-1]+arr[i-2])
				}
				delete(mapa, arr[i])
			}

			for i := 0; i < n; i++ {
				for j := 0; j < n; j++ {
					sb.WriteString(fmt.Sprintf("%d ", arr[(i+j)%n]))
				}
				sb.WriteString("\n")
			}
		}
		fmt.Print(sb.String())

	}
}

func Next(mapa map[int]bool, num int) int {
	for k, v := range mapa {
		if v && k != num {
			return k
		}
	}
	return -1
}

func NewCF1644B(r *bufio.Reader) *CF1644B {
	return &CF1644B{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1644B(bufio.NewReader(os.Stdin)).Run()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"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 readString(in *bufio.Reader) string {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	return nStr
}

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(in int) [][]int {
	var out [][]int

	out = make([][]int, in)

	for i := range out {
		out[i] = make([]int, in)
		fixIndex := 0

		for j := range out[i] {
			if i < j {
				out[i][j] = in - j
			} else {
				out[i][j] = in - (j + 1)
			}

			if i == j {
				out[i][j] = in
			}

			if j > 1 && out[i][j] == (out[i][j-1]+out[i][j-2]) {
				tmp := out[i][j]
				out[i][j] = out[i][fixIndex]
				out[i][fixIndex] = tmp
				fixIndex++
			}
		}
	}

	return out
}

func main() {
	in := bufio.NewReader(os.Stdin)
	n := readInt(in)

	for i := 0; i < n; i++ {
		input := readInt(in)

		res := Solve(input)

		fmt.Printf("%+v\n", res)
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o := make([]int, n)
		for j, k := n-1, 0; j >= 0; j-- {
			o[k], k = j+1, k+1
		}
		k, q := 0, o
		for j := 0; j < n; j++ {
			if k > 0 {
				o[k-1], o[k] = o[k], o[k-1]
			}
			for l := 0; l < n; l++ {
				fmt.Fprint(w, o[l], " ")
			}
			k++
			o = q
			fmt.Fprintln(w, "")
		}
	}
	w.Flush()
}

func main() {
	solve()
}


Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.The pasture is a rectangle consisting of R × C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.

First line contains two integers R (1 ≤ R ≤ 500) and C (1 ≤ C ≤ 500), denoting the number of rows and the numbers of columns respectively. Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.

If it is impossible to protect all sheep, output a single line with the word "No". Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf. If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.

NoteIn the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.In the second example, there are no empty spots to put dogs that would guard the lone sheep.In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.

package main

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

func solve() {
	r := readInt()
	c := readInt()
	res := make([]string, r)
	for i := 0; i < r; i++ {
		res[i] = readString()
	}
	for i := 0; i < r; i++ {
		row := res[i]
		for j := 0; j < len(row); j++ {
			if row[j] == 'W' {
				if j > 0 && row[j-1] == 'S' {
					fmt.Println("No")
					return
				}
				if j+1 < c && row[j+1] == 'S' {
					fmt.Println("No")
					return
				}
				if i > 0 && res[i-1][j] == 'S' {
					fmt.Println("No")
					return
				}
				if i+1 < r && res[i+1][j] == 'S' {
					fmt.Println("No")
					return
				}
			}
		}
		res[i] = row
	}
	fmt.Println("Yes")
	for i := 0; i < r; i++ {
		fmt.Println(strings.Replace(res[i], ".", "D", -1))
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

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

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

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

package main

import (
	"fmt"
	// "strings"
)

func setD(mas [][]rune, x, y int) {
	if mas[x][y] == 'W' {
		mas[0][0] = 'N'
	} else if mas[x][y] == '.' {
		mas[x][y] = 'D'
	}
}

func main() {
	var n, m int
	var s string
	fmt.Scan(&n, &m)
	var mas [][]rune = make([][]rune, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&s)
		mas[i] = []rune(s)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if mas[i][j] == 'S' {
				if i > 0 {
					setD(mas, i - 1, j)
				}
				if i < n - 1 {
					setD(mas, i + 1, j)
				}
				if j > 0 {
					setD(mas, i, j - 1)
				}
				if j < m - 1 {
					setD(mas, i, j + 1)
				}
			}
		}
	}
	if mas[0][0] != 'N' {
		fmt.Println("Yes")
		for i := 0; i < n; i++ {
			fmt.Println(string(mas[i]))
		}
	} else {
		fmt.Println("No")
	}
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (i *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(i.Writer, f, a...)
}

func (i *writerImpl) Flush() {
	i.Writer.Flush()
}

func main() {
	defer writer.Flush()
    R := reader.NextInt64()
    _ = reader.NextInt64()
    grid := [][]rune{}
    for ; R > 0; R-- {
        tmp := []rune{}
        for _, r := range reader.NextString() {
            if r == '.' {
                tmp = append(tmp, 'D')
            } else {
                tmp = append(tmp, r)
            }
        }
        grid = append(grid, tmp)
    }
    if !possible(grid) {
        writer.Printf("No")
        return
    }
    writer.Printf("Yes\n")
    for _, row := range grid {
        writer.Printf("%s\n", string(row))
    }
}

type pos struct {
    i int
    j int
}

func (p pos) valid(maxI, maxJ int) bool {
    return p.i >= 0 && p.i < maxI && p.j >= 0 && p.j < maxJ
}

func possible(grid [][]rune) bool {
    maxI := len(grid)
    maxJ := len(grid[0])
    for i := range grid {
        for j := range grid[i] {
            if grid[i][j] == 'S' {
                for _, p := range []pos{
                    {i+1, j},
                    {i-1, j},
                    {i, j+1},
                    {i, j-1},
                }{
                    if p.valid(maxI, maxJ) && grid[p.i][p.j] == 'W' {
                        return false
                    }
                }
            }
        }
    }
    return true
}

package main

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

func solve() {
	r := readInt()
	c := readInt()
	res := make([]string, r)
	for i := 0; i < r; i++ {
		res[i] = readString()
	}
	for i := 0; i < r; i++ {
		row := res[i]
		for j := 0; j < len(row); j++ {
			if row[j] == 'W' {
				if j > 0 && row[j-1] == 'S' {
					fmt.Println("No")
					return
				}
				if j+1 < c && row[j+1] == 'S' {
					fmt.Println("No")
					return
				}
				if i > 0 && res[i-1][j] == 'S' {
					fmt.Println("No")
					return
				}
				if i+1 < r && res[i+1][j] == 'S' {
					fmt.Println("No")
					return
				}
			}
		}
		res[i] = row
	}
	for i := 0; i < r; i++ {
		fmt.Println(strings.Replace(res[i], ".", "D", -1))
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

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

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

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

package main

import (
	"fmt"
	// "strings"
)

func setD(mas [][]rune, x, y int) {
	// if mas[x][y] == 'W' {
	// 	mas[0][0] = 'N'
	// } else if mas[x][y] == '.' {
	// 	mas[x][y] = 'D'
	// }
}

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	var mas [][]rune = make([][]rune, n)
	for i := 0; i < n; i++ {
		mas[i] = make([]rune, m)
	}
	fmt.Scan(&mas)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if mas[i][j] == 'S' {
				if i > 0 {
					setD(mas, i - 1, j)
				}
				if i < n - 1 {
					setD(mas, i + 1, j)
				}
				if j > 0 {
					setD(mas, i, j - 1)
				}
				if j < m - 1 {
					setD(mas, i, j + 1)
				}
			}
		}
	}
	if mas[0][0] != 'N' {
		fmt.Println("Yes")
		for i := 1; i < n; i++ {
			fmt.Println(mas[i])
		}
	} else {
		fmt.Println("No")
	}
}

package main

import (
	"fmt"
	// "strings"
)

func setD(mas [][]rune, x, y int) {
	if mas[x][y] == 'W' {
		mas[0][0] = 'N'
	} else if mas[x][y] == '.' {
		mas[x][y] = 'D'
	}
}

func main() {
	var n, m int
	var s string
	fmt.Scan(&n, &m)
	var mas [][]rune = make([][]rune, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&s)
		mas[i] = []rune(s)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if mas[i][j] == 'S' {
				if i > 0 {
					setD(mas, i - 1, j)
				}
				if i < n - 1 {
					setD(mas, i + 1, j)
				}
				if j > 0 {
					setD(mas, i, j - 1)
				}
				if j < m - 1 {
					setD(mas, i, j + 1)
				}
			}
		}
	}
	if mas[0][0] != 'N' {
		fmt.Println("Yes")
		for i := 1; i < n; i++ {
			fmt.Println(string(mas[i]))
		}
	} else {
		fmt.Println("No")
	}
}


The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array $$$a$$$ of $$$n$$$ different numbers. In one query you can ask the position of the second maximum element in a subsegment $$$a[l..r]$$$. Find the position of the maximum element in the array in no more than 40 queries.A subsegment $$$a[l..r]$$$ is all the elements $$$a_l, a_{l + 1}, ..., a_r$$$. After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.

The first line contains a single integer $$$n$$$ $$$(2 \leq n \leq 10^5)$$$ — the number of elements in the array.

NoteIn the sample suppose $$$a$$$ is $$$[5, 1, 4, 2, 3]$$$. So after asking the $$$[1..5]$$$ subsegment $$$4$$$ is second to max value, and it's position is $$$3$$$. After asking the $$$[4..5]$$$ subsegment $$$2$$$ is second to max value and it's position in the whole array is $$$4$$$.Note that there are other arrays $$$a$$$ that would produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.

package main

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

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

func ask(l, r int) int {
	if l == r {
		return -1
	}
	fmt.Fprintf(out, "? %d %d\n", l, r)
	out.Flush()

	var t int
	fmt.Fscan(in, &t)
	return t
}

func guess(l, r int) int {
	if l > r {
		panic("impossible")
	}
	if l == r {
		return l
	}
	if r-l == 1 {
		x := ask(l, r)
		if x == l {
			return r
		} else if x == r {
			return l
		} else {
			panic("impossible")
		}
	}

	x := ask(l, r)
	mid := l + (r-l)/2
	if x <= mid { // second max is in the left half
		x2 := ask(l, mid)
		if x2 == x {
			return guess(l, mid)
		} else {
			return guess(mid+1, r)
		}
	} else { // second max is in the right half
		x2 := ask(mid+1, r)
		if x2 == x {
			return guess(mid+1, r)
		} else {
			return guess(l, mid)
		}
	}
}

func main() {
	var n int
	fmt.Fscan(in, &n)

	fmt.Fprintf(out, "! %d\n", guess(1, n))
	out.Flush()
}

package main

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

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

func ask(l, r int) int {
	fmt.Fprintf(out, "? %d %d\n", l, r)
	out.Flush()

	var t int
	fmt.Fscan(in, &t)
	return t
}

func guess(l, r int) int {
	if l > r {
		panic("impossible")
	}
	if l == r {
		return l
	}
	if r-l == 1 {
		x := ask(l, r)
		if x == l {
			return r
		} else if x == r {
			return l
		} else {
			panic("impossible")
		}
	}

	x := ask(l, r)
	mid := l + (r-l)/2
	if x <= mid { // second max is in the left half
		x2 := ask(l, mid)
		if x2 == x {
			return guess(l, mid)
		} else {
			return guess(mid+1, r)
		}
	} else { // second max is in the right half
		// The size of the right half is 1.
		if mid+1 == r {
			return guess(l, mid)
		}
		x2 := ask(mid+1, r)
		if x2 == x {
			return guess(mid+1, r)
		} else {
			return guess(l, mid)
		}
	}
}

func main() {
	var n int
	fmt.Fscan(in, &n)

	fmt.Fprintf(out, "! %d\n", guess(1, n))
	out.Flush()
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	n := readNum(reader)

	query := func(l, r int) int {
		fmt.Printf("? %d %d\n", l, r)
		return readNum(reader)
	}

	res := solve(n, query)

	fmt.Printf("! %d\n", res)
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n int, query func(int, int) int) int {
	// max is always between [left, right]
	left, right := 1, n

	for left < right {
		second := query(left, right)
		if left+1 == right {
			// only two, the other one is max
			return left + right - second
		}
		mid := (left + right) / 2
		if mid <= second {
			x := query(mid, right)
			if x == second {
				left = mid
			} else {
				right = mid - 1
			}
		} else {
			// mid > second
			x := query(left, mid)
			if x == second {
				right = mid
			} else {
				left = mid + 1
			}
		}
	}
	return right
}

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() {
	n := sint64()
	s := req(1, n)

	// highest num in left half
	if s != 1 && s == req(1, s) {
		l := int64(1)
		r := s - 1
		ans := l
		for l <= r {
			m := (l + r) / 2
			if req(m, s) == s {
				ans = m
				l = m + 1
			} else {
				r = m - 1
			}
		}
		submit(ans)
	}

	// highest num in right half
	l := s + 1
	r := n
	ans := r
	for l <= r {
		m := (l + r) / 2
		if req(s, m) == s {
			ans = m
			r = m - 1
		} else {
			l = m + 1
		}
	}
	submit(ans)
}

func req(l, r int64) int64 {
	printf("? %d %d\n", l, r)
	writer.Flush()
	return sint64()
}

func submit(x int64) {
	printf("! %d\n", x)
	writer.Flush()
	os.Exit(0)
}

package main

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

func main() {
	var n int
	in := bufio.NewReader(os.Stdin)
	_, _ = fmt.Fscan(in, &n)

	left := 1
	right := n

	for left < right {
		var mid, second int

		fmt.Printf("? %d %d\n", left, right)
		_, _ = fmt.Fscan(in, &second)

		if left+1 == right {
			ans := left + right - second
			fmt.Printf("! %d\n", ans)
			return
		}

		mid = (left + right) / 2

		if second <= mid {
			fmt.Printf("? %d %d\n", left, mid)
			var leftSecond int
			_, _ = fmt.Fscan(in, &leftSecond)

			if second == leftSecond {
				right = mid
			} else {
				left = mid
			}
		} else {
			fmt.Printf("? %d %d\n", mid, right)
			var rightSecond int
			_, _ = fmt.Fscan(in, &rightSecond)

			if second == rightSecond {
				left = mid
			} else {
				right = mid
			}
		}
	}

	fmt.Printf("! %d\n", left)
}

package main

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

const (
	MOD int64 = 1e9+7
	NL byte = byte('\n')
	SP byte = byte(' ')
)

var (
	in *bufio.Scanner
	out *bufio.Writer

	n int
	res int
)

func main() {
	initBuffer()
	var t int = 1
	// t = getInt()
	for i := 0; i < t; i++ {
		solve()
	}
	// test()
	out.Flush()
}

func solve() {
	initialize()
	input()
	count()
	output()
}

func initialize() {

}

func input() {
	n = getInt()
}

func count() {
	var i1, i2 int = 1, n
	printIt(i1, i2)
	var x int = getInt()
	if x == 1 {
		i1 = 2
	} else if x == n {
		i2 = n - 1
	} else {
		printIt(1, x)
		if x == getInt() {
			i2 = x - 1
		} else {
			i1 = x + 1
		}
	}
	var k int
	for i1 < i2 {
		k = (i1 + i2) / 2
		if x < i1 {
			printIt(x, k)
			if x == getInt() {
				i2 = k
			} else {
				i1 = k + 1
			}
		} else {
			if i1 + 1 == i2 {
				printIt(i2, x)
				if x == getInt() {
					i1++
				} else {
					i2--
				}
			} else {
				printIt(k, x)
				if x == getInt() {
					i1 = k
				} else {
					i2 = k - 1
				}
			}
		}
	}
	res = i1
}

func printIt(a, b int) {
	out.WriteString("? ")
	out.Write(intToBytes(a))
	out.WriteByte(SP)
	out.Write(intToBytes(b))
	out.WriteByte(NL)
	out.Flush()
}

func output() {
	out.WriteString("! ")
	out.Write(intToBytes(res))
}

func test() {
	fmt.Print()
}

func initBuffer() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer([]byte{}, 4 * 1024 * 1024)
	in.Split(scanWords)
	out = bufio.NewWriter(os.Stdout)
}

func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	i := bytes.IndexByte(data, '\n')
	j := bytes.IndexByte(data, ' ')
	ind := min(i, j)
	if ind < 0 {
		ind = max(i, j)
	}
	if ind >= 0 {
		return ind + 1, dropCR(data[0:ind]), nil
	}
	if atEOF {
		return len(data), dropCR(data), nil
	}
	return 0, nil, nil
}

func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[:len(data)-1]
	}
	return data
}

func getFloat() float64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res1 int64
	for i := 0; i < len(b); i++ {
		if b[i] == '.' {
			b = b[i+1:]
			break
		}
		res1 *= 10
		res1 += int64(b[i]) - 48
		if i == len(b) - 1 {
			b = b[:0]
		}
	}
	var res2 float64
	for i := len(b); i > 0; i-- {
		res2 += float64(b[i-1]) - 48
		res2 /= 10
	}
	if isNeg {
		res1 *= -1
		res2 *= -1
	}
	return float64(res1) + res2
}

func getInt() int {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getInt64() int64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int64
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int64(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getString() string {
	in.Scan()
	return in.Text()
}

func intToBytes(n int) []byte {
	var res [33]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 33
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 33 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func int64ToBytes(n int64) []byte {
	var res [65]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 65
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 65 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func bsLower(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsLower64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsUpper(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func bsUpper64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func max(n1, n2 int) int {
	if n1 > n2 {
		return n1
	}
	return n2
}

func max64(n1, n2 int64) int64 {
	if n1 > n2 {
		return n1
	}
	return n2
}

func min(n1, n2 int) int {
	if n1 < n2 {
		return n1
	}
	return n2
}

func min64(n1, n2 int64) int64 {
	if n1 < n2 {
		return n1
	}
	return n2
}

func yes(res, isUpperCase bool) bool {
	var arr [4]string = [4]string{"No", "Yes", "NO", "YES"}
	var ind int
	if res == true {
		ind = 1
	}
	if isUpperCase == true {
		ind += 2
	}
	out.WriteString(arr[ind])
	out.WriteByte(NL)
	return res
}

package main

import (
	"fmt"
)

func main() {
	var n, m2, res, leftStart, leftEnd, rightStart, rightEnd int

	fmt.Scan(&n)
	start, end := 1, n
	fmt.Printf("? %d %d\n", start, end)
	fmt.Scan(&m2)
	if n == 2 {
		if m2 == 2 {
			fmt.Print("! 1")
		} else {
			fmt.Print("! 2")
		}
		return
	}

	for {
		leftStart, leftEnd = start, start+(end-start)/2
		rightStart, rightEnd = leftEnd+1, end
		if m2 >= start && m2 <= end {
			if end-start == 1 {
				if start == m2 {
					start = end
				}
				break
			}
			if rightStart == rightEnd && m2 == rightEnd {
				leftStart, leftEnd = start, start
				rightStart, rightEnd = end-1, end-1
			}
		}
		//fmt.Println(start, end, leftStart, leftEnd, rightStart, rightEnd)
		if m2 <= leftEnd {
			fmt.Printf("? %d %d\n", min(leftStart, m2), leftEnd)
			fmt.Scan(&res)
			if res == m2 {
				start, end = leftStart, leftEnd
			} else {
				start, end = rightStart, rightEnd
			}
		} else {
			fmt.Printf("? %d %d\n", rightStart, max(rightEnd, m2))
			fmt.Scan(&res)
			if res != m2 {
				start, end = leftStart, leftEnd
			} else {
				start, end = rightStart, rightEnd
			}
		}
		if start == end {
			break
		}
	}
	fmt.Printf("! %d", start)
}

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

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

package main

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

type pair [2]int

func cachedQuery(m map[pair]int, l, r int) int {
	x, ok := m[pair{l, r}]
	if ok {
		return x
	}
	fmt.Printf("? %d %d\n", l, r)
	scan(&x)
	m[pair{l, r}] = x
	return x
}

func solveC(n int) {
	mem := make(map[pair]int)
	l, r := 1, n
	move := 0

	secondMaxPos := cachedQuery(mem, 1, n)
	if secondMaxPos == 1 {
		l = 1
		r = n
		move = 1
	} else if secondMaxPos == n {
		l = 1
		r = n
		move = -1
	} else {
		pos := cachedQuery(mem, 1, secondMaxPos)
		if pos != secondMaxPos {
			l = secondMaxPos
			r = n
			move = 1
		} else {
			l = 1
			r = secondMaxPos
			move = -1
		}
	}

	//fmt.Println("@", l, r)

	for r-l > 1 {
		m := l + (r-l)/2
		if move == -1 {
			x := cachedQuery(mem, m, secondMaxPos)
			if x == secondMaxPos {
				l = m
			} else {
				r = m
			}
		} else {
			x := cachedQuery(mem, secondMaxPos, m)
			if x == secondMaxPos {
				r = m
			} else {
				l = m
			}
		}

		//fmt.Println("@", l, r)
	}

	if move == -1 {
		fmt.Printf("! %d\n", l)
	} else {
		fmt.Printf("! %d\n", r)
	}

	//fmt.Println(l, r) 1 4 2 5 3
}

func main() {
	//defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)

	for t := 0; t < T; t++ {
		n := 0
		scan(&n)
		solveC(n)
	}
}

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

import (
	. "fmt"
	"os"
)

type (
	input struct{ n int }
	req   struct{ l, r int }
	resp  struct{ v int }
	guess struct{ ans int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(req) resp) (gs guess) {
	q := func(l, r int) int {
		if l == r {
			panic(-1)
		}
		return Q(req{l, r}).v
	}
	n := in.n
	ans := 0
	defer func() { gs.ans = ans }()

	//p := q(1, n)
	//
	//if n == 2 {
	//	ans = 3 - p
	//	return
	//}

	l, r := 1, n+1
	for {
		if r-l == 2 {
			ans = l + r - 1 - q(l, r-1)
			return
		}

		if r-l == 3 {
			p, pl, pr := q(l, r-1), q(l, l+1), q(l+1, l+2)
			if pl == pr {
				if p > pl {
					ans = pl - 1
					return
				}
				ans = pl + 1
				return
			}
			if pl == p {
				ans = 2*l + 1 - p
				return
			}
			ans = 2*l + 3 - p
			return
		}

		p := q(l, r-1)
		m := (l + r) >> 1
		if p < m {
			pp := q(l, m-1)
			if pp == p {
				r = m
			} else {
				l = m
			}
		} else {
			pp := q(m, r-1)
			if pp == p {
				l = m
			} else {
				r = m
			}
		}
	}
}

func ioq() {
	in := os.Stdin
	out := os.Stdout

	Q := func(req req) (resp resp) {
		Fprintln(out, "?", req.l, req.r)
		//out.Flush()
		Fscan(in, &resp.v)
		return
	}

	d := input{}
	Fscan(in, &d.n)
	gs := run(d, Q)
	ans := gs.ans
	Fprintln(out, "!", ans)
	//out.Flush()
}

func main() { ioq() }

package main

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

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

func ask(l, r int) int {
	fmt.Fprintf(out, "? %d %d\n", l, r)
	out.Flush()

	var t int
	fmt.Fscan(in, &t)
	return t
}

func guess(l, r int) int {
	if l > r {
		panic("impossible")
	}
	if l == r {
		return l
	}
	if r-l == 1 {
		x := ask(l, r)
		if x == l {
			return r
		} else if x == r {
			return l
		} else {
			panic("impossible")
		}
	}

	x := ask(l, r)
	mid := l + (r-l)/2
	if x <= mid {
		// second max is in the left half
		x2 := ask(l, mid)
		if x2 == x {
			return guess(l, mid)
		} else {
			return guess(mid+1, r)
		}
	} else {
		// second max is in the right half
		x2 := ask(mid+1, r)
		if x2 == x {
			return guess(mid+1, r)
		} else {
			return guess(l, mid)
		}
	}
}

func main() {
	var n int
	fmt.Fscan(in, &n)

	fmt.Fprintf(out, "! %d\n", guess(1, n))
	out.Flush()
}

package main

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

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

func ask(l, r int) int {
	fmt.Fprintf(out, "? %d %d\n", l, r)
	out.Flush()

	var t int
	fmt.Fscan(in, &t)
	return t
}

func guess(l, r int) int {
	if l > r {
		panic("impossible")
	}
	if l == r {
		return l
	}
	if r-l == 1 {
		x := ask(l, r)
		if x == l {
			return r
		} else if x == r {
			return l
		} else {
			panic("impossible")
		}
	}

	x := ask(l, r)
	if x == l {
		return guess(l+1, r)
	} else if x == r {
		return guess(l, r-1)
	}

	left := ask(l, x)
	if left == x {
		return guess(l, x-1)
	} else {
		return guess(x+1, r)
	}
}

func main() {
	var n int
	fmt.Fscan(in, &n)

	fmt.Fprintf(out, "! %d\n", guess(1, n))
	out.Flush()
}

package main

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

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

func ask(l, r int) int {
	fmt.Fprintf(out, "? %d %d\n", l, r)
	out.Flush()

	var t int
	fmt.Fscan(in, &t)
	return t
}

func guess(l, r int) int {
	if l > r {
		panic("impossible")
	}
	if l == r {
		return l
	}
	if r-l == 1 {
		x := ask(l, r)
		if x == l {
			return r
		} else if x == r {
			return l
		} else {
			panic("impossible")
		}
	}

	x := ask(l, r)
	if x == l {
		return guess(l+1, r)
	} else if x == r {
		return guess(l, r-1)
	}

	left := ask(l, x)
	if left == x {
		return guess(l, x-1)
	} else {
		return guess(x+1, r)
	}
}

func main() {
	var n int
	fmt.Fscan(in, &n)

	fmt.Fprintf(out, "! %d\n", guess(1, n))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	n := readNum(reader)

	query := func(l, r int) int {
		fmt.Printf("? %d %d\n", l, r)
		return readNum(reader)
	}

	res := solve(n, query)

	fmt.Printf("! %d\n", res)
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n int, query func(int, int) int) int {
	// max is always between [left, right]
	left, right := 1, n

	for left < right {
		second := query(left, right)
		if left+1 == right {
			// only two, the other one is max
			return left + right - second
		}
		if second-left > right-second {
			x := query(left, second)
			if x != second {
				// A[x] < A[second], max has to be after second
				left = second + 1
			} else {
				right = second - 1
			}
		} else {
			x := query(second, right)
			if x != second {
				right = second - 1
			} else {
				left = second + 1
			}
		}
	}
	return right
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	n := readNum(reader)

	query := func(l, r int) int {
		fmt.Printf("? %d %d\n", l, r)
		return readNum(reader)
	}

	res := solve(n, query)

	fmt.Printf("! %d\n", res)
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n int, query func(int, int) int) int {
	// max is always between [left, right]
	left, right := 1, n

	for left < right {
		second := query(left, right)
		if left+1 == right {
			// only two, the other one is max
			return left + right - second
		}
		if second-left < right-second {
			x := query(left, second)
			if x != second {
				// A[x] < A[second], max has to be after second
				left = second + 1
			} else {
				right = second - 1
			}
		} else {
			x := query(second, right)
			if x != second {
				right = second - 1
			} else {
				left = second + 1
			}
		}
	}
	return right
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	n := readNum(reader)

	query := func(l, r int) int {
		fmt.Printf("? %d %d\n", l, r)
		return readNum(reader)
	}

	res := solve(n, query)

	fmt.Printf("! %d\n", res)
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n int, query func(int, int) int) int {
	// max is always between [left, right]
	left, right := 1, n

	for left < right {
		second := query(left, right)
		if left+1 == right {
			// only two, the other one is max
			return left + right - second
		}
		choose := rand.Int31n(100)
		if choose < 50 {
			// try first part
			x := query(left, second)
			if x != second {
				// A[x] < A[second], max has to be after second
				left = second + 1
			} else {
				right = second - 1
			}
		} else {
			x := query(second, right)
			if x != second {
				right = second - 1
			} else {
				left = second + 1
			}
		}
	}
	return right
}

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() {
	n := sint64()
	s := req(1, n)
	s2 := req(1, s)

	// highest num in left half
	if s == s2 {
		l := int64(1)
		r := s - 1
		for l < r {
			m := (l + r) / 2
			nxt := req(m, s) // query right side
			if nxt == s {
				l = m + 1
			} else {
				r = m - 1
			}
		}
		submit((l+r)/2 + 1)
	}

	// highest num in right half
	l := s + 1
	r := n
	for l < r {
		m := (l + r) / 2
		nxt := req(s, m) // query left side
		if nxt == s {
			r = m - 1
		} else {
			l = m + 1
		}
	}
	submit((l+r)/2 - 1)
}

func req(l, r int64) int64 {
	printf("? %d %d\n", l, r)
	writer.Flush()
	return sint64()
}

func submit(x int64) {
	printf("! %d\n", x)
	writer.Flush()
	os.Exit(0)
}

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() {
	n := sint64()
	s := req(1, n)
	s2 := req(1, s)

	// highest num in left half
	if s == s2 {
		l := int64(1)
		r := s - 1
		for l <= r {
			m := (l + r) / 2
			nxt := req(m, s) // query right side
			if l == r {
				if nxt == s {
					submit(l)
				} else {
					submit(l - 1)
				}
				return
			}
			if nxt == s {
				l = m + 1
			} else {
				r = m - 1
			}
		}
	}

	// highest num in right half
	l := s + 1
	r := n
	for l <= r {
		m := (l + r) / 2
		nxt := req(s, m) // query left side
		if l == r {
			if nxt == s {
				submit(r)
			} else {
				submit(r + 1)
			}
			return
		}
		if nxt == s {
			r = m - 1
		} else {
			l = m + 1
		}
	}
}

func req(l, r int64) int64 {
	printf("? %d %d\n", l, r)
	writer.Flush()
	return sint64()
}

func submit(x int64) {
	printf("! %d\n", x)
	writer.Flush()
}

package main

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

func main() {
	var n int
	in := bufio.NewReader(os.Stdin)
	_, _ = fmt.Fscan(in, &n)

	left := 1
	right := n

	for left < right {
		var mid int

		fmt.Printf("? %d %d\n", left, right)
		_, _ = fmt.Fscan(in, &mid)

		if left+1 == right {
			ans := left + right - mid
			fmt.Printf("! %d\n", ans)
			return
		}

		fmt.Printf("? %d %d\n", left, mid)
		var inside int
		_, _ = fmt.Fscan(in, &inside)

		if inside == mid {
			right = mid - 1
		} else {
			left = mid + 1
		}
	}

	fmt.Printf("! %d\n", left)
}


User ainta loves to play with cards. He has a cards containing letter "o" and b cards containing letter "x". He arranges the cards in a row, and calculates the score of the deck by the formula below.  At first, the score is 0.  For each block of contiguous "o"s with length x the score increases by x2.  For each block of contiguous "x"s with length y the score decreases by y2.   For example, if a = 6, b = 3 and ainta have arranged the cards in the order, that is described by string "ooxoooxxo", the score of the deck equals 22 - 12 + 32 - 22 + 12 = 9. That is because the deck has 5 blocks in total: "oo", "x", "ooo", "xx", "o".User ainta likes big numbers, so he wants to maximize the score with the given cards. Help ainta make the score as big as possible. Note, that he has to arrange all his cards.

The first line contains two space-separated integers a and b (0 ≤ a, b ≤ 105; a + b ≥ 1) — the number of "o" cards and the number of "x" cards.

In the first line print a single integer v — the maximum score that ainta can obtain. In the second line print a + b characters describing the deck. If the k-th card of the deck contains "o", the k-th character must be "o". If the k-th card of the deck contains "x", the k-th character must be "x". The number of "o" characters must be equal to a, and the number of "x " characters must be equal to b. If there are many ways to maximize v, print any. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

/*********************************************************************************
*     File Name           :     C.go
*     Created By          :     YIMMON, yimmon.zhuang@gmail.com
*     Creation Date       :     [2014-03-13 19:56]
*     Last Modified       :     [2014-03-13 20:21]
*     Description         :
**********************************************************************************/

package main

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

func main() {
    scanner.Split(bufio.ScanWords)

    var n, m int64 = scanner.NextInt(), scanner.NextInt()
    var ans int64 = -m * m
    if m == 0 {
        fmt.Println(n * n)
        for i := 0; i < int(n); i++ {
            fmt.Print("o")
        }
    } else if m == 1 {
        fmt.Println(n*n - 1)
        for i := 0; i < int(n); i++ {
            fmt.Print("o")
        }
        fmt.Print("x")
    } else {
        var tmp, f int64 = 1, m
        if f > n+1 {
            f = n + 1
        }
        for i := int64(2); i <= f; i++ {
            t := (m/i)*(m/i)*(i-m%i) + (m/i+1)*(m/i+1)*(m%i)
            t = (i - 2) + (n-i+2)*(n-i+2) - t
            if t > ans {
                ans, tmp = t, i
            }
        }
        fmt.Println(ans)
        r := m % tmp
        x := m / tmp
        for i := int64(1); i < tmp; i++ {
            for j := 0; j < int(x); j++ {
                fmt.Print("x")
            }
            if r > 0 {
                fmt.Print("x")
                r--
            }
            if i == 1 {
                for j := 0; j < int(n-tmp+2); j++ {
                    fmt.Print("o")
                }
            } else {
                fmt.Print("o")
            }
        }
        for j := 0; j < int(x); j++ {
            fmt.Print("x")
        }
    }
    fmt.Println()
}

type Scanner struct {
    *bufio.Scanner
}

var scanner = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
    if !self.Scan() {
        panic(self.Err())
    }
    return self.Text()
}

func (self *Scanner) NextInt() int64 {
    x, err := strconv.Atoi(self.Next())
    if err != nil {
        panic(err)
    }
    return int64(x)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var a, b int64
	fmt.Fscanf(reader, "%d %d", &a, &b)
	if a*b == 0 {
		fmt.Printf("%d\n", a*a-b*b)
		for i := int64(0); i < a; i++ {
			fmt.Printf("o")
		}
		for i := int64(0); i < b; i++ {
			fmt.Printf("x")
		}
		return
	}

	o := make([]int64, a+1)
	x := make([]int64, b+1)

	for i := int64(1); i <= a; i++ {
		o[i] = (a-i+1)*(a-i+1) + (i - 1)
	}
	for i := int64(1); i <= b; i++ {
		x[i] = (b/i+1)*(b/i+1)*(b%i) + (b/i)*(b/i)*(i-b%i)
	}
	ans := a*a - b*b
	var besta, bestb int64
	for i := int64(1); i <= a; i++ {
		if i <= b && ans <= o[i]-x[i] {
			ans = o[i] - x[i]
			besta, bestb = i, i
		}
		if i+1 <= b && ans <= o[i]-x[i+1] {
			ans = o[i] - x[i+1]
			besta, bestb = i, i+1
		}
	}
	fmt.Printf("%d\n", ans)
	aList := make([]int64, besta)
	bList := make([]int64, bestb)
	aList[0] = a - besta + 1
	for i := int64(1); i < besta; i++ {
		aList[i] = 1
	}
	for i := int64(0); i < (b % bestb); i++ {
		bList[i] = b/bestb + 1
	}
	for i := b % bestb; i < bestb; i++ {
		bList[i] = b / bestb
	}
	for i := int64(0); i < besta; i++ {
		for j := int64(0); j < bList[i]; j++ {
			fmt.Printf("x")
		}
		for j := int64(0); j < aList[i]; j++ {
			fmt.Printf("o")
		}
	}
	if bestb > besta {
		for j := int64(0); j < bList[bestb-1]; j++ {
			fmt.Printf("x")
		}
	}
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var a, b int64
	fmt.Fscanf(reader, "%d %d", &a, &b)
	if a == 0 {
		fmt.Printf("%d", -b*b)
		return
	} else if b == 0 {
		fmt.Printf("%d", a*a)
		return
	}

	o := make([]int64, a+1)
	x := make([]int64, b+1)

	for i := int64(1); i <= a; i++ {
		o[i] = (a-i+1)*(a-i+1) + (i - 1)
	}
	for i := int64(1); i <= b; i++ {
		x[i] = (b/i+1)*(b/i+1)*(b%i) + (b/i)*(b/i)*(i-b%i)
	}
	ans := a*a - b*b
	for i := int64(1); i <= a; i++ {
		if i <= b && ans < o[i]-x[i] {
			ans = o[i] - x[i]
		}
		if i+1 <= b && ans < o[i]-x[i+1] {
			ans = o[i] - x[i+1]
		}
	}
	fmt.Printf("%d", ans)
}


Given an integer $$$n$$$, find the maximum value of integer $$$k$$$ such that the following condition holds:  $$$n$$$ & ($$$n-1$$$) & ($$$n-2$$$) & ($$$n-3$$$) & ... ($$$k$$$) = $$$0$$$  where & denotes the bitwise AND operation.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 3 \cdot 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$).

For each test case, output a single integer — the required integer $$$k$$$.

NoteIn the first testcase, the maximum value for which the continuous & operation gives 0 value, is 1.In the second testcase, the maximum value for which the continuous & operation gives 0 value, is 3. No value greater then 3, say for example 4, will give the & sum 0.   $$$5 \, \& \, 4 \neq 0$$$,  $$$5 \, \& \, 4 \, \& \, 3 = 0$$$. Hence, 3 is the answer.

package main

import (
	"fmt"
)

func bLen(x int64) int {
	l := 1
	for x >= 1024 {
		l <<= 10
		x /= 1024
	}
	for x >= 4 {
		l <<= 2
		x /= 4
	}
	for x != 0 {
		l <<= 1
		x /= 2
	}
	l >>= 1
	l -= 1
	return l
}

func main() {
	n := readInt()
	for i := int64(0); i < n; i++ {
		num := readInt()

		fmt.Println(bLen(num))
	}
}

func readInt() int64 {
	var n int64
	fmt.Scan(&n)
	return n
}

package main
import(
    "fmt"
    "bufio"
    "os"
    //"math"
)
func main() {
	var t int
	var in = bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var x int
		fmt.Fscan(in,&x)
		tmp:=1
		for j:=1;j<=x;j++ {
			tmp*=2
			if tmp > x {
				fmt.Println((tmp/2)-1)
				break
			}
		}
	}
}

package main

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

var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
var mu sync.Mutex

func init()        { in.Split(bufio.ScanWords) }
func gets() string { in.Scan(); return in.Text() }
func geti() int    { i, _ := strconv.ParseInt(gets(), 10, 64); return int(i) }

func putln(a ...interface{})          { mu.Lock(); defer mu.Unlock(); fmt.Fprintln(out, a...) }
func putf(f string, a ...interface{}) { mu.Lock(); defer mu.Unlock(); fmt.Fprintf(out, f, a...) }

func flush()   { mu.Lock(); defer mu.Unlock(); out.Flush() }
func flusher() { time.Sleep(10 * time.Millisecond); flush(); go flusher() }
func main()    { go flusher(); defer flush(); main1() }

func main1() {
	// var x int64
	// var y int
	// if unsafe.Sizeof(x) != unsafe.Sizeof(y) {
	// 	panic("bad")
	// }
	for tc := geti(); tc > 0; tc-- {
		n := geti()
		a := 1
		for 2*a <= n {
			a *= 2
		}
		putln(a - 1)
	}
}

package main

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

func main() {
	var n int64
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n = toInt(scanner.Bytes())
	for i := int64(0); i < n; i++ {
		var k int64
		scanner.Scan()
		k = toInt(scanner.Bytes())
		var wow int64 = int64(math.Log2(float64(k)))
		fmt.Println(int64(math.Pow(2, float64(wow)) - 1))
	}
}

func toInt(buf []byte) (n int64) {
	for _, v := range buf {
		n = n*10 + int64(v-'0')
	}
	return
}

package main

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

/*
And then there were K

Given an integer n, find the maximum value of integer k
such that the following condition holds:
n & (n−1) & (n−2) & (n−3) & ... (k) = 0
where & denotes the bitwise AND operation.

Input

The first line contains a single integer t: (1≤t≤3⋅104).
Then t test cases follow.

The first line of each test case contains a single integer n: (1≤n≤109).

Output
For each test case, output a single integer, the required integer k.
*/
func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    var number_test_case int
    fmt.Sscan(scanner.Text(), &number_test_case)
    for i:=0; i < number_test_case; i++ {
        var n uint64
        scanner.Scan()
        fmt.Sscan(scanner.Text(), &n)
        fmt.Println(findK(n))
    }
}

// The value of k is the 2^(b-1) - 1
// With b the highest bit of n
func findK(n uint64) int{
    b := bits.Len64(n)
    return (1 << (b-1)) - 1
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    var number_test_case int
    fmt.Sscan(scanner.Text(), &number_test_case)
    for i:=0; i < number_test_case; i++ {
        var k, n int
        scanner.Scan()
        fmt.Sscan(scanner.Text(), &n)
        for (1 << k) <= n {
            k ++
        }
        fmt.Println((1 << (k - 1)) - 1)
    }
}

package main

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

type CF1527A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1527A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1527A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1527A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1527A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1527A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1527A
Date: 16/09/21
User: pepradere
URL: https://codeforces.com/problemset/problem/1527/A
Problem: CF1527A
**/
func (in *CF1527A) Run() {
	t := in.NextInt()
	for ; t > 0; t-- {
		n := in.NextInt64()
		count := int64(1)
		for n > 1 {
			count++
			n /= 2
		}

		fmt.Println(1<<(count-1) - 1)
	}
}

func NewCF1527A(r *bufio.Reader) *CF1527A {
	return &CF1527A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1527A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)

	for test := parseInt(getString(reader)); test > 0; test-- {
		n := parseInt(getString(reader))
		writer.WriteString(fmt.Sprintln(int(math.Pow(2, math.Floor(math.Log2(float64(n)))) - 1)))
	}
	writer.Flush()
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

func parseInt(str string) int {
	var res int
	size := len(str)
	for i := 0; i < size; i++ {
		digit := int(str[i] - '0')
		for j := (i + 1); j < size; j++ {
			digit = digit<<3 + digit<<1
		}
		res += digit
	}
	return res
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func solve(n int) int {
	l := 1
	for l<<1 <= n {
		l <<= 1
	}
	return l - 1
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		fmt.Println(solve(n))
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func solve(n int) int {
	l := 1
	for 2*l <= n {
		l *= 2
	}
	return l - 1
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		fmt.Println(solve(n))
	}
}

package main

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

func main() {
	var tcs int
	in := bufio.NewReader(os.Stdin)
	_, _ = fmt.Fscan(in, &tcs)

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

		i := 1
		for 2*i <= n {
			i *= 2
		}

		fmt.Println(i - 1)
	}
}

package main

import (
    "bufio"
    "fmt"
    "os"
)
//type int int64
var scan = fmt.Fscan
var println = fmt.Fprintln

var in *bufio.Reader
var out *bufio.Writer
func init(){
    in = bufio.NewReader(os.Stdin)
    out = bufio.NewWriter(os.Stdout)
}

func main(){
	defer out.Flush()
	var t int; scan(in,&t)
	for ;t>0;t-- {
		var a int; scan(in,&a)
		ans := 0
		for a>0{
			a>>=1
			ans++
		}
		ans--
		println(out,(1<<ans)-1)
	}
}

package main
import (
    "os"
    "fmt"
    "bufio"
)
var rd = bufio.NewReader(os.Stdin)
func main() {
    q := 0
    fmt.Fscan(rd, &q)
    for t := 0; t < q; t++ {
        n := 0
        fmt.Fscan(rd, &n)
        var res uint = 0
        for n > 0 {
            res++
            n /= 2
        }
        fmt.Println((1 << (res - 1)) - 1)
    }
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	var l, q int64
	l = MAX(x, y)
	q = MIN(x, y)
	var m int64
	for l*q > 0 {
		a := l % q
		b := l - a
		cc := int64(b / q)
		remain := l - cc*q
		l = MAX(remain, q)
		q = MIN(remain, q)
		if l*q == 0 {
			m = l + q
			break
		}

	}
	return m

}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n int
		fmt.Fscan(input, &n)
		begin := n
		count := 0
		ansss := 1
		for i := 0; i > -1; i++ {
			begin = int(begin / 2)
			if begin >= 1 {
				count++
				ansss = ansss * 2
			} else {
				break
			}

		}
		fmt.Println(ansss - 1)
	}

}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"math/bits"
	"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 uint
		Fscan(in, &n)
		Fprintln(out, 1<<(bits.Len(n)-1)-1)
	}

	T := 1
	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) }

package main

import (
	"fmt"
)

var cache = map[int64]int64{
	1:  0,
	2:  1,
	5:  3,
	17: 15,
}

func binLen(n int64) int {
	x := 0
	for n != 0 {
		n /= 2
		x++
	}
	return x
}
func solve() {
	n := readInt()
	if v, ok := cache[n]; ok {
		fmt.Println(v)
		return
	}
	x := int64(1<<binLen(n) - 1)
	cache[n] = x
	fmt.Println(x)
}

func main() {
	n := readInt()
	for i := int64(0); i < n; i++ {
		solve()
	}
}

func readInt() int64 {
	var n int64
	fmt.Scan(&n)
	return n
}

package main

import (
	"fmt"
)

var cache = map[int64]int64{
	1:  0,
	2:  1,
	5:  3,
	17: 15,
}

func toBinary(n int64) []bool {
	bytz := []bool{}
	for n != 0 {
		bytz = append(bytz, n%2 == 1)
		n /= 2
	}
	return bytz
}

func toNumNeg(bytz []bool) int64 {
	x := int64(0)
	for i := len(bytz) - 1; i >= 0; i-- {
		x *= 2
		if !bytz[i] {
			x += 1
		}
	}
	return x
}
func solve() {
	n := readInt()
	if v, ok := cache[n]; ok {
		fmt.Println(v)
		return
	}
	x := toNumNeg(toBinary(n))
	cache[n] = x
	fmt.Println(x)
}

func main() {
	n := readInt()
	for i := int64(0); i < n; i++ {
		solve()
	}
}

func readInt() int64 {
	var n int64
	fmt.Scan(&n)
	return n
}

package main

import (
	"fmt"
)

var cache = map[int64]int64{
	2:  1,
	5:  3,
	17: 15,
}

func toBinary(n int64) []bool {
	bytz := []bool{}
	for n != 0 {
		bytz = append(bytz, n%2 == 1)
		n /= 2
	}
	return bytz
}

func toNumNeg(bytz []bool) int64 {
	x := int64(0)
	for i := len(bytz) - 1; i >= 0; i-- {
		x *= 2
		if !bytz[i] {
			x += 1
		}
	}
	return x
}
func solve() {
	n := readInt()
	if v, ok := cache[n]; ok {
		fmt.Println(v)
		return
	}
	x := toNumNeg(toBinary(n))
	if x == 0 {
		x = 1
	}
	cache[n] = x
	fmt.Println(x)
}

func main() {
	n := readInt()
	for i := int64(0); i < n; i++ {
		solve()
	}
}

func readInt() int64 {
	var n int64
	fmt.Scan(&n)
	return n
}

package main

import (
	"fmt"
)

var cache = map[int64]int64{
	2:  1,
	5:  3,
	17: 15,
}

func toBinary(n int64) []bool {
	bytz := []bool{}
	for n != 0 {
		bytz = append(bytz, n%2 == 1)
		n /= 2
	}
	return bytz
}

func toNumNeg(bytz []bool) int64 {
	x := int64(0)
	for i := len(bytz) - 1; i >= 0; i-- {
		x *= 2
		if !bytz[i] {
			x += 1
		}
	}
	return x
}
func solve() {
	n := readInt()
	if v, ok := cache[n]; ok {
		fmt.Println(v)
		return
	}
	x := toNumNeg(toBinary(n))
	cache[n] = x
	fmt.Println(x)
}

func main() {
	n := readInt()
	for i := int64(0); i < n; i++ {
		solve()
	}
}

func readInt() int64 {
	var n int64
	fmt.Scan(&n)
	return n
}

package main
import(
    "fmt"
    "bufio"
    "os"
    //"math"
)
func main() {
	var t int
	var in = bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var x int
		fmt.Fscan(in,&x)
		for j:=x-1;j>0;j--{
			if j & (j-1) == 0 {
				fmt.Println(j-1)
				break
			}
		}
	}
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "math"
)
func main() {
	var t int
	var in = bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var x int
		fmt.Fscan(in,&x)
		if x < 4 {
			fmt.Println(1)
		} else if x < 8 {
			fmt.Println(3)
		} else {
			fmt.Println(int(math.Sqrt(float64(x)))*4-1)
		}
	}
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "math"
)
func main() {
	var t int
	var in = bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var x int
		fmt.Fscan(in,&x)
		if x < 4 {
			fmt.Println(1)
		} else if x < 8 {
			fmt.Println(3)
		} else {
			fmt.Println((int(math.Sqrt(float64(x)))-1)*4-1)
		}
	}
}

package main
import(
    "fmt"
)
func main() {
	var t int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		var x int
		fmt.Scan(&x)
		if x <= 2 {
			fmt.Println(1)
		} else {
			fmt.Println(x-2)
		}
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scanln(&n)
	for i := 0; i < n; i++ {
		var k int64
		fmt.Scanln(&k)
		var wow int64 = int64(math.Log2(float64(k)))
		fmt.Println(math.Pow(2, float64(wow)) - 1)
	}
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
    var number_test_case int
    fmt.Sscan(scanner.Text(), &number_test_case)
    for i:=0; i < number_test_case; i++ {
        var l int // lenght of the string
        var s string
        scanner.Scan()
        fmt.Sscan(scanner.Text(), &l)
        scanner.Scan()
        s = scanner.Text()
        count0 := 0
        for _,c := range s{
            if c =='0' {
                count0 ++
            }
        }
        if count0 > 1 && count0 % 2 == 1 {
            fmt.Println("ALICE")
        } else {
            fmt.Println("BOB")
        }
    }
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
    var number_test_case int
    fmt.Sscan(scanner.Text(), &number_test_case)
    for i:=0; i < number_test_case; i++ {
        var number uint64
        scanner.Scan()
        fmt.Sscan(scanner.Text(), &number)
        fmt.Println(number - uint64(bits.OnesCount64(number)))
    }
}

package main

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

type CF1527A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1527A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1527A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1527A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1527A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1527A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1527A
Date: 16/09/21
User: pepradere
URL: https://codeforces.com/problemset/problem/1527/A
Problem: CF1527A
**/
func (in *CF1527A) Run() {
	t := in.NextInt()
	for ; t > 0; t-- {
		n := in.NextInt64()
		aux := n
		count := int64(1)
		if n == 1 {
			count = 0
		}
		for n > 1 {
			if n%2 != 0 {
				count++
			}
			n /= 2
		}

		fmt.Println(aux - count)
	}
}

func NewCF1527A(r *bufio.Reader) *CF1527A {
	return &CF1527A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1527A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1527A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1527A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1527A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1527A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1527A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1527A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1527A
Date: 16/09/21
User: pepradere
URL: https://codeforces.com/problemset/problem/1527/A
Problem: CF1527A
**/
func (in *CF1527A) Run() {
	t := in.NextInt()
	for ; t > 0; t-- {
		n := in.NextInt64()
		aux := n
		count := int64(1)
		for n > 1 {
			if n%2 != 0 {
				count++
			}
			n /= 2
		}

		fmt.Println(aux - count)
	}
}

func NewCF1527A(r *bufio.Reader) *CF1527A {
	return &CF1527A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1527A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)

	for test := parseInt(getString(reader)); test > 0; test-- {
		n := parseInt(getString(reader))
		writer.WriteString(fmt.Sprintln(math.Pow(2, math.Floor(math.Log2(float64(n)))) - 1))
	}
	writer.Flush()
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

func parseInt(str string) int {
	var res int
	size := len(str)
	for i := 0; i < size; i++ {
		digit := int(str[i] - '0')
		for j := (i + 1); j < size; j++ {
			digit = digit<<3 + digit<<1
		}
		res += digit
	}
	return res
}


The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an n × m rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.

The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 100) — the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters — the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements.

Print two integers — the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.

package main

import "fmt"

type coords struct {
    row, col int
}

func main() {
	var rows, cols int
	_, err := fmt.Scanf("%d %d\n", &rows, &cols)
	if err != nil {
		panic(err)
	}

	var crime_scenes []coords
	cur_row := 1
	for cur_row <= rows {
		var cur_line string
		_, err := fmt.Scanln(&cur_line)
		if err != nil {
			panic(err)
		}

		cur_col := 1
		for cur_col <= cols {
			if (cur_line[cur_col-1] == '*') {
				crime_scenes = append(crime_scenes, coords{cur_row, cur_col})
			}

			cur_col += 1
		}

		cur_row += 1
	}

	var result coords
	if crime_scenes[0].row == crime_scenes[1].row {
		if crime_scenes[0].col == crime_scenes[2].col {
			result = coords{crime_scenes[2].row, crime_scenes[1].col}
		} else {
			result = coords{crime_scenes[2].row, crime_scenes[0].col}
		}
	} else {
		if crime_scenes[0].col == crime_scenes[2].col {
			result = coords{crime_scenes[0].row, crime_scenes[1].col}
		} else {
			if crime_scenes[1].row == crime_scenes[2].row {
				result = coords{crime_scenes[0].row, crime_scenes[2].col}
			} else {
				result = coords{crime_scenes[1].row, crime_scenes[2].col}
			}
		}
	}

	fmt.Printf("%d %d", result.row, result.col)

}


One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length    The picture corresponds to the first example

The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109,  - 109 ≤ y1 < y2 ≤ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.

Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≤ ci ≤ 4) — the color of i-th rectangle.

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

type Int int64

func main() {
	defer w.Flush()
	var n Int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n Int) {
	in := make([][4]Int, n)
	for i := Int(0); i < n; i++ {
		I(&in[i][0], &in[i][1], &in[i][2], &in[i][3])
	}
	O("YES\n")
	for i := Int(0); i < n; i++ {
		O(2*(abs(in[i][0]%2))+abs(in[i][1]%2)+1, "\n")
	}
}

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


Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.

The first line contains two integers n and m — the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b — the numbers of students tied by the i-th lace (1 ≤ a, b ≤ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.

Print the single number — the number of groups of students that will be kicked out from the club.

NoteIn the first sample Anna and Maria won't kick out any group of students — in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then — two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.

package main
import "fmt"

func main() {
	var nn, mm, aa, bb, k, l int
	var x[110][110]int
	var cnt[110]int
	var s[110]int
	var notice bool

	fmt.Scan(&nn, &mm)
	for i:=1; i<=nn; i++ {
		cnt[1]=0
		for j:=1; j<=nn; j++ {
			x[i][j]=0
		}
	}

	for i:=1; i<=mm; i++ {
		fmt.Scan(&aa, &bb)
		x[aa][bb]=1
		x[bb][aa]=1
		cnt[aa]+=1
		cnt[bb]+=1
	}
	k=0
	l=0
	for {
		k=0
		for i:=1; i<=nn; i++ {
			if cnt[i]==1 {
				s[k]=i
				k++
			}
		}
		if k==0 {
			break
		}
		for i:=1; i<=nn; i++ {
			notice=false
			for j:=0; j<k; j++ {
				if s[j]==i {
					notice=true
					break
				}
			}
			if notice==true {
				for j:=1; j<=nn; j++ {
					if x[i][j]==1 {
						x[i][j]=0
						x[j][i]=0
						cnt[i]--
						cnt[j]--
					}
				}
			}
		}
		l++
	}
	fmt.Println(l)
}