This is an interactive problem.There is an unknown integer $$$x$$$ ($$$1\le x\le n$$$). You want to find $$$x$$$.At first, you have a set of integers $$$\{1, 2, \ldots, n\}$$$. You can perform the following operations no more than $$$10000$$$ times:  A $$$a$$$: find how many numbers are multiples of $$$a$$$ in the current set.  B $$$a$$$: find how many numbers are multiples of $$$a$$$ in this set, and then delete all multiples of $$$a$$$, but $$$x$$$ will never be deleted (even if it is a multiple of $$$a$$$). In this operation, $$$a$$$ must be greater than $$$1$$$.  C $$$a$$$: it means that you know that $$$x=a$$$. This operation can be only performed once. Remember that in the operation of type B $$$a>1$$$ must hold.Write a program, that will find the value of $$$x$$$.

The first line contains one integer $$$n$$$ ($$$1\le n\le 10^5$$$). The remaining parts of the input will be given throughout the interaction process.

NoteNote that to make the sample more clear, we added extra empty lines. You shouldn't print any extra empty lines during the interaction process.In the first test $$$n=10$$$ and $$$x=4$$$.Initially the set is: $$$\{1,2,3,4,5,6,7,8,9,10\}$$$.In the first operation, you ask how many numbers are multiples of $$$4$$$ and delete them. The answer is $$$2$$$ because there are two numbers divisible by $$$4$$$: $$$\{4,8\}$$$. $$$8$$$ will be deleted but $$$4$$$ won't, because the number $$$x$$$ will never be deleted. Now the set is $$$\{1,2,3,4,5,6,7,9,10\}$$$.In the second operation, you ask how many numbers are multiples of $$$2$$$. The answer is $$$4$$$ because there are four numbers divisible by $$$2$$$: $$$\{2,4,6,10\}$$$.In the third operation, you ask how many numbers are multiples of $$$8$$$. The answer is $$$0$$$ because there isn't any number divisible by $$$8$$$ in the current set.In the fourth operation, you know that $$$x=4$$$, which is the right answer.

package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)

	comp := make([]bool, n+1)
	var pr []int
	for i := 2; i <= n; i++ {
		if !comp[i] {
			pr = append(pr, i)
			if i <= n/i {
				for j := i * i; j <= n; j += i {
					comp[j] = true
				}
			}
		}
	}

	set := make(map[int]bool)
	for i := 1; i <= n; i++ {
		set[i] = true
	}

	ask := func(s string, x int) int {
		fmt.Println(s, x)
		var res int
		fmt.Scan(&res)
		return res
	}

	ans := 1

	calcPow := func(p int) {
		ans *= p
		if p <= n/p {
			for q := p * p; q <= n; q *= p {
				if ask("A", q) == 1 {
					ans *= p
				}
			}
		}
	}

	rt := int(math.Sqrt(float64(len(pr))))
	gotFirst := false
	for i, p := range pr {
		cnt := len(set)
		for q := p; q <= n; q += p {
			delete(set, q)
		}
		cnt -= len(set)
		res := ask("B", p)
		if res != cnt {
			calcPow(p)
		}
		if !gotFirst && (i%rt == rt-1 || i == len(pr)-1) && ask("A", 1) != len(set) {
			gotFirst = true
			for j := i - rt + 1; j <= i; j++ {
				if ask("A", pr[j]) == 1 {
					calcPow(pr[j])
					break
				}
			}
		}
	}

	fmt.Println("C", ans)
}

package main

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

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

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

func printfl(f string, a...interface{}) {
	printf(f, a...)
	writer.Flush()
}

func primes(N int) []int {
	k := make([]bool, N + 1)

	checkN := int(math.Sqrt(float64(N))) + 1
	numberPrimaryNumbers := 0
	for i := 2; i <= checkN && i <= N; i ++ {
		if k[i] == false {
			numberPrimaryNumbers += 1
			for j := 2*i; j <= N; j += i {
				k[j] = true
			}
		}
	}

	primaryNumbers := make([]int, 0, numberPrimaryNumbers)

	for i := 2; i <= N; i++ {
		if k[i] == false {
			primaryNumbers = append(primaryNumbers, i)
		}
	}
	return primaryNumbers
}

func main() {
	defer writer.Flush()
	var N, tmp, tmp1 int
	scanf("%d\n", &N)

	primaryNumbers := primes(N)

	var primaryFactors []int
	l := len(primaryNumbers)
	sqrtL := int(math.Sqrt(float64(l)))
	println(sqrtL)
	shouldBeDeleted := 0
	lastL := l
	for i := 0; i < l; i += sqrtL {
		shouldBeDeleted = 0
		for j := i; j < l && j < i + sqrtL; j ++ {
			printfl("B %d\n", primaryNumbers[j])
			scanf("%d\n", &tmp)
			shouldBeDeleted += tmp
		}
		printfl("A 1\n")
		scanf("%d\n", &tmp1)

		if tmp1 != lastL - shouldBeDeleted {
			for j := i; j < l && j < i + sqrtL; j ++ {
				printfl("A %d\n", primaryNumbers[j])
				scanf("%d\n", &tmp)
				if tmp == 1 {
					primaryFactors = append(primaryFactors, primaryNumbers[j])
				}
			}
		}
		lastL = tmp1
	}

    // need to find degrees
    result := 1
    for i := 0; i < len(primaryFactors); i ++ {
    	result *= primaryFactors[i]
	}

	for i := 0; i < len(primaryFactors); {
		if N / result >= primaryFactors[i] {
			printfl("A %d\n", result*primaryFactors[i])
			scanf("%d\n", &tmp)
			if tmp == 1 {
				result *= primaryFactors[i]
			} else {
				i ++
			}
		} else {
			i ++
		}

	}

	printfl("C %d\n", result)
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func primes(n int) []int {
	sv := make([]int, n+1)
	var p []int
	for i := 2; i <= n; i++ {
		if sv[i] == 0 {
			for j := i + i; j <= n; j += i {
				sv[j] = 1
			}
			p = append(p, i)
		}
	}
	return p
}

const kB = 70

func main() {
	defer rw.Flush()
	n := rw.NextInt()
	p := primes(n)
	has := n
	ans := 1
	for i := 0; i < len(p); i += kB {
		for j := i; j < len(p) && j < i+kB; j++ {
			rw.Println("B", p[j])
			rw.Flush()
			cnt := rw.NextInt()
			has -= cnt
		}
		rw.Println("A", 1)
		rw.Flush()
		now := rw.NextInt()
		if has < now {
			has = now
			for j := i; j < len(p) && j < i+kB; j++ {
				rw.Println("A", p[j])
				rw.Flush()
				cnt := rw.NextInt()
				x := p[j]
				for cnt > 0 {
					ans *= p[j]
					if int64(x)*int64(p[j]) > int64(n) {
						break
					}
					x *= p[j]
					rw.Println("A", x)
					rw.Flush()
					cnt = rw.NextInt()
				}
			}
		}
	}
	rw.Println("C", ans)
	rw.Flush()
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

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

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

func printfl(f string, a...interface{}) {
	printf(f, a...)
	writer.Flush()
}

func primes(N int) []int {
	k := make([]bool, N + 1)

	checkN := int(math.Sqrt(float64(N))) + 1
	numberPrimaryNumbers := 0
	for i := 2; i <= checkN && i <= N; i ++ {
		if k[i] == false {
			numberPrimaryNumbers += 1
			for j := 2*i; j <= N; j += i {
				k[j] = true
			}
		}
	}

	primaryNumbers := make([]int, 0, numberPrimaryNumbers)

	for i := 2; i <= N; i++ {
		if k[i] == false {
			primaryNumbers = append(primaryNumbers, i)
		}
	}
	return primaryNumbers
}

func main() {
	defer writer.Flush()
	var N, tmp, tmp1 int
	scanf("%d\n", &N)

	primaryNumbers := primes(N)

	var primaryFactors []int
	l := len(primaryNumbers)
	sqrtL := int(math.Sqrt(float64(l)))
	println(sqrtL)
	shouldBeDeleted := 0
	lastL := l
	for i := 0; i < l; i += sqrtL {
		shouldBeDeleted = 0
		for j := i; j < l && j < i + sqrtL; j ++ {
			printfl("B %d\n", primaryNumbers[j])
			scanf("%d\n", &tmp)
			shouldBeDeleted += tmp
		}
		printfl("A 1\n")
		scanf("%d\n", &tmp1)

		if tmp1 != lastL - shouldBeDeleted {
			for j := i; j < l && j < i + sqrtL; j ++ {
				printfl("A %d\n", primaryNumbers[j])
				scanf("%d\n", &tmp)
				if tmp == 1 {
					primaryFactors = append(primaryFactors, primaryNumbers[j])
				}
			}
		}
		lastL = tmp1
	}

    // need to find degrees
    result := 1
    for i := 0; i < len(primaryFactors); i ++ {
    	result *= primaryFactors[i]
	}

	for i := 0; i < len(primaryNumbers); {
		if N / result >= primaryNumbers[i] {
			printfl("A %d\n", result*primaryNumbers[i])
			scanf("%d\n", &tmp)
			if tmp == 1 {
				result *= primaryNumbers[i]
			} else {
				i ++
			}
		} else {
			i ++
		}

	}

	printfl("C %d\n", result)
}

package main

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

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

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

func printfl(f string, a...interface{}) {
	printf(f, a...)
	writer.Flush()
}

func primes(N int) []int {
	k := make([]bool, N + 1)

	checkN := int(math.Sqrt(float64(N))) + 1
	numberPrimaryNumbers := 0
	for i := 2; i <= checkN && i <= N; i ++ {
		if k[i] == false {
			numberPrimaryNumbers += 1
			for j := 2*i; j <= N; j += i {
				k[j] = true
			}
		}
	}

	primaryNumbers := make([]int, 0, numberPrimaryNumbers)

	for i := 2; i <= N; i++ {
		if k[i] == false {
			primaryNumbers = append(primaryNumbers, i)
		}
	}
	return primaryNumbers
}

func main() {
	defer writer.Flush()
	var N, tmp, tmp1 int
	scanf("%d\n", &N)

	primaryNumbers := primes(N)

	var primaryFactors []int
	l := len(primaryNumbers)
	sqrtL := int(math.Sqrt(float64(l)))
	println(sqrtL)
	shouldBeDeleted := 0
	lastL := l
	for i := 0; i < l; i += sqrtL {
		shouldBeDeleted = 0
		for j := i; j < l && j < i + sqrtL; j ++ {
			printfl("B %d\n", primaryNumbers[j])
			scanf("%d\n", &tmp)
			shouldBeDeleted += tmp
		}
		printfl("A 1\n")
		scanf("%d\n", &tmp1)

		if tmp1 != lastL - shouldBeDeleted {
			for j := i; j < l && j < i + sqrtL; j ++ {
				printfl("A %d\n", primaryNumbers[j])
				scanf("%d\n", &tmp)
				if tmp == 1 {
					primaryFactors = append(primaryFactors, primaryNumbers[j])
				}
			}
		}
		lastL = tmp1
	}

    // need to find degrees
    result := 1
    for i := 0; i < len(primaryFactors); i ++ {
    	result *= primaryFactors[i]
	}

	for i := 0; i < len(primaryNumbers); {
		if result * primaryNumbers[i] < N {
			printfl("A %d\n", result*primaryNumbers[i])
			scanf("%d\n", &tmp)
			if tmp == 1 {
				result *= primaryNumbers[i]
			} else {
				i ++
			}
		} else {
			i ++
		}

	}

	printfl("C %d\n", result)
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func primes(n int) []int {
	sv := make([]int, n+1)
	var p []int
	for i := 2; i <= n; i++ {
		if sv[i] == 0 {
			for j := i + i; j <= n; j += i {
				sv[j] = 1
			}
			p = append(p, i)
		}
	}
	return p
}

const kB = 70

func main() {
	defer rw.Flush()
	n := rw.NextInt()
	p := primes(n)
	has := n
	ans := 1
	for i := 0; i < len(p); i += kB {
		for j := i; j < len(p) && j < i+kB; j++ {
			rw.Println("B", p[j])
			rw.Flush()
			cnt := rw.NextInt()
			has -= cnt
		}
		rw.Println("A", 1)
		rw.Flush()
		cnt := rw.NextInt()
		if has < cnt {
			has = cnt
			for j := i; j < len(p) && j < i+kB; j++ {
				rw.Println("A", p[j])
				rw.Flush()
				cnt := rw.NextInt()
				x := p[j]
				for cnt > 0 && int64(x*p[j]) <= int64(n) {
					x *= p[j]
					ans *= p[j]
					rw.Println("A", x)
					rw.Flush()
					cnt = rw.NextInt()
				}
			}
		}
	}
	rw.Println("C", ans)
	rw.Flush()
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func primes(n int) []int {
	sv := make([]int, n+1)
	var p []int
	for i := 2; i <= n; i++ {
		if sv[i] == 0 {
			for j := i + i; j <= n; j += i {
				sv[j] = 1
			}
			p = append(p, i)
		}
	}
	return p
}

const kB = 70

func main() {
	defer rw.Flush()
	n := rw.NextInt()
	p := primes(n)
	has := n
	ans := 1
	for i := 0; i < len(p); i += kB {
		for j := i; j < len(p) && j < i+kB; j++ {
			rw.Println('B', p[j])
			rw.Flush()
			cnt := rw.NextInt()
			has -= cnt
		}
		rw.Println('A', 1)
		rw.Flush()
		cnt := rw.NextInt()
		if has < cnt {
			has = cnt
			for j := i; j < len(p) && j < i+kB; j++ {
				rw.Println('B', p[j])
				rw.Flush()
				cnt := rw.NextInt()
				x := p[j]
				for cnt > 0 && int64(x*p[j]) <= int64(n) {
					x *= p[j]
					ans *= p[j]
					rw.Println('B', x)
					rw.Flush()
					cnt = rw.NextInt()
				}
			}
		}
	}
	rw.Println('C', ans)
	rw.Flush()
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func primes(n int) []int {
	sv := make([]int, n+1)
	var p []int
	for i := 2; i <= n; i++ {
		if sv[i] == 0 {
			for j := i + i; j <= n; j += i {
				sv[j] = 1
			}
			p = append(p, i)
		}
	}
	return p
}

const kB = 70

func main() {
	defer rw.Flush()
	n := rw.NextInt()
	p := primes(n)
	has := n
	ans := 1
	for i := 0; i < len(p); i += kB {
		for j := i; j < len(p) && j < i+kB; j++ {
			rw.Println("B", p[j])
			rw.Flush()
			cnt := rw.NextInt()
			has -= cnt
		}
		rw.Println("A", 1)
		rw.Flush()
		cnt := rw.NextInt()
		if has < cnt {
			has = cnt
			for j := i; j < len(p) && j < i+kB; j++ {
				rw.Println("A", p[j])
				rw.Flush()
				cnt := rw.NextInt()
				x := p[j]
				for cnt > 0 {
					x *= p[j]
					ans *= p[j]
					if int64(x*p[j]) > int64(n) {
						break
					}
					rw.Println("A", x)
					rw.Flush()
					cnt = rw.NextInt()
				}
			}
		}
	}
	rw.Println("C", ans)
	rw.Flush()
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func primes(n int) []int {
	sv := make([]int, n+1)
	var p []int
	for i := 2; i <= n; i++ {
		if sv[i] == 0 {
			for j := i + i; j <= n; j += i {
				sv[j] = 1
			}
			p = append(p, i)
		}
	}
	return p
}

const kB = 70

func main() {
	defer rw.Flush()
	n := rw.NextInt()
	p := primes(n)
	has := n
	ans := 1
	for i := 0; i < len(p); i += kB {
		for j := i; j < len(p) && j < i+kB; j++ {
			rw.Println("B", p[j])
			rw.Flush()
			cnt := rw.NextInt()
			has -= cnt
		}
		rw.Println("A", 1)
		rw.Flush()
		cnt := rw.NextInt()
		if has < cnt {
			has = cnt
			for j := i; j < len(p) && j < i+kB; j++ {
				rw.Println("A", p[j])
				rw.Flush()
				cnt := rw.NextInt()
				x := p[j]
				for cnt > 0 {
					ans *= p[j]
					if int64(x*p[j]) > int64(n) {
						break
					}
					x *= p[j]
					rw.Println("A", x)
					rw.Flush()
					cnt = rw.NextInt()
				}
			}
		}
	}
	rw.Println("C", ans)
	rw.Flush()
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func primes(n int) []int {
	sv := make([]int, n+1)
	var p []int
	for i := 2; i <= n; i++ {
		if sv[i] == 0 {
			for j := i + i; j <= n; j += i {
				sv[j] = 1
			}
			p = append(p, i)
		}
	}
	return p
}

const kB = 70

func main() {
	defer rw.Flush()
	n := rw.NextInt()
	p := primes(n)
	has := n
	ans := 1
	for i := 0; i < len(p); i += kB {
		for j := i; j < len(p) && j < i+kB; j++ {
			rw.Println("B", p[j])
			rw.Flush()
			cnt := rw.NextInt()
			has -= cnt
		}
		rw.Println("A", 1)
		rw.Flush()
		cnt := rw.NextInt()
		if has < cnt {
			has = cnt
			for j := i; j < len(p) && j < i+kB; j++ {
				rw.Println("A", p[j])
				rw.Flush()
				cnt := rw.NextInt()
				x := p[j]
				for cnt > 0 && int64(x*p[j]) <= int64(n) {
					x *= p[j]
					ans *= p[j]
					rw.Println("A", x)
					rw.Flush()
					cnt = rw.NextInt()
				}
			}
		}
	}
	rw.Println('C', ans)
	rw.Flush()
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}


Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcase right behind her bed. Alina thinks that the bookcase is a good choice for a persistent data structure. Initially the bookcase is empty, thus there is no book at any position at any shelf.The bookcase consists of n shelves, and each shelf has exactly m positions for books at it. Alina enumerates shelves by integers from 1 to n and positions at shelves — from 1 to m. Initially the bookcase is empty, thus there is no book at any position at any shelf in it.Alina wrote down q operations, which will be consecutively applied to the bookcase. Each of the operations has one of four types: 1 i j — Place a book at position j at shelf i if there is no book at it. 2 i j — Remove the book from position j at shelf i if there is a book at it. 3 i — Invert book placing at shelf i. This means that from every position at shelf i which has a book at it, the book should be removed, and at every position at shelf i which has not book at it, a book should be placed. 4 k — Return the books in the bookcase in a state they were after applying k-th operation. In particular, k = 0 means that the bookcase should be in initial state, thus every book in the bookcase should be removed from its position.After applying each of operation Alina is interested in the number of books in the bookcase. Alina got 'A' in the school and had no problem finding this values. Will you do so?

The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 103, 1 ≤ q ≤ 105) — the bookcase dimensions and the number of operations respectively. The next q lines describes operations in chronological order — i-th of them describes i-th operation in one of the four formats described in the statement. It is guaranteed that shelf indices and position indices are correct, and in each of fourth-type operation the number k corresponds to some operation before it or equals to 0.

For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order.

NoteThis image illustrates the second sample case.

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}

func getI() int {
  return int(getI64())
}

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

type State struct {
	bookCount int
	shelfIdxs []int
}

type Command struct {
	command int

}

type Node struct {
	opNo     int
	children []*Node
}

func (node *Node) visit () {
	//fmt.Println("HELLO", node.opNo, node.children)
	for _, child := range node.children {
		command := commands[child.opNo - 1]

		if command[0] == 1 {
			i, j := command[1], command[2]
			if !shelves[i][j] {
				bookCount[child.opNo] = bookCount[child.opNo - 1] + 1
				shelves[i][j] = true
				child.visit()
				shelves[i][j]= false
			} else {
				bookCount[child.opNo] = bookCount[child.opNo - 1]
				child.visit()
			}
		} else if command[0] == 2 {
			i, j := command[1], command[2]
			if shelves[i][j] {
				bookCount[child.opNo] = bookCount[child.opNo - 1] - 1
				shelves[i][j] = false
				child.visit()
				shelves[i][j] = true
			} else {
				bookCount[child.opNo] = bookCount[child.opNo - 1]
				child.visit()
			}
		} else if command[0] == 3 {
			i := command[1]
			newBookCount := 0
			for idx:=0; idx < m; idx++ {
				if shelves[i][idx] { shelves[i][idx] = false } else {
					shelves[i][idx] = true
					newBookCount += 1
				}
			}

			bookCount[child.opNo] = bookCount[child.opNo - 1] - (m - newBookCount) + newBookCount
			child.visit()

			for idx:=0; idx < m; idx++ {
				shelves[i][idx] = !shelves[i][idx]
			}

		} else if command[0] == 4 {
			stateNo := command[1]
			bookCount[child.opNo] = bookCount[stateNo]
			child.visit()
		}
	}
}

var (
	m, n, q int
	commands [100001][]int
	bookCount [100001]int
	shelves [1000][1000]bool
	nodes [100001]*Node
)

func main()  {

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	n = getI()
	m = getI()
	q = getI()

	bookCount[0] = 0
	nodes[0] = &Node{opNo: 0, children:make([]*Node, 0)}

	for opNo := 0; opNo < q; opNo++ {
		command := getI()
		if command == 1 {
			commands[opNo] = []int{command, getI() - 1, getI() - 1}
			node := &Node{opNo: opNo+1, children:make([]*Node, 0)}
			nodes[opNo + 1] = node
			nodes[opNo].children = append(nodes[opNo].children, node)
		} else if command == 2 {
			commands[opNo] = []int{command, getI() - 1, getI() - 1}
			node := &Node{opNo: opNo+1, children:make([]*Node, 0)}
			nodes[opNo + 1] = node
			nodes[opNo].children = append(nodes[opNo].children, node)
		} else if command == 3 {
			commands[opNo] = []int{command, getI() - 1}
			node := &Node{opNo: opNo+1, children:make([]*Node, 0)}
			nodes[opNo + 1] = node
			nodes[opNo].children = append(nodes[opNo].children, node)
		} else {
			stateNo := getI()
			commands[opNo] = []int{command, stateNo}
			node := &Node{opNo: opNo+1, children:make([]*Node, 0)}
			nodes[opNo + 1] = node
			nodes[stateNo].children = append(nodes[stateNo].children, node)
		}
	}

	nodes[0].visit()

	for i:=1; i<=q; i++{
		writer.WriteString(strconv.Itoa(bookCount[i]))
		writer.WriteByte('\n')
	}
}

package main

import "bufio"
import "strconv"
import "os"

type Query struct {
	cat int
	row int
	col int
}

var n, m, q, total int
var queries []Query
var adj, next, last, sum, inv, res, cur []int
var book [][]int

func add(u int, v int) {
	adj = append(adj, v)
	next = append(next, last[u])
	last[u] = len(next) - 1
}

func has(row int, col int) bool {
	return (book[row][col] ^ inv[row]) == 1
}

func toggle(row int, col int) {
	old := book[row][col]
	new := old ^ 1
	book[row][col] = new
	delta := (new ^ inv[row]) - (old ^ inv[row])
	sum[row] += delta
	total += delta
}

func invert(row int) {
	old := sum[row]
	new := m - sum[row]
	sum[row] = new
	inv[row] ^= 1
	total += new - old
}

func dfs(u int) {
	cat := queries[u].cat
	row := queries[u].row
	col := queries[u].col

	hasbook := has(row, col)
	good := ((cat == 1) && !hasbook) || ((cat == 2) && hasbook)

	switch queries[u].cat {
	case 1, 2:
		if good {
			toggle(row, col)
		}
	case 3:
		invert(row)
	}

	res[u] = total
	for e := last[u]; e != -1; e = next[e] {
		v := adj[e]
		dfs(v)
	}

	switch queries[u].cat {
	case 1, 2:
		if good {
			toggle(row, col)
		}
	case 3:
		invert(row)
	}
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	in.Scan()
	m, _ = strconv.Atoi(in.Text())

	in.Scan()
	q, _ = strconv.Atoi(in.Text())

	sum = make([]int, n+1)
	inv = make([]int, n+1)
	book = make([][]int, n+1)
	for i := 0; i <= n; i++ {
		sum[i] = 0
		inv[i] = 0
		book[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			book[i][j] = 0
		}
	}

	queries = append(queries, Query{
		cat: 0,
		row: 0,
		col: 0,
	})

	for i := 0; i <= q; i++ {
		last = append(last, -1)
		res = append(res, -2)
		cur = append(cur, -1)
	}

	res[0] = 0
	cur[0] = 0

	for i := 1; i <= q; i++ {
		in.Scan()
		cat, _ := strconv.Atoi(in.Text())

		in.Scan()
		row, _ := strconv.Atoi(in.Text())

		col := 0
		if cat <= 2 {
			in.Scan()
			col, _ = strconv.Atoi(in.Text())
		}

		queries = append(queries, Query{
			cat: cat,
			row: row,
			col: col,
		})

		if cat <= 3 {
			add(cur[i-1], i)
			cur[i] = i
		} else {
			cur[i] = cur[row]
		}
	}

	total = 0
	dfs(0)

	for i := 1; i <= q; i++ {
		if queries[i].cat == 4 {
			res[i] = res[queries[i].row]
		}
		out.WriteString(strconv.Itoa(res[i]))
		out.WriteByte('\n')
	}
}

package main

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

type node struct {
	children  [] int
	queryType int
	r         int
	c         int
	k         int
	i         int
	ans       int
}

var library [][]bool
var graph []*node
var n, m, q int

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)

	defer writer.Flush()

	scanner.Split(bufio.ScanWords)
	n, m, q = readInt(scanner), readInt(scanner), readInt(scanner)

	library = make([][]bool, n)
	for i, _ := range library {
		library[i] = make([]bool, m)
	}

	graph = make([]*node, q+1)
	temp := node{}
	graph[0] = &temp

	for i := 1; i <= q; i++ {
		queryType := readInt(scanner)
		if queryType == 1 || queryType == 2 {
			r, c := readInt(scanner)-1, readInt(scanner)-1
			newNode := node{queryType: queryType, r: r, c: c}
			graph[i] = &newNode
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 3 {
			k := readInt(scanner) - 1
			newNode := node{queryType: queryType, k: k}
			graph[i] = &newNode
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 4 {
			to := readInt(scanner)
			graph[i] = graph[to]
		}
	}

	dfs(graph[0], 0)

	for i := 1; i <= q; i++ {
		writer.WriteString(strconv.Itoa(graph[i].ans))
		writer.WriteByte('\n')
	}

}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	ret, _ := strconv.Atoi(scanner.Text())
	return ret
}

func dfs(current *node, count int) {
	changed := false

	if current.queryType == 1 && !library[current.r][current.c] {
		library[current.r][current.c] = true
		count++
		changed = true
	} else if current.queryType == 2 && library[current.r][current.c] {
		library[current.r][current.c] = false
		count--
		changed = true
	} else if current.queryType == 3 {
		changed = true
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
			if library[current.k][i] {
				count++
			} else {
				count--
			}
		}
	}

	current.ans = count
	for _, v := range current.children {
		dfs(graph[v], count)
	}

	if !changed {
		return
	}

	if current.queryType == 1 {
		library[current.r][current.c] = false
	} else if current.queryType == 2 {
		library[current.r][current.c] = true
	} else if current.queryType == 3 {
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
		}
	}
}

package main

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

type node struct {
	children  [] int
	queryType int
	r         int
	c         int
	k         int
	i         int
	ans       int
}

var library [][]bool
var graph []*node
var n, m, q int

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	n, m, q = readInt(scanner), readInt(scanner), readInt(scanner)

	library = make([][]bool, n)
	for i, _ := range library {
		library[i] = make([]bool, m)
	}

	graph = make([]*node, q+1)
	temp := node{}
	graph[0] = &temp

	for i := 1; i <= q; i++ {
		queryType := readInt(scanner)
		if queryType == 1 || queryType == 2 {
			r, c := readInt(scanner)-1, readInt(scanner)-1
			newNode := node{queryType: queryType, r: r, c: c}
			graph[i] = &newNode
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 3 {
			k := readInt(scanner) - 1
			newNode := node{queryType: queryType, k: k}
			graph[i] = &newNode
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 4 {
			to := readInt(scanner)
			graph[i] = graph[to]
		}
	}

	dfs(graph[0], 0)

	for i := 1; i <= q; i++ {
		fmt.Println(graph[i].ans)
	}
}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	ret, _ := strconv.Atoi(scanner.Text())
	return ret
}

func dfs(current *node, count int) {
	changed := false

	if current.queryType == 1 && !library[current.r][current.c] {
		library[current.r][current.c] = true
		count++
		changed = true
	} else if current.queryType == 2 && library[current.r][current.c] {
		library[current.r][current.c] = false
		count--
		changed = true
	} else if current.queryType == 3 {
		changed = true
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
			if library[current.k][i] {
				count++
			} else {
				count--
			}
		}
	}

	current.ans = count
	for _, v := range current.children {
		dfs(graph[v], count)
	}

	if !changed {
		return
	}

	if current.queryType == 1 {
		library[current.r][current.c] = false
	} else if current.queryType == 2 {
		library[current.r][current.c] = true
	} else if current.queryType == 3 {
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
		}
	}
}

package main

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

type node struct {
	children  [] int
	queryType int
	r         int
	c         int
	k         int
	ans       int
}

var library [][]bool
var graph []*node
var n, m, q int

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)

	defer writer.Flush()

	scanner.Split(bufio.ScanWords)
	n, m, q = readInt(scanner), readInt(scanner), readInt(scanner)

	library = make([][]bool, n)
	for i, _ := range library {
		library[i] = make([]bool, m)
	}

	graph = make([]*node, q+1)
	temp := node{}
	graph[0] = &temp

	for i := 1; i <= q; i++ {
		queryType := readInt(scanner)
		if queryType == 1 || queryType == 2 {
			r, c := readInt(scanner)-1, readInt(scanner)-1
			newNode := node{queryType: queryType, r: r, c: c}
			graph[i] = &newNode
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 3 {
			k := readInt(scanner) - 1
			newNode := node{queryType: queryType, k: k}
			graph[i] = &newNode
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 4 {
			to := readInt(scanner)
			graph[i] = graph[to]
		}
	}

	dfs(graph[0], 0)

	for i := 1; i <= q; i++ {
		writer.WriteString(strconv.Itoa(graph[i].ans))
		writer.WriteByte('\n')
	}

}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	ret, _ := strconv.Atoi(scanner.Text())
	return ret
}

func dfs(current *node, count int) {
	changed := false

	if current.queryType == 1 && !library[current.r][current.c] {
		library[current.r][current.c] = true
		count++
		changed = true
	} else if current.queryType == 2 && library[current.r][current.c] {
		library[current.r][current.c] = false
		count--
		changed = true
	} else if current.queryType == 3 {
		changed = true
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
			if library[current.k][i] {
				count++
			} else {
				count--
			}
		}
	}

	current.ans = count
	for _, v := range current.children {
		dfs(graph[v], count)
	}

	if !changed {
		return
	}

	if current.queryType == 1 {
		library[current.r][current.c] = false
	} else if current.queryType == 2 {
		library[current.r][current.c] = true
	} else if current.queryType == 3 {
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
		}
	}
}

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}

func getI() int {
  return int(getI64())
}

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

type State struct {
	bookCount int
	shelfIdxs []int
}

type Command struct {
	command int

}

func main()  {
	var (
		m, n, q int
		newShelf []byte
		newState State
		shelves [][]byte
		commands [][]int
	)

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	n = getI()
	m = getI()
	q = getI()

	for opNo := 0; opNo < q; opNo++ {
		command := getI()
		if command == 1 {
			commands = append(commands, []int{command, getI() - 1, getI() - 1})
		} else if command == 2 {
			commands = append(commands, []int{command, getI() - 1, getI() - 1})
		} else if command == 3 {
			commands = append(commands, []int{command, getI() - 1})
		} else {
			commands = append(commands, []int{command, getI()})
		}
	}

	statesToStore := make(map[int]bool)
	for _, commandSlice := range commands {
		if commandSlice[0] == 4 {
			statesToStore[commandSlice[1]] = true
		}
	}

	shelves = make([][]byte, 1, 1)
	shelves[0] = make([]byte, m, m)

	states := make(map[int]State)
	states[0] = State{bookCount: 0, shelfIdxs: make([]int, n, n)}
	currentState := State{bookCount: 0, shelfIdxs: make([]int, n, n)}

	for opNo, commandSlice := range commands {
		command := commandSlice[0]

		if command == 1 {
			i, j := commandSlice[1], commandSlice[2]
			newState = State{bookCount: currentState.bookCount, shelfIdxs: currentState.shelfIdxs}
			if shelves[currentState.shelfIdxs[i]][j] == 0 {
				newShelf = make([]byte, m, m)
				copy(newShelf, shelves[currentState.shelfIdxs[i]])
				newShelf[j] = 1

				newShelfIdxs := make([]int, n, n)
				copy(newShelfIdxs, currentState.shelfIdxs)
				newState.shelfIdxs = newShelfIdxs

				shelves = append(shelves, newShelf)
				newState.shelfIdxs[i] = len(shelves) - 1
				newState.bookCount += 1
			}
		} else if command == 2 {
			i, j := commandSlice[1], commandSlice[2]
			newState = State{bookCount: currentState.bookCount, shelfIdxs: currentState.shelfIdxs}
			if shelves[currentState.shelfIdxs[i]][j] == 1 {
				newShelf = make([]byte, m, m)
				copy(newShelf, shelves[currentState.shelfIdxs[i]])
				newShelf[j] = 0

				newShelfIdxs := make([]int, n, n)
				copy(newShelfIdxs, currentState.shelfIdxs)
				newState.shelfIdxs = newShelfIdxs

				shelves = append(shelves, newShelf)
				newState.shelfIdxs[i] = len(shelves) - 1
				newState.bookCount -= 1
			}

		} else if command == 3 {
			i := commandSlice[1]
			newState = State{bookCount: currentState.bookCount, shelfIdxs: currentState.shelfIdxs}
			newShelf = make([]byte, m, m)
			copy(newShelf, shelves[currentState.shelfIdxs[i]])
			newBookCount := 0
			for idx, value := range newShelf {
				if value == 1 {
					newShelf[idx] = 0
				} else {
					newShelf[idx] = 1
					newBookCount += 1
				}
			}

			newShelfIdxs := make([]int, n, n)
			copy(newShelfIdxs, currentState.shelfIdxs)
			newState.shelfIdxs = newShelfIdxs

			shelves = append(shelves, newShelf)
			newState.shelfIdxs[i] = len(shelves) - 1
			newState.bookCount = currentState.bookCount - (m - newBookCount) + newBookCount

		} else {
			i := commandSlice[1]
			newState = State{bookCount: states[i].bookCount, shelfIdxs:states[i].shelfIdxs}
		}
		if _, exists := statesToStore[opNo]; exists {
			states[opNo+1] = newState
		}
		currentState = newState
		writer.WriteString(strconv.Itoa(newState.bookCount))
		writer.WriteByte('\n')
	}
}

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}

func getI() int {
  return int(getI64())
}

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

type State struct {
	bookCount int
	shelfIdxs []int
}

type Command struct {
	command int

}

func main()  {
	var (
		m, n, q int
		newShelf []byte
		newState State
		shelves [][]byte
		commands [][]int
	)

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	n = getI()
	m = getI()
	q = getI()

	for opNo := 0; opNo < q; opNo++ {
		command := getI()
		if command == 1 {
			commands = append(commands, []int{command, getI() - 1, getI() - 1})
		} else if command == 2 {
			commands = append(commands, []int{command, getI() - 1, getI() - 1})
		} else if command == 3 {
			commands = append(commands, []int{command, getI() - 1})
		} else {
			commands = append(commands, []int{command, getI()})
		}
	}

	statesToStore := make(map[int]bool)
	for _, commandSlice := range commands {
		if commandSlice[0] == 4 {
			statesToStore[commandSlice[1]] = true
		}
	}

	shelves = make([][]byte, 1, 1)
	shelves[0] = make([]byte, m, m)

	states := make(map[int]State)
	states[0] = State{bookCount: 0, shelfIdxs: make([]int, n, n)}
	currentState := State{bookCount: 0, shelfIdxs: make([]int, n, n)}

	for opNo, commandSlice := range commands {
		command := commandSlice[0]

		if command == 1 {
			i, j := commandSlice[1], commandSlice[2]
			newState = State{bookCount: currentState.bookCount, shelfIdxs: currentState.shelfIdxs}
			if shelves[currentState.shelfIdxs[i]][j] == 0 {
				newShelf = make([]byte, m, m)
				copy(newShelf, shelves[currentState.shelfIdxs[i]])
				newShelf[j] = 1

				newShelfIdxs := make([]int, n, n)
				copy(newShelfIdxs, currentState.shelfIdxs)
				newState.shelfIdxs = newShelfIdxs

				shelves = append(shelves, newShelf)
				newState.shelfIdxs[i] = len(shelves) - 1
				newState.bookCount += 1
			}
		} else if command == 2 {
			i, j := commandSlice[1], commandSlice[2]
			newState = State{bookCount: currentState.bookCount, shelfIdxs: currentState.shelfIdxs}
			if shelves[currentState.shelfIdxs[i]][j] == 1 {
				newShelf = make([]byte, m, m)
				copy(newShelf, shelves[currentState.shelfIdxs[i]])
				newShelf[j] = 0

				newShelfIdxs := make([]int, n, n)
				copy(newShelfIdxs, currentState.shelfIdxs)
				newState.shelfIdxs = newShelfIdxs

				shelves = append(shelves, newShelf)
				newState.shelfIdxs[i] = len(shelves) - 1
				newState.bookCount -= 1
			}

		} else if command == 3 {
			i := commandSlice[1]
			newState = State{bookCount: currentState.bookCount, shelfIdxs: currentState.shelfIdxs}
			newShelf = make([]byte, m, m)
			copy(newShelf, shelves[currentState.shelfIdxs[i]])
			newBookCount := 0
			for idx, value := range newShelf {
				if value == 1 {
					newShelf[idx] = 0
				} else {
					newShelf[idx] = 1
					newBookCount += 1
				}
			}

			newShelfIdxs := make([]int, n, n)
			copy(newShelfIdxs, currentState.shelfIdxs)
			newState.shelfIdxs = newShelfIdxs

			shelves = append(shelves, newShelf)
			newState.shelfIdxs[i] = len(shelves) - 1
			newState.bookCount = currentState.bookCount - (m - newBookCount) + newBookCount

		} else {
			i := commandSlice[1]
			newState = State{bookCount: states[i].bookCount, shelfIdxs:states[i].shelfIdxs}
		}

		if _, exists := statesToStore[opNo]; exists {
			states[opNo] = newState
		}

		currentState = newState
		writer.WriteString(strconv.Itoa(newState.bookCount))
		writer.WriteByte('\n')
	}
}

package main

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

var scanner *bufio.Scanner
var writer *bufio.Writer

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}

func getI() int {
  return int(getI64())
}

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

type State struct {
	bookCount int
	shelfIdxs []int
}

type Command struct {
	command int

}

func main()  {
	var (
		m, n, q int
		newShelf []byte
		newState State
		shelves [][]byte
		commands [][]int
	)

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	n = getI()
	m = getI()
	q = getI()

	for opNo := 0; opNo < q; opNo++ {
		command := getI()
		if command == 1 {
			commands = append(commands, []int{command, getI() - 1, getI() - 1})
		} else if command == 2 {
			commands = append(commands, []int{command, getI() - 1, getI() - 1})
		} else if command == 3 {
			commands = append(commands, []int{command, getI() - 1})
		} else {
			commands = append(commands, []int{command, getI()})
		}
	}

	statesToStore := make(map[int]bool)
	for _, commandSlice := range commands {
		if commandSlice[0] == 4 {
			statesToStore[commandSlice[1]] = true
		}
	}

	shelves = make([][]byte, 1, 1)
	shelves[0] = make([]byte, m, m)

	states := make(map[int]State)
	states[0] = State{bookCount: 0, shelfIdxs: make([]int, n, n)}
	currentState := State{bookCount: 0, shelfIdxs: make([]int, n, n)}

	for opNo, commandSlice := range commands {
		command := commandSlice[0]

		if command == 1 {
			i, j := commandSlice[1], commandSlice[2]
			newState = currentState
			if shelves[currentState.shelfIdxs[i]][j] == 0 {
				newShelf = make([]byte, m, m)
				copy(newShelf, shelves[currentState.shelfIdxs[i]])
				newShelf[j] = 1

				shelves = append(shelves, newShelf)
				newState.shelfIdxs[i] = len(shelves) - 1
				newState.bookCount += 1
			}
		} else if command == 2 {
			i, j := commandSlice[1], commandSlice[2]
			newState = currentState
			if shelves[currentState.shelfIdxs[i]][j] == 1 {
				newShelf = make([]byte, m, m)
				copy(newShelf, shelves[currentState.shelfIdxs[i]])
				newShelf[j] = 0

				shelves = append(shelves, newShelf)
				newState.shelfIdxs[i] = len(shelves) - 1
				newState.bookCount -= 1
			}

		} else if command == 3 {
			i := commandSlice[1]
			newState = currentState
			newShelf = make([]byte, m, m)
			copy(newShelf, shelves[currentState.shelfIdxs[i]])
			newBookCount := 0
			for idx, value := range newShelf {
				if value == 1 {
					newShelf[idx] = 0
				} else {
					newShelf[idx] = 1
					newBookCount += 1
				}
			}

			shelves = append(shelves, newShelf)
			newState.shelfIdxs[i] = len(shelves) - 1
			newState.bookCount = currentState.bookCount - (m - newBookCount) + newBookCount

		} else {
			i := commandSlice[1]
			newState = State{bookCount: states[i].bookCount, shelfIdxs:states[i].shelfIdxs}
		}

		if _, exists := statesToStore[opNo+1]; exists {
			newShelfIdxs := make([]int, n, n)
			copy(newShelfIdxs, newState.shelfIdxs)
			states[opNo+1] = State{bookCount: newState.bookCount, shelfIdxs: newShelfIdxs}
		}

		currentState = newState
		writer.WriteString(strconv.Itoa(newState.bookCount))
		writer.WriteByte('\n')
	}
}

package main

import "bufio"
import "strconv"
import "os"

type Query struct {
	cat int
	row int
	col int
}

var n, m, q, total int
var queries []Query
var adj, next, last, sum, inv, res []int
var book [][]int

func add(u int, v int) {
	adj = append(adj, v)
	next = append(next, last[u])
	last[u] = len(next) - 1
}

func has(row int, col int) bool {
	return (book[row][col] ^ inv[row]) == 1
}

func toggle(row int, col int) {
	old := book[row][col]
	new := old ^ 1
	book[row][col] = new
	delta := (new ^ inv[row]) - (old ^ inv[row])
	sum[row] += delta
	total += delta
}

func invert(row int) {
	old := sum[row]
	new := m - sum[row]
	sum[row] = new
	inv[row] ^= 1
	total += new - old
}

func dfs(u int) {
	row := queries[u].row
	col := queries[u].col

	switch queries[u].cat {
	case 1:
		if !has(row, col) {
			toggle(row, col)
		}
	case 2:
		if has(row, col) {
			toggle(row, col)
		}
	case 3:
		invert(row)
	}

	res[u] = total
	for e := last[u]; e != -1; e = next[e] {
		v := adj[e]
		dfs(v)
	}

	switch queries[u].cat {
	case 1:
		if has(row, col) {
			toggle(row, col)
		}
	case 2:
		if !has(row, col) {
			toggle(row, col)
		}
	case 3:
		invert(row)
	}
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	in.Scan()
	m, _ = strconv.Atoi(in.Text())

	in.Scan()
	q, _ = strconv.Atoi(in.Text())

	sum = make([]int, n+1)
	inv = make([]int, n+1)
	book = make([][]int, n+1)
	for i := 0; i <= n; i++ {
		sum[i] = 0
		inv[i] = 0
		book[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			book[i][j] = 0
		}
	}

	queries = append(queries, Query{
		cat: 0,
		row: 0,
		col: 0,
	})

	for i := 0; i <= q; i++ {
		last = append(last, -1)
		res = append(res, -1)
	}

	res[0] = 0
	cur := 0

	for i := 1; i <= q; i++ {
		in.Scan()
		cat, _ := strconv.Atoi(in.Text())

		in.Scan()
		row, _ := strconv.Atoi(in.Text())

		col := 0
		if cat <= 2 {
			in.Scan()
			col, _ = strconv.Atoi(in.Text())
		}

		queries = append(queries, Query{
			cat: cat,
			row: row,
			col: col,
		})

		if cat <= 3 {
			add(cur, i)
			cur = i
		} else {
			cur = row
		}
	}

	total = 0
	dfs(0)

	for i := 1; i <= q; i++ {
		if queries[i].cat == 4 {
			res[i] = res[queries[i].row]
		}
		out.WriteString(strconv.Itoa(res[i]))
		out.WriteByte('\n')
	}
}

package main

import "bufio"
import "strconv"
import "os"

type Query struct {
	cat int
	row int
	col int
}

var n, m, q, total int
var queries []Query
var adj, next, last, sum, inv, res []int
var book [][]int

func add(u int, v int) {
	adj = append(adj, v)
	next = append(next, last[u])
	last[u] = len(next) - 1
}

func toggle(row int, col int) {
	old := book[row][col]
	new := old ^ 1
	book[row][col] = new
	delta := (new ^ inv[row]) - (old ^ inv[row])
	sum[row] += delta
	total += delta
}

func invert(row int) {
	old := sum[row]
	new := m - sum[row]
	sum[row] = new
	inv[row] ^= 1
	total += new - old
}

func dfs(u int) {
	row := queries[u].row
	col := queries[u].col

	switch queries[u].cat {
	case 1, 2:
		toggle(row, col)
	case 3:
		invert(row)
	}

	res[u] = total
	for e := last[u]; e != -1; e = next[e] {
		v := adj[e]
		dfs(v)
	}

	switch queries[u].cat {
	case 1, 2:
		toggle(row, col)
	case 3:
		invert(row)
	}
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	in.Scan()
	m, _ = strconv.Atoi(in.Text())

	in.Scan()
	q, _ = strconv.Atoi(in.Text())

	sum = make([]int, n+1)
	inv = make([]int, n+1)
	book = make([][]int, n+1)
	for i := 0; i <= n; i++ {
		sum[i] = 0
		inv[i] = 0
		book[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			book[i][j] = 0
		}
	}

	queries = append(queries, Query{
		cat: 0,
		row: 0,
		col: 0,
	})

	for i := 0; i <= q; i++ {
		last = append(last, -1)
		res = append(res, -1)
	}

	res[0] = 0
	cur := 0

	for i := 1; i <= q; i++ {
		in.Scan()
		cat, _ := strconv.Atoi(in.Text())

		in.Scan()
		row, _ := strconv.Atoi(in.Text())

		col := 0
		if cat <= 2 {
			in.Scan()
			col, _ = strconv.Atoi(in.Text())
		}

		queries = append(queries, Query{
			cat: cat,
			row: row,
			col: col,
		})

		if cat <= 3 {
			add(cur, i)
			cur = i
		} else {
			cur = row
		}
	}

	total = 0
	dfs(0)

	for i := 1; i <= q; i++ {
		if queries[i].cat == 4 {
			res[i] = res[queries[i].row]
		}
		out.WriteString(strconv.Itoa(res[i]))
		out.WriteByte('\n')
	}
}

package main

import "bufio"
import "strconv"
import "os"

type Query struct {
	cat int
	row int
	col int
}

var n, m, q, total int
var queries []Query
var adj, next, last, sum, inv, res []int
var book [][]int

func add(u int, v int) {
	adj = append(adj, v)
	next = append(next, last[u])
	last[u] = len(next) - 1
}

func has(row int, col int) bool {
	return (book[row][col] ^ inv[row]) == 1
}

func toggle(row int, col int) {
	old := book[row][col]
	new := old ^ 1
	book[row][col] = new
	delta := (new ^ inv[row]) - (old ^ inv[row])
	sum[row] += delta
	total += delta
}

func invert(row int) {
	old := sum[row]
	new := m - sum[row]
	sum[row] = new
	inv[row] ^= 1
	total += new - old
}

func dfs(u int) {
	cat := queries[u].cat
	row := queries[u].row
	col := queries[u].col

	hasbook := has(row, col)
	good := ((cat == 1) && !hasbook) || ((cat == 2) && hasbook)

	switch queries[u].cat {
	case 1, 2:
		if good {
			toggle(row, col)
		}
	case 3:
		invert(row)
	}

	res[u] = total
	for e := last[u]; e != -1; e = next[e] {
		v := adj[e]
		dfs(v)
	}

	switch queries[u].cat {
	case 1, 2:
		if good {
			toggle(row, col)
		}
	case 3:
		invert(row)
	}
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	in.Scan()
	m, _ = strconv.Atoi(in.Text())

	in.Scan()
	q, _ = strconv.Atoi(in.Text())

	sum = make([]int, n+1)
	inv = make([]int, n+1)
	book = make([][]int, n+1)
	for i := 0; i <= n; i++ {
		sum[i] = 0
		inv[i] = 0
		book[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			book[i][j] = 0
		}
	}

	queries = append(queries, Query{
		cat: 0,
		row: 0,
		col: 0,
	})

	for i := 0; i <= q; i++ {
		last = append(last, -1)
		res = append(res, -1)
	}

	res[0] = 0
	cur := 0

	for i := 1; i <= q; i++ {
		in.Scan()
		cat, _ := strconv.Atoi(in.Text())

		in.Scan()
		row, _ := strconv.Atoi(in.Text())

		col := 0
		if cat <= 2 {
			in.Scan()
			col, _ = strconv.Atoi(in.Text())
		}

		queries = append(queries, Query{
			cat: cat,
			row: row,
			col: col,
		})

		if cat <= 3 {
			add(cur, i)
			cur = i
		} else {
			cur = row
		}
	}

	total = 0
	dfs(0)

	for i := 1; i <= q; i++ {
		if queries[i].cat == 4 {
			res[i] = res[queries[i].row]
		}
		out.WriteString(strconv.Itoa(res[i]))
		out.WriteByte('\n')
	}
}

package main

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

type node struct {
	children  [] int
	queryType int
	r         int
	c         int
	k         int
	i         int
	ans       int
}

var library [][]bool
var graph []node
var n, m, q int

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	n, m, q = readInt(scanner), readInt(scanner), readInt(scanner)

	library = make([][]bool, n)
	for i, _ := range library {
		library[i] = make([]bool, m)
	}

	graph = make([]node, q+1)
	graph[0] = node{}

	for i := 1; i <= q; i++ {
		queryType := readInt(scanner)
		if queryType == 1 || queryType == 2 {
			r, c := readInt(scanner)-1, readInt(scanner)-1
			graph[i] = node{queryType: queryType, r: r, c: c}
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 3 {
			k := readInt(scanner) - 1
			graph[i] = node{queryType: queryType, k: k}
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 4 {
			to := readInt(scanner)
			graph[i] = node{queryType: queryType, i: to}
		}
	}

	dfs(&graph[0], 0)

	for i := 1; i <= q; i++ {
		if graph[i].queryType == 4 {
			fmt.Println(graph[graph[i].i].ans)
		} else {
			fmt.Println(graph[i].ans)
		}
	}
}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	ret, _ := strconv.Atoi(scanner.Text())
	return ret
}

func dfs(current *node, count int) {
	changed := false

	if current.queryType == 4 {
		current = &graph[current.i]
	}

	if current.queryType == 1 && !library[current.r][current.c] {
		library[current.r][current.c] = true
		count++
		changed = true
	} else if current.queryType == 2 && library[current.r][current.c] {
		library[current.r][current.c] = false
		count--
		changed = true
	} else if current.queryType == 3 {
		changed = true
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
			if library[current.k][i] {
				count++
			} else {
				count--
			}
		}
	}

	current.ans = count
	for _, v := range current.children {
		dfs(&graph[v], count)
	}

	if !changed {
		return
	}

	if current.queryType == 1 {
		library[current.r][current.c] = false
	} else if current.queryType == 2 {
		library[current.r][current.c] = true
	} else if current.queryType == 3 {
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
		}
	}
}

package main

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

type node struct {
	children  [] int
	queryType int
	r         int
	c         int
	k         int
	i         int
	ans       int
}

var library [][]bool
var graph []node
var n, m, q int

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	n, m, q = readInt(scanner), readInt(scanner), readInt(scanner)

	library = make([][]bool, n)
	for i, _ := range library {
		library[i] = make([]bool, m)
	}

	graph = make([]node, q+1)
	graph[0] = node{}

	for i := 1; i <= q; i++ {
		queryType := readInt(scanner)
		if queryType == 1 || queryType == 2 {
			r, c := readInt(scanner)-1, readInt(scanner)-1
			graph[i] = node{queryType: queryType, r: r, c: c}
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 3 {
			k := readInt(scanner) - 1
			graph[i] = node{queryType: queryType, k: k}
			graph[i-1].children = append(graph[i-1].children, i)
		} else if queryType == 4 {
			to := readInt(scanner)
			graph[i] = node{queryType: queryType, i: to}
		}
	}

	dfs(&graph[0], 0)

	for i := 1; i <= q; i++ {
		fmt.Println(graph[i].ans)
	}
}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	ret, _ := strconv.Atoi(scanner.Text())
	return ret
}

func dfs(current *node, count int) {
	changed := false

	if current.queryType == 4 {
		current = &graph[current.i]
	}

	if current.queryType == 1 && !library[current.r][current.c] {
		library[current.r][current.c] = true
		count++
		changed = true
	} else if current.queryType == 2 && library[current.r][current.c] {
		library[current.r][current.c] = false
		count--
		changed = true
	} else if current.queryType == 3 {
		changed = true
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
			if library[current.k][i] {
				count++
			} else {
				count--
			}
		}
	}

	current.ans = count
	for _, v := range current.children {
		dfs(&graph[v], count)
	}

	if !changed {
		return
	}

	if current.queryType == 1 {
		library[current.r][current.c] = false
	} else if current.queryType == 2 {
		library[current.r][current.c] = true
	} else if current.queryType == 3 {
		for i := 0; i < m; i++ {
			library[current.k][i] = !library[current.k][i]
		}
	}
}


You've got an n × m pixel picture. Each pixel can be white or black. Your task is to change the colors of as few pixels as possible to obtain a barcode picture.A picture is a barcode if the following conditions are fulfilled:   All pixels in each column are of the same color.  The width of each monochrome vertical line is at least x and at most y pixels. In other words, if we group all neighbouring columns of the pixels with equal color, the size of each group can not be less than x or greater than y.

The first line contains four space-separated integers n, m, x and y (1 ≤ n, m, x, y ≤ 1000; x ≤ y). Then follow n lines, describing the original image. Each of these lines contains exactly m characters. Character "." represents a white pixel and "#" represents a black pixel. The picture description doesn't have any other characters besides "." and "#".

In the first line print the minimum number of pixels to repaint. It is guaranteed that the answer exists.

NoteIn the first test sample the picture after changing some colors can looks as follows:  .##...##...##...##...##...##.. In the second test sample the picture after changing some colors can looks as follows:  .#.#..#.#.

package main

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

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

	var n, m, x, y int
	Fscan(in, &n, &m, &x, &y)
	cnt := make([]int, m)
	var s string
	for i := 0; i < n; i++ {
		Fscan(in, &s)
		for j, c := range s {
			if c == '.' {
				cnt[j]++
			}
		}
	}

	sum := make([][2]int, m+1)
	for i := 1; i <= m; i++ {
		sum[i][0] = sum[i-1][0] + cnt[i-1]
		sum[i][1] = sum[i-1][1] + n - cnt[i-1]
	}
	dp := make([][2]int, m+1)
	for i := range dp {
		dp[i][0] = 1e8
		dp[i][1] = 1e8
	}
	dp[0][0] = 0
	dp[0][1] = 0
	for i := 1; i <= m; i++ {
		for j := x; j <= y; j++ {
			if i-j < 0 {
				break
			}
			dp[i][0] = min(dp[i][0], dp[i-j][1]+sum[i][0]-sum[i-j][0])
			dp[i][1] = min(dp[i][1], dp[i-j][0]+sum[i][1]-sum[i-j][1])
		}
	}
	Fprint(out, min(dp[m][0], dp[m][1]))
}

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

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/225/C

package main

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

func min(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

func initDP(m int) [][]int {
	dp := make([][]int, 2)
	for i := 0; i < 2; i++ {
		dp[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			dp[i][j] = 1e8
		}
	}
	dp[0][0] = 0
	dp[1][0] = 0
	return dp
}

func initPrefix(whites []int, n int, m int) [][]int {
	prefix := make([][]int, 2)
	for i := 0; i < 2; i++ {
		prefix[i] = make([]int, m+1)
	}
	for i := 1; i <= m; i++ {
		prefix[0][i] = prefix[0][i-1] + whites[i-1]
		prefix[1][i] = prefix[1][i-1] + n - whites[i-1]
	}
	return prefix
}

func Solve(n int, m int, x int, y int, picture []string) int {
	whites := make([]int, m)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if string(picture[i][j]) == "." {
				whites[j] += 1
			}
		}
	}
	dp := initDP(m)
	prefix := initPrefix(whites, n, m)
	for i := 1; i <= m; i++ {
		for a := x; a <= y; a++ {
			if i-a >= 0 {
				dp[0][i] = min(dp[0][i], dp[1][i-a]+prefix[0][i]-prefix[0][i-a])
				dp[1][i] = min(dp[1][i], dp[0][i-a]+prefix[1][i]-prefix[1][i-a])
			}
		}
	}
	return min(dp[0][m], dp[1][m])
}

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

	var n, m, x, y int
	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &m, &x, &y)
	picture := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &picture[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, m, x, y, picture))
}

package main

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

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

	var n, m, x, y int
	Fscan(in, &n, &m, &x, &y)
	cnt := make([]int, m)
	pic := make([]string, n)
	for i := range pic {
		Fscan(in, &pic[i])
		for j, c := range pic[i] {
			if c == '.' {
				cnt[j]++
			}
		}
	}

	dp := make([][][2]int, m)
	for i := range dp {
		dp[i] = make([][2]int, m)
		for j := range dp[i] {
			dp[i][j][0] = -1
			dp[i][j][1] = -1
		}
	}

	var f func(int, int8, int) int
	f = func(i int, isDot int8, con int) (res int) {
		if i == m {
			if con-1 < x || con-1 > y {
				return 1e8
			}
			return
		}
		if con-1 >= y {
			return 1e8
		}
		if val := dp[i][con][isDot]; val != -1 {
			return val
		}
		if isDot == 0 { // #
			res = f(i+1, 0, con+1) + cnt[i]
			if con >= x {
				res = min(res, f(i+1, 1, 1)+n-cnt[i]) // .
			}
		} else { // .
			res = f(i+1, 1, con+1) + n - cnt[i]
			if con >= x {
				res = min(res, f(i+1, 0, 1)+cnt[i]) // #
			}
		}
		if res < 1e8{
			dp[i][con][isDot] = res
		}
		return
	}
	Fprint(out, min(f(1, 0, 1)+cnt[0], f(1, 1, 1)+n-cnt[0]))
}

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

package main

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

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

	var n, m, x, y int
	Fscan(in, &n, &m, &x, &y)
	cnt := make([]int, m)
	pic := make([]string, n)
	for i := range pic {
		Fscan(in, &pic[i])
		for j, c := range pic[i] {
			if c == '.' {
				cnt[j]++
			}
		}
	}

	dp := make([][][2]int, m)
	for i := range dp {
		dp[i] = make([][2]int, m)
		for j := range dp[i] {
			dp[i][j][0] = -1
			dp[i][j][1] = -1
		}
	}

	var f func(int, int8, int) int
	f = func(i int, isDot int8, con int) (res int) {
		if i == m {
			if con-1 < x || con-1 > y {
				return 1e8
			}
			return
		}
		if con-1 >= y {
			return 1e8
		}
		if val := dp[i][con][isDot]; val != -1 {
			return val
		}
		if isDot == 0 { // #
			res = f(i+1, 0, con+1) + cnt[i]
			if con >= x {
				res = min(res, f(i+1, 1, 1)+n-cnt[i]) // .
			}
		} else { // .
			res = f(i+1, 1, con+1) + n - cnt[i]
			if con >= x {
				res = min(res, f(i+1, 0, 1)+cnt[i]) // #
			}
		}
		dp[i][con][isDot] = res
		return
	}
	Fprint(out, min(f(1, 0, 1)+cnt[0], f(1, 1, 1)+n-cnt[0]))
}

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

package main

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

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

	var n, m, x, y int
	Fscan(in, &n, &m, &x, &y)
	cnt := make([]int, m)
	pic := make([]string, n)
	for i := range pic {
		Fscan(in, &pic[i])
		for j, c := range pic[i] {
			if c == '.' {
				cnt[j]++
			}
		}
	}

	cache := map[string]int{}
	var f func(int, int8, int) int
	f = func(i int, isDot int8, con int) (res int) {
		if i == m {
			return
		}
		if con > y {
			return 1e8
		}
		hash := Sprintf("%d;%d;%d", i, isDot, con)
		if val, ok := cache[hash]; ok {
			return val
		}
		if isDot == 0 { // #
			res = f(i+1, isDot, con+1) + cnt[i]
			if con >= x {
				res = min(res, f(i+1, isDot^1, 1)+n-cnt[i]) // .
			}
		} else { // .
			res = f(i+1, isDot, con+1) + n - cnt[i]
			if con >= x {
				res = min(res, f(i+1, isDot^1, 1)+cnt[i]) // #
			}
		}
		cache[hash] = res
		return
	}
	Fprint(out, min(f(1, 0, 1)+cnt[0], f(1, 1, 1)+n-cnt[0]))
}

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

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/225/C

package main

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

func min(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

func initDP(m int) [][]int {
	dp := make([][]int, 2)
	for i := 0; i < 2; i++ {
		dp[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			dp[i][j] = math.MaxInt32
		}
	}
	dp[0][0] = 0
	dp[1][0] = 0
	return dp
}

func initPrefix(whites []int, n int, m int) [][]int {
	prefix := make([][]int, 2)
	for i := 0; i < 2; i++ {
		prefix[i] = make([]int, m+1)
	}
	for i := 1; i <= m; i++ {
		prefix[0][i] = prefix[0][i-1] + whites[i-1]
		prefix[1][i] = prefix[1][i-1] + n - whites[i-1]
	}
	return prefix
}

func sum(prefix []int, start int, end int) int {
	return prefix[end] - prefix[start-1]
}

func Solve(n int, m int, x int, y int, picture []string) int {
	whites := make([]int, m)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if string(picture[i][j]) == "." {
				whites[j] += 1
			}
		}
	}
	dp := initDP(m)
	prefix := initPrefix(whites, n, m)
	for i := 0; i <= m; i++ {
		for a := x; a <= y; a++ {
			if i-a >= 0 {
				dp[0][i] = min(dp[0][i], dp[1][i-a]+sum(prefix[0], i-a+1, i))
				dp[1][i] = min(dp[1][i], dp[0][i-a]+sum(prefix[1], i-a+1, i))
			}
		}
	}
	return min(dp[0][m], dp[1][m])
}

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

	var n, m, x, y int
	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &m, &x, &y)
	picture := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &picture[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, m, x, y, picture))
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/225/C

package main

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

func min(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

func initDP(m int) [][]int {
	dp := make([][]int, 2)
	for i := 0; i < 2; i++ {
		dp[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			dp[i][j] = math.MaxInt16
		}
	}
	dp[0][0] = 0
	dp[1][0] = 0
	return dp
}

func initPrefix(whites []int, n int, m int) [][]int {
	prefix := make([][]int, 2)
	for i := 0; i < 2; i++ {
		prefix[i] = make([]int, m+1)
	}
	for i := 1; i <= m; i++ {
		prefix[0][i] = prefix[0][i-1] + whites[i-1]
		prefix[1][i] = prefix[1][i-1] + n - whites[i-1]
	}
	return prefix
}

func sum(prefix []int, start int, end int) int {
	return prefix[end] - prefix[start-1]
}

func Solve(n int, m int, x int, y int, picture []string) int {
	whites := make([]int, m)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if string(picture[i][j]) == "." {
				whites[j] += 1
			}
		}
	}
	dp := initDP(m)
	prefix := initPrefix(whites, n, m)
	for i := 1; i <= m; i++ {
		for a := x; a <= y; a++ {
			if i-a >= 0 {
				dp[0][i] = min(dp[0][i], dp[1][i-a]+sum(prefix[0], i-a+1, i))
				dp[1][i] = min(dp[1][i], dp[0][i-a]+sum(prefix[1], i-a+1, i))
			}
		}
	}
	return min(dp[0][m], dp[1][m])
}

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

	var n, m, x, y int
	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &m, &x, &y)
	picture := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &picture[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, m, x, y, picture))
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/225/C

package main

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

func min(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

func initDP(m int) [][]int {
	dp := make([][]int, 2)
	for i := 0; i < 2; i++ {
		dp[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			dp[i][j] = math.MaxInt16
		}
	}
	dp[0][0] = 0
	dp[1][0] = 0
	return dp
}

func initPrefix(whites []int, n int, m int) [][]int {
	prefix := make([][]int, 2)
	for i := 0; i < 2; i++ {
		prefix[i] = make([]int, m+1)
	}
	for i := 1; i <= m; i++ {
		prefix[0][i] = prefix[0][i-1] + whites[i-1]
		prefix[1][i] = prefix[1][i-1] + n - whites[i-1]
	}
	return prefix
}

func sum(prefix []int, start int, end int) int {
	return prefix[end] - prefix[start-1]
}

func Solve(n int, m int, x int, y int, picture []string) int {
	whites := make([]int, m)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if string(picture[i][j]) == "." {
				whites[j] += 1
			}
		}
	}
	dp := initDP(m)
	prefix := initPrefix(whites, n, m)
	for i := 0; i <= m; i++ {
		for a := x; a <= y; a++ {
			if i-a >= 0 {
				dp[0][i] = min(dp[0][i], dp[1][i-a]+sum(prefix[0], i-a+1, i))
				dp[1][i] = min(dp[1][i], dp[0][i-a]+sum(prefix[1], i-a+1, i))
			}
		}
	}
	return min(dp[0][m], dp[1][m])
}

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

	var n, m, x, y int
	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &m, &x, &y)
	picture := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &picture[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, m, x, y, picture))
}


Seiji Maki doesn't only like to observe relationships being unfolded, he also likes to observe sequences of numbers, especially permutations. Today, he has his eyes on almost sorted permutations.A permutation $$$a_1, a_2, \dots, a_n$$$ of $$$1, 2, \dots, n$$$ is said to be almost sorted if the condition $$$a_{i + 1} \ge a_i - 1$$$ holds for all $$$i$$$ between $$$1$$$ and $$$n - 1$$$ inclusive.Maki is considering the list of all almost sorted permutations of $$$1, 2, \dots, n$$$, given in lexicographical order, and he wants to find the $$$k$$$-th permutation in this list. Can you help him to find such permutation?Permutation $$$p$$$ is lexicographically smaller than a permutation $$$q$$$ if and only if the following holds: in the first position where $$$p$$$ and $$$q$$$ differ, the permutation $$$p$$$ has a smaller element than the corresponding element in $$$q$$$.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$) — the number of test cases. Each test case consists of a single line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le k \le 10^{18}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print a single line containing the $$$k$$$-th almost sorted permutation of length $$$n$$$ in lexicographical order, or $$$-1$$$ if it doesn't exist.

NoteFor the first and second test, the list of almost sorted permutations with $$$n = 1$$$ is $$$\{[1]\}$$$.For the third and fifth test, the list of almost sorted permutations with $$$n = 3$$$ is $$$\{[1, 2, 3], [1, 3, 2], [2, 1, 3], [3, 2, 1]\}$$$.

package main

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

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

	var T, n int
	var k int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		if n < 61 && k > 1<<(n-1) {
			Fprintln(out, -1)
			continue
		}
		k--
		cur := 1
		for i := n - 2; i >= 0; i-- {
			if i > 59 || k>>i&1 == 0 {
				Fprint(out, cur, " ")
				cur++
				continue
			}
			st := i
			for ; i >= 0 && k>>i&1 > 0; i-- {
			}
			for j := st; j >= i; j-- {
				Fprint(out, cur+j-i, " ")
			}
			cur += st - i + 1
		}
		if cur == n {
			Fprint(out, n)
		}
		Fprintln(out)
	}
}
func main() { CF1508B(os.Stdin, os.Stdout) }

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	o := bufio.NewWriter(os.Stdout)
	var T, n int
	var k int64
	for Scan(&T); T > 0; T-- {
		Scan(&n, &k)
		if n < 61 && k > 1<<(n-1) {
			Fprintln(o, -1)
			continue
		}
		k--
		x := 1
		for i := n - 2; i >= 0; i-- {
			if i > 59 || k>>i&1 == 0 {
				Fprint(o, x, " ")
				x++
				continue
			}
			l := i
			for ; i >= 0 && k>>i&1 > 0; i-- {
			}
			for j := l; j >= i; j-- {
				Fprint(o, x+j-i, " ")
			}
			x += l - i + 1
		}
		if x == n {
			Fprint(o, n)
		}
		Fprintln(o)
	}
	o.Flush()
}

package main

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

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

	var T, n int
	var k int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		if n < 61 && k > 1<<(n-1) {
			Fprintln(out, -1)
			continue
		}
		k--
		cur := 1
		for i := n - 2; i >= 0; i-- {
			if i > 60 || k>>i&1 == 0 {
				Fprint(out, cur, " ")
				cur++
				continue
			}
			st := i
			for ; i >= 0 && k>>i&1 > 0; i-- {
			}
			for j := st; j >= i; j-- {
				Fprint(out, cur+j-i, " ")
			}
			cur += st - i + 1
		}
		if cur == n {
			Fprint(out, n)
		}
		Fprintln(out)
	}
}

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		s, _ := reader.ReadBytes('\n')
		var n int
		pos := readInt(s, 0, &n)
		var k uint64
		readUint64(s, pos+1, &k)

		res := solve(n, int64(k))

		if len(res) == 0 {
			buf.WriteString("-1\n")
		} else {
			for i := 0; i < n; i++ {
				buf.WriteString(fmt.Sprintf("%d ", res[i]))
			}
			buf.WriteByte('\n')
		}
	}
	fmt.Print(buf.String())
}

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
}

const N = 100010

var F [N]int64

func init() {
	F[0] = 1
	F[1] = 1
	var sum int64 = 2

	for i := 2; i < N; i++ {
		F[i] = sum
		sum += F[i]
	}
}

func solve(n int, k int64) []int {
	if n < 62 && k > 1<<uint64(n-1) {
		return nil
	}
	k--
	P := make([]int, n)

	for b := 0; b <= min(60, n-1); b++ {
		if (k>>b)&1 == 1 {
			P[n-2-b] = 1
		}
	}
	ans := make([]int, 0, n)
	cur, sz := 1, 1
	for i := 0; i < n; i++ {
		if P[i] == 0 {
			for j := cur + sz - 1; j >= cur; j-- {
				ans = append(ans, j)
			}
			cur += sz
			sz = 1
		} else {
			sz++
		}
	}

	return ans
}

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

func reverse(arr []int) {
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		arr[i], arr[j] = arr[j], arr[i]
	}
}

package main

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

const maxN = 100023

func rev(l int, r int, s *[maxN]int64) {
  for i, j := l, r; i <= j; i, j = i+1, j-1 {
    (*s)[i], (*s)[j] = (*s)[j], (*s)[i]
  }
}

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

func main() {
  in := bufio.NewReader(os.Stdin)
  out := bufio.NewWriter(os.Stdout)
  defer out.Flush()
  var tt int
  fmt.Fscan(in, &tt)
  for tc := 1; tc <= tt; tc++ {
    var n, k, mx int64
    fmt.Fscan(in, &n, &k)
    k--
    mx = 62
    if k >= (1 << min((n-1), mx)) {
      fmt.Fprintln(out, -1)
      continue
    }
    var perm[maxN] int64
    for i := 0; i < int(n); i++ {
      perm[i] = int64(i)
    }
    for r := 0 ; int64(r) < min(n-1, mx); {
      if k & (1 << r) != 0 {
        st := r
        for ; true; {
          r++
          if k & (1 << r) != 0 {
            continue
          }else {
            break
          }
        }
        rev(int(n) - 1 - r, int(n) - st - 1, &perm)
      }else {
        r++
        continue
      }
    }
    for i := 0; i < int(n); i++ {
      fmt.Fprint(out, perm[i]+1, " ")
    }
    fmt.Fprintln(out)
  }
}

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 t > 0 {
		t--
		var n int
		var k int64
		fmt.Fscan(in, &n, &k)
		dp := make([]int64, n+1)
		dp[n] = 1
		for i := n-1;i>=0;i-- {
			for j := 1; j <= n-i; j++ {
				dp[i] += dp[i+j]
				if dp[i] > k {
					dp[i] = k
					break
				}
			}
		}
		if dp[0] < k {
			fmt.Fprintln(out, -1)
			continue
		}
		for i := 0; i < n; i++ {
			for j := 1; j <= n-i; j++ {
				if dp[i+j] >= k {
					for k := i+j-1; k >= i; k-- {
						fmt.Fprint(out, k+1, " ")
					}
					i+=j-1
					break
				}else{
					k -= dp[i+j]
				}
			}
		}
		fmt.Fprint(out, "\n")
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		s, _ := reader.ReadBytes('\n')
		var n int
		pos := readInt(s, 0, &n)
		var k uint64
		readUint64(s, pos+1, &k)

		res := solve(n, int64(k))

		if len(res) == 0 {
			buf.WriteString("-1\n")
		} else {
			for i := 0; i < n; i++ {
				buf.WriteString(fmt.Sprintf("%d ", res[i]))
			}
			buf.WriteByte('\n')
		}
	}
	fmt.Print(buf.String())
}

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
}

const N = 100010

var F [N]int64

func init() {
	F[0] = 1
	F[1] = 1
	var sum int64 = 2

	for i := 2; i < N; i++ {
		F[i] = sum
		sum += F[i]
	}
}

func solve(n int, k int64) []int {
	if F[n] < k {
		return nil
	}

	P := make([]int, n)
	i := n
	cur := 1

	for i > 0 {
		j := i
		for i > 0 && F[i-1] < k {
			k -= F[i-1]
			i--
			cur++
		}
		i--
		// F[i-1] >= k
		x := cur
		for j > i {
			P[j-1] = x
			x--
			j--
		}
		cur++
	}
	reverse(P)
	return P
}

func reverse(arr []int) {
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		arr[i], arr[j] = arr[j], arr[i]
	}
}


There was a big bank robbery in Tablecity. In order to catch the thief, the President called none other than Albert – Tablecity’s Chief of Police. Albert does not know where the thief is located, but he does know how he moves.Tablecity can be represented as 1000 × 2 grid, where every cell represents one district. Each district has its own unique name “(X, Y)”, where X and Y are the coordinates of the district in the grid. The thief’s movement is as Every hour the thief will leave the district (X, Y) he is currently hiding in, and move to one of the districts: (X - 1, Y), (X + 1, Y), (X - 1, Y - 1), (X - 1, Y + 1), (X + 1, Y - 1), (X + 1, Y + 1) as long as it exists in Tablecity. Below is an example of thief’s possible movements if he is located in district (7,1):Albert has enough people so that every hour he can pick any two districts in Tablecity and fully investigate them, making sure that if the thief is located in one of them, he will get caught. Albert promised the President that the thief will be caught in no more than 2015 hours and needs your help in order to achieve that.

The first line of output contains integer N – duration of police search in hours. Each of the following N lines contains exactly 4 integers Xi1, Yi1, Xi2, Yi2 separated by spaces, that represent 2 districts (Xi1, Yi1), (Xi2, Yi2) which got investigated during i-th hour. Output is given in chronological order (i-th line contains districts investigated during i-th hour) and should guarantee that the thief is caught in no more than 2015 hours, regardless of thief’s initial position and movement.   N ≤ 2015  1 ≤ X ≤ 1000  1 ≤ Y ≤ 2

В этой задаче нет примеров ввода-вывода.
This problem doesn't have sample input and output.

Смотрите замечание ниже.
See the note below.

NoteLet's consider the following output:25 1 50 28 1 80 2This output is not guaranteed to catch the thief and is not correct. It is given to you only to show the expected output format. There exists a combination of an initial position and a movement strategy such that the police will not catch the thief.Consider the following initial position and thief’s movement:In the first hour, the thief is located in district (1,1). Police officers will search districts (5,1) and (50,2) and will not find him.At the start of the second hour, the thief moves to district (2,2). Police officers will search districts (8,1) and (80,2) and will not find him.Since there is no further investigation by the police, the thief escaped!

package main

import (
	"fmt"
	"os"
)

func main() {
	printf("2000\n")
	for x := 1; x <= 1000; x++ {
		printf("%d 1 %d 2\n", x, x)
	}
	for x := 1000; x >= 1; x-- {
		printf("%d 2 %d 1\n", x, x)
	}
}

func printf(format string, x ...interface{}) {
	fmt.Fprintf(os.Stdout, format, x...)
}