function main()

  S = split(chomp(readline()),"")

  Q = parse(Int,readline())

  for i in 1:Q
    T = split(chomp(readline())," ")
    if T[1] == "1"
      S = reverse(S)
    elseif T[2] == "1"
      unshift!(S,T[3])
    else
      push!(S,T[3])
    end
  end

  println(join(S))

end

main()


function main()
	N,P=map(x->parse(Int,x),split(readline()))
	s=chomp(readline())
	ans=0
	k=1
	now=0
	dp=zeros(Int,P)
	dp[1]=1
	for i=N:-1:1
		t=Int(s[i])-48
		if P==2||P==5
			if t%P==0
				ans+=i
			end
		else
			now=(now+t*k)%P;
			k=k*10%P
			ans+=dp[now+1]
			dp[now+1]+=1
		end
	end
	println(ans)
end
main()

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

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

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.


function main()
	N,P=map(x->parse(Int,x),split(readline()))
	s=chomp(readline())
	ans=0
	k=1
	now=0
	dp=zeros(Int,P)
	dp[1]=1
	for i=N:-1:1
		t=Int(s[i])-48
		if P==2||P==5
			if t%P==0
				ans+=N-i+1
			end
		else
			now=(now+t*k)%P;
			k=k*10%P
			ans+=dp[now+1]
			dp[now+1]+=1
		end
	end
	println(ans)
end
main()


function main()
	N=parse(Int,readline())
	xd=Pair{Int,Int}[]
	for s=readlines()
		x,d=map(x->parse(Int,x),split(s))
		push!(xd,x=>d)
	end
	sort!(xd)
	w=zeros(Int,N+1)
	v=zeros(Int,N+1)
	w[1]=1
	v[1]=2*10^9
	id=1
	r=1
	for (x,d)=reverse(xd)
		while v[id]<x+d
			id-=1
		end
		r=(r+w[id])%998244353
		id+=1
		v[id]=x
		w[id]=r
	end
	println(r)
end
main()

Score : 600 points

Problem Statement

There are N robots numbered 1 to N placed on a number line. Robot i is placed at coordinate X_i. When activated, it will travel the distance of D_i in the positive direction, and then it will be removed from the number line. All the robots move at the same speed, and their sizes are ignorable.

Takahashi, who is a mischievous boy, can do the following operation any number of times (possibly zero) as long as there is a robot remaining on the number line.

Choose a robot and activate it. This operation cannot be done when there is a robot moving.

While Robot i is moving, if it touches another robot j that is remaining in the range [X_i, X_i + D_i) on the number line, Robot j also gets activated and starts moving. This process is repeated recursively.

How many possible sets of robots remaining on the number line are there after Takahashi does the operation some number of times? Compute this count modulo 998244353, since it can be enormous.

Constraints

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

-10^9 \leq X_i \leq 10^9

1 \leq D_i \leq 10^9

X_i \neq X_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 D_1
:
X_N D_N

Output

Print the number of possible sets of robots remaining on the number line, modulo 998244353.

Sample Input 1

2
1 5
3 3

Sample Output 1

3

There are three possible sets of robots remaining on the number line: \{1, 2\}, \{1\}, and \{\}.

These can be achieved as follows:

If Takahashi activates nothing, the robots \{1, 2\} will remain.

If Takahashi activates Robot 1, it will activate Robot 2 while moving, after which there will be no robots on the number line. This state can also be reached by activating Robot 2 and then Robot 1.

If Takahashi activates Robot 2 and finishes doing the operation, the robot \{1\} will remain.

Sample Input 2

3
6 5
-1 10
3 3

Sample Output 2

5

There are five possible sets of robots remaining on the number line: \{1, 2, 3\}, \{1, 2\}, \{2\}, \{2, 3\}, and \{\}.

Sample Input 3

4
7 10
-10 3
4 3
-4 3

Sample Output 3

16

None of the robots influences others.

Sample Input 4

20
-8 1
26 4
0 5
9 1
19 4
22 20
28 27
11 8
-3 20
-25 17
10 4
-18 27
24 28
-11 19
2 27
-2 18
-1 12
-24 29
31 29
29 7

Sample Output 4

110

Remember to print the count modulo 998244353.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function bsearch(a,x);l,r=0,length(a)+1;while r-l>1;m=(r+l)>>1;if a[m]<x;l=m;else;r=m;end;end;l;end

function ftAdd(ft::Array{Int,1},k::Int,x::Int)
	t = k
	n = length(ft)
	while t<=n
		ft[t] = max(ft[t],x)
		t += t&(-t)
	end
	ft
end

function ftMax(ft::Array{Int,1},rr::Int)
	r = rr
	s = -10^9
	while r>0
		s = max(ft[r],s)
		r -= r&(-r)
	end
	s
end

function main()
	p = 998244353
	n = readline() |> parseInt
	a = Tuple{Int,Int}[(0,0) for i in 1:n]
	for i in 1:n
		x,d = readline() |> split |> parseMap
		a[i] = (x,d)
	end
	a = sort(a,by=x->x[1])
	b = Int[a[i][1] for i in 1:n]
	push!(b,10^18)
	dp = zeros(Int,n+1,2)
	dp[n+1,:] = [0,1]
	ft = -10^9*ones(Int,n)
	for i in n:-1:1
		ftAdd(ft,i,a[i][1]+a[i][2])
		j = bsearch(b,a[i][1]+a[i][2])
		x = ftMax(ft,j)
		k = bsearch(b,x)
		ftAdd(ft,i,x)
		dp[i,1] = mod(dp[k+1,1]+dp[k+1,2],p)
		dp[i,2] = mod(dp[i+1,1]+dp[i+1,2],p)
	end
	println(mod(dp[1,1]+dp[1,2],p))
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
N = parse(readline())
println(Int(cld(N,2)))

Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50


function main()

  A = zeros(Int,3,3)
  check = zeros(Int,3,3)

  for i in 1:3
    (A[i,:]) = map(x -> parse(Int,x), split(readline()))
  end

  N = parse(Int,readline())

  for i in 1:N

    b = parse(Int,readline())

    for i in 1:3, j in 1:3
      if A[i,j] == b
        check[i,j] += 1
      end
    end

  end

  if check[1,1] == check[1,2] == check[1,3] == 1 || check[2,1] == check[2,2] == check[2,3] == 1 || check[3,1] == check[3,2] == check[3,3] == 1
    println("Yes")
  elseif check[1,1] == check[2,1] == check[3,1] == 1 || check[1,2] == check[2,2] == check[3,2] == 1 || check[1,3] == check[2,3] == check[3,3] == 1
    println("Yes")
  elseif check[1,1] == check[2,2] == check[3,3] == 1 || check[1,3] == check[2,2] == check[3,1] == 1
    println("Yes")
  else
    println("No")
  end

end

main()

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.


using DataStructures
parseline(str=readline()) = parse.(Int, split(str))
function FriendSuggestions()
    N, M, K = parseline()
    direct = [Int[] for n in 1:N]
    friends = IntDisjointSets(N)
    for m in 1:M
        A, B = parseline()
        union!(friends, A, B)
        push!(direct[A], B)
        push!(direct[B], A)
    end
    for k in 1:K
        C, D = parseline()
        push!(direct[C], D)
        push!(direct[D], C)
    end
    for n in 1:N
        _ = find_root!(friends, n)
    end
    ans = zeros(Int, N)
    p = friends.parents
    for n in 1:N
        ans[n] = length(p[p.==p[n]]) - 1
        for d in direct[n]
            p[n]!=p[d] && continue
            ans[n] -= 1
        end
        println(ans[n], " ")
    end
end
FriendSuggestions()

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


type UnionFind{T}
    parent:: Vector{T}  # parent[root] is the negative of the size

    function UnionFind{T}(nodes::T)
        if nodes <= 0
            throw(ArgumentError("invalid argument for nodes: $nodes"))
        end

        parent = -ones(T, nodes)
        new{T}(parent)
    end
end

UnionFind(nodes::Integer) = UnionFind{typeof(nodes)}(nodes)

function root{T}(uf::UnionFind{T}, x)::T
    if uf.parent[x] < 0
        return x
    else
        # uf.parent[x] = root{T}(uf, uf.parent[x])
        # return uf.parent[x]
        return uf.parent[x] = root(uf, uf.parent[x])
    end
end

function issame{T}(uf::UnionFind{T}, x::T, y::T)
    return root(uf, x) == root(uf, y)
end

function size{T}(uf::UnionFind{T}, x::T)::T
    return -uf.parent[root(uf, x)]
end

function unite!{T}(uf::UnionFind{T}, x::T, y::T)
    x = root(uf, x)
    y = root(uf, y)
    if x == y
        return false
    end
    if uf.parent[x] > uf.parent[y]
        x, y = y, x
    end
    # unite smaller tree(y) to bigger one(x)
    uf.parent[x] += uf.parent[y]
    uf.parent[y] = x
    return true
end

quit() = exit(0)
in_1() = parse(Int, readline())
in_a() = [parse(Int,s) for s in split(readline())]
in_t() = (in_a()...,)
yesno(b) = b ? "Yes" : "No"

function main()
  N, M, K = in_a()
  AB = [in_t() for _ in 1:M]
  CD = [in_t() for _ in 1:K]
  uf = UnionFind(N)
  for (a,b) in AB
    unite!(uf, a, b)
  end
  groupsize = zeros(Int, N)
  for i in 1:N
    groupsize[root(uf, i)] += 1
  end
  adjn = [count(identity, (a==i || b==i for (a,b) in AB)) for i in 1:N]
  map(1:N) do i
    ans = groupsize[root(uf, i)] - 1 - adjn[i]
    ans -= count(identity, (c==i && issame(uf, i, d) for (c, d) in CD))
    ans -= count(identity, (d==i && issame(uf, i, c) for (c, d) in CD))
  end |> arr->join(string.(arr), ' ') |> println
end

isempty(ARGS) && main()


parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function root(par::Vector{Int},x::Int)
    index=par[x]
    while par[index]!=index
        index=par[index]
    end
    return index
end

function unite(par::Vector{Int},siz::Vector{Int},x::Int,y::Int)
    rx,ry=root(par,x),root(par,y)
    if rx==ry
        return 0
    elseif rx>ry
        rx,ry=ry,rx
    end
    par[ry]=rx
    siz[rx]+=siz[ry]
end

function same(par::Vector{Int},x::Int,y::Int)
    return root(par,x)==root(par,y)
end

function main()
    n,m,k=parseMap(split(readline()))
    par=[i for i in 1:n]
    siz=[1 for i in 1:n]
    ans=[-1 for i in 1:n]   # 自分を除いておく

    # 友達候補をUnion-Findで実装
    for i in 1:m
        a,b=parseMap(split(readline()))
        unite(par,siz,a,b)
        ans[a]-=1   # 友達なら候補ではないので-1
        ans[b]-=1   # 友達なら候補ではないので-1
    end

    # ブロック関係の相手を友達関係から除外
    for i in 1:k
        c,d=parseMap(split(readline()))
        if same(par,c,d)
            ans[c]-=1   # 友達候補に含まれていれば除外
            ans[d]-=1   # 友達候補に含まれていれば除外
        end
    end

    for i in 1:n
        ans[i]+=siz[root(par,i)]
    end
    println(join(ans," "))
end
main()


N,M,K=map(x->parse(Int64,x),split(readline()))

Yuko=zeros(Bool,N,N)
Block=zeros(Bool,N,N)

for i in 1:M
    A,B=map(x->parse(Int64,x),split(readline()))
    Yuko[A,B]=true
    Yuko[B,A]=true
end
for i in 1:K
    A,B=map(x->parse(Int64,x),split(readline()))
    Block[A,B]=true
    Block[B,A]=true
end
a2a(V1,V2)=    [(V1[i] || V2[i])  for i in 1:length(V1)]
function abc_d(Yuko,Block)
    Tomo=copy(Yuko)
    for i in 1:N
        tmp=copy(Tomo[i,:])
        flag=true
        while flag
            for j in 1:N
                if Tomo[i,j]==true
                    #println("add",i,",",j)
                    #Tomo[i,:]+=Tomo[j,:]
                    Tomo[i,:]=a2a(Tomo[i,:],Tomo[j,:])
                end
            end
            if tmp == Tomo[i,:]
                flag=false
            end
            tmp=copy(Tomo[i,:])
        end
    end

    cTomo=zeros(Int,N)
    for i in 1:N ,j in 1:N
        if (Tomo[i,j] && (!Yuko[i,j] && !Block[i,j]))
            i!=j && (cTomo[i]+=1)
        end
    end
    cTomo
end

cTomo= abc_d(Yuko,Block)

print(cTomo[1])
for i in 2:N
    print(" ",cTomo[i])
end


type BIT
	n::Int
	bit::Array{Int}
	BIT(n)=new(n,zeros(Int,n))
end
function sum(bit::BIT,i::Int)
	ans=0
	while i>0
		ans+=bit.bit[i]
		i-=i&-i
	end
	ans
end
function add(bit::BIT,i::Int,a::Int)
	while i<=bit.n
		bit.bit[i]+=a
		i+=i&-i
	end
end
function main()
	N=parse(Int,readline())
	S=Char[]
	for c=chomp(readline())
		push!(S,c)
	end
	bits=[BIT(N) for _=1:26]
	for i=1:N
		x=Int(S[i])-96
		add(bits[x],i,1)
	end
	Q=parse(Int,readline())
	for _=1:Q
		qi,a,b=split(readline())
		if qi=="1"
			a=parse(Int,a)
			x=Int(S[a])-96
			add(bits[x],a,-1)
			S[a]=b[1]
			x=Int(S[a])-96
			add(bits[x],a,1)
		else
			a=parse(Int,a)
			b=parse(Int,b)
			cnt=0
			for i=1:26
				if sum(bits[i],b)>sum(bits[i],a-1)
					cnt+=1
				end
			end
			println(cnt)
		end
	end
end
main()

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function ftAdd(ft::Array{Int,1},k::Int,x::Int)
	t = k
	n = length(ft)
	while t<=n
		ft[t] += x
		t += t&(-t)
	end
	ft
end

function ftSum(ft::Array{Int,1},ll::Int,rr::Int)
	l = ll
	r = rr
	s = 0
	while r>0
		s += ft[r]
		r -= r&(-r)
	end
	while l>0
		s -= ft[l]
		l -= l&(-l)
	end
	s
end

function main()
	n = readline() |> parseInt
	s = readline() |> chomp
	t = [Int(s[i])-96 for i in 1:n]
	d = [zeros(Int,n) for i in 1:26]
	for i in 1:n
		ftAdd(d[t[i]],i,1)
	end
	q = readline() |> parseInt
	for iter in 1:q
		qr = readline() |> chomp |> split
		if qr[1][1]=='1'
			k = parseInt(qr[2])
			ftAdd(d[t[k]],k,-1)
			ftAdd(d[Int(qr[3][1])-96],k,1)
			t[k] = Int(qr[3][1])-96
		else
			l = parseInt(qr[2])-1
			r = parseInt(qr[3])
			f = 0
			for i in 1:26
				if ftSum(d[i],l,r)>0
					f += 1
				end
			end
			println(f)
		end
	end
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n,r = readline() |> split |> parseMap
	println(n>=10?r:r+100*(10-n))
end

main()

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


parseInt(x) = parse(Int, x)

function main()
    N, K = parseInt.(split(readline()))
    ans = 0
    while N != 0
        N = div(N, K)
        ans += 1
    end
    println(ans)
    end
main()

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


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n,k=parseMap(split(readline()))
    print(ndigits(n,k))
end
main()


parseInt(x) = parse(Int, x)
n,k = parseInt.(split(readline()))
num = 1
cnt = 0
while num <= n
    global num = num *  k
    global cnt+=1
end
println(cnt)


parseInt(x) = parse(Int, x)
n,k = parseInt.(split(readline()))
num = 1
cnt = 0
while num <= n
    num *= k
    cnt+=1
println(cnt)


quit() = exit(0)
in_1() = parse(Int, readline())
in_a() = [parse(Int,s) for s in split(readline())]
in_t() = (in_a()...,)
yesno(b) = b ? "Yes" : "No"

function main()
  N = in_1()
  X = in_a()
  @assert length(X) == N
  ans = 2^50
  for p in 1:100
    tired = sum((x-p)^2 for x in X)
    ans = min(ans, tired)
  end
  ans |> println
end

isempty(ARGS) && main()

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


parseInt(x) = parse(Int, x)
n = parseInt(readline())
x = parseInt.(split(readline()))

ans = 100000000000000000000
tmp = 0
for i in 1:100
    tmp = 0
    for j in 1:n
        tmp += (x[j] - i)^2
    end
    if tmp < ans
        ans = tmp
    end
end
println(ans)


parseInt(x) = parse(Int, x)
n = parseInt(readline())
x = parseInt.(split(readline()))

ans = 100000000000000000000
tmp = 0
for i in 1:100
    global tmp = 0
    for j in 1:n
        global tmp += (x[j] - i)^2
    end
    if global tmp < global ans
        global ans = global tmp
    end
end
println(ans)


parseInt(x) = parse(Int, x)
n = parseInt(readline())

x = parseInt.(split(readline()))
#K=split(readline())
#x=map(x->parse(Int,x),K)
ans = n * 101^2

for i in 1:100
    sum = 0
    for j in 1:n
        sum += (x[j] - i)^2
    end
    if sum < ans
        ans = sum
    end
end
print(ans)


parseInt(s)=parse(Int,s)
MOD=10^9+7
N,a,b=parseInt.(split(readline()))

f=let
    A=1
    for i in 1:a
        A*=((N-i+1)*invmod(i,MOD))%MOD
        A%=MOD
    end
    B=A
    for i in a+1:b
        B*=((N-i+1)*invmod(i,MOD))%MOD
        B%=MOD
    end
    (powermod(2,N,MOD)-1-A-B+3MOD)%MOD
end

print(f)

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).


parseInt(s)=parse(Int,s)
n,k=parseInt.(split(readline()))
MOD=10^9+7

fac=Array{Int}(2n)
ifac=Array{Int}(2n)
fac[1]=ifac[1]=1

function makefact(n,M=MOD)
    for i in 2:n
        fac[i]=(fac[i-1]*i)%M
        ifac[i]=invmod(fac[i],M)
    end
end

function CMOD(n,k,M=MOD)
    (k==0 || n==k ) && return 1
    return ((fac[n]*ifac[k])%M*ifac[n-k])%M
end
makefact(2n)

s=0
k=min(n-1,k)
for i in 0:k
    s += (CMOD(n,i)*CMOD(n-1,i))%MOD
    s %= MOD
end
print(s)

Score : 500 points

Problem Statement

There is a building with n rooms, numbered 1 to n.

We can move from any room to any other room in the building.

Let us call the following event a move: a person in some room i goes to another room j~ (i \neq j).

Initially, there was one person in each room in the building.

After that, we know that there were exactly k moves happened up to now.

We are interested in the number of people in each of the n rooms now. How many combinations of numbers of people in the n rooms are possible?

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

Constraints

All values in input are integers.

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

2 \leq k \leq 10^9

Input

Input is given from Standard Input in the following format:

n k

Output

Print the number of possible combinations of numbers of people in the n rooms now, modulo (10^9 + 7).

Sample Input 1

3 2

Sample Output 1

10

Let c_1, c_2, and c_3 be the number of people in Room 1, 2, and 3 now, respectively. There are 10 possible combination of (c_1, c_2, c_3):

(0, 0, 3)

(0, 1, 2)

(0, 2, 1)

(0, 3, 0)

(1, 0, 2)

(1, 1, 1)

(1, 2, 0)

(2, 0, 1)

(2, 1, 0)

(3, 0, 0)

For example, (c_1, c_2, c_3) will be (0, 1, 2) if the person in Room 1 goes to Room 2 and then one of the persons in Room 2 goes to Room 3.

Sample Input 2

200000 1000000000

Sample Output 2

607923868

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

Sample Input 3

15 6

Sample Output 3

22583772


parseInt(s)=parse(Int,s)
n,k=parseInt.(split(readline()))
MOD=10^9+7

fac=Array{Int}(2n)
ifac=Array{Int}(2n)
fac[1]=ifac[1]=1

function makefact(n,M=MOD)
    for i in 2:n
        fac[i]=(fac[i-1]*i)%M
        ifac[i]=invmod(fac[i],M)
    end
end

function CMOD(n,k,M=MOD)
    (k==0 || n==k ) && return 1
    return ((fac[n]*ifac[k])%M*ifac[n-k])%M
end
makefact(2n)

s=0
k=min(n-k,k)
for i in 0:k
    s += (CMOD(n,i)*CMOD(n-1,i))%MOD
    s %= MOD
end
print(s)


const n,k=parse.(split(readline()))
const I=Int[]
ans=nc=nmc=1
M=10^9+7
for i=1:min(n-1,k)
  r=i==1 ? 1 : (M-div(M,i))*I[M%i]%M
  push!(I,r)
  nc=nc*(n-i+1)%M*r%M
  nmc=nmc*(n-i)%M*r%M
  ans+=nc*nmc%M
end
println(ans%M)


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	k,q = readline() |> split |> parseMap
	d = readline() |> split |> parseMap
	for query in 1:q
		n,x,m = readline() |> split |> parseMap
		c = [d[i]%m for i in 1:k]
		zc = [0 for i in 1:k]
		if c[1]==0
			zc[1] = 1
		end
		for i in 2:k
			if c[i] == 0
				zc[i] += 1
			end
			c[i] += c[i-1]
			zc[i] += zc[i-1]
		end
		u = div(n-1,k)
		v = (n-1)%k
		os = v==0?div(x%m+u*c[k],m):div(x%m+u*c[k]+c[v],m)
		zs = v==0?u*zc[k]:u*zc[k]+zc[v]
		println(n-1-os-zs)
	end
end

main()

Score : 600 points

Problem Statement

We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}.

Process the following q queries in order:

The i-th query contains three integers n_i, x_i, and m_i.
Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray}
Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i).

Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0).

Constraints

All values in input are integers.

1 \leq k, q \leq 5000

0 \leq d_i \leq 10^9

2 \leq n_i \leq 10^9

0 \leq x_i \leq 10^9

2 \leq m_i \leq 10^9

Input

Input is given from Standard Input in the following format:

k q
d_0 d_1 ... d_{k - 1}
n_1 x_1 m_1
n_2 x_2 m_2
:
n_q x_q m_q

Output

Print q lines.

The i-th line should contain the response to the i-th query.

Sample Input 1

3 1
3 1 4
5 3 2

Sample Output 1

1

For the first query, the sequence {a_j} will be 3,6,7,11,14.

(a_0~\textrm{mod}~2) > (a_1~\textrm{mod}~2)

(a_1~\textrm{mod}~2) < (a_2~\textrm{mod}~2)

(a_2~\textrm{mod}~2) = (a_3~\textrm{mod}~2)

(a_3~\textrm{mod}~2) > (a_4~\textrm{mod}~2)

Thus, the response to this query should be 1.

Sample Input 2

7 3
27 18 28 18 28 46 1000000000
1000000000 1 7
1000000000 2 10
1000000000 3 12

Sample Output 2

224489796
214285714
559523809


function main()

  (A,B,C) = map(x -> parse(Int,x), split(readline()))

  if 3 - length(Set([A,B,C])) == 1
    println("Yes")
  else
    println("No")
  end

end

main()

Score: 100 points

Problem Statement

A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.

You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.

Constraints

A, B, and C are all integers between 1 and 9 (inclusive).

Input

Input is given from Standard Input in the following format:

A B C

Output

If the given triple is poor, print Yes; otherwise, print No.

Sample Input 1

5 7 5

Sample Output 1

Yes

A and C are equal, but B is different from those two numbers, so this triple is poor.

Sample Input 2

4 4 4

Sample Output 2

No

A, B, and C are all equal, so this triple is not poor.

Sample Input 3

4 9 6

Sample Output 3

No

Sample Input 4

3 3 4

Sample Output 4

Yes


readline()
f = 1
for a = split(readline())
    n = parse(Int, a)
    if n % 2 == 0 && n % 3 != 0 && n % 5 != 0
        global f = 0
    end
end

println((f == 1) ? "APPROVED" : "DENIED")

Score: 200 points

Problem Statement

You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.

According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:

All even numbers written on the document are divisible by 3 or 5.

If the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.

Notes

The condition in the statement can be rephrased as "If x is an even number written on the document, x is divisible by 3 or 5".
Here "if" and "or" are logical terms.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

If the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.

Sample Input 1

5
6 7 9 10 31

Sample Output 1

APPROVED

The even numbers written on the document are 6 and 10.

All of them are divisible by 3 or 5, so the immigrant should be allowed entry.

Sample Input 2

3
28 27 24

Sample Output 2

DENIED

28 violates the condition, so the immigrant should not be allowed entry.


readline()
f = 1
for a in split(readline())
    n = parse(Int, a)
    if n % 2 == 0 && n % 3 != 0 && n % 5 != 0
        global f = 0
    end
end

println((f == 1) ? "APPROVED" : "DENIED")


readline()
f=false
for a=split(readline())
  n=parse(Int,a)
  if n%2==0&&n%3!=0&&n%5!=0
    f=true
  end
end
println(f ? "DENIED" : "APPROVED")


getLine
f=0
for a=split(readline())
  n=parse(Int,a)
  if n%2==0&&n%3!=0&&n%5!=0
    f=1
  end
end
println(f ? "DENIED" : "APPROVED")


#!/usr/bin/env -S JULIA_VERSION=0.5.0 julia
# contest: abc155, problem: abc155_c, alphabet: C

function main()
  N = parse(Int, readline())
  dic = Dict{String, Int}()
  maxn = 0
  for i in 1:N
    s = strip(readline())
    n = get(dic, s, 0) + 1
    dic[s] = n
    maxn = max(maxn, n)
  end
  for s in sort([s for (s,n) in dic if n==maxn])
    println(s)
  end
end

if isempty(get(ENV, "ATCODER_LOCAL", ""))
  isempty(ARGS) && main()
end

Score: 300 points

Problem Statement

We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.

Print all strings that are written on the most number of votes, in lexicographical order.

Constraints

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

S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.

The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi


r()=readline()
N=parse(Int,r())
Input=Array(String,N)
for i in 1:N
    Input[i]=chomp(r())
end

maxs=0
koho=Array(String,N)
nkoho=Array(Int,N)
for i in 1:N
    s=Input[i]
    newkoho=true
    for j in  1:maxs
       if koho[j]==s
            nkoho[j]+=1
            newkoho=false
            break
        end
    end
    if newkoho
        maxs+=1
        koho[maxs]=s
        nkoho[maxs]=1
    end
end
koho,nkoho

maxN=maximum(nkoho)
mArray=koho[nkoho.==maxN]
for i in sort(mArray)
    println(i)
end


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    n=parseInt(readline())
    s=sort([readline() for _ in 1:n])
    cnt=Dict{String,Int}()
    for i in s
        try
            cnt[i]+=1
        catch
            cnt[i]=1
        end
    end
    M=0
    for i in values(cnt)
        M=max(M,i)
    end
    st=Vector{String}()
    for i in keys(cnt)
        if cnt[i]==M
            push!(st,i)
        end
    end
    sort!(st)
    for i in st
        println(i)
    end
end
main()


function main()

  N = parse(Int,readline())
  S = chomp.(readlines())

  num_max = 0
  ans = String[]

  dict = Dict{String,Int64}()

  for i in 1:N
    dict[S[i]] = 0
  end

  for i in 1:N
    dict[S[i]] += 1
    num_max = max(num_max, dict[S[i]])
  end

  for i in 1:N
    if dict[S[i]] == num_max && in(S[i],ans) == false
      push!(ans,S[i])
    end
  end

  sort!(ans)

  println.(ans)

end

main()


ParseInt(x) = parse(Int,x)
ParseMap(x::Array{SubString{String},1}) = map(ParseInt,x)
function main()
    a,b = split(readline())
    c,d = ParseMap(split(readline()))
    e = readline()
    if a == e
        c -= 1
    elseif b == e
        d -= 1
    end
println(c," ",d)
end
main()

Score : 100 points

Problem Statement

We have A balls with the string S written on each of them and B balls with the string T written on each of them.

From these balls, Takahashi chooses one with the string U written on it and throws it away.

Find the number of balls with the string S and balls with the string T that we have now.

Constraints

S, T, and U are strings consisting of lowercase English letters.

The lengths of S and T are each between 1 and 10 (inclusive).

S \not= T

S=U or T=U.

1 \leq A,B \leq 10

A and B are integers.

Input

Input is given from Standard Input in the following format:

S T
A B
U

Output

Print the answer, with space in between.

Sample Input 1

red blue
3 4
red

Sample Output 1

2 4

Takahashi chose a ball with red written on it and threw it away.
Now we have two balls with the string S and four balls with the string T.

Sample Input 2

red blue
5 5
blue

Sample Output 2

5 4

Takahashi chose a ball with blue written on it and threw it away.
Now we have five balls with the string S and four balls with the string T.


a,b=split(readline())
n,m=parse.(split(readline()))
s=chomp(readline())
s==a?n-=1:m-=1
print("$n $m")


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    s,t=split(chomp(readline()))
    a,b=parseMap(split(readline()))
    u=chomp(readline())
    a=ifelse(u==s,a-1,a)
    b=ifelse(u==t,b-1,b)
    println("$a $b")
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	s,t = readline() |> chomp |> split
	a,b = readline() |> split |> parseMap
	u = readline() |> chomp
	if u==s
		a-=1
	else
		b-=1
	end
	println(a," ",b)
end

main()


Score : 200 points

Problem Statement

Given is a string S. Replace every character in S with x and print the result.

Constraints

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace every character in S with x and print the result.

Sample Input 1

sardine

Sample Output 1

xxxxxxx

Replacing every character in S with x results in xxxxxxx.

Sample Input 2

xxxx

Sample Output 2

xxxx

Sample Input 3

gone

Sample Output 3

xxxx


n = parse(Int, readline())
v = parse.(Int, split(readline(), ' '))

d = Dict()
for i in v
  d[i] = get(d, i, 0) + 1
  if d[i] == 2
    println("NO")
    exit(0)
  end
end
println("YES")

Score : 300 points

Problem Statement

Given is a sequence of integers A_1, A_2, ..., A_N.
If its elements are pairwise distinct, print YES; otherwise, print NO.

Constraints

2 ≤ N ≤ 200000

1 ≤ A_i ≤ 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

If the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.

Sample Input 1

5
2 6 1 4 5

Sample Output 1

YES

The elements are pairwise distinct.

Sample Input 2

6
4 1 3 1 6 2

Sample Output 2

NO

The second and fourth elements are identical.

Sample Input 3

2
10000000 10000000

Sample Output 3

NO


function main()

  N = parse(Int,readline())
  A = map(x -> parse(Int,x), split(readline()))

  sort!(A)
  check = true

  for i in 1:N-1

    if A[i] == A[i+1]
      println("NO")
      check = false
      break
    end
  end

  if check == true
    println("YES")
  end

end

main()


if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
macro p(z);:((x::Array{SubString{String},1}->parse(Int,x)).(split($z)));end
function main()
  n,k=@p(readline())
  p=(x->(1+x)/2).(@p(readline()))
  sp=Float64[]
  sum=0
  for i=p
    sum+=i
    push!(sp,sum)
  end
  print(k==n ? sp[k] : maximum(push!([sp[i+k]-sp[i] for i=1:n-k],sp[k])))
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Score : 400 points

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Constraints

1 ≤ K ≤ N ≤ 200000

1 ≤ p_i ≤ 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

Sample Input 2

4 1
6 6 6 6

Sample Output 2

3.500000000000

Regardless of which die we choose, the expected value of the number shown is 3.5.

Sample Input 3

10 4
17 13 13 12 15 20 10 13 17 11

Sample Output 3

32.000000000000


N=chomp(readline())
K=parse(Int,readline())
dp=zeros(Int,length(N)+1,2,4)
dp[1,1,1]=1
for i=1:length(N),j=1:2
	lim=j==1 ? Int(N[i])-48 : 9
	for k=1:K+1,l=0:lim
		nk=k+(l!=0)
		nj=j==2||l<lim ? 2 : 1
		if nk<=K+1
			dp[i+1,nj,nk]+=dp[i,j,k]
		end
	end
end
println(dp[length(N)+1,1,K+1]+dp[length(N)+1,2,K+1])

Score : 500 points

Problem Statement

Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.

Constraints

1 \leq N < 10^{100}

1 \leq K \leq 3

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the count.

Sample Input 1

100
1

Sample Output 1

19

The following 19 integers satisfy the condition:

1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100

Sample Input 2

25
2

Sample Output 2

14

The following 14 integers satisfy the condition:

11,12,13,14,15,16,17,18,19,21,22,23,24,25

Sample Input 3

314159
2

Sample Output 3

937

Sample Input 4

9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
3

Sample Output 4

117879300


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function abmp(a,b,p);if b==0;1;elseif b%2==0;k=abmp(a,div(b,2),p);(k*k)%p;else;(a*abmp(a,b-1,p))%p;end;end;;
function binarr(n,p);z=1;for i in 1:n;z=z*i%p;end;fac=Array{Int}(n+1);inv=Array{Int}(n+1);inv[n+1]=abmp(z,p-2,p)
for i in 0:n-1;inv[n-i]=(inv[n+1-i]*(n-i))%p;end;for i in 0:n;fac[i+1]=(((z*inv[i+1])%p)*inv[n+1-i])%p;end;fac;end;
function euc(a,m)
	b = m
	x = 1
	u = 0
	while b>0
		q = div(a,b)
		tmp = u
		u = x-q*u
		x = tmp
		tmp = b
		b = a-q*b
		a = tmp
	end
	x<0?x+abs(m):x
end

function main()
	p = 10^9+7
	r1,c1,r2,c2 = readline() |> split |> parseMap
	ans = 0
	x = binarr(r2+c1,p)[c1+1]
	for i in 1:c2-c1+1
		x = (x*(r2+c1+i)%p)*euc(c1+i,p)%p
		ans = (ans+x)%p
	end
	y = binarr(r1+c1-1,p)[c1+1]
	for i in 1:c2-c1+1
		y = (y*(r1+c1-1+i)%p)*euc(c1+i,p)%p
		ans = mod(ans-y,p)
	end
	println(ans)
end

main()

Score : 600 points

Problem Statement

Snuke is standing on a two-dimensional plane. In one operation, he can move by 1 in the positive x-direction, or move by 1 in the positive y-direction.

Let us define a function f(r, c) as follows:

f(r,c) := (The number of paths from the point (0, 0) to the point (r, c) that Snuke can trace by repeating the operation above)

Given are integers r_1, r_2, c_1, and c_2.
Find the sum of f(i, j) over all pair of integers (i, j) such that r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2, and compute this value modulo (10^9+7).

Constraints

1 ≤ r_1 ≤ r_2 ≤ 10^6

1 ≤ c_1 ≤ c_2 ≤ 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r_1 c_1 r_2 c_2

Output

Print the sum of f(i, j) modulo (10^9+7).

Sample Input 1

1 1 2 2

Sample Output 1

14

For example, there are two paths from the point (0, 0) to the point (1, 1): (0,0) → (0,1) → (1,1) and (0,0) → (1,0) → (1,1), so f(1,1)=2.

Similarly, f(1,2)=3, f(2,1)=3, and f(2,2)=6. Thus, the sum is 14.

Sample Input 2

314 159 2653 589

Sample Output 2

602215194


parseInt(x) = parse(Int, x)
h, a = map(parseInt, split(readline()))
print(ceil(Int, h/a))

Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.

Serval wins when the monster's health becomes 0 or below.

Find the number of attacks Serval needs to make before winning.

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

Print the number of attacks Serval needs to make before winning.

Sample Input 1

10 4

Sample Output 1

3

After one attack, the monster's health will be 6.

After two attacks, the monster's health will be 2.

After three attacks, the monster's health will be -2.

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


function main()
  a,b = parse.(split(readline()))
  println(Int(ceil(a/b)))
end

main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
H,N = parseMap(split(readline()))
A = parseMap(split(readline()))
println(H > sum(A) ? "No":"Yes")
end
main()


H,A = parse.(split(readline()))
println(Int(cld(H,A)))


parseInt(x) = parse(Int,x)
function main()
  a,b = parseInt.(split(readline()))
  if a<=sum(parse.(split(readline())))
    println("Yes")
  else
    println("No")
  end
end

main()

Score : 200 points

Problem Statement

Raccoon is fighting with a monster.

The health of the monster is H.

Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.
There is no other way to decrease the monster's health.

Raccoon wins when the monster's health becomes 0 or below.

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Constraints

1 \leq H \leq 10^9

1 \leq N \leq 10^5

1 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 A_2 ... A_N

Output

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Sample Input 1

10 3
4 5 6

Sample Output 1

Yes

The monster's health will become 0 or below after, for example, using the second and third moves.

Sample Input 2

20 3
4 5 6

Sample Output 2

No

Sample Input 3

210 5
31 41 59 26 53

Sample Output 3

Yes

Sample Input 4

211 5
31 41 59 26 53

Sample Output 4

No


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
H,N = parse.(split(readline()))
println(H > sum(parse.(split(readline()))) ? "No":"Yes")
end
main()


n,k=[parse(Int,i) for i=split(readline())]
l=sort!([parse(Int,i) for i=split(readline())])
print(length(l) <= k ? 0 : sum(l[1:end-k]))

Score : 300 points

Problem Statement

Fennec is fighting with N monsters.

The health of the i-th monster is H_i.

Fennec can do the following two actions:

Attack: Fennec chooses one monster. That monster's health will decrease by 1.

Special Move: Fennec chooses one monster. That monster's health will become 0.

There is no way other than Attack and Special Move to decrease the monsters' health.

Fennec wins when all the monsters' healths become 0 or below.

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

Constraints

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

0 \leq K \leq 2 \times 10^5

1 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 ... H_N

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

Sample Input 1

3 1
4 1 5

Sample Output 1

5

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

Sample Input 2

8 9
7 9 3 2 3 8 4 6

Sample Output 2

0

She can use Special Move on all the monsters.

Sample Input 3

3 0
1000000000 1000000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


function ans(H)

  if H == 1
    return 1
  else
    return 2ans(floor(Int,H/2))+1
  end

end

function main()

  H = parse(Int,readline())

  println(ans(H))

end

main()

Score : 400 points

Problem Statement

Caracal is fighting with a monster.

The health of the monster is H.

Caracal can attack by choosing one monster. When a monster is attacked, depending on that monster's health, the following happens:

If the monster's health is 1, it drops to 0.

If the monster's health, X, is greater than 1, that monster disappears. Then, two new monsters appear, each with the health of \lfloor X/2 \rfloor.

(\lfloor r \rfloor denotes the greatest integer not exceeding r.)

Caracal wins when the healths of all existing monsters become 0 or below.

Find the minimum number of attacks Caracal needs to make before winning.

Constraints

1 \leq H \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H

Output

Find the minimum number of attacks Caracal needs to make before winning.

Sample Input 1

2

Sample Output 1

3

When Caracal attacks the initial monster, it disappears, and two monsters appear, each with the health of 1.

Then, Caracal can attack each of these new monsters once and win with a total of three attacks.

Sample Input 2

4

Sample Output 2

7

Sample Input 3

1000000000000

Sample Output 3

1099511627775


const N,D,A=map(x->parse(Int,x),split(readline()))
const H=Pair{Int,Int}[]
function update(x,h,cnt,ans,id,sz,P)
	while id<sz&&P[id+1].first<x
		cnt-=P[id+1].second
		id+=1
	end
	h-=cnt*A
	if h>0
		t=div(h+A-1,A)
		ans+=t
		cnt+=t
		push!(P,x+D*2=>t)
		sz+=1
	end
	cnt,ans,id,sz
end
function main()
	for i=1:N
		x,h=map(x->parse(Int,x),split(readline()))
		push!(H,x=>h)
	end
	sort!(H)
	cnt=ans=id=sz=0
	P=Pair{Int,Int}[]
	for (x,h)=H
		cnt,ans,id,sz=update(x,h,cnt,ans,id,sz,P)
	end
	println(ans)
end
main()

Score : 600 points

Problem Statement

Silver Fox is fighting with N monsters.

The monsters are standing in a row, and we can assume them to be standing on a number line. The i-th monster, standing at the coordinate X_i, has the health of H_i.

Silver Fox can use bombs to attack the monsters.
Using a bomb at the coordinate x decreases the healths of all monsters between the coordinates x-D and x+D (inclusive) by A.
There is no way other than bombs to decrease the monster's health.

Silver Fox wins when all the monsters' healths become 0 or below.

Find the minimum number of bombs needed to win.

Constraints

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

0 \leq D \leq 10^9

1 \leq A \leq 10^9

0 \leq X_i \leq 10^9

1 \leq H_i \leq 10^9

X_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D A
X_1 H_1
:
X_N H_N

Output

Print the minimum number of bombs needed to win.

Sample Input 1

3 3 2
1 2
5 4
9 2

Sample Output 1

2

First, let us use a bomb at the coordinate 4 to decrease the first and second monsters' health by 2.

Then, use a bomb at the coordinate 6 to decrease the second and third monsters' health by 2.

Now, all the monsters' healths are 0.
We cannot make all the monsters' health drop to 0 or below with just one bomb.

Sample Input 2

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

Sample Output 2

5

We should use five bombs at the coordinate 5.

Sample Input 3

3 0 1
300000000 1000000000
100000000 1000000000
200000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


n, m = parse.(Int, split(readline()))

println( n==m ? "Yes" : "No" )

Score : 100 points

Problem Statement

Takahashi is participating in a programming contest, AXC001. He has just submitted his code to Problem A.

The problem has N test cases, all of which must be passed to get an AC verdict.

Takahashi's submission has passed M cases out of the N test cases.

Determine whether Takahashi's submission gets an AC.

Constraints

1 \leq N \leq 100

0 \leq M \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

If Takahashi's submission gets an AC, print Yes; otherwise, print No.

Sample Input 1

3 3

Sample Output 1

Yes

All three test cases have been passed, so his submission gets an AC.

Sample Input 2

3 2

Sample Output 2

No

Only two out of the three test cases have been passed, so his submission does not get an AC.

Sample Input 3

1 1

Sample Output 3

Yes


parseInt(x) = parse(Int, x)
n,m = parseInt.(split(readline()))
if n == m
    println("Yes")
else
    println("No")
end


a,b=map(x->parse(Int,x),split(readline()))
if a>b
  a,b=b,a
end
for i=1:b
  print(a)
end

Score : 200 points

Problem Statement

Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller?

Constraints

1 \leq a \leq 9

1 \leq b \leq 9

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.)

Sample Input 1

4 3

Sample Output 1

3333

We have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller.

Sample Input 2

7 7

Sample Output 2

7777777


#=
B:
- Julia version:0.5.2
- Author: abap
- Date: 2020-01-12
=#
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
a,b = parse.(split(readline()))

if a<b
    for i in 1:b
    print(a)
    end
else
    for i in 1:a
    print(b)
    end
end

println("")


function parseInt(x)
    parse(Int, x)
end

function main()
    n = parseInt(readline())
    p = parseInt.(split(readline()))
    min = 10 ^ 7
    count = 0
    for pi in p
        if pi < min
            min = pi
            count += 1
        end
    end
    print(count)
end

main()

Score : 300 points

Problem Statement

Given is a permutation P_1, \ldots, P_N of 1, \ldots, N.
Find the number of integers i (1 \leq i \leq N) that satisfy the following condition:

For any integer j (1 \leq j \leq i), P_i \leq P_j.

Constraints

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

P_1, \ldots, P_N is a permutation of 1, \ldots, N.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 ... P_N

Output

Print the number of integers i that satisfy the condition.

Sample Input 1

5
4 2 5 1 3

Sample Output 1

3

i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.

Similarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.

Sample Input 2

4
4 3 2 1

Sample Output 2

4

All integers i (1 \leq i \leq N) satisfy the condition.

Sample Input 3

6
1 2 3 4 5 6

Sample Output 3

1

Only i=1 satisfies the condition.

Sample Input 4

8
5 7 4 2 6 8 1 3

Sample Output 4

4

Sample Input 5

1
1

Sample Output 5

1


function main()

  N = parse(Int,readline())

  P = map(x -> parse(Int,x), split(readline()))

  count = 0

  for i in 1:N
    for j in N:-1:1

    if j > i
      continue
    elseif P[i] > P[j]
      break
    elseif j == 1
      count += 1
    end

  	end
  end

  println(count)

end

main()


function Handstand2()
    N = parse(Int, readline())
    C = zeros(Int, 10, 10)
    for n in 1:N
        s = string(n)
        h, t = parse(Int, s[1]), parse(Int, s[end])
        C[h+1, t+1] += 1
    end
    ans = 0
    for i in 1:10
        for j in 1:10
            ans += C[i, j] * C[j, i]
        end
    end
    println(ans)
end
Handstand2()

Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008


#=
E:
- Julia version:0.5.2
- Author: abap
- Date: 2020-01-12
=#
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
N = parseInt(readline())
A = parseMap(split(readline()))
x = lcm(A)
ans = 0
for i in A
	ans += x ÷ i
end
anser = ans % (10^9 + 7)
println(Int64(anser))

Score : 500 points

Problem Statement

Given are N positive integers A_1,...,A_N.

Consider positive integers B_1, ..., B_N that satisfy the following condition.

Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.

Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.

Since the answer can be enormous, print the sum modulo (10^9 +7).

Constraints

1 \leq N \leq 10^4

1 \leq A_i \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).

Sample Input 1

3
2 3 4

Sample Output 1

13

Let B_1=6, B_2=4, and B_3=3, and the condition will be satisfied.

Sample Input 2

5
12 12 12 12 12

Sample Output 2

5

We can let all B_i be 1.

Sample Input 3

3
1000000 999999 999998

Sample Output 3

996989508

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


#=
B:
- Julia version:0.5.2
- Author: abap
- Date: 2020-01-12
=#
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
N = parseInt(readline())

A = parseMap(split(readline()))
x = lcm(A)
ans = 0
for i in A
	ans += x / i
end
anser = ans % (10^9 + 7)
println(BigInt(anser))


function main()
	mod=10^9+7
	N=parse(Int,readline())
	A=map(x->parse(Int,x),split(readline()))
	ans=0
	for a=A
		ans+=invmod(a,mod)
		ans%=mod
	end
	for i=2:10^3
		c=0
		for j=1:N
			n=0
			while A[j]%i==0
				A[j]=div(A[j],i)
				n+=1
			end
			if c<n
				c=n
			end
		end
		ans=ans*i^c%mod
	end
	sort!(A)
	i=1
	while i<=N&&A[i]==1
		i+=1
	end
	while i<=N
		while i<N&&A[i]==A[i+1]
			i+=1
		end
		ans=ans*A[i]%mod
		i+=1
	end
	println(ans)
end
main()


function main()
  println(readline()+1)
end

main()

Score : 100 points

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


#=
C:
- Julia version:
- Author: abap
- Date: 2020-01-12
=#
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
N,M = parseMap(split(readline()))
problems = 1
wa = 0
ac = 0
for i in 1:M
	p,s = split(readline())
	p = parseInt(p)
	if s == "WA" && problems == p
		wa += 1
	elseif s == "AC" && problems == p
		ac += 1
		problems += 1
	end
end
println("$ac $wa")


N,M=map(x->parse(Int,x),split(readline()))
c=zeros(Int,N)
a=b=0
for s=readlines()
  p,s=split(s)
  p=parse(Int,p)
  if s=="AC"
    if c[p]>=0
      a+=1
      b+=c[p]
      c[p]=-1
    end
  else
    if c[p]>=0
      c[p]+=1
    end
  end
end
println("$a $b")

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0


#=
C:
- Julia version:
- Author: abap
- Date: 2020-01-12
=#
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
N,M = parseMap(split(readline()))
d = Dict()
wa = 0
ac = 0
for i in 1:M
	#println(d)
	p,s = split(readline())
	p = parseInt(p)
	#println("$p $s")
	if !(in(p,keys(d)))
		push!(d,p=>"wa")
	end
	if s == "WA" &&  d[p] == "wa"
		wa += 1
		#print("$(d[p]) なので、wa")
	elseif s == "AC" && d[p] == "wa"
		d[p] = "a"
		#println("ac")
	end
end
for i in values(d)
	if i == "a"
		ac += 1
	end
end
println("$ac $wa")


parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

h,w=parseMap(split(readline()))
s=[chomp(readline()) for i in 1:h]

function dfs(x,y,dis)
    for (i,j) in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]
        if 0<i<w+1 && 0<j<h+1 && s[j][i]!='#'
            if dis[y,x]+1<dis[j,i] || dis[j,i]==-1
                dis[j,i]=dis[y,x]+1
                dfs(i,j,dis)
            end
        end
    end
    return dis
end

function main()
    ans=0
    for i in 1:h
        for j in 1:w
            if s[i][j]=='#'
                continue
            end
            dis=fill(-1,(h,w))
            dis[i,j]=0
            dfs(j,i,dis)
            res=findmax(dis)
            ans=max(ans,res[1])
        end
    end
    println(ans)
end
main()

Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.


parseInt(x) = parse(Int, x)
parseBigInt(x) = parse(BigInt, x)
parseFloat(x) = parse(Float64, x)
parseBigFloat(x) = parse(BigFloat, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

h,w=parseMap(split(readline()))
s=[chomp(readline()) for i in 1:h]
function dfs(x,y,dis)
    if !(0<x<w+1 && 0<y<h+1 && s[y][x]!='#')
        return 0
    end
    for (i,j) in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]
        if 0<i<w+1 && 0<j<h+1 && s[j][i]!='#' && dis[y,x]+1<dis[j,i]
            dis[j,i]=dis[y,x]+1
            dfs(i,j,dis)
        end
    end
    return dis
end
function main()
    ans=0
    for i in 1:h
        for j in 1:w
            dis=fill(10^9,(h,w))
            dis[i,j]=0
            dfs(j,i,dis)
            res=0
            for k in 1:h,l in 1:w
                if dis[k,l]<10^9
                    res=max(res,dis[k,l])
                end
            end
            ans=max(ans,res)
        end
    end
    println(ans)
end
main()


const H,W=map(x->parse(Int,x),split(readline()))
const S=String[]
for i=1:H
	push!(S,chomp(readline()))
end
function calc(sx,sy)
	ret=ones(Int,H,W)*1919810
	ret[sx,sy]=0
	Q=Pair{Int,Int}[sx=>sy]
	while !isempty(Q)
		x,y=shift!(Q)
		for (tx,ty)=[x-1=>y,x+1=>y,x=>y-1,x=>y+1]
			if 1<=tx<=H&&1<=ty<=W&&S[tx][ty]!='#'&&ret[x,y]+1<ret[tx,ty]
				ret[tx,ty]=ret[x,y]+1
				push!(Q,tx=>ty)
			end
		end
	end
	ret
end
function main()
	ans=0
	for i=1:H,j=1:W
		if S[i][j]=='#'
			continue
		end
		d=calc(i,j)
		for I=1:H,J=1:W
			if ans<d[I,J]<1919810
				ans=d[I,J]
			end
		end
	end
	println(ans)
end
main()


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	a = zeros(Int,n,2)
	for i in 1:n
		a[i,:]=readline() |> split |> parseMap
	end
	r = 0
	rp = (0,0)
	for i in 1:n-1
		x1 = a[i,1]
		y1 = a[i,2]
		for j in i+1:n
			x2 = a[j,1]
			y2 = a[j,2]
			v = abs(x2-x1)^2+abs(y2-y1)^2
			if v>r
				r = v
				rp = (i,j)
			end
		end
	end
	f = (a[rp[1],2]-a[rp[2],2])
	g = (-a[rp[1],1]+a[rp[2],1])
	h = (a[rp[2],2]*a[rp[1],1]-a[rp[1],2]*a[rp[2],1])
	q = 0
	qx = 0
	for i in 1:n
		if i!=rp[1]&&i!=rp[2]
			x = a[i,1]
			y = a[i,2]
			v = abs(f*x+g*y+h)
			if v>q
				q = v
				qx = i
			end
    end

	end
	if qx==0
		va = a[rp[1],:]
		vb = a[rp[2],:]
		println(sqrt((va[1]-vb[1])^2+(va[2]-vb[2])^2)*0.5)
	else
		va = a[rp[1],:]
		vb = a[rp[2],:]
		vc = a[qx,:]
		la = sqrt((va[1]-vb[1])^2+(va[2]-vb[2])^2)
		lb = sqrt((vc[1]-vb[1])^2+(vc[2]-vb[2])^2)
		lc = sqrt((va[1]-vc[1])^2+(va[2]-vc[2])^2)
		ss = (la+lb+lc)*0.5
		s = sqrt(ss*(ss-la)*(ss-lb)*(ss-lc))
		ra = acos((lb^2+lc^2-la^2)/(2*lb*lc))
		println(0.5*la/sin(pi-ra))
	end
end

main()

Score : 600 points

Problem Statement

Given are N points (x_i, y_i) in a two-dimensional plane.

Find the minimum radius of a circle such that all the points are inside or on it.

Constraints

2 \leq N \leq 50

0 \leq x_i \leq 1000

0 \leq y_i \leq 1000

The given N points are all different.

The values in input are all integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum radius of a circle such that all the N points are inside or on it.

Your output will be considered correct if the absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

2
0 0
1 0

Sample Output 1

0.500000000000000000

Both points are contained in the circle centered at (0.5,0) with a radius of 0.5.

Sample Input 2

3
0 0
0 1
1 0

Sample Output 2

0.707106781186497524

Sample Input 3

10
10 9
5 9
2 0
0 0
2 7
3 3
2 5
10 0
3 7
1 9

Sample Output 3

6.726812023536805158

If the absolute or relative error from our answer is at most 10^{-6}, the output will be considered correct.


#=
A:
- Julia version:
- Author: abap
- Date: 2020-01-11
=#
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
N = parseInt(readline())
S = []
T = []

for i in 1:N
	s, t = split(readline())
  	t = parse(Int,t)
	push!(S,s)
    push!(T,t)
end
X = readline()
for i in  1:N
	title = S[i]
	if title == X
		global index = i
	end
end
res = 0
for j in index+1:N
	res += T[j]
end

println(res)

Score : 200 points

Problem Statement

Niwango created a playlist of N songs.
The title and the duration of the i-th song are s_i and t_i seconds, respectively.
It is guaranteed that s_1,\ldots,s_N are all distinct.

Niwango was doing some work while playing this playlist. (That is, all the songs were played once, in the order they appear in the playlist, without any pause in between.)
However, he fell asleep during his work, and he woke up after all the songs were played.
According to his record, it turned out that he fell asleep at the very end of the song titled X.

Find the duration of time when some song was played while Niwango was asleep.

Constraints

1 \leq N \leq 50

s_i and X are strings of length between 1 and 100 (inclusive) consisting of lowercase English letters.

s_1,\ldots,s_N are distinct.

There exists an integer i such that s_i = X.

1 \leq t_i \leq 1000

t_i is an integer.

Input

Input is given from Standard Input in the following format:

N
s_1 t_1
\vdots
s_{N} t_N
X

Output

Print the answer.

Sample Input 1

3
dwango 2
sixth 5
prelims 25
dwango

Sample Output 1

30

While Niwango was asleep, two songs were played: sixth and prelims.

The answer is the total duration of these songs, 30.

Sample Input 2

1
abcde 1000
abcde

Sample Output 2

0

No songs were played while Niwango was asleep.

In such a case, the total duration of songs is 0.

Sample Input 3

15
ypnxn 279
kgjgwx 464
qquhuwq 327
rxing 549
pmuduhznoaqu 832
dagktgdarveusju 595
wunfagppcoi 200
dhavrncwfw 720
jpcmigg 658
wrczqxycivdqn 639
mcmkkbnjfeod 992
htqvkgkbhtytsz 130
twflegsjz 467
dswxxrxuzzfhkp 989
szfwtzfpnscgue 958
pmuduhznoaqu

Sample Output 3

6348


(K,X) = map(x -> parse(Int,x),split(readline()))
println(500K>=X ? "Yes":"No")

Score : 100 points

Problem Statement

Takahashi has K 500-yen coins. (Yen is the currency of Japan.)
If these coins add up to X yen or more, print Yes; otherwise, print No.

Constraints

1 \leq K \leq 100

1 \leq X \leq 10^5

Input

Input is given from Standard Input in the following format:

K X

Output

If the coins add up to X yen or more, print Yes; otherwise, print No.

Sample Input 1

2 900

Sample Output 1

Yes

Two 500-yen coins add up to 1000 yen, which is not less than X = 900 yen.

Sample Input 2

1 501

Sample Output 2

No

One 500-yen coin is worth 500 yen, which is less than X = 501 yen.

Sample Input 3

4 2000

Sample Output 3

Yes

Four 500-yen coins add up to 2000 yen, which is not less than X = 2000 yen.


N=parse(readline())
s=chomp(readline())
c=0
for i=3:N
  if s[i-2:i]=="ABC"
    c+=1
  end
end
println(c)

Score : 200 points

Problem Statement

We have a string S of length N consisting of uppercase English letters.

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print number of occurrences of ABC in S as contiguous subsequences.

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


parseline(str=readline()) = parse.(Int, split(str))
function SemiCommonMultiple()
    N, M = parseline()
    A = parseline()
    X = lcm(div.(A, 2))
    println(X>M ? 0 : (M - X)÷2X + 1)
end
SemiCommonMultiple()

Score : 400 points

Problem Statement

Given are a sequence A= {a_1,a_2,......a_N} of N positive even numbers, and an integer M.

Let a semi-common multiple of A be a positive integer X that satisfies the following condition for every k (1 \leq k \leq N):

There exists a non-negative integer p such that X= a_k \times (p+0.5).

Find the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^9

2 \leq a_i \leq 10^9

a_i is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 ... a_N

Output

Print the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Sample Input 1

2 50
6 10

Sample Output 1

2

15 = 6 \times 2.5

15 = 10 \times 1.5

45 = 6 \times 7.5

45 = 10 \times 4.5

Thus, 15 and 45 are semi-common multiples of A. There are no other semi-common multiples of A between 1 and 50, so the answer is 2.

Sample Input 2

3 100
14 22 40

Sample Output 2

0

The answer can be 0.

Sample Input 3

5 1000000000
6 6 2 6 2

Sample Output 3

166666667


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function main()
    s,t=split(readline())
    println("$t$s")
end
main()

Score : 100 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

Constraints

S and T are strings consisting of lowercase English letters.

The lengths of S and T are between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

Print the resulting string.

Sample Input 1

oder atc

Sample Output 1

atcoder

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

Sample Input 2

humu humu

Sample Output 2

humuhumu


function main()

  (S,T) = split(chomp(readline())," ")

  println(T*S)

end

main()


a,b,k=[parse(Int128,i) for i in split(readline())]
if k>a
  a=0
  b=max(0,a+b-k)
else
  a-=k
end
print(a," ",b)

Score : 200 points

Problem Statement

Takahashi has A cookies, and Aoki has B cookies.
Takahashi will do the following action K times:

If Takahashi has one or more cookies, eat one of his cookies.

Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.

If they both have no cookies, do nothing.

In the end, how many cookies will Takahashi and Aoki have, respectively?

Constraints

0 \leq A \leq 10^{12}

0 \leq B \leq 10^{12}

0 \leq K \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the numbers of Takahashi's and Aoki's cookies after K actions.

Sample Input 1

2 3 3

Sample Output 1

0 2

Takahashi will do the following:

He has two cookies, so he eats one of them.

Now he has one cookie left, and he eats it.

Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.

Thus, in the end, Takahashi will have 0 cookies, and Aoki will have 2.

Sample Input 2

500000000000 500000000000 1000000000000

Sample Output 2

0 0

Watch out for overflows.


#=
B:
- Julia version: 0.5.2
- Author: abap
- Date: 2019-12-29
=#
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
A,B,K = parseMap(split(readline()))
for i in 1:K
	if A > 0
		A -= 1
	elseif B != 0
		B -= 1
	end
end
println("$A $B")


N,K=map(x->parse(Int,x),split(readline()))
R,S,P=map(x->parse(Int,x),split(readline()))
C=zeros(Int,N)
s=readline()
ans=0
for i=1:N
	if i<=K||s[i-K]!=s[i]||C[i-K]==0
		C[i]=1
		ans+=s[i]=='r'?P:s[i]=='s'?R:S
	end
end
println(ans)

Score : 400 points

Problem Statement

At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:

The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)

Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):

R points for winning with Rock;

S points for winning with Scissors;

P points for winning with Paper.

However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.)

Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.

The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.

What is the maximum total score earned in the game by adequately choosing the hand to play in each round?

Notes

In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.

If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;

if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;

if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;

if both players play the same hand, it is a draw.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq N-1

1 \leq R,S,P \leq 10^4

N,K,R,S, and P are all integers.

|T| = N

T consists of r, p, and s.

Input

Input is given from Standard Input in the following format:

N K
R S P
T

Output

Print the maximum total score earned in the game.

Sample Input 1

5 2
8 7 6
rsrpr

Sample Output 1

27

The machine will play {Rock, Scissors, Rock, Paper, Rock}.

We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27.

Sample Input 2

7 1
100 10 1
ssssppr

Sample Output 2

211

Sample Input 3

30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr

Sample Output 3

4996


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function abmp(a,b,p)
	if b == 0
		1
	elseif b%2 == 0
		k = abmp(a,div(b,2),p)
		(k*k)%p
	else
		(a*abmp(a,b-1,p))%p
	end
end
function binarr(n,p)
	z = 1
	for i in 1:n
		z = z*i%p
	end
	fac = Array{Int}(n+1)
	inv = Array{Int}(n+1)
	inv[n+1] = abmp(z,p-2,p)
	for i in 0:n-1
		inv[n-i] = (inv[n+1-i]*(n-i))%p
	end
	for i in 0:n
		fac[i+1] = (((z*inv[i+1])%p)*inv[n+1-i])%p
	end
	fac
end

function euc(a,m)
	b = m
	x = 1
	u = 0
	while b>0
		q = div(a,b)
		tmp = u
		u = x-q*u
		x = tmp
		tmp = b
		b = a-q*b
		a = tmp
	end
	x<0?x+abs(m):x
end

function main()
	mod = 10^9+7
	n = readline() |> parseInt
	e = [Int[] for i in 1:n]
	for i in 1:n-1
		a,b = readline() |> split |> parseMap
		push!(e[a],b)
		push!(e[b],a)
	end
	layer = -ones(Int,n)
	layer[1] = 0
	stack = Int[1]
	head = zeros(Int,n)
	while !isempty(stack)
		now = shift!(stack)
		for i in e[now]
			if layer[i]<0
				layer[i] = layer[now] + 1
				push!(stack,i)
			else
				head[now] = i
			end
		end
	end
	ls = Tuple{Int,Int}[(layer[i],i) for i in 1:n]
	ls = sort(ls,by=x->x[1],rev=true)
	d = zeros(Int,n)
	da = [Int[] for i in 1:n]
	for i in 1:n-1
		u = ls[i][2]
		push!(da[head[u]],d[u]+1)
		d[head[u]] += d[u]+1
	end
	ct = zeros(Int,n)
	bt = zeros(Int,n+1)
	bt[1] = 1
	for i in 1:n
		bt[i+1] = (bt[i]*2)%mod
	end
	z = 1
	if length(da[1])>1
		for j in da[1]
			z = (z+bt[j+1]-1)%mod
		end
		ct[1] = (bt[n]-z+mod)%mod
	end
	for i in 2:n
		if !isempty(da[i])
			s = 0
			z = 1
			for j in da[i]
				s += j
				z = (z+bt[j+1]-1)%mod
			end
			z = (z+bt[n-s]-1)%mod
			ct[i] = (bt[n]-z+mod)%mod
		end
	end
	x = euc(bt[n+1],mod)
	println((sum(ct)*x)%mod)
end

main()

Score : 600 points

Problem Statement

Given is a tree T with N vertices. The i-th edge connects Vertex A_i and B_i (1 \leq A_i,B_i \leq N).

Now, each vertex is painted black with probability 1/2 and white with probability 1/2, which is chosen independently from other vertices. Then, let S be the smallest subtree (connected subgraph) of T containing all the vertices painted black. (If no vertex is painted black, S is the empty graph.)

Let the holeyness of S be the number of white vertices contained in S. Find the expected holeyness of S.

Since the answer is a rational number, we ask you to print it \bmod 10^9+7, as described in Notes.

Notes

When you print a rational number, first write it as a fraction \frac{y}{x}, where x, y are integers, and x is not divisible by 10^9 + 7
(under the constraints of the problem, such representation is always possible).

Then, you need to print the only integer z between 0 and 10^9 + 6, inclusive, that satisfies xz \equiv y \pmod{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

Print the expected holeyness of S, \bmod 10^9+7.

Sample Input 1

3
1 2
2 3

Sample Output 1

125000001

If the vertices 1, 2, 3 are painted black, white, black, respectively, the holeyness of S is 1.

Otherwise, the holeyness is 0, so the expected holeyness is 1/8.

Since 8 \times 125000001 \equiv 1 \pmod{10^9+7}, we should print 125000001.

Sample Input 2

4
1 2
2 3
3 4

Sample Output 2

375000003

The expected holeyness is 3/8.

Since 8 \times 375000003 \equiv 3 \pmod{10^9+7}, we should print 375000003.

Sample Input 3

4
1 2
1 3
1 4

Sample Output 3

250000002

The expected holeyness is 1/4.

Sample Input 4

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

Sample Output 4

570312505


function f()
    l = readline()
    N, A, B = parse.(Int, split(l))

    if ((B-A)%2 == 0)
        Nt = Int((B-A)/2)
    else
        Nt = Int(min(A-1,N-B)) + Int((B-A+1)/2)
    end
    println(Nt)
end
f()

Score : 300 points

Problem Statement

2N players are running a competitive table tennis training on N tables numbered from 1 to N.

The training consists of rounds.
In each round, the players form N pairs, one pair per table.
In each pair, competitors play a match against each other.
As a result, one of them wins and the other one loses.

The winner of the match on table X plays on table X-1 in the next round,
except for the winner of the match on table 1 who stays at table 1.

Similarly, the loser of the match on table X plays on table X+1 in the next round,
except for the loser of the match on table N who stays at table N.

Two friends are playing their first round matches on distinct tables A and B.
Let's assume that the friends are strong enough to win or lose any match at will.
What is the smallest number of rounds after which the friends can get to play a match against each other?

Constraints

2 \leq N \leq 10^{18}

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the smallest number of rounds after which the friends can get to play a match against each other.

Sample Input 1

5 2 4

Sample Output 1

1

If the first friend loses their match and the second friend wins their match, they will both move to table 3 and play each other in the next round.

Sample Input 2

5 2 3

Sample Output 2

2

If both friends win two matches in a row, they will both move to table 1.


l = readline()
N, A, B = parse.(Int, split(l))

if ((B-A)%2 == 0)
    Nt = (B-A)/2
else
    Nt = min(A-1,N-B) + (B-A+1)/2
end
println(int(Nt))


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n,m,v,p = readline() |> split |> parseMap
	a = readline() |> split |> parseMap |> sort
	l = 0
	r = n-p+1
	while r-l>1
		c = (l+r)>>1
		if c+p-1>=v&&a[c]+m>=a[n-p+1]
			r = c
		else
			s = 0
			f = 0
			for i in c+1:n-p+1
				if a[c]+m<a[i]
					f = 1
				else
					s += m-a[i]+a[c]
				end
			end
			if s<m*(v-c-p+1)||f==1
				l = c
			else
				r = c
			end
		end
	end
	println(n-l)
end

main()

Score : 700 points

Problem Statement

N problems are proposed for an upcoming contest. Problem i has an initial integer score of A_i points.

M judges are about to vote for problems they like. Each judge will choose exactly V problems, independently from the other judges,
and increase the score of each chosen problem by 1.

After all M judges cast their vote, the problems will be sorted in non-increasing order of score, and the first P problems will be chosen for the problemset.
Problems with the same score can be ordered arbitrarily, this order is decided by the chief judge.

How many problems out of the given N have a chance to be chosen for the problemset?

Constraints

2 \le N \le 10^5

1 \le M \le 10^9

1 \le V \le N - 1

1 \le P \le N - 1

0 \le A_i \le 10^9

Input

Input is given from Standard Input in the following format:

N M V P
A_1 A_2 ... A_N

Output

Print the number of problems that have a chance to be chosen for the problemset.

Sample Input 1

6 1 2 2
2 1 1 3 0 2

Sample Output 1

5

If the only judge votes for problems 2 and 5, the scores will be 2 2 1 3 1 2.
The problemset will consist of problem 4 and one of problems 1, 2, or 6.

If the only judge votes for problems 3 and 4, the scores will be 2 1 2 4 0 2.
The problemset will consist of problem 4 and one of problems 1, 3, or 6.

Thus, problems 1, 2, 3, 4, and 6 have a chance to be chosen for the problemset. On the contrary, there is no way for problem 5 to be chosen.

Sample Input 2

6 1 5 2
2 1 1 3 0 2

Sample Output 2

3

Only problems 1, 4, and 6 have a chance to be chosen.

Sample Input 3

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

Sample Output 3

8


Score : 300 points

Problem Statement

Takahashi is organizing a party.

At the party, each guest will receive one or more snack pieces.

Takahashi predicts that the number of guests at this party will be A or B.

Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.

We assume that a piece cannot be divided and distributed to multiple guests.

Constraints

1 \leq A, B \leq 10^5

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.

Sample Input 1

2 3

Sample Output 1

6

When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests.

Sample Input 2

123 456

Sample Output 2

18696

Sample Input 3

100000 99999

Sample Output 3

9999900000


parseInt(x::String) = parse(Int, x)
parseInt(x::SubString{String}) = parse(Int, x)
parseInt(x::Char) = parse(Int, x)
parseInt(x::Float64) = trunc(Int, x)
parseFloat(x::String) = parse(Float64, x)
parseFloat(x::Int) = trunc(Float64, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
    n=parseInt(readline())
    a=parseMap(split(readline()))
    res=1
    cnt=0
    for i in a
        if i==res
            res+=1
        else
            cnt+=1
        end
    end
    if cnt==n
        println(-1)
    else
        println(cnt)
    end
end
main()

Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.


println(sum(parse.(split(readline())))>21 ? "bust" : "win")

Score : 100 points

Problem Statement

Given are three integers A_1, A_2, and A_3.

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Constraints

1 \leq A_i \leq 13 \ \ (i=1,2,3)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Sample Input 1

5 7 9

Sample Output 1

win

5+7+9=21, so print win.

Sample Input 2

13 7 2

Sample Output 2

bust

13+7+2=22, so print bust.


s = chomp(readline())
cnt = 0
for i in 1:length(s)
    #println(s[i], " ", s[length(s)-i+1])
    if s[i] != s[length(s)-i+1]
        global cnt+=1
    end
end
println(Int(cnt/2))

Score : 200 points

Problem Statement

Takahashi loves palindromes. Non-palindromic strings are unacceptable to him. Each time he hugs a string, he can change one of its characters to any character of his choice.

Given is a string S. Find the minimum number of hugs needed to make S palindromic.

Constraints

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of hugs needed to make S palindromic.

Sample Input 1

redcoder

Sample Output 1

1

For example, we can change the fourth character to o and get a palindrome redooder.

Sample Input 2

vvvvvv

Sample Output 2

0

We might need no hugs at all.

Sample Input 3

abcdabc

Sample Output 3

2


#=
B:
- Julia version: 0.5.2
- Author: abap
- Date: 2019-12-21
=#

S = readline()

len = length(S)

count = 0

for i in  1:div(len,2)
     if S[i] != S[len-i]
         count += 1
      end
end

println(count)


function main()
  n=parse(Int,readline())
  m=fill(-1,n,n)
  for i=1:n
    for j=1:parse(Int,readline())
      a,b=parse.(split(readline()))
      m[i,a]=b
    end
  end
  ans=0
  for i=[[i+1 for i=0:n if b&(1<<i)!=0] for b=0:(1<<n)-1]
    flag=true
    for j=i
      for k=1:n
        if m[j,k] == -1
          continue
        elseif m[j,k] != (k∈i)
          flag=false
        end
      end
    end
    flag ? ans=max(ans,length(i)) : 0
  end
  print(ans)
end
main()

Score : 300 points

Problem Statement

There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not.

Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind.

How many honest persons can be among those N people at most?

Constraints

All values in input are integers.

1 \leq N \leq 15

0 \leq A_i \leq N - 1

1 \leq x_{ij} \leq N

x_{ij} \neq i

x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2)

y_{ij} = 0, 1

Input

Input is given from Standard Input in the following format:

N
A_1
x_{11} y_{11}
x_{12} y_{12}
:
x_{1A_1} y_{1A_1}
A_2
x_{21} y_{21}
x_{22} y_{22}
:
x_{2A_2} y_{2A_2}
:
A_N
x_{N1} y_{N1}
x_{N2} y_{N2}
:
x_{NA_N} y_{NA_N}

Output

Print the maximum possible number of honest persons among the N people.

Sample Input 1

3
1
2 1
1
1 1
1
2 0

Sample Output 1

2

If Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons.

Sample Input 2

3
2
2 1
3 0
2
3 1
1 0
2
1 1
2 0

Sample Output 2

0

Assuming that one or more of them are honest immediately leads to a contradiction.

Sample Input 3

2
1
2 0
1
1 0

Sample Output 3

1


xor(a,b)=a$b
N=parse(Int,readline())
A=zeros(Int,N)
B=zeros(Int,N)
for i=1:N
	a=parse(Int,readline())
	for _=1:a
		x,y=map(x->parse(Int,x),split(readline()))
		if y==1
			A[i]=xor(A[i],2^(x-1))
		else
			B[i]=xor(B[i],2^(x-1))
		end
	end
end
ans=0
for m=0:2^N-1
	t=m
	f=0
	cnt=0
	for i=1:N
		if (m>>(i-1))%2==1
			t=t|A[i]
			f=f|B[i]
			cnt+=1
		end
	end
	if (t&f)==0
		ans=max(ans,cnt)
	end
end
println(ans)


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function carry(x::Array{Int,1},n)
	k = length(x)
	for i in 1:k-1
		if x[k-i+1] > n-1
			x[k-i] += div(x[k-i+1],n)
			x[k-i+1] = x[k-i+1]%n
		end
	end
	if x[1] > n-1
		0
	else
		x
	end
end

function main()
	n = readline() |> parseInt
	a = [zeros(Int,n) for i in 1:n]
	for i in 1:n
		x = readline() |> parseInt
		a[i][i] = 1
		for j in 1:x
			p,q = readline() |> split |> parseMap
			if q == 1
				a[i][p] = 1
			else
				a[i][p] = -1
			end
		end
	end
	ct = 0
	z = zeros(Int,n)
	while z!=0
		f = 0
		ans = z[1]==0?-copy(a[1]):copy(a[1])
		for i in 2:n
			tmp = z[i]==0?-copy(a[i]):copy(a[i])
			for j in 1:n
				if ans[j]==0
					ans[j] = tmp[j]
				elseif tmp[j]!=0&&tmp[j]!=ans[j]
					f = 1
				end
			end
		end
		if f == 0
			ct = max(ct,sum(z))
		end
		z[n] += 1
		z=carry(z,2)
	end
	println(ct)
end

main()


const MOD = 10^9+7

function solve()
    N = parse(Int,readline())
    A = parse.(Int,split(readline()))
    ans = 0
    for i in 0:60
        cnt1 = 0
        for j in 1:N
            (A[j]>>i&1==1) && (cnt1+=1)
        end
        ans += (N-cnt1)cnt1 * mod(2^i,MOD)
        ans %= MOD;
    end
    println(ans)
end
solve()

Score : 400 points

Problem Statement

We have N integers. The i-th integer is A_i.

Find \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

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

0 \leq A_i < 2^{60}

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

Print the value \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

6

We have (1\mbox{ XOR } 2)+(1\mbox{ XOR } 3)+(2\mbox{ XOR } 3)=3+2+1=6.

Sample Input 2

10
3 1 4 1 5 9 2 6 5 3

Sample Output 2

237

Sample Input 3

10
3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820

Sample Output 3

103715602

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


function main()
	H,W=map(x->parse(Int,x),split(readline()))
	A=zeros(Int,H,W)
	for i=1:H
		j=0
		for a=map(x->parse(Int,x),split(readline()))
			A[i,j+=1]=a
		end
	end
	for i=1:H
		j=0
		for a=map(x->parse(Int,x),split(readline()))
			j+=1
			A[i,j]=abs(A[i,j]-a)
		end
	end
	dp=zeros(BigInt,H,W);
	dp[1,1]=big"2"^(A[1,1]+12800);
	for i=1:H,j=1:W
		if i<H
			dp[i+1,j]|=(dp[i,j]<<A[i+1,j])|(dp[i,j]>>A[i+1,j])
		end
		if j<W
			dp[i,j+1]|=(dp[i,j]<<A[i,j+1])|(dp[i,j]>>A[i,j+1])
		end
	end
	ans=12800
	x=dp[H,W]
	for j=0:25600
		if x%2==1
			ans=min(ans,abs(12800-j))
		end
		x>>=1
	end
	println(ans)
end
main()

Score : 500 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.

The square (i, j) has two numbers A_{ij} and B_{ij} written on it.

First, for each square, Takahashi paints one of the written numbers red and the other blue.

Then, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.

Let the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).

Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.

Find the minimum unbalancedness possible.

Constraints

2 \leq H \leq 80

2 \leq W \leq 80

0 \leq A_{ij} \leq 80

0 \leq B_{ij} \leq 80

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
A_{11} A_{12} \ldots A_{1W}
:
A_{H1} A_{H2} \ldots A_{HW}
B_{11} B_{12} \ldots B_{1W}
:
B_{H1} B_{H2} \ldots B_{HW}

Output

Print the minimum unbalancedness possible.

Sample Input 1

2 2
1 2
3 4
3 4
2 1

Sample Output 1

0

By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0.

Sample Input 2

2 3
1 10 80
80 10 1
1 2 3
4 5 6

Sample Output 2

2


parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	h,w = readline() |> split |> parseMap
	a = zeros(Int,h,w)
	b = zeros(Int,h,w)
	ms = 80
	for i in 1:h
		a[i,:] = readline() |> split |> parseMap
	end
	for i in 1:h
		b[i,:] = readline() |> split |> parseMap
	end
	base = 80*161
	dp = zeros(BigInt,h,w)
	dp[1,1] += BigInt(2)^(base+1+a[1,1]-b[1,1])
	dp[1,1] += BigInt(2)^(base+1-a[1,1]+b[1,1])
	for x in 1:h
		for y in 1:w
			if y<w
				v1 = abs(a[x,y+1]-b[x,y+1])
				dp[x,y+1] = dp[x,y+1]|dp[x,y]>>v1|dp[x,y]<<v1
			end
			if x<h
				v2 = abs(a[x+1,y]-b[x+1,y])
				dp[x+1,y] = dp[x+1,y]|dp[x,y]>>v2|dp[x,y]<<v2
			end
		end
	end
	z = base
	for i in 0:2*base+1
		if (dp[h,w]>>i)%2==1
			z = min(z,abs(base+2-i))
		end
	end
	println(z)
end

main()