package main

import "fmt"

func main() {
	var S, ans string
	fmt.Scan(&S)
	if S[2] == S[3] && S[4] == S[5] {
		ans = "Yes"
	} else {
		ans = "No"
	}
	fmt.Println(ans)
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


package main

import "fmt"

func main() {
	var str string
	fmt.Scan(&str)
	if str[2] == str[3] && str[4] == str[5] {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


package main

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

var sc = bufio.NewScanner(os.Stdin)

func scanString() (s string) {
	sc.Scan()
	return sc.Text()
}

func main() {
	input := scanString()
	if input[2] == input[3] && input[4] == input[5] {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}


package main

import "fmt"

func main() {
	var x int64
	fmt.Scan(&x)

	b500 := x / 500
	x -= b500 * 500
	b5 := x / 5

	fmt.Println(b500*1000 + b5*5)

}

Score : 200 points

Problem Statement

Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)

Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?

(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)

Constraints

0 \leq X \leq 10^9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the maximum number of happiness points that can be earned.

Sample Input 1

1024

Sample Output 1

2020

By exchanging his money so that he gets two 500-yen coins and four 5-yen coins, he gains 2020 happiness points, which is the maximum number of happiness points that can be earned.

Sample Input 2

0

Sample Output 2

0

He is penniless - or yenless.

Sample Input 3

1000000000

Sample Output 3

2000000000

He is a billionaire - in yen.


package main

import (
	"fmt"
)

func main() {
	var x int
	fmt.Scan(&x)
	a := x / 500
	b := (x % 500) / 5
	res := a*1000 + b*5
	fmt.Println(res)
}


package main

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

func main() {
	scan_init()
	k := scanInt()
	n := scanInt()
	// a := make([]int,n)
	circle := make(map[int]int)
	for i:=0; i<n; i++ {
		number := scanInt() % k
		circle[number] = number
	}
	end := 0
	start := k+1
	// zero := false
	for i,_ := range circle {
		if start > i{
			start = i
		}
		if end < i {
			end = i
		}
	}

	sum := end-start
	secondstart := k+1
	secondend := 0
	secondsum := 0
	for i,_ := range circle {
		if secondstart > k-i{
			secondstart = k-i
		}
		if secondend < k-i {
			secondend = k-i
		}
	}
	secondsum = secondend - secondstart
	if sum > secondsum {
		sum = secondsum
	}
	fmt.Println(sum)
}
var sc = bufio.NewScanner(os.Stdin)
func scan_init () {
	sc.Split(bufio.ScanWords)
}
func scanInt () int{
sc.Scan()
	i,_ := strconv.Atoi(sc.Text())
	return i
}
//func scan () string{
//  sc.Scan()
//	return sc.Text()
//}

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

2 \leq N \leq 2 \times 10^5

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	var N,M int
	fmt.Scan(&N,&M)
	var A string
  	buf := make([]byte, 1000000)
  	sc.Buffer(buf, 1000000000)
	if sc.Scan() {
		A = sc.Text()
	}
	max := 0
	var diff,val_before,A_0 int
	var distace_diff []int
	for index, i := range strings.Fields(A) {
		value, _ := strconv.Atoi(i)
		if index > 0{
			diff = value - val_before
			distace_diff = append(distace_diff,diff)
			if max < diff{
				max = diff
			}
		} else {
			A_0 = value
		}
		if len(distace_diff) == M-1 {
			distace_diff = append(distace_diff,A_0 + (N - value))
			diff = A_0 + (N - value)
			if max < diff{
				max = diff
			}
		}
		val_before = value
	}
	var count int
	var total int
	for _,i := range distace_diff {
		if (count == 0 ) && (i == max){
			count += 1
		} else {
			total += i
		}
	}
	fmt.Println(total)
}


package main

import "fmt"

func main(){
	var K, N, ans int
	fmt.Scan(&K, &N)
	A := make([]int, N)

	for i := 0 ; i < N ; i++ {
		fmt.Scan(&A[i])
	}

	if A[1] - A[0] < K-A[N-1]+A[0]{
		for i := 1 ; i < N ; i++{
			ans += A[i]-A[i-1]
			//fmt.Println(ans)
		}
	}else{
		for i := 2 ; i < N ; i++ {
			ans += A[i]-A[i-1]
		}
		if N > 3 {
			ans += K-A[N-1]+A[0]
		}
	}

	fmt.Println(ans)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func scanIntNums() (nums []int) {
	sc.Scan()
	numString := strings.Split(sc.Text(), " ")

	nums = make([]int, len(numString))
	var err error

	for i := 0; i < len(nums); i++ {
		nums[i], err = strconv.Atoi(numString[i])
		if err != nil {
			panic(err)
		}
	}
	return nums
}

func main() {
	inputs := scanIntNums()
	K := inputs[0]
	N := inputs[1]

	distances := scanIntNums()

	var dist int
	max := K - distances[N-1] + distances[0]
	for i := 0; i < N-1; i++ {
		dist = distances[i+1] - distances[i]
		if max < dist {
			max = dist
		}
	}

	fmt.Println(K - max)
}


package main

import "fmt"

var k, n int
var as []int

func main() {
	fmt.Scan(&k, &n)
	as = make([]int, n)
	for i := range as {
		fmt.Scan(&as[i])
	}
	ans := as[n-1] - as[0]
	for i := 1; i < n; i++ {
		chmin(&ans, k + as[i-1] - as[i])
	}
	fmt.Println(ans)
}

const INF = 1 << 60

func chmax(a *int, b int) {
	if *a < b {
		*a = b
	}
}

func chmin(a *int, b int) {
	if *a > b {
		*a = b
	}
}


package main

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

func main() {
	io := NewIo(os.Stdin, os.Stdout)
	defer io.Flush()
	n, x, y := io.NextInt(), io.NextInt()-1, io.NextInt()-1
	dp := make([][]int, n)
	for i := range dp {
		dp[i] = make([]int, n)
		for j := range dp[i] {
			if i == j {
				dp[i][j] = 0
			} else if j == i-1 || j == i+1 {
				dp[i][j] = 1
			} else if (i == x && j == y) || (i == y && j == x) {
				dp[i][j] = 1
			} else {
				dp[i][j] = inf
			}
		}
	}
	for k := 0; k < n; k++ {
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if dp[i][k] == inf || dp[k][j] == inf {
					continue
				}
				dp[i][j] = Min(dp[i][j], dp[i][k]+dp[k][j])
			}
		}
	}
	cnt := make([]int, n)
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			cnt[dp[i][j]]++
		}
	}
	for i := 1; i < n; i++ {
		io.Println(cnt[i])
	}
}

const inf = math.MaxInt64

// Io is I/O object
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

// NewIo returns a new Io instance
func NewIo(r io.Reader, w io.Writer) *Io {
	return &Io{
		reader: bufio.NewReader(r),
		writer: bufio.NewWriter(w),
	}
}

// Flush flushes writer
func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

// NextLine scans a line from stdin
func (io *Io) NextLine() string {
	var buffer []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buffer = append(buffer, line...)
		if !isPrefix {
			break
		}
	}
	return string(buffer)
}

// Next return a word from stdin
func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	r := io.tokens[io.nextToken]
	io.nextToken++
	return r
}

// NextInt returns an integer from stdin
func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

// Println is a wrapper of fmt.Fprintln
func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

// Min returns minimum value of inputs
func Min(a ...int) (min int) {
	for i, ai := range a {
		if i == 0 || ai < min {
			min = ai
		}
	}
	return min
}

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

3 \leq N \leq 2 \times 10^3

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


package main

import (
	"fmt"
	"os"
)

var n, x, y int
var graph [][]int

type Que []int

func (q *Que) push(x int) {
	*q = append(*q, x)
}
func (q *Que) pop() int {
	top := (*q)[0]
	*q = (*q)[1:]
	return top
}

func main() {
	fmt.Scan(&n, &x, &y)
	graph = make([][]int, n)
	// build
	for i := 0; i < n-1; i++ {
		graph[i] = append(graph[i], i+1)
		graph[i+1] = append(graph[i+1], i)
	}
	graph[x-1] = append(graph[x-1], y-1)
	graph[y-1] = append(graph[y-1], x-1)

	dist := make([][]int, n)
	dists := make(map[int]int)
	for i := range dist {
		dist[i] = make([]int, n)
		for j := range dist[i] {
			dist[i][j] = -1
		}
		dist[i][i] = 0
		dists[0] += 2
	}

	for i := range dist {
		que := make(Que, 0)
		que.push(i)
		for len(que) > 0 {
			j := que.pop()
			d := dist[i][j]
			for _, nv := range graph[j] {
				if dist[i][nv] != -1 {
					continue
				}
				dist[i][nv] = d + 1
				dists[d+1]++
				que.push(nv)
			}
		}
	}

	fmt.Fprintln(os.Stderr, dist)
	for k := 1; k < n; k++ {
		fmt.Println(dists[k] / 2)
	}
}


package main

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

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

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

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

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
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
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	n := scanInt()
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		a, b := scanInt()-1, scanInt()-1
		g[a] = append(g[a], b)
		g[b] = append(g[b], a)
	}

	dp := make([]int, n)
	cnt := make([]int, n)
	par := make([]int, n)
	factorialInit(n)
	memo := make([]int, 0, n)

	var dfs func(v, p int) int
	dfs = func(v, p int) int {
		memo = append(memo, v)
		par[v] = p
		dp[v] = 1
		for _, to := range g[v] {
			if to == p {
				continue
			}
			c := dfs(to, v)
			cnt[v] += c
			dp[v] = (dp[v] * dp[to]) % mod
			dp[v] = (dp[v] * ifac[c]) % mod
		}
		dp[v] = (dp[v] * fac[cnt[v]]) % mod

		cnt[v]++
		return cnt[v]
	}

	dfs(0, 0)

	ans := make([]int, n)
	dp2 := make([]int, n)
	dp2[0] = 1
	ans[0] = dp[0]

	for i := 1; i < n; i++ {
		v := memo[i]
		p := par[v]
		dp2[v] = dp[p] * ifac[cnt[p]-1] % mod * fac[cnt[v]] % mod * fac[n-1-cnt[v]] % mod * ifac[n-cnt[p]] % mod
		dp2[v] = div(dp2[v], dp[v]) * dp2[p] % mod
		ans[v] = dp[v] * dp2[v] % mod * ifac[cnt[v]-1] % mod * fac[n-1] % mod * ifac[n-cnt[v]] % mod
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(wr, ans[i])
	}

}

const mod = 1000000007

var fac, ifac, inv []int

func factorialInit(n int) {
	fac = make([]int, n+1)
	ifac = make([]int, n+1)
	inv = make([]int, n+1)

	fac[0], fac[1] = 1, 1
	ifac[0], ifac[1] = 1, 1
	inv[1] = 1

	for i := 1; i < n; i++ {
		fac[i+1] = fac[i] * (i + 1) % mod
		inv[i+1] = mod - inv[mod%(i+1)]*(mod/(i+1))%mod
		ifac[i+1] = ifac[i] * inv[i+1] % mod
	}
}

func div(b, c int) int {
	a, p, x, u := c, mod, 1, 0
	for p != 0 {
		t := a / p
		a, p = p, a-t*p
		x, u = u, x-t*u
	}
	res := b * x % mod
	if res < 0 {
		res += mod
	}
	return res
}

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
For each k=1, ..., N, solve the problem below:

Consider writing a number on each vertex in the tree in the following manner:

First, write 1 on Vertex k.

Then, for each of the numbers 2, ..., N in this order, write the number on the vertex chosen as follows:

Choose a vertex that still does not have a number written on it and is adjacent to a vertex with a number already written on it. If there are multiple such vertices, choose one of them at random.

Find the number of ways in which we can write the numbers on the vertices, modulo (10^9+7).

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For each k=1, 2, ..., N in this order, print a line containing the answer to the problem.

Sample Input 1

3
1 2
1 3

Sample Output 1

2
1
1

The graph in this input is as follows:

For k=1, there are two ways in which we can write the numbers on the vertices, as follows:

Writing 1, 2, 3 on Vertex 1, 2, 3, respectively

Writing 1, 3, 2 on Vertex 1, 2, 3, respectively

Sample Input 2

2
1 2

Sample Output 2

1
1

The graph in this input is as follows:

Sample Input 3

5
1 2
2 3
3 4
3 5

Sample Output 3

2
8
12
3
3

The graph in this input is as follows:

Sample Input 4

8
1 2
2 3
3 4
3 5
3 6
6 7
6 8

Sample Output 4

40
280
840
120
120
504
72
72

The graph in this input is as follows:


package main

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

func mul(x int) int {
	ans := 1
	for i := 1; i <= 2; i++ {
		ans *= i
	}
	return ans
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	N, err := strconv.Atoi(scanner.Text())
	if err != nil {
		log.Fatal(err)
	}

	scanner.Scan()
	M, err := strconv.Atoi(scanner.Text())
	if err != nil {
		log.Fatal(err)
	}

	// 組み合わせ問題 Nの中から2つのパターンとMの中から2つのパターン
	answer := ((N * (N - 1)) / mul(N)) + ((M * (M - 1)) / mul(M))

	fmt.Println(answer)
}

Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


package main

import (
	"fmt"
)

func main()  {
	var input1 int
	var input2 int

	_, _ = fmt.Scan(&input1, &input2)

	ans := input1 * (input1 - 1 ) /2 + input2 * (input2 - 1) / 2

	fmt.Println(ans)
}


package main

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

func main() {
	N, M := sc.nextInt(), sc.nextInt()
	even := (N*N - N) / 2
	var odd int
	if M >= 2 {
		odd = (M*M - M) / 2
	}
	fmt.Println(odd + even)
}

var (
	sc scanner
)

func init() {
	sc = scanner{
		buf: make([]string, 0, 0),
		cur: 0,
		r:   bufio.NewReader(os.Stdin),
	}
}

type scanner struct {
	buf []string
	cur int
	r   *bufio.Reader
}

func (s *scanner) readln() {
	rbuf := make([]byte, 0, 0)
	for {
		line, prefix, err := s.r.ReadLine()
		if err != nil {
			panic(err)
		}
		rbuf = append(rbuf, line...)
		if prefix == false {
			break
		}
	}
	s.cur = 0
	s.buf = strings.Split(*(*string)(unsafe.Pointer(&rbuf)), " ")
}
func (s *scanner) isFull() bool {
	if s.cur == len(s.buf) {
		return true
	}
	return false
}
func (s *scanner) resetCur() {
	s.cur = 0
}
func (s *scanner) next() string {
	if s.cur == 0 {
		s.readln()
	}
	res := s.buf[s.cur]
	s.cur++
	if s.isFull() {
		s.resetCur()
	}
	return res
}
func (s *scanner) nexts() []string {
	s.readln()
	s.resetCur()
	return s.buf
}
func (s *scanner) nextInt() int {
	if s.cur == 0 {
		s.readln()
	}
	res, _ := strconv.Atoi(s.buf[s.cur])
	s.cur++
	if s.isFull() {
		s.resetCur()
	}
	return res
}
func (s *scanner) nextInts() []int {
	s.readln()
	res := make([]int, len(s.buf))
	for i := range s.buf {
		res[i], _ = strconv.Atoi(s.buf[i])
	}
	s.resetCur()
	return res
}
func (s *scanner) nextFloat() float64 {
	if s.cur == 0 {
		s.readln()
	}
	res, _ := strconv.ParseFloat(s.buf[s.cur],
		64)
	s.cur++
	if s.isFull() {
		s.resetCur()
	}
	return res
}
func (s *scanner) nextFloats() []float64 {
	s.readln()
	res := make([]float64, len(s.buf))
	for i := range s.buf {
		res[i], _ = strconv.ParseFloat(s.buf[i],
			64)
	}
	s.resetCur()
	return res
}
func digits(x int) int {
	return len(strconv.Itoa(x))
}
func powInt(x, p int) (result int) {
	result = 1
	for i := 0; i < p; i++ {
		result *= x
	}
	return
}
func max(x, y int) int {
	return int(math.Max(float64(x), float64(y)))
}
func min(x, y int) int {
	return int(math.Min(float64(x), float64(y)))
}
func abs(x int) int {
	return int(math.Abs(float64(x)))
}
func varDump(value ...interface{}) {
	for _, v := range value {
		fmt.Fprintf(os.Stderr, "%#v\n", v)
	}
}
func yes() {
	fmt.Println("Yes")
}
func no() {
	fmt.Println("No")
}

// Pair a
type Pair struct {
	first  int
	second int
}

// Queue a
type Queue struct {
	v []Pair
}

// Push for queue
func (q *Queue) Push(v Pair) {
	q.v = append(q.v, v)
}

// Pop for queue
func (q *Queue) Pop() Pair {
	r := q.v[0]
	q.v = q.v[1:]
	return r
}

// Front for queue
func (q Queue) Front() Pair {
	return q.v[0]
}

// Empty return true if empty
func (q Queue) Empty() bool {
	return len(q.v) == 0
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func readInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

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

func readFloat64() float64 {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return float64(i)
}

func main() {
	sc.Split(bufio.ScanWords)
	s := readString()
	n := len(s)
	s1 := s[0 : (n-1)/2]
	s2 := s[((n+3)/2)-1 : n]

	if isPalindrome(s) && isPalindrome(s1) && isPalindrome(s2) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

func isPalindrome(s string) bool {
	n := len(s)
	for i := 0; i < n; i++ {
		if i == n-i-1 {
			break
		}
		if s[i] != s[n-i-1] {
			return false
		}
	}
	return true
}

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


package main

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

var sc = bufio.NewScanner(os.Stdin)

func Powerset1(nums []int) [][]int {
	if len(nums) == 0 {
		return [][]int{ []int{} }
	}

	length := int(math.Pow(2, float64(len(nums))))
	result := make([][]int, length)

	for i := 0; i < length; i++ {
		bi := i
		s := []int{}
		for _, n := range nums {
			if bi % 2 != 0 {
				s = append(s, n)
			}
			bi = bi / 2

		}
		result[i] = s
	}

	return result
}

func Combination1(nums []int, n int) [][]int {
	ps := Powerset2(nums)
	result := make([][]int, CombinationCount(len(nums), n))
	index := 0
	for _, s := range ps {
		if len(s) == n {
			result[index] = s
			index++
		}
	}
	return result
}

func CombinationCount(n, m int) int {
	return Fact(n) / (Fact(n-m) * Fact(m))
}

func Fact(n int) int {
	if n == 0 {
		return 1
	} else {
		return n * Fact(n-1)
	}
}
func Powerset2(nums []int) [][]int {
	length := int(math.Pow(2, float64(len(nums))))
	result := make([][]int, length)

	index := 0
	result[index] = []int{}
	index++

	for _, n := range nums {
		max := index
		for i := 0; i < max; i++ {
			result[index] = copyAndAppend(result[i], n)
			index++
		}
	}

	return result
}

func copyAndAppend(nums []int, n int) []int {
	dst := make([]int, len(nums)+1)
	copy(dst, nums)
	dst[len(nums)] = n
	return dst
}

func main() {
	sc.Split(bufio.ScanWords)
	L := nextInt()
	a := math.Pow(float64(L) / float64(3), 3)
	fmt.Printf("%.6f\n", a)
	return
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextFloat64() float64 {
	sc.Scan()
	f, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

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

func sumInt(a ...int) int {
	s := 0;
	for _, v := range a {
		s += v
	};
	return s
}
func maxInt(a ...int) int {
	m := a[0];
	for _, x := range a {
		if x > m {
			m = x
		}
	};
	return m
}
func minInt(a ...int) int {
	m := a[0];
	for _, x := range a {
		if x < m {
			m = x
		}
	};
	return m
}

func readInt() (N int) {
	fmt.Scan(&N)
	return
}

func readChar() (s string) {
	fmt.Scan(&s)
	return
}

// 指定の要素数の数列を読み込んでスライスで返す関数
func scanNums(len int) (nums []int) {
	var num int
	for i := 0; i < len; i++ {
		fmt.Scan(&num)
		nums = append(nums, num)
	}
	return
}

// 指定の要素数の文字列群を読み込んでスライスで返す関数
func scanStrings(len int) (strings []string) {
	var str string
	for i := 0; i < len; i++ {
		fmt.Scanf("%s", &str)
		strings = append(strings, str)
	}
	return
}

// 絶対値
func abs(a int) int {
	return int(math.Abs(float64(a)))
}

// ２乗
func pow(p, q int) int {
	return int(math.Pow(float64(p), float64(q)))
}

func min(nums ...int) int {
	if len(nums) == 0 {
		panic("funciton min() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int(math.Min(float64(res), float64(nums[i])))
	}
	return res
}

func minInt64(nums ...int64) int64 {
	if len(nums) == 0 {
		panic("funciton min() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int64(math.Min(float64(res), float64(nums[i])))
	}
	return res
}

func max(nums ...int) int {
	if len(nums) == 0 {
		panic("funciton max() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int(math.Max(float64(res), float64(nums[i])))
	}
	return res
}

// 文字列群aの中に文字列bが含まれるかどうかを判定する。
func strSearch(a []string, b string) bool {
	for i := 0; i < len(a); i++ {
		if a[i] == b {
			return true
		}
	}
	return false
}

// 1行の文字列を入力
// Example:
// -----------------
// hogehoge
// -----------------
func StrStdin() string {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	return strings.TrimSpace(scanner.Text())
}

// 1つの符号付き整数値(64bit)入力
// Exapmle:
// ------------------
// 1000000007
// ------------------
func Int64Stdin() int64 {
	stringInput := StrStdin()
	num, _ := strconv.ParseInt(strings.TrimSpace(stringInput), 10, 64)
	return num
}

// 1つの符号なし整数値(64bit)入力
// Exapmle:
// ------------------
// 1000000007
// ------------------
func Uint64Stdin() uint64 {
	stringInput := StrStdin()
	num, _ := strconv.ParseUint(strings.TrimSpace(stringInput), 10, 64)
	return num
}

// 1つの符号付き浮動小数点数(64bit)の入力
// Exapmle:
// ------------------
// 33.4
// ------------------
func Float64Stdin() float64 {
	stringInput := StrStdin()
	num, _ := strconv.ParseFloat(strings.TrimSpace(stringInput), 64)
	return num
}

// 空白や空文字だけの値を除去したSplit
func SplitWithoutEmpty(stringTargeted string, delim string) []string {
	stringSplited := strings.Split(stringTargeted, delim)
	strs := []string{}

	for _, str := range stringSplited {
		if str != "" {
			strs = append(strs, str)
		}
	}
	return strs
}

// 空白区切りで複数の文字列を1行で入力
// Example:
// --------------------
// hoge fuga hage
// --------------------
func SliceStrsStdin(delim string) []string {
	stringInput := StrStdin()
	return SplitWithoutEmpty(stringInput, delim)
}

// 空白区切りで複数の符号付き整数値(64bit)を1行で入力
// Example:
// --------------------
// 45 -100 23
// --------------------
func SliceInt64Stdin() []int64 {
	stringSplited := SliceStrsStdin(" ")

	int64Slice := []int64{}

	for ni := range stringSplited {
		num, _ := strconv.ParseInt(stringSplited[ni], 10, 64)
		int64Slice = append(int64Slice, num)
	}

	return int64Slice
}

// 空白区切りで複数の符号付き整数値を1行で入力
// Example:
// --------------------
// 45 -100 23
// --------------------
func SliceIntStdin() []int {
	stringSplited := SliceStrsStdin(" ")

	intSlice := []int{}

	for ni := range stringSplited {
		num, _ := strconv.Atoi(stringSplited[ni])
		intSlice = append(intSlice, num)
	}

	return intSlice
}

// 空白区切りで複数の符号なし整数値(64bit)を1行で入力
// Example:
// --------------------
// 43 7 21
// --------------------
func SliceUint64Stdin() []uint64 {
	stringSplited := SliceStrsStdin(" ")

	uint64Slice := []uint64{}

	for ni := range stringSplited {
		num, _ := strconv.ParseUint(stringSplited[ni], 10, 64)
		uint64Slice = append(uint64Slice, num)
	}

	return uint64Slice
}

// 空白区切りで複数の符号付き浮動小数点数(64bit)を入力
// Example:
// --------------------
// 45.0 -62.1 11.7
// --------------------
func SliceFloat64Stdin() []float64 {
	stringSplited := SliceStrsStdin(" ")

	float64Slice := []float64{}

	for ni := range stringSplited {
		num, _ := strconv.ParseFloat(stringSplited[ni], 64)
		float64Slice = append(float64Slice, num)
	}

	return float64Slice
}

Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


package main

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

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

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)
	defer out.Flush() // !!!!caution!!!! you must use Fprint(out, ) not Print()
	/* --- code --- */
	// ベースがあってそこから引いていけると気づけば勝てる
	N := nextInt()
	A := nextInts(N)
	T, D := make(map[int]int), make(map[int]int)

	for _, a := range A {
		if _, ok := T[a]; ok {
			T[a] += 1
		} else {
			T[a] = 1
		}
	}
	ans := 0
	for k, v := range T {
		if _, ok := D[k]; !ok {
			D[k] = ncr(v, 2)
			ans += D[k]
		}
	}
	for _, a := range A {
		fmt.Fprintln(out, ans-(T[a]-1))
	}
}

// -*-*-*-*-*-*-*-*-
// * I/O utilities *
// -*-*-*-*-*-*-*-*-

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

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

func nextFloat64() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

func chars(s string) []string {
	ret := make([]string, len([]rune(s)))
	for i, v := range []rune(s) {
		ret[i] = string(v)
	}
	return ret
}

func PrintOut(src interface{}, joinner string) {
	switch reflect.TypeOf(src).Kind() {
	case reflect.Slice:
		s := reflect.ValueOf(src)
		for idx := 0; idx < s.Len(); idx++ {
			fmt.Fprintf(out, "%v", s.Index(idx))
			if idx < s.Len()-1 {
				fmt.Fprintf(out, "%s", joinner)
			} else {
				fmt.Fprintln(out)
			}
		}
	default:
		fmt.Fprintln(out, "fuck")
	}
}

// -*-*-*-*-*-*-*-
// *  Structures *
// -*-*-*-*-*-*-*-

// Queue ... only for integers
type Queue struct {
	queue []int
}

// Enqueue ... the so-called enqueue
func (q *Queue) Enqueue(x int) {
	q.queue = append(q.queue, x)
}

// Dequeue ... the so-called dequeue
func (q *Queue) Dequeue() (ret int) {
	ret, q.queue = q.queue[0], q.queue[1:]
	return
}

// Len ... return length of queue
func (q *Queue) Len() (ret int) {
	return len(q.queue)
}

// -*-*-*-*-*-*-*-*-
// * tool snippets *
// -*-*-*-*-*-*-*-*-
func duplicate2Int(base [][]int) (ret [][]int) {
	ret = make([][]int, len(base))
	for i, v := range base {
		ret[i] = append([]int{}, v...)
	}
	return
}

func min(a, b int) int {
	x, y := toFloat64(a), toFloat64(b)
	if x < y {
		return a
	} else {
		return b
	}
}

func max(a, b int) int {
	x, y := toFloat64(a), toFloat64(b)
	if x > y {
		return a
	} else {
		return b
	}
}

func toFloat64(v interface{}) float64 {
	r := reflect.ValueOf(v)
	if r.IsValid() {
		switch r.Kind() {
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
			reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
			reflect.Float32, reflect.Float64:
			var x float64
			return r.Convert(reflect.TypeOf(x)).Interface().(float64)
		default:
			return 0

		}
	}
	return 0
}

// -*-*-*-*-*-*-*-*-*-*-*-*-*-
// * Algorithms Utility Zone *
// -*-*-*-*-*-*-*-*-*-*-*-*-*-

// -*-*-*-*-*-*-*-
// * 1. nibutan  *
// -*-*-*-*-*-*-*-
func lower_bound(arr []int, target int) int {
	l, r := 0, len(arr)
	for l < r {
		mid := (l + r) / 2
		if arr[mid] < target {
			l = mid + 1
		} else {
			r = mid
		}
	}
	return l
}

func upper_bound(arr []int, target int) int {
	l, r := 0, len(arr)
	for l < r {
		mid := (l + r) / 2
		if target < arr[mid] {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l
}

// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
// * math flavor typical theories *
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
func gcd(a, b int) int {
	if a > b {
		return gcd(b, a)
	}
	for a != 0 {
		a, b = b%a, a
	}
	return b
}

func pow(a, b int) (ret int) {
	ret = a
	// 10^2 = 100ってことは10に10を1回掛けることだね
	// なので初期値を含めると上限b-1未満
	for i := 0; i < b-1; i++ {
		ret *= a
	}
	return
}

func powMod(n, m, mod int) (ret int) {
	ret = 1
	for m > 0 {
		if m&1 == 1 {
			ret *= n
			ret %= mod
		}
		n *= n
		n %= mod
		m >>= 1
	}
	return ret
}

func ncr(n, r int) int {
	// せいぜいn<10^2くらいの精度しかなくない？
	res := 1
	for i := 1; i <= r; i++ {
		res = res * (n - i + 1) / i
	}
	return res
}

func ncrMod(n, r, mod int) int {
	// 呼び出すたびにテーブルを作るのは愚です（どうしようかね）
	_n := 1000000
	g1 := make([]int, _n+1)
	g1[0], g1[1] = 1, 1
	g2 := make([]int, _n+1)
	g2[0], g2[1] = 1, 1
	inverse := make([]int, _n+1)
	inverse[0], inverse[1] = 0, 1
	for i := 2; i <= _n; i++ {
		g1[i] = (g1[i-1] * i) % mod
		inverse[i] = mod - inverse[mod%i]*(mod/i)%mod
		g2[i] = (g2[i-1] * inverse[i]) % mod
	}

	return g1[n] * (g2[r] * g2[n-r] % mod) % mod
}

func next_permutation(arr []int) func() []int {
	/*
		how to use it:
			this is a generator, so should be invoked such as below example.

			"""code"""
			np := next_permutation(arr)
			for{
				lis := np()
				if len(lis) == 0{
					break
				}
				fmt.Println(lis)
			}
			"""code end"""
	*/
	first := true
	ret := append([]int{}, arr...)
	_next_permutation := func() []int {
		if first {
			first = false
			return arr
		}
		n := len(ret)
		for i := n - 2; i >= 0; i-- {
			if ret[i] < ret[i+1] {
				j := n
				for {
					j -= 1
					if ret[i] < ret[j] {
						break
					}
				}
				ret[i], ret[j] = ret[j], ret[i]
				for k := n - 1; i < k; i, k = i+1, k-1 {
					ret[i+1], ret[k] = ret[k], ret[i+1]
				}
				return ret
			}
		}
		return []int{}
	}
	return _next_permutation
}

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

3 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


package main

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

type combMemo map[int]int

var (
	readString func() string
	combs      = make(combMemo)
)

func init() {
	readString = newReadString(os.Stdin)
}

func comb(m, n int) int {
	if m > n && n > 0 {
		c, ok := combs[m]
		if ok {
			return c
		}
		combs[m] = comb(m-1, n-1) + comb(m-1, n)
		return combs[m]
	}
	return 1
}

func main() {
	n := readInt()
	a_c := make(map[int]int, n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		in := readInt()
		a[i] = in
		a_c[in] = a_c[in] + 1
	}

	for i := range a {
		c := 0
		for k := range a_c {
			if k == a[i] {
				ex := a_c[k] - 1
				if ex < 2 {
					continue
				}
				c = c + comb(ex, 2)
				continue
			}
			in := a_c[k]
			if in < 2 {
				continue
			}
			c = c + comb(in, 2)
		}
		fmt.Println(c)
	}
}

/*-------------------inputs-------------------*/

func readInt() int {
	return int(readInt64())
}

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

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

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)

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

/*-------------------utilities-------------------*/

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

func subL(list []int) []int {
	sl := make([]int, len(list)-1)
	for i := range list {
		if i == len(list)-1 {
			continue
		}
		sub := list[i+1] - list[i]
		sl[i] = sub
	}
	return sl
}

func Combinations(n, k int) [][]int {
	combins := binomial(n, k)
	data := make([][]int, combins)
	if len(data) == 0 {
		return data
	}
	data[0] = make([]int, k)
	for i := range data[0] {
		data[0][i] = i
	}
	for i := 1; i < combins; i++ {
		next := make([]int, k)
		copy(next, data[i-1])
		nextCombination(next, n, k)
		data[i] = next
	}
	return data
}

func nextCombination(s []int, n, k int) {
	for j := k - 1; j >= 0; j-- {
		if s[j] == n+j-k {
			continue
		}
		s[j]++
		for l := j + 1; l < k; l++ {
			s[l] = s[j] + l - j
		}
		break
	}
}

func binomial(n, k int) int {
	errNegInput := "combin: negative input"
	badSetSize := "combin: n < k"
	if n < 0 || k < 0 {
		panic(errNegInput)
	}
	if n < k {
		panic(badSetSize)
	}
	if k > n/2 {
		k = n - k
	}
	b := 1
	for i := 1; i <= k; i++ {
		b = (n - k + i) * b / i
	}
	return b
}


package main

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

var isProduct = false

var memo = map[int]int{}

func getTotal(t int) int {

	if t < 2 {
		return 0
	}
	if t == 2 {
		return 1
	}

	if _, ok := memo[t]; !ok {
		memo[t] = getTotal(t-1) * (t / (t - 2))
	}

	return memo[t]
}

func main() {
	nextReader = newScanner()

	N := nextInt()
	A := nextInts(N)

	d := map[int]int{}

	for _, a := range A {
		d[a]++
	}

	for i := 0; i < N; i++ {
		s := A[i]
		t := 0

		for key, count := range d {
			if key == s {
				c := count - 1
				if c < 1 {
					continue
				}
				t += getTotal(c)
			} else {
				t += getTotal(count)
			}
		}

		fmt.Println(t)
	}

}

/* --------------------------------------------------------------------------
 *  sort
 * ------------------------------------------------------------------------- */

// type S struct {
//   sort int
// }
// type Sorter []S

// func (a Sorter) Len() int           { return len(a) }
// func (a Sorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
// func (a Sorter) Less(i, j int) bool { return a[i].sort < a[j].sort }

// type Sorter []Sortable

// func (a Sorter) Len() int           { return len(a) }
// func (a Sorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
// func (a Sorter) Less(i, j int) bool { return a[i].Axis < a[j].Axis }

/* --------------------------------------------------------------------------
 *  math
 * ------------------------------------------------------------------------- */
func lcm(x, y int) int {
	return x * y / gcd(x, y)
}

func gcd(x, y int) int {
	tmp := x % y
	if tmp > 0 {
		return gcd(y, tmp)
	} else {
		return y
	}
}

func abs(i int) int {
	return int(math.Abs(float64(i)))
}

//prime
type PrimeGenerator struct {
	Next func() uint32
}

func NewPrimeGenerator() *PrimeGenerator {
	primeGenerator := PrimeGenerator{}

	multiples := map[uint32]uint32{}
	var num uint32
	var d uint32 // ←ココ
	primeGenerator.Next = func() uint32 {
		if num == 0 {
			num = 1
			return 2
		}
		if d == 0 { // ←ココ
			d = 4
			return 3
		}

		for {
			num += d
			d = 6 - d
			var k uint32 = 2 // ←ココ

			factor, hasFactor := multiples[num]
			if hasFactor {
				delete(multiples, num)
				// ↓ココ
				if (num+factor)%3 == 0 {
					k = 1
				}
			} else {
				factor = num
			}

			// ↓ココ
			for newNum := num + (factor << k); ; newNum += factor << k {
				_, hasNewFactor := multiples[newNum]
				if !hasNewFactor {
					multiples[newNum] = factor
					break
				}
				k ^= 3 // ←ココ
			}

			if !hasFactor {
				return num
			}
		}
	}

	return &primeGenerator
}

/* --------------------------------------------------------------------------
 *  debug
 * ------------------------------------------------------------------------- */
func debugln(s ...interface{}) {
	if !isProduct {
		fmt.Println(s...)
	}
}

func debug(s ...interface{}) {
	if !isProduct {
		fmt.Print(s...)
	}
}
func debugf(f string, s ...interface{}) {
	if !isProduct {
		fmt.Printf(f, s...)
	}
}

/* --------------------------------------------------------------------------
 *  std.io
 * ------------------------------------------------------------------------- */
var nextReader func() string

func newScanner() func() string {
	r := bufio.NewScanner(os.Stdin)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)
	return func() string {
		r.Scan()
		return r.Text()
	}
}
func nextString() string {
	return nextReader()
}
func nextInt64() int64 {
	v, _ := strconv.ParseInt(nextReader(), 10, 64)
	return v
}
func nextInt() int {
	v, _ := strconv.Atoi(nextReader())
	return v
}
func nextInts(n int) []int {
	r := make([]int, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt()
	}
	return r
}
func nextInt64s(n int) []int64 {
	r := make([]int64, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt64()
	}
	return r
}
func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(nextReader(), 64)
	return f
}

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

/* --------------------------------------------------------------------------
 *  memo
 * ------------------------------------------------------------------------- */


package main

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

var sc = bufio.NewScanner(os.Stdin)
var buff []byte

func init() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(buff, bufio.MaxScanTokenSize*1024)
	log.SetFlags(log.Lshortfile)
}

func main() {
	N := nextInt()
	A := nextInts(N)
	cmb := NewCombination()
	cmb.build()
	cnt := map[int]int{}
	ans := make([]int, N)
	for _, a := range A {
		cnt[a]++
	}
	for k := 0; k < N; k++ {
		cnt[A[k]] -= 1
		for _, v := range cnt {
			ans[k] += cmb.c(v, 2)
		}
		cnt[A[k]] += 1
	}
	for k := 0; k < N; k++ {
		fmt.Println(ans[k])
	}
}

type Combination struct {
	max, mod int
	fact     []int
	factInv  []int
	inv      []int
}

func NewCombination() *Combination {
	c := &Combination{}
	c.max = 21000
	c.mod = 2147483647
	c.fact = make([]int, c.max)
	c.factInv = make([]int, c.max)
	c.inv = make([]int, c.max)
	return c
}

func (c *Combination) build() {
	c.inv[1], c.fact[0], c.factInv[0] = 1, 1, 1
	var i int
	for i = 2; i < c.max; i++ {
		c.inv[i] = c.inv[c.mod%i] * (c.mod - c.mod/i) % c.mod
	}
	for i = 1; i < c.max; i++ {
		c.fact[i] = c.fact[i-1] * i % c.mod
		c.factInv[i] = c.factInv[i-1] * c.inv[i] % c.mod
	}
}

func (c *Combination) c(n, k int) int {
	if n < 0 || k < 0 || n < k {
		return 0
	}
	return c.fact[n] * c.factInv[k] % c.mod * c.factInv[n-k] % c.mod
}

func (c *Combination) p(n, k int) int {
	if k < 0 || k > n {
		return 0
	}
	return c.fact[n] * c.factInv[n-k] % c.mod
}

func (c *Combination) h(n, k int) int {
	if n == 0 && k == 0 {
		return 1
	}
	return c.c(n+k-1, k)
}

// c:N 個の整数の中から，相異なる K 個の整数を選ぶパターンの数C(N,K)
// p:N 個の整数の中から，相異なる K 個の整数を選び，順番に並べるパターンの数P(N,K)
// h:N 個の整数の中から，重複を許して K 個の整数を選ぶパターンの数H(N,K)

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

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func nextInts(n int) (r []int) {
	r = make([]int, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt()
	}
	return r
}

var dy = []int{0, 1, 0, -1}
var dx = []int{1, 0, -1, 0}
var MAX = math.MaxInt32

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

func lcm(a, b int) int {
	return a * b / gcd(a, b)
}
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (p Pairs) Len() int {
	return len(p)
}
func (p Pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p Pairs) Less(i, j int) bool {
	if p[i].a == p[j].a {
		return p[i].b < p[j].b
	}
	return p[i].a < p[j].a
}


package main

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

const (
	initialBufSize = 10000
	maxBufSize     = 10000000
)

var (
	sc = func() *bufio.Scanner {
		sc := bufio.NewScanner(os.Stdin)
		buf := make([]byte, initialBufSize)
		sc.Buffer(buf, maxBufSize)
		sc.Split(bufio.ScanWords)
		return sc
	}()
)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextFloat() float64 {
	sc.Scan()
	f, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func main() {
	n := nextInt()
	a := make([]int, n)
	count := make(map[int]int)
	for i := 0; i < n; i++ {
		a[i] = nextInt()
		count[a[i]]++
	}

	combs := make(map[int]int)
	sum := 0
	for k, v := range count {
		comb := v * (v - 1) / 2
		combs[k] = comb
		sum += comb
	}

	for i := 0; i < n; i++ {
		ans := sum - combs[a[i]]
		c := count[a[i]] - 1
		ans += c * (c - 1) / 2
		fmt.Println(ans)
	}
}


Score : 100 points

Problem Statement

Print the K-th element of the following sequence of length 32:

1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51

Constraints

1 \leq K \leq 32

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the K-th element.

Sample Input 1

6

Sample Output 1

2

The 6-th element is 2.

Sample Input 2

27

Sample Output 2

5

The 27-th element is 5.


package main

import (
	"fmt"
)

func main() {
	var H, W int
	var area int
	var ans int

	fmt.Scanf("%d %d", &H, &W)
	area = H * W
	if area%2 == 1 {
		ans = area/2 + 1
	} else if area%2 == 0 {
		ans = area / 2
	}
	fmt.Println(ans)
}

Score : 200 points

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.
There is a bishop at the top-left square on this board.
How many squares can this bishop reach by zero or more movements?

Here the bishop can only move diagonally.
More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

For example, in the following figure, the bishop can move to any of the red squares in one move:

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

The bishop can reach the cyan squares in the following figure:

Sample Input 2

7 3

Sample Output 2

11

The bishop can reach the cyan squares in the following figure:

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000


package main

import "fmt"

var (
	H int
	W int
)

func main() {
	fmt.Scanln(&H, &W)
	multi := H * W
	result := 0
	if multi%2 == 0 {
		result = multi / 2
	} else {
		result = (multi / 2) + 1
	}
	fmt.Println(result)
}


package main

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

const (
	maxBufSize = 1024 * 1024
)

var (
	sc = func() *bufio.Scanner {
		sc := bufio.NewScanner(os.Stdin)
		buf := make([]byte, 1)
		sc.Buffer(buf, maxBufSize)
		sc.Split(bufio.ScanWords)
		return sc
	}()
	out = bufio.NewWriter(os.Stdout)
)

func solve(h int64, w int64) int64 {
	return ((w + 1)/ 2) * ((h + 1) / 2) + (w /2 ) * (h / 2)
}

func main() {
	defer out.Flush()

	h := nextInt()
	w := nextInt()
	res := solve(h, w)
	fmt.Fprintln(out, res)
}

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

func nextStringAsBytes() []byte {
	sc.Scan()
	return []byte(sc.Text())
}

func nextInt() int64 {
	a, _ := strconv.ParseInt(next(), 10, 64)
	return a
}

func nextFloat() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int64) []int64 {
	ret := make([]int64, n)
	for i := int64(0); i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}

func nextFloats(n int64) []float64 {
	ret := make([]float64, n)
	for i := int64(0); i < n; i++ {
		ret[i] = nextFloat()
	}
	return ret
}
func nextStrings(n int64) []string {
	ret := make([]string, n)
	for i := int64(0); i < n; i++ {
		ret[i] = next()
	}
	return ret
}

func containStr(arr []string, target string) bool {
	for _, v := range arr {
		if v == target {
			return true
		}
	}
	return false
}

func PrintOut(src interface{}, joinner string) {
	switch reflect.TypeOf(src).Kind() {
	case reflect.Slice:
		s := reflect.ValueOf(src)
		for idx := 0; idx < s.Len(); idx++ {
			fmt.Fprintf(out, "%v", s.Index(idx))
			if idx < s.Len()-1 {
				fmt.Fprintf(out, "%s", joinner)
			} else {
				fmt.Fprintln(out)
			}
		}
	default:
		fmt.Fprintln(out, "orz")
	}
}

// mapからkeysとvaluesを返す
func splitKeyValue(m map[interface{}]interface{}) (keys, values []interface{}) {
	switch reflect.TypeOf(m).Kind() {
	case reflect.Map:
		s := reflect.ValueOf(m)
		for _, v := range s.MapKeys() {
			keys = append(keys, v)
			values = append(values, s.MapIndex(v))
		}
	default:
		fmt.Fprintln(out, "orz")
	}
	return
}


package main

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

var n int

func dfs(s []byte, x, kind int) {
	if x == n {
		putf("%s\n", string(s))
		return
	}
	for i := 0; i < kind; i++ {
		s[x] = byte('a' + i)
		if i < kind-1 {
			dfs(s, x+1, kind)
		} else {
			dfs(s, x+1, kind+1)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, initialBufSize), maxBufSize)
	defer wt.Flush()

	n = nextInt()

	dfs(make([]byte, n), 0, 1)
}

const (
	initialBufSize = 100000
	maxBufSize     = 10000000
)

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

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

func nextInt() int {
	i, _ := strconv.Atoi(next())
	return i
}

func putf(format string, a ...interface{}) {
	fmt.Fprintf(wt, format, a...)
}

func puts(a ...interface{}) {
	fmt.Fprintln(wt, a...)
}

Score : 400 points

Problem Statement

In this problem, we only consider strings consisting of lowercase English letters.

Strings s and t are said to be isomorphic when the following conditions are satisfied:

|s| = |t| holds.

For every pair i, j, one of the following holds:

s_i = s_j and t_i = t_j.

s_i \neq s_j and t_i \neq t_j.

For example, abcac and zyxzx are isomorphic, while abcac and ppppp are not.

A string s is said to be in normal form when the following condition is satisfied:

For every string t that is isomorphic to s, s \leq t holds. Here \leq denotes lexicographic comparison.

For example, abcac is in normal form, but zyxzx is not since it is isomorphic to abcac, which is lexicographically smaller than zyxzx.

You are given an integer N.
Print all strings of length N that are in normal form, in lexicographically ascending order.

Constraints

1 \leq N \leq 10

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Assume that there are K strings of length N that are in normal form: w_1, \ldots, w_K in lexicographical order.
Output should be in the following format:

w_1
:
w_K

Sample Input 1

1

Sample Output 1

a

Sample Input 2

2

Sample Output 2

aa
ab


package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

type discount struct {
	a     int
	b     int
	price int
}

func main() {
	sc.Split(bufio.ScanWords)
	A, B, M := nextInt(), nextInt(), nextInt()

	a := make([]int, A)
	for i := 0; i < A; i++ {
		a[i] = nextInt()
	}

	b := make([]int, B)
	for i := 0; i < B; i++ {
		b[i] = nextInt()
	}

	m := make([]discount, M)
	for i := 0; i < M; i++ {
		m[i] = discount{nextInt(), nextInt(), nextInt()}
	}

	min_a := getMin(a)
	min_b := getMin(b)

	min := min_a + min_b
	for _, d := range m {
		price := a[d.a-1] + b[d.b-1] - d.price
		if price < min {
			min = price
		}
	}
	fmt.Println(min)
}

func getMin(x []int) int {
	min := 100000
	for _, v := range x {
		if v < min {
			min = v
		}
	}
	return min
}

Score : 200 points

Problem Statement

You are visiting a large electronics store to buy a refrigerator and a microwave.

The store sells A kinds of refrigerators and B kinds of microwaves. The i-th refrigerator ( 1 \le i \le A ) is sold at a_i yen (the currency of Japan), and the j-th microwave ( 1 \le j \le B ) is sold at b_j yen.

You have M discount tickets. With the i-th ticket ( 1 \le i \le M ), you can get a discount of c_i yen from the total price when buying the x_i-th refrigerator and the y_i-th microwave together. Only one ticket can be used at a time.

You are planning to buy one refrigerator and one microwave. Find the minimum amount of money required.

Constraints

All values in input are integers.

1 \le A \le 10^5

1 \le B \le 10^5

1 \le M \le 10^5

1 \le a_i , b_i , c_i \le 10^5

1 \le x_i \le A

1 \le y_i \le B

c_i \le a_{x_i} + b_{y_i}

Input

Input is given from Standard Input in the following format:

A B M
a_1 a_2 ... a_A
b_1 b_2 ... b_B
x_1 y_1 c_1
\vdots
x_M y_M c_M

Output

Print the answer.

Sample Input 1

2 3 1
3 3
3 3 3
1 2 1

Sample Output 1

5

With the ticket, you can get the 1-st refrigerator and the 2-nd microwave for 3+3-1=5 yen.

Sample Input 2

1 1 2
10
10
1 1 5
1 1 10

Sample Output 2

10

Note that you cannot use more than one ticket at a time.

Sample Input 3

2 2 1
3 5
3 5
2 2 2

Sample Output 3

6

You can get the 1-st refrigerator and the 1-st microwave for 6 yen, which is the minimum amount to pay in this case.
Note that using a ticket is optional.


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, 1000000009)
	sc.Split(bufio.ScanWords)
	n, _ := strconv.Atoi(stringLineScan())
	tree := make([][]int, n)
	for i := 0; i < n; i++ {
		t := make([]int, 0)
		tree[i] = t
	}
	for i := 0; i < n-1; i++ {
		a, _ := strconv.Atoi(stringLineScan())
		b, _ := strconv.Atoi(stringLineScan())
		a--
		b--
		tree[a] = append(tree[a], b)
		tree[b] = append(tree[b], a)
	}
	three := make([][]int, n)
	for i := 0; i < n; i++ {
		t := make([]int, 0)
		three[i] = t
	}
	for i := 0; i < n; i++ {
		searched := make([]bool, n)
		match := make([]int, 0)
		searched, match = dfs(i, 0, tree, searched, match)
		three[i] = match
	}
	m1 := make([]int, 0)
	m2 := make([]int, 0)
	m3 := make([]int, 0)
	f1, f2, f3, can := true, true, true, true
	for i := 1; i <= n; i += 3 {
		m1 = append(m1, i)
	}
	for i := 2; i <= n; i += 3 {
		m2 = append(m2, i)
	}
	for i := 3; i <= n; i += 3 {
		m3 = append(m3, i)
	}
	l1, l2, l3 := len(m1), len(m2), len(m3)
	i1, i2, i3 := 0, 0, 0
	ans := make([]int, n)
	ans[0] = m1[i1]
	i1++
	count := 1
	for i, list := range three {
		if ans[i] == 0 {
			if f1 {
				ans[i] = m1[i1]
				i1++
				count++
				if i1 == l1 {
					f1 = false
				}
			} else if f2 {
				ans[i] = m2[i2]
				i2++
				count++
				if i2 == l2 {
					f2 = false
				}
			} else {
				ans[i] = m3[i3]
				i3++
				count++
				if i3 == l3 {
					f3 = false
				}
			}
		}
		z := ans[i] % 3
		for _, v := range list {
			if ans[v] == 0 {
				m := 0
				if f1 && z == 0 {
					m = 1
				} else if f2 && z == 0 {
					m = 2
				} else if f1 && z == 2 {
					m = 1
				} else if f2 && z == 1 {
					m = 2
				} else if f3 {
					m = 3
				}
				if m == 0 {
					can = false
					break
				}
				if m == 1 {
					ans[v] = m1[i1]
					i1++
					count++
					if i1 == l1 {
						f1 = false
					}
				} else if m == 2 {
					ans[v] = m2[i2]
					i2++
					count++
					if i2 == l2 {
						f2 = false
					}
				} else {
					ans[v] = m3[i3]
					i3++
					count++
					if i3 == l3 {
						f3 = false
					}
				}
			} else {
				if (z + ans[v])%3 != 0{
					can = false
					break
				}
			}
		}
		if count == n {
			break
		}
		if !can {
			break
		}
	}
	for i := 0; i < n; i++ {
		if ans[i] == 0 {
			if f1 {
				ans[i] = m1[i1]
				i1++
				count++
				if i1 == l1 {
					f1 = false
				}
			} else if f2 {
				ans[i] = m2[i2]
				i2++
				count++
				if i2 == l2 {
					f2 = false
				}
			} else {
				ans[i] = m3[i3]
				i3++
				count++
				if i3 == l3 {
					f3 = false
				}
			}
		}
	}
	if can {
		fmt.Println(strings.Trim(fmt.Sprint(ans), "[]"))
	} else {
		fmt.Println(-1)
	}
}
func dfs(init int, depth int, tree [][]int, searched []bool, match []int) ([]bool, []int) {
	depth++
	searched[init] = true
	for _, i := range tree[init] {
		if depth == 3 && searched[i] == false {
			match = append(match, i)
			searched[i] = true
		} else if searched[i] == false {
			searched[i] = true
			searched, match = dfs(i, depth, tree, searched, match)
		}
	}
	return searched, match
}

Score : 600 points

Problem Statement

We have a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects Vertex a_i and Vertex b_i.

Takahashi loves the number 3. He is seeking a permutation p_1, p_2, \ldots , p_N of integers from 1 to N satisfying the following condition:

For every pair of vertices (i, j), if the distance between Vertex i and Vertex j is 3, the sum or product of p_i and p_j is a multiple of 3.

Here the distance between Vertex i and Vertex j is the number of edges contained in the shortest path from Vertex i to Vertex j.

Help Takahashi by finding a permutation that satisfies the condition.

Constraints

2\leq N\leq 2\times 10^5

1\leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}

Output

If no permutation satisfies the condition, print -1.

Otherwise, print a permutation satisfying the condition, with space in between.
If there are multiple solutions, you can print any of them.

Sample Input 1

5
1 2
1 3
3 4
3 5

Sample Output 1

1 2 5 4 3

The distance between two vertices is 3 for the two pairs (2, 4) and (2, 5).

p_2 + p_4 = 6

p_2\times p_5 = 6

Thus, this permutation satisfies the condition.


package main
import (
    "fmt"
    )

func main() {
    var s string
    fmt.Scan(&s)
    if s == "AAA" || s == "BBB" {
        fmt.Println("No")
    } else {
        fmt.Println("Yes")
    }
}

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


package main

import (
	"bufio"
	_ "container/heap"
	"fmt"
	"os"
	_ "sort"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

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

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

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

func lcm(a, b int) int {
	return a / gcd(a, b) * b
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
const MOD = int(1e+9) + 7
const INF = 1 << 60

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	defer wtr.Flush()
	S := sc.nextStr()
	if S[0] == S[1] && S[1] == S[2] {
		fmt.Fprintln(wtr, "No")
	} else {
		fmt.Fprintln(wtr, "Yes")
	}
}


package main
import ("fmt")

func main(){

  var a int;
  var b int;
  var c int;

  var ret int;

  var fl int;

  fmt.Scanf("%d %d %d", &a, &b, &c );

  for; fl <=a ;{
      for i :=0; i<b; i++{
          fl+=1
          ret+=1
          if fl==a{
              break
          }
      }
      for j :=0; j<c; j++{

           fl+=1
          if fl==a{
              break
          }
      }

      if fl == a{
           fmt.Println(ret);
          break
      }
  }

  //for ind, c:= range X{
   //     if ind == a{
  //          break;
  //      }
  //      if c == 'A'{
   //       ret += 1;
  //      }
 // }

}

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


package main

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

func main() {
	var input = irLine(1)
	var A, B = float64(input[0][0]), float64(input[0][1])

	var a int = int(A / 8.00 * 100.00)

	// fmt.Printf("A:%f B:%f a:%d\n", A, B, a)

	var b int = a

	for i := 0; ; b-- {
		if int(float64(b)/100.0*10.0) < int(B) {
			if i > 0 {
				b++
			}
			break
		}
		i++
	}

	if int(float64(b)*8.0/100.0) != int(A) {
		fmt.Printf("-1")
		return
	}

	fmt.Printf("%d", b)
	return
}

func minf(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

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

func paw(f float64, num int) float64 {
	var result float64
	for i := 0; num > i; i++ {
		result *= result
	}
	return result
}

//StrStdin Read standard input data
func StrStdin(line int) [][]string {
	var stringInput [][]string
	scanner := bufio.NewScanner(os.Stdin)

	for i := 0; i < line; i++ {
		scanner.Scan()
		if s := scanner.Text(); s != "" {
			stringInput = append(stringInput, strings.Split(strings.TrimSpace((scanner.Text())), " "))
		} else {
			break
		}
	}

	return stringInput
}

//rLine Read standard input data
func rLine(line int) [][]string {
	var rdr = bufio.NewReaderSize(os.Stdin, 1000000)
	var buf = make([]byte, 0, 1000000)
	var stringInput [][]string

	for i := 0; i < line; {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			stringInput = append(stringInput, strings.Split(strings.TrimSpace((string(buf))), " "))
			buf = []byte{}
			i++
		}
	}
	return stringInput
}

func irLine(i int) [][]int {
	return alitoas(rLine(i))
}

//itoa converts string to integer
func itoa(s string) int {
	var i, _ = strconv.Atoi(s)
	return i
}

func itoas(S []string) []int {
	var result = make([]int, len(S))
	for i, s := range S {
		result[i] = itoa(s)
	}
	return result
}

func alitoas(S [][]string) [][]int {
	var result = make([][]int, len(S))
	for i, s := range S {
		result[i] = itoas(s)
	}
	return result
}

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

func gcd3(a, b, c int) int {
	return gcd(gcd(a, b), c)
}

Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.

When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.

When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.


package main

import (
	"fmt"
)

func main() {

	var A, B int
	fmt.Scan(&A, &B)

	for ans := 1; ans <= 10000; ans++ {
		if (ans*108-((ans*108)%100)-ans*100 == A*100) && (ans*110-((ans*110)%100)-ans*100 == B*100) {
			fmt.Println(ans)
			return
		}
	}
	fmt.Println(-1)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, 1000000009)
	sc.Split(bufio.ScanWords)
	a, _ := strconv.Atoi(stringLineScan())
	b, _ := strconv.Atoi(stringLineScan())
	ans := -1
	for i := 1; i <= 100000; i++ {
		x := i * 8 / 100
		y := i * 10 / 100
		if x == a && y == b {
			ans = i
			break
		}
	}
	fmt.Println(ans)
}


package main

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

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

func main() {
	sc.Split(bufio.ScanWords)
	defer out.Flush() // !!!!coution!!!! you must use Fprint(out, ) not Print()
	/* --- code --- */
	s := next()
	initS := s
	q := nextInt()
	reverse := false
	head := make([]byte, q)
	tail := make([]byte, q)
	for i := 0; i < q; i++ {
		query := nextInt()
		if query == 1 {
			reverse = !reverse
		} else {
			f := nextInt()
			c := next()
			if (f == 1 && !reverse) || (f == 2 && reverse) {
				// s = c + s
				head = append(head, []byte(c)...)
			} else {
				// s = s + c
				tail = append(tail, []byte(c)...)
			}
		}
	}
	if reverse {
		// s = reverseStr(s)
		swap(tail)
		// fmt.Println(s)
		fmt.Println(string(tail) + initS + string(head))
	} else {
		swap(head)
		// fmt.Println(s)
		fmt.Println(string(head) + initS + string(tail))
	}

}

func swap(arr []byte) {
	for i := 0; i < len(arr)/2; i++ {
		arr[i], arr[len(arr)-1-i] = arr[len(arr)-1-i], arr[i]
	}
}

func reverseStr(s string) string {
	rs := []rune(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		rs[i], rs[j] = rs[j], rs[i]
	}
	return string(rs)
}

// -*-*-*-*-*-*-*-*-
// * I/O utilities *
// -*-*-*-*-*-*-*-*-

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

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

func nextInt64() int64 {
	a, _ := strconv.ParseInt(next(), 10, 64)
	return a
}

func nextFloat64() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

func split(s string) []string {
	ret := make([]string, len([]rune(s)))
	for i, v := range []rune(s) {
		ret[i] = string(v)
	}
	return ret
}

// -*-*-*-*-*-*-*-*-
// * tool snippets *
// -*-*-*-*-*-*-*-*-
func duplicate2Int(base [][]int) (ret [][]int) {
	ret = make([][]int, len(base))
	for i, v := range base {
		ret[i] = append([]int{}, v...)
	}
	return
}

func max(a, b int) int {
	return int(max64(int64(a), int64(b)))
}

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

func min(a, b int) int {
	return int(min64(int64(a), int64(b)))
}

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

// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
// * math flavor typical theories *
// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
func gcd(a, b int) int {
	if a > b {
		return gcd(b, a)
	}
	for a != 0 {
		a, b = b%a, a
	}
	return b
}

func pow(a, b int) (ret int) {
	ret = a
	// 10^2 = 100ってことは10に10を1回掛けることだね
	// なので初期値を含めると上限b-1未満
	for i := 0; i < b-1; i++ {
		ret *= a
	}
	return
}

func powMod(n, m, mod int) (ret int) {
	ret = 1
	for m > 0 {
		if m&1 == 1 {
			ret *= n
			ret %= mod
		}
		n *= n
		n %= mod
		m >>= 1
	}
	return ret
}

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


package main

import (
	"fmt"
)

func main() {
	var S string
	fmt.Scan(&S)
	center := make([]string, len(S))
	for i, c := range []rune(S) {
		center[i] = string(c)
	}
	var Q int
	fmt.Scan(&Q)
	pre, epi := []string{""}, []string{""}
	reverseCount := 0
	for i := 0; i < Q; i++ {
		var T int
		fmt.Scan(&T)
		if T == 1 {
			pre, epi = epi, pre
			reverseCount += 1
		} else {
			var F int
			var C string
			fmt.Scan(&F, &C)
			if F == 1 {
				if reverseCount%2 == 0 {
					pre = append([]string{C}, pre...)
				} else {
					epi = append(epi, C)
				}
			} else {
				if reverseCount%2 == 0 {
					pre = append(pre, C)
				} else {
					epi = append([]string{C}, epi...)
				}
			}
		}
	}
	if reverseCount%2 == 1 {
		// reverseAny(pre)
		// reverseAny(center)
		// reverseAny(epi)
		pre = oldSkoolFuckinAdHocReverseForAtCoderGolangVer(pre)
		center = oldSkoolFuckinAdHocReverseForAtCoderGolangVer(center)
		epi = oldSkoolFuckinAdHocReverseForAtCoderGolangVer(epi)
	}
	ans := ""
	for _, p := range pre {
		ans += p
	}
	for _, c := range center {
		ans += c
	}
	for _, e := range epi {
		ans += e
	}
	fmt.Println(ans)
}

func oldSkoolFuckinAdHocReverseForAtCoderGolangVer(arr []string) []string {
	ret := make([]string, len(arr))
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		ret[i], ret[j] = arr[j], arr[i]
	}
	return ret
}

// func reverseAny(slice interface{}) {
// 	n := reflect.ValueOf(slice).Len()
// 	swap := reflect.Swapper(slice)
// 	for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
// 		swap(i, j)
// 	}
// }


package main

import (
	"fmt"
)

// +=での文字列連結はパフォーマンス的にあまりよろしくないらしい
// https://qiita.com/ruiu/items/2bb83b29baeae2433a79
// https://qiita.com/ono_matope/items/d5e70d8a9ff2b54d5c37
// https://drken1215.hatenablog.com/entry/2020/03/08/144800
// いくつか別解があるらしい
func main() {
	var S, t, l string
	var Q, rev int
	fmt.Scan(&S, &Q)
	for i := 0; i < Q; i++ {
		var T int
		fmt.Scan(&T)
		switch T {
		case 1:
			// 0と1を反転させる
			rev = 1 - rev
		case 2:
			var F int
			var C string
			fmt.Scan(&F, &C)
			F--
			if rev == 1 {
				F = 1 - F
			}
			if F == 0 {
				t = C + t
			} else {
				l = t + C
			}
		}
	}
	if rev == 1 {
		fmt.Println(Reverse(l) + Reverse(S) + Reverse(t))
	} else {
		fmt.Println(t + S + l)
	}
}

func Reverse(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}


package main

import "fmt"

// O(n)
func reverse(s string) string {
	ans := ""
	for i := len(s) - 1; i >= 0; i-- {
		ans += string(s[i])
	}
	return ans
}

func main() {
	var s, c string
	var q, t, f int
	var isReverse bool = false
	fmt.Scan(&s)
	fmt.Scan(&q)
	for i := 1; i <= q; i++ {
		fmt.Scan(&t)
		switch t {
		case 1:
			if isReverse {
				isReverse = false
			} else {
				isReverse = true
			}
		case 2:
			fmt.Scan(&f, &c)
			switch f {
			case 1:
				if isReverse {
					s = s + c
				} else {
					s = c + s
				}
			case 2:
				if isReverse {
					s = c + s
				} else {
					s = s + c
				}
			}
		}
	}
	for isReverse {
		reverse(s)
	}
	fmt.Println(s)
}


package main

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

var scanner = bufio.NewScanner(os.Stdin)

func scanInt() int {
	scanner.Scan()
	a, err := strconv.Atoi(scanner.Text())
	if err != nil {
		fmt.Println(err)
	}
	return a
}

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

func scanByte() byte {
	scanner.Scan()
	return []byte(scanner.Text())[0]
}

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

	s := scanText()

	space := make([]byte, 5*int(math.Pow(10, 5)))
	si := 2 * int(math.Pow(10, 5))
	ei := si + len(s)
	for i := si; i < ei; i++ {
		space[i] = s[i-si]
	}

	b := false

	q := scanInt()
	for i := 0; i < q; i++ {
		t := scanInt()
		if t == 1 {
			b = !b
		} else { // t == 2
			f := scanInt()
			c := scanByte()
			if f == 1 && !b || f == 2 && b { // 先頭
				si--
				space[si] = c
			} else {
				ei++
				space[ei-1] = c
			}
		}
	}

	if !b {
		fmt.Println(string(space[si:ei]))

	} else {
		sub := space[si:ei]
		for left, right := 0, len(sub)-1; left < right; left, right = left+1, right-1 {
			sub[left], sub[right] = sub[right], sub[left]
		}
		fmt.Println(string(sub))
	}
}


package main

import (
	"fmt"
)

func main() {
	rev := false
	s1 := make([]rune, 300000)
	s2 := make([]rune, 200000)

	var s string
	fmt.Scanf("%s", &s)
	r := []rune(s)
	for i := 0; i < len(r); i++ {
		s1[i] = r[i]
	}
	len1, len2 := len(s), 0

	var Q int
	fmt.Scanf("%d", &Q)
	for Q > 0 {
		Q--
		var t int
		fmt.Scanf("%d", &t)
		if t == 1 {
			rev = !rev
			continue
		}
		var f int
		var c string
		fmt.Scanf("%d %s", &f, &c)
		if (f == 1 && rev) || (f == 2 && !rev) {
			s1[len1] = []rune(c)[0]
			len1++
		} else {
			s2[len2] = []rune(c)[0]
			len2++
		}
	}
	s1 = s1[:len1]
	s2 = s2[:len2]
	if rev {
		s1, s2 = s2, s1
	}
	fmt.Printf("%s\n", string(append(reverse(s2), s1...)))
}

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


package main

import (
        "fmt"
        "strconv"
        "strings"
)

func calc(p int, s string, c map[string]bool) bool {
        t := strings.TrimLeft(s, "0")
        r, ok := c[t]
        if ok {
                return r
        }
        if len(t) < 10 {
                v, _ := strconv.Atoi(t)
                c[t] = int(v)%p == 0
                return c[t]
        }
        v, _ := strconv.Atoi(t[0:10])
        s = s[10:]
        u := v % p
        for len(s) > 0 {
                v, _ := strconv.Atoi(t[0:1])
                u = (u*10 + v) % p
                s = s[1:]
        }
        c[t] = u == 0
        return c[t]

}
func main() {
        var N, P int
        fmt.Scanf("%d %d", &N, &P)
        lp := len(fmt.Sprintf("%d", P))
        c := map[string]bool{}
        var S string
        fmt.Scanf("%s", &S)
        ret := 0
        for i := 0; i <= N-lp; i++ {
                for j := i + lp - 1; j <= N; j++ {
                        if calc(P, S[i:j], c) {
                                ret += 1
                        }
                }
        }
        fmt.Println(ret)
}

Score : 500 points

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

Compute this count to help Takahashi.

Constraints

1 \leq N \leq 2 \times 10^5

S consists of digits.

|S| = N

2 \leq P \leq 10000

P is a prime number.

Input

Input is given from Standard Input in the following format:

N P
S

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

Sample Input 1

4 3
3543

Sample Output 1

6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

3: divisible by 3.

35: not divisible by 3.

354: divisible by 3.

3543: divisible by 3.

5: not divisible by 3.

54: divisible by 3.

543: divisible by 3.

4: not divisible by 3.

43: not divisible by 3.

3: divisible by 3.

Six of these are divisible by 3, so print 6.

Sample Input 2

4 2
2020

Sample Output 2

10

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

Note that substrings beginning with a 0 also count.

Sample Input 3

20 11
33883322005544116655

Sample Output 3

68


package main

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

var sc *bufio.Scanner

func init() {
	// 100MB
	bufsize := 100 * 1024 * 1024
	sc = bufio.NewScanner(os.Stdin)
	sc.Buffer(make([]byte, bufio.MaxScanTokenSize), bufsize)
	sc.Split(bufio.ScanWords)
}
func readInt() int              { return readIntArray(1)[0] }
func readInt2() (int, int)      { a := readIntArray(2); return a[0], a[1] }
func readInt3() (int, int, int) { a := readIntArray(3); return a[0], a[1], a[2] }
func readIntArray(n int) []int {
	s := readStringArray(n)
	a := make([]int, n)
	for i := range a {
		a[i], _ = strconv.Atoi(s[i])
	}
	return a
}
func readString() string            { return readStringArray(1)[0] }
func readString2() (string, string) { a := readStringArray(2); return a[0], a[1] }
func readStringArray(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		a[i] = sc.Text()
	}
	return a
}
func max(ns ...int) int {
	max := ns[0]
	for _, n := range ns {
		if n > max {
			max = n
		}
	}
	return max
}
func min(ns ...int) int {
	min := ns[0]
	for _, n := range ns {
		if n < min {
			min = n
		}
	}
	return min
}
func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func main() {
	A := make([][]int, 3)
	for i := range A {
		a1, a2, a3 := readInt3()
		A[i] = []int{a1, a2, a3}
	}
	success := [][]int{
		A[0], A[1], A[2],
		[]int{A[0][0], A[1][0], A[2][0]},
		[]int{A[0][1], A[1][1], A[2][1]},
		[]int{A[0][2], A[1][2], A[2][2]},
		[]int{A[0][0], A[1][1], A[2][2]},
		[]int{A[0][2], A[1][1], A[2][0]},
	}

	N := readInt()
	b := make([]int, N)
	for i := 0; i < N; i++ {
		b[i] = readInt()
	}

	for i := range success {
		if check(success[i], b) {
			fmt.Println("Yes")
			return
		}
	}
	fmt.Println("No")

}

func check(l []int, b []int) bool {
	m := make(map[int]struct{})
	for i := range b {
		m[b[i]] = struct{}{}
	}

	for i := range l {
		if _, ok := m[l[i]]; !ok {
			return false
		}
	}

	return true
}

Score : 200 points

Problem Statement

We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

Constraints

All values in input are integers.

1 \leq A_{i, j} \leq 100

A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2))

1 \leq N \leq 10

1 \leq b_i \leq 100

b_i \neq b_j (i \neq j)

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3}
A_{2, 1} A_{2, 2} A_{2, 3}
A_{3, 1} A_{3, 2} A_{3, 3}
N
b_1
\vdots
b_N

Output

If we will have a bingo, print Yes; otherwise, print No.

Sample Input 1

84 97 66
79 89 11
61 59 7
7
89
7
87
79
24
84
30

Sample Output 1

Yes

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

Sample Input 2

41 7 46
26 89 2
78 92 8
5
6
45
16
57
17

Sample Output 2

No

We will mark nothing.

Sample Input 3

60 88 34
92 41 43
65 73 48
10
60
43
88
11
48
73
65
41
92
34

Sample Output 3

Yes

We will mark all the squares.


package main

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

// input scanner
var sc = bufio.NewScanner(os.Stdin)

const (
	initialBufSize = 10e4
	maxBufSize     = 10e6
)

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

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

func nextInt64() int64 {
	a, _ := strconv.ParseInt(next(), 10, 64)
	return a
}

func nextFloat64() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

// util
func max(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n > ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

func min(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n < ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

func sameUnion(g [][]int, a int) map[int]struct{} {
	set := make(map[int]struct{}, len(g))
	st := make([]int, 0, len(g))
	st = append(st, a)
	for len(st) > 0 {
		current := st[0]
		st = st[1:]
		set[current] = struct{}{}
		for _, next := range g[current] {
			_, exist := set[next]
			if !exist {
				st = append([]int{next}, st...)
			}
		}
	}
	return set
}

// main
func main() {
	buf := make([]byte, initialBufSize)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords)
	n := nextInt()
	g := make([][]int, n)
	bg := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, 0, n)
		bg[i] = make([]int, 0, n)
	}
	m := nextInt()
	k := nextInt()
	for i := 0; i < m; i++ {
		ai := nextInt()
		bi := nextInt()
		g[ai-1] = append(g[ai-1], bi-1)
		g[bi-1] = append(g[bi-1], ai-1)
	}
	for i := 0; i < k; i++ {
		ci := nextInt()
		di := nextInt()
		bg[ci-1] = append(bg[ci-1], di-1)
		bg[di-1] = append(bg[di-1], ci-1)
	}
	// fmt.Println(g)
	// fmt.Println(bg)
	ans := make([]string, n)
	for i := 0; i < n; i++ {
		u := sameUnion(g, i)
		// fmt.Println(u)
		var fcount int
		for _, x := range g[i] {
			_, exist := u[x]
			if exist {
				fcount++
			}
		}
		var bcount int
		for _, x := range bg[i] {
			_, exist := u[x]
			if exist {
				bcount++
			}
		}
		ans[i] = strconv.Itoa(len(u) - fcount - bcount - 1)
	}
	fmt.Println(strings.Join(ans, " "))
}

Score : 400 points

Problem Statement

An SNS has N users - User 1, User 2, \cdots, User N.

Between these N users, there are some relationships - M friendships and K blockships.

For each i = 1, 2, \cdots, M, there is a bidirectional friendship between User A_i and User B_i.

For each i = 1, 2, \cdots, K, there is a bidirectional blockship between User C_i and User D_i.

We define User a to be a friend candidate for User b when all of the following four conditions are satisfied:

a \neq b.

There is not a friendship between User a and User b.

There is not a blockship between User a and User b.

There exists a sequence c_0, c_1, c_2, \cdots, c_L consisting of integers between 1 and N (inclusive) such that c_0 = a, c_L = b, and there is a friendship between User c_i and c_{i+1} for each i = 0, 1, \cdots, L - 1.

For each user i = 1, 2, ... N, how many friend candidates does it have?

Constraints

All values in input are integers.

2 ≤ N ≤ 10^5

0 \leq M \leq 10^5

0 \leq K \leq 10^5

1 \leq A_i, B_i \leq N

A_i \neq B_i

1 \leq C_i, D_i \leq N

C_i \neq D_i

(A_i, B_i) \neq (A_j, B_j) (i \neq j)

(A_i, B_i) \neq (B_j, A_j)

(C_i, D_i) \neq (C_j, D_j) (i \neq j)

(C_i, D_i) \neq (D_j, C_j)

(A_i, B_i) \neq (C_j, D_j)

(A_i, B_i) \neq (D_j, C_j)

Input

Input is given from Standard Input in the following format:

N M K
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_K D_K

Output

Print the answers in order, with space in between.

Sample Input 1

4 4 1
2 1
1 3
3 2
3 4
4 1

Sample Output 1

0 1 0 1

There is a friendship between User 2 and 3, and between 3 and 4. Also, there is no friendship or blockship between User 2 and 4. Thus, User 4 is a friend candidate for User 2.

However, neither User 1 or 3 is a friend candidate for User 2, so User 2 has one friend candidate.

Sample Input 2

5 10 0
1 2
1 3
1 4
1 5
3 2
2 4
2 5
4 3
5 3
4 5

Sample Output 2

0 0 0 0 0

Everyone is a friend of everyone else and has no friend candidate.

Sample Input 3

10 9 3
10 1
6 7
8 2
2 5
8 4
7 3
10 9
6 4
5 8
2 6
7 5
3 1

Sample Output 3

1 3 5 4 3 3 3 3 1 0


package main

import (
	"fmt"
)

func main() {
	var sanka, hyouji int
	fmt.Scan(&sanka, &hyouji)
	// fmt.Printf("A %d, B %d\n", a, b)

	if sanka >= 10 {
		fmt.Printf("%d\n", hyouji)
	} else {
		fmt.Printf("%d\n", hyouji-100*(10-sanka))
	}
}

Score : 100 points

Problem Statement

Takahashi is a member of a programming competition site, ButCoder.

Each member of ButCoder is assigned two values: Inner Rating and Displayed Rating.

The Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or more contests. Otherwise, the Displayed Rating will be their Inner Rating minus 100 \times (10 - K) when the member has participated in K contests.

Takahashi has participated in N contests, and his Displayed Rating is R. Find his Inner Rating.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq R \leq 4111

Input

Input is given from Standard Input in the following format:

N R

Output

Print his Inner Rating.

Sample Input 1

2 2919

Sample Output 1

3719

Takahashi has participated in 2 contests, which is less than 10, so his Displayed Rating is his Inner Rating minus 100 \times (10 - 2) = 800.

Thus, Takahashi's Inner Rating is 2919 + 800 = 3719.

Sample Input 2

22 3051

Sample Output 2

3051


package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n int64
	var k int
	fmt.Scan(&n, &k)
	s := strconv.FormatInt(n, k)
	fmt.Println(len(s))
}

Score : 200 points

Problem Statement

Given is an integer N. Find the number of digits that N has in base K.

Notes

For information on base-K representation, see Positional notation - Wikipedia.

Constraints

All values in input are integers.

1 \leq N \leq 10^9

2 \leq K \leq 10

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of digits that N has in base K.

Sample Input 1

11 2

Sample Output 1

4

In binary, 11 is represented as 1011.

Sample Input 2

1010101 10

Sample Output 2

7

Sample Input 3

314159265 3

Sample Output 3

18


package main

import "fmt"

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

	var digit int
	tmp := 1
	for l := 0; ; l++ {
		if n < tmp {
			digit = l + 1
			break
		}
		tmp *= k
	}

	fmt.Print(digit)
}


package main

import (
	"fmt"
  	"math"
)

func main() {
	var n,k float64
	fmt.Scan(&n,&k)

	for i := 0.0; i < n; i++ {
      if n == 1 {
        	fmt.Println(1)
        	return
      } else if n < math.Pow(k, i) {
			fmt.Println(i)
			return
		}
	}
}


package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	xs := make([]int, n)
	for i := range xs {
		fmt.Scan(&xs[i])
	}

	min := math.MaxInt32
	for i := 1; i <= 100; i++ {
		sum := 0
		for _, x := range xs {
			sum += (x - i) * (x - i)
		}
		if sum < min {
			min = sum
		}
	}
	fmt.Println(min)
}

Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354


package main

import (
	"fmt"
	"sort"
)

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

	x := make([]int,n)

	for i,_ := range x {
		fmt.Scan(&x[i])
	}

	sort.Ints(x)

	minSums := 1000000
	for p := x[0]; p <= x[n-1]; p++ {
		sums := 0
		for i := 0; i < n; i++ {
			sums += square(x[i] - p)
		}
		if minSums > sums {
			minSums = sums
		}
	}
	fmt.Println(minSums)
}

func square(x int) int {
	return x * x
}


package main

import (
	"fmt"
	"math"
)

var (
	factorialMemo []int
)

func repeatedSquaring(n, p, m int) int {
	if p == 0 {
		return 1
	}
	if p%2 == 0 {
		t := repeatedSquaring(n, p/2, m)
		return t * t % m
	}
	return n * repeatedSquaring(n, p-1, m) % m
}

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

	divisor := int(math.Pow10(9) + 7)
	all := repeatedSquaring(2, n, divisor) - 1

	for _, k := range []int{a, b} {
		x := 1
		y := 1
		for i := 0; i < k; i++ {
			x = x * (n - i) % divisor
			y = y * (i + 1) % divisor
		}

		r := x * repeatedSquaring(y, divisor-2, divisor) % divisor
		// fmt.Println(all, a, b, x, y, r, divisor)
		all -= r
	}

	if all < 0 {
		all += divisor
	}

	fmt.Println(all)
}

Score : 400 points

Problem Statement

Akari has n kinds of flowers, one of each kind.

She is going to choose one or more of these flowers to make a bouquet.

However, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.

How many different bouquets are there that Akari can make?

Find the count modulo (10^9 + 7).

Here, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.

Constraints

All values in input are integers.

2 \leq n \leq 10^9

1 \leq a < b \leq \textrm{min}(n, 2 \times 10^5)

Input

Input is given from Standard Input in the following format:

n a b

Output

Print the number of bouquets that Akari can make, modulo (10^9 + 7). (If there are no such bouquets, print 0.)

Sample Input 1

4 1 3

Sample Output 1

7

In this case, Akari can choose 2 or 4 flowers to make the bouquet.

There are 6 ways to choose 2 out of the 4 flowers, and 1 way to choose 4, so there are a total of 7 different bouquets that Akari can make.

Sample Input 2

1000000000 141421 173205

Sample Output 2

34076506

Print the count modulo (10^9 + 7).


package main

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

// input scanner
var sc = bufio.NewScanner(os.Stdin)

const (
	initialBufSize = 10e4
	maxBufSize     = 10e6
)

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

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

func nextInt64() int64 {
	a, _ := strconv.ParseInt(next(), 10, 64)
	return a
}

func nextFloat64() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

// util
func max(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n > ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

func min(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n < ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

func mul(a int64, b int64) int64 {
	return ((a * b) % mod)
}

func power(x int64, y int64) int64 {
	if y == 0 {
		return 1
	} else if y%2 == 0 {
		ans := int64(power(x, y/2)) % mod
		ans = mul(ans, ans)
		return ans
	} else {
		ans := int64(power(x, y/2)) % mod
		ans = mul(mul(ans, ans), x)
		return ans
	}
}

var mod = int64(1000000007)

// main
func main() {
	buf := make([]byte, initialBufSize)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords)
	n := nextInt()
	a := nextInt()
	b := nextInt()

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

	// prepare factrorial
	fac[0] = 1
	invfac[0] = 1
	for i := 1; i <= n; i++ {
		fac[i] = (int64(i) * fac[i-1]) % mod
	}
	invfac[n] = power(fac[n], mod-2)
	for i := 1; i < n; i++ {
		invfac[n-i] = (invfac[n-i+1] * int64(n-i+1)) % mod
	}
	var ans int64
	ans = mul(mul(fac[n], invfac[0]), invfac[n])
	for i := 1; i < n/2; i++ {
		c := mul(mul(fac[n], invfac[n-i]), invfac[i])
		ans = (ans + mul(2, c)) % mod
	}
	if n%2 == 0 {
		ans = (ans + mul(mul(fac[n], invfac[n-n/2]), invfac[n/2])) % mod
	}
	ca := mul(mul(fac[n], invfac[n-a]), invfac[a])
	cb := mul(mul(fac[n], invfac[n-b]), invfac[b])
	ans = (ans - ca) % mod
	if ans < 0 {
		ans = ans + mod
	}
	ans = (ans - cb) % mod
	if ans < 0 {
		ans = ans + mod
	}
	fmt.Println(ans)
}