local h, w = io.read("*n", "*n")
if h % 2 == 0 or w % 2 == 0 then
  print(h * w // 2)
else
  local a = w // 2
  local b = h // 2
  print(a * b + (a + 1) * (b + 1))

end

Score : 200 points

Problem Statement

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

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

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

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

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

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

Sample Input 2

7 3

Sample Output 2

11

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

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000


local n=io.read("n")
local s=""
local function dfs(s,max)
    if #s==n then
        print(s)
    else
        for c=97,max do
            dfs(s..string.char(c),(c==max and max+1 or max))
        end
    end
end
dfs(s,97)

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

Score : 400 points

Problem Statement

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

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

|s| = |t| holds.

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

s_i = s_j and t_i = t_j.

s_i \neq s_j and t_i \neq t_j.

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

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

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

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

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

Constraints

1 \leq N \leq 10

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

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

w_1
:
w_K

Sample Input 1

1

Sample Output 1

a

Sample Input 2

2

Sample Output 2

aa
ab

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.


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) assert(type(default_val) ~= 'table') local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local A = {}
for i=1,26 do
    A[i] = string.char(0x60 + i)
end
local N = read.n()

local ans = {}
local build = {}
local function dfs_cps(i, mx, cont)
    if i == N then
        table.insert(ans, table.concat(build))
        return cont()
    else
        local function make_cont(j, lim)
            return function()
                if j <= lim then
                    build[i+1] = A[j]
                    return dfs_cps(i+1, math.max(j,mx), make_cont(j+1,lim))
                else
                    return cont()
                end
            end
        end
        return make_cont(1, mx+1)()
    end
end
dfs_cps(0, 0, function()
    for i=1,#ans do
        print(ans[i])
    end
    --print(#ans)
end)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local A, B = read.nn()
local price_base = B * 10
for i=0,9 do
    local p = price_base + i
    if p * 8 // 100 == A then
        print(p)
        return
    end
end
print(-1)

Score : 300 points

Problem Statement

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

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

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

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

2 2

Sample Output 1

25

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

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

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

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

Sample Input 2

8 10

Sample Output 2

100

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

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

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

Sample Input 3

19 99

Sample Output 3

-1

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


local s = io.read()
local q = io.read("*n", "*l")
local w = true
local left, right = {}, {}
for i = 1, q do
  local sq = io.read()
  if sq == "1" then
    w = not w
  else
    local f, c = sq:sub(3, 3), sq:sub(5, 5)
    if f == "1" then
      if w then
        table.insert(left, c)
      else
        table.insert(right, c)
      end
    else
      if w then
        table.insert(right, c)
      else
        table.insert(left, c)
      end
    end
  end
end
if w then
  for i = #left, 1, -1 do
    io.write(left[i])
  end
  io.write(s)
  for i = 1, #right do
    io.write(right[i])
  end
  io.write("\n")
else
  for i = #right, 1, -1 do
    io.write(right[i])
  end
  io.write(s:reverse())
  for i = 1, #left do
    io.write(left[i])
  end
  io.write("\n")
end

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

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

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

If T_i = 1: reverse the string S.

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

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

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

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

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

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

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

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

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

Operation 2: Reverse S. S becomes ap.

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

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

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

Sample Output 2

aabc

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

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local stagenum = self.stagenum
  local ret = self.emptyvalue
  while left <= right do
    local stage, sz = 1, bls(1, stagenum - 1)
    local len = right - left + 1
    while (left - 1) % sz ~= 0 or len < sz do
      stage, sz = stage + 1, brs(sz, 1)
    end
    ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)])
    left = left + sz
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = brs(idx + 1, 1)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local function comp(a, b)
  return a[1] < b
end

local function lower_bound(ary, x)
  local num = #ary
  if num == 0 then return 1 end
  if not comp(ary[1], x) then return 1 end
  if comp(ary[num], x) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = mfl((min + max) / 2)
    if comp(ary[mid], x) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local mod = 998244353
local function badd(x, y) return (x + y) % mod end

local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = {}
  t[i][1], t[i][2] = io.read("*n", "*n")
end
table.sort(t, function(a, b) return a[1] < b[1] end)
t[n + 1] = {3 * 1000000007, 0}

local stReach = SegTree.new(n + 1, mma, 0)
for i = 1, n + 1 do
  stReach:setValue(i, i)
end

local dp = {}
for i = 1, n + 1 do
  dp[i] = 0
end
dp[n + 1] = 1
for i = n, 1, -1 do
  local dstLen = lower_bound(t, t[i][1] + t[i][2])
  local dstPos = stReach:getRange(i, dstLen - 1)
  stReach:setValue(i, dstPos)
  dp[i] = badd(dp[dstPos + 1], dp[i + 1])
end
print(dp[1])

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.


a = io.read("*n")
print(math.ceil(a / 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


A={}
R={}
for i=1,9 do
  A[io.read"*n"]=i
end
N=io.read"*n"
function bingo(t)
  for i=1,3 do
    if(t[i]and t[i+3]and t[i+6])then return true end
  end
  for i=1,3 do
    if(t[i*3-2]and t[i*3-1]and t[i*3])then return true end
  end
  return (t[1]and t[5]and t[9]or t[3]and t[5]and t[7])and true or false
end
for i=1,N do
  local k=A[io.read"*n"]
  if(k)then R[k]=0 end
end
print(bingo(R)and"Yes"or"No")

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.


local a = {}
local c = {}
for i = 1, 9 do
  a[i] = io.read("*n")
  c[i] = false
end
local n = io.read("*n")
for i = 1, n do
  local b = io.read("*n")
  for j = 1, 9 do
    if a[j] == b then
      c[j] = true
    end
  end
end
local f = false
if c[1] and c[4] and c[7] then f = true end
if c[2] and c[5] and c[8] then f = true end
if c[3] and c[6] and c[9] then f = true end
if c[1] and c[2] and c[3] then f = true end
if c[4] and c[5] and c[6] then f = true end
if c[7] and c[8] and c[9] then f = true end
if c[1] and c[5] and c[9] then f = true end
if c[3] and c[5] and c[7] then f = true end
print(f and "Yes" or "No")


local par={}
local rank={}

local function init(n)
    for i=1,n do
        par[i]=i
        rank[i]=1
    end
end

local function root(x)
    return par[x]==x and x or root(par[x])
end

local function same(x,y)
    return root(x)==root(y)
end

local function unite(x,y)
    x=root(x)
    y=root(y)
    if x==y then
        return
    end
    if rank[x]>rank[y] then
        x,y=y,x
    end
    par[x]=y
    rank[y]=rank[y]+rank[x]
end

local function size(x)
    return rank[root(x)]
end

local n,m,k=io.read("n","n","n")

init(n)

local graph={}
for i=1,n do
    graph[i]={}
end
for i=1,m do
    local a,b=io.read("n","n")
    table.insert(graph[a],b)
    table.insert(graph[b],a)
    unite(a,b)
end
for i=1,k do
    local c,d=io.read("n","n")
    if same(c,d) then
        table.insert(graph[c],d)
        table.insert(graph[d],c)
    end
end

for i=1,n do
    local kouho=size(i)-#graph[i]-1
    io.write(kouho.." ")
end
print()

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


local mfl, mce, mmi = math.floor, math.ceil, math.min
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = false
    end
  end
end
SegTree.create = function(self, n)
  local stagenum, mul = 1, 1
  self.cnt, self.stage = {1}, {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = false end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  local stagenum = self.stagenum
  while right - left + 1 < bls(1, stagenum - start_stage) do
    start_stage = start_stage + 1
  end
  local ret = false
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = bls(1, stagenum - stage)
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = ret or self.stage[stage][mce(l / sz)]
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
    if ret then break end
  end
  return ret
end
SegTree.setValue = function(self, idx, value)
  self.stage[self.stagenum][idx] = value
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] or self.stage[i + 1][rem]
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n = io.read("*n", "*l")
local s = io.read()
local q = io.read("*n", "*l")
local sts = {}
local t = {}
for i = 1, 26 do
  sts[i] = SegTree.new(n, function(a, b) return a or b end, false)
end
for i = 1, n do
  t[i] = s:sub(i, i):byte() - 96
  sts[t[i]]:setValue(i, true)
end

for iq = 1, q do
  local iqstr = io.read()
  local ic = iqstr:sub(1, 1)
  if ic == "1" then
    local i, cs = iqstr:match("%d (%d+) (%w)")
    i = tonumber(i)
    local c = cs:byte() - 96
    if t[i] ~= c then
      sts[t[i]]:setValue(i, false)
      t[i] = c
      sts[c]:setValue(i, true)
    end
  else
    local l, r = iqstr:match("%d (%d+) (%d+)")
    l, r = tonumber(l), tonumber(r)
    local cnt = 0
    for i = 1, 26 do
      if sts[i]:getRange(l, r) then cnt = cnt + 1 end
    end
    print(cnt)
  end
end

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.


local mfl, mce, mmi = math.floor, math.ceil, math.min
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = false
    end
  end
end
SegTree.create = function(self, n)
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = false end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = false
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = ret or self.stage[stage][mce(l / sz)]
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
    if ret then break end
  end
  return ret
end
SegTree.setValue = function(self, idx, value)
  self.stage[self.stagenum][idx] = value
  for i = self.stagenum - 1, 1, -1 do
    local dst = mce(idx / 2)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.stage[i + 1][idx] or self.stage[i + 1][rem]
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n = io.read("*n", "*l")
local s = io.read()
local q = io.read("*n", "*l")
local sts = {}
local t = {}
for i = 1, 26 do
  sts[i] = SegTree.new(n, function(a, b) return a or b end, false)
end
for i = 1, n do
  t[i] = s:sub(i, i):byte() - 96
  sts[t[i]]:setValue(i, true)
end

for iq = 1, q do
  local iqstr = io.read()
  local ic = iqstr:sub(1, 1)
  if ic == "1" then
    local i, cs = iqstr:match("%d (%d+) (%w)")
    i = tonumber(i)
    local c = cs:byte() - 96
    if t[i] ~= c then
      sts[t[i]]:setValue(i, false)
      t[i] = c
      sts[c]:setValue(i, true)
    end
  else
    local l, r = iqstr:match("%d (%d+) (%d+)")
    l, r = tonumber(l), tonumber(r)
    local cnt = 0
    for i = 1, 26 do
      if sts[i]:getRange(l, r) then cnt = cnt + 1 end
    end
    print(cnt)
  end
end


local mma, mmi = math.max, math.min
local msq = math.sqrt
local n, k = io.read("*n", "*n")
local t = {}
for i = 1, n do
  t[i] = {io.read("*n", "*n", "*n")}
end

local function func(x, y)
  local q = {}
  for i = 1, n do
    q[i] = t[i][3] * t[i][3] * ((t[i][1] - x) * (t[i][1] - x) + (t[i][2] - y) * (t[i][2] - y))
  end
  table.sort(q)
  return msq(q[k])
end

local function solve(tbl)
  local xmin, xmax, ymin, ymax = tbl[1], tbl[2], tbl[3], tbl[4]
  local dv = 64
  local dxrange = (xmax - xmin) / (dv * 2)
  local dyrange = (ymax - ymin) / (dv * 2)
  local xp = {}
  for i = 1, dv do
    xp[i] = xmin + dxrange * (i * 2 - 1)
  end
  local yp = {}
  for i = 1, dv do
    yp[i] = ymin + dyrange * (i * 2 - 1)
  end
  local mp, minval = false, 0
  for i = 1, dv do
    for j = 1, dv do
      local v = func(xp[i], yp[j])
      if not mp or v < minval then
        minval = v
        mp = {i, j}
      end
    end
  end
  tbl[1] = xmin + mma(0, dxrange * (mp[1] * 2 - 1 - dv / 4))
  tbl[2] = mmi(xmax, xmin + dxrange * (mp[1] * 2 - 1 + dv / 4))
  tbl[3] = ymin + mma(0, dyrange * (mp[2] * 2 - 1 - dv / 4))
  tbl[4] = mmi(ymax, ymin + dyrange * (mp[2] * 2 - 1 + dv / 4))
end

local tbl = {-1000, 1000, -1000, 1000}
local rep = 10000000
while true do
  solve(tbl)
  if 1.85 < os.clock() then break end
end
local xm, ym = (tbl[1] + tbl[2]) / 2, (tbl[3] + tbl[4]) / 2
local ret = func(xm, ym)
print(string.format("%.10f", ret))

Score : 600 points

Problem Statement

Takahashi wants to grill N pieces of meat on a grilling net, which can be seen as a two-dimensional plane. The coordinates of the i-th piece of meat are \left(x_i, y_i\right), and its hardness is c_i.

Takahashi can use one heat source to grill the meat. If he puts the heat source at coordinates \left(X, Y\right), where X and Y are real numbers, the i-th piece of meat will be ready to eat in c_i \times \sqrt{\left(X - x_i\right)^2 + \left(Y-y_i\right)^2} seconds.

Takahashi wants to eat K pieces of meat. Find the time required to have K or more pieces of meat ready if he put the heat source to minimize this time.

Constraints

All values in input are integers.

1 \leq N \leq 60

1 \leq K \leq N

-1000 \leq x_i , y_i \leq 1000

\left(x_i, y_i\right) \neq \left(x_j, y_j\right) \left(i \neq j \right)

1 \leq c_i \leq 100

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
\vdots
x_N y_N c_N

Output

Print the answer.

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

Sample Input 1

4 3
-1 0 3
0 0 3
1 0 2
1 1 40

Sample Output 1

2.4

If we put the heat source at \left(-0.2, 0\right), the 1-st, 2-nd, and 3-rd pieces of meat will be ready to eat within 2.4 seconds. This is the optimal place to put the heat source.

Sample Input 2

10 5
-879 981 26
890 -406 81
512 859 97
362 -955 25
128 553 17
-885 763 2
449 310 57
-656 -204 11
-270 76 40
184 170 16

Sample Output 2

7411.2252


local a, b = io.read("*n", "*n")
if a < 10 then
  print(b + 100 * (10 - a))
else
  print(b)
end

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


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local N, K = read.nn()
if N == 0 then
    print(1)
    return
end
local n = N
local ans = 0
while n > 0 do
    ans = ans + 1
    n = n // K
    --print(n)
end
print(ans)

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


local n, k = io.read("*n", "*n")
local d = 1
local x = k
while true do
  if n < x then
    print(d)
    break
  else
    x = x * k
    d = d + 1
  end
end


local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local ret = 1
for x = 1, 100 do
  local r = 0
  for i = 1, n do
    r = r + (t[i] - x) * (t[i] - x)
  end
  if x == 1 then ret = r
  else ret = math.min(ret, r)
  end
end
print(ret)

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


local bit=require"bit"
local ffi=require"ffi"
local function int(x)return ffi.new("int64_t",x)end
local function printn(x)print(tonumber(x))end
local function for64(a,b,c)c=c or 1 local x=a return c<0 and function()local ret=x x=x+c return ret>=b and ret or nil end or function() local ret=x x=x+c return ret<=b and ret or nil end end
local MOD=int(1000000007)
local function add(a,b)return (a+b)%MOD end
local function sub(a,b)a=(a-b)%MOD return a<0 and a+MOD or a end
local function mul(a,b)return a*b%MOD end
local function pow(a,b)
	local ret=int(1)
	while(b>0)do
		if(b%2==1)then ret=mul(ret,a)end
		a=mul(a,a)
		b=b/2
	end
	return ret
end
local function inv(x)
	local a=x
	local b,u,v=int(MOD),int(1),int(0)
	repeat
		local t=a/b
		a,b=b,a-t*b
		u,v=v,u-t*v
	until(b==0)
	u=u%MOD
	return u<0 and u+MOD or u
end
local function div(a,b)return mul(a,inv(b))end
local function C(n,k)
	if(n<k)then return 0 end
	local ret=int(1)
	for i in for64(n,n-k+1,-1) do
		ret=mul(ret,i)
	end
	for i in for64(2,k) do
		ret=div(ret,i)
	end
	return ret
end

n=int(io.read"*n")
a=int(io.read"*n")
b=int(io.read"*n")
printn(sub(sub(sub(pow(2,n),C(n,a)),C(n,b)),1))

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


local MOD=1000000007
local function add(a,b)return (a+b)%MOD end
local function sub(a,b)return (a-b)%MOD end
local function mul_(a,b)
	local ret=0
	while(b>0)do
		if(b%2==1)then ret=(ret+a)%MOD end
		a=(a+a)%MOD
		b=math.floor(b*.5)
	end
	return ret
end
local function mul(a,b)
	return a<b and mul_(b,a)or mul_(a,b)
end
local function pow(a,b)
	local ret=1
	while(b>0)do
		if(b%2==1)then ret=mul(ret,a)end
		a=mul(a,a)
		b=math.floor(b*.5)
	end
	return ret
end
local function inv(x)
	local a=x
	local b,u,v=MOD,1,0
	repeat
		local t=math.floor(a/b)
		a,b=b,a-t*b
		u,v=v,u-t*v
	until(b==0)
	return u%MOD
end
local function div(a,b)return mul(a,inv(b))end
local function C(n,k)
	if(n<k)then return 0 end
	local ret=1
	for i=n,n-k+1,-1 do
		ret=mul(ret,i)
	end
	for i=2,k do
		ret=div(ret,i)
	end
	return ret
end

n=io.read"*n"
a=io.read"*n"
b=io.read"*n"
print(sub(sub(sub(pow(2,n),C(n,a)),C(n,b)),1))


local mod = 1000000007
local mfl = math.floor

local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

local function badd(x, y)
  return (x + y) % mod
end

local function bsub(x, y)
  return x < y and x - y + mod or x - y
end

local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local function modinv(src)
  return modpow(src, mod - 2)
end

local function getComb(n, k)
  local ret = 1
  for i = 1, k do
    ret = bmul(ret, n + 1 - i)
    ret = bmul(ret, modinv(i))
  end
  return ret
end

local n, a, b = io.read("*n", "*n", "*n")
local ret = modpow(2, n)
ret = bsub(ret, 1)
ret = bsub(ret, getComb(n, a))
ret = bsub(ret, getComb(n, b))
print(ret)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local floor = math.floor
local MOD = floor(10^9)+7

-- returns (b^n) % m
local function modpow(b, n, m)
    if n == 0 then
        return 1
    elseif n % 2 == 0 then
        -- be careful about overflow of b*b
        return modpow(b * b % m, n//2, m)
    else
        return b * modpow(b, n - 1, m) % m
    end
end

-- returns n! % m
local function modfactorial(n, m)
    local a = 1
    for i=1, n do
        a = a * i % m
    end
    return a
end

local function make_mod_nCr(nmax, m)
    -- Create n! table
    local mfac = {}
    mfac[0] = 1
    for i=1,nmax do
        mfac[i] = mfac[i-1] * i % m
    end
    -- Create (n!)^(-1) table
    -- (n!)^(-1) ≡ (n!)^(m - 2)  <mod m>
    local invmfac = {}
    invmfac[nmax] = modpow(mfac[nmax], m - 2, m)
    for i=nmax,1,-1 do
        -- ((a-1)!)^(-1) ≡ a * (a!)^(-1)  <mod m>
        invmfac[i-1] = i * invmfac[i] % m
    end
    -- returns nCr % m
    local function mod_nCr(n, r)
        -- nCr ≡ n! * (r!)^(-1) * ((n-r)!)^(-1)  <mod m>
        local a = mfac[n] * invmfac[r] % m
        a = a * invmfac[n-r] % m
        return a
    end
    return mod_nCr
end

-- larger n and small r
local function make_mod_nCr_2(rmax, m)
    -- Create n! table
    local mfac = {}
    mfac[0] = 1
    for i=1,rmax do
        mfac[i] = mfac[i-1] * i % m
    end
    -- Create (n!)^(-1) table
    -- (n!)^(-1) ≡ (n!)^(m - 2)  <mod m>
    local invmfac = {}
    invmfac[rmax] = modpow(mfac[rmax], m - 2, m)
    for i=rmax,1,-1 do
        -- ((a-1)!)^(-1) ≡ a * (a!)^(-1)  <mod m>
        invmfac[i-1] = i * invmfac[i] % m
    end
    -- partial factory ()
    local pmfac
    -- returns nCr % m
    local function mod_nCr(n, r)
        if r > n then
            return 0
        end
        if r > n/2 then
            r = n - r
        end
        -- nCr ≡ (n * (n - 1) * ... * (n-r+1)) * (r!)^(-1)  <mod m>
        local a = 1
        for i=n, n-r+1, -1 do
            a = (a * i) % m
        end
        a = a * invmfac[r] % m
        return a
    end
    return mod_nCr
end

----
local n, k = read.nn()

local nCr = make_mod_nCr(n, MOD)

local ans = 0
for i=0,math.min(n-1,k) do
    local empty_room_pattern = nCr(n, i)
    local nonempty_room_pattern = nCr(n-1, i)
    ans = (ans + (empty_room_pattern * nonempty_room_pattern % MOD)) % MOD
end

print(ans)

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


a, b, c = io.read("*n", "*n", "*n")
if a == b and a ~= c then
  print("Yes")
elseif a == c and a ~= b then
  print("Yes")
elseif b == c and a ~= b then
  print("Yes")
else
  print("No")
end

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


n = io.read("*n")
a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
v = true
for i = 1, n do
  if a[i] % 2 == 0 then
    if a[i] % 3 ~= 0 and a[i] % 5 ~= 0 then
      v = false break
    end
  end
end
print(v and "APPROVED" or "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.


n=io.read("*n","*l")
t={}
frequency=0
for i=1,n do
    s=io.read()
    if not t[s] then
        t[s]=1
    else
        t[s]=t[s]+1
    end
    frequency=math.max(frequency,t[s])
end
table.sort(t)
for k,v in pairs(t) do
    if t[k]==frequency then
        print(k)
    end
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


local mmi = math.min
local sn = io.read()
local n = #sn
local t = {}
for i = 1, n do
  t[i] = tonumber(sn:sub(i, i))
end
local p1, p2 = 1, 0
for i = 1, n do
  local np1 = 1000000007
  if i ~= n and 0 < t[i + 1] then
    np1 = mmi(p1 + 9 - t[i], p2 + t[i] + 1)
  end
  local np2 = mmi(p1 + 10 - t[i], p2 + t[i])
  p1, p2 = np1, np2
end
print(mmi(p1, p2))

Score: 500 points

Problem Statement

In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)

To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.

What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?

Assume that you have sufficient numbers of banknotes, and so does the clerk.

Constraints

N is an integer between 1 and 10^{1,000,000} (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible number of total banknotes used by you and the clerk.

Sample Input 1

36

Sample Output 1

8

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.

The payment cannot be made with less than eight banknotes in total, so the answer is 8.

Sample Input 2

91

Sample Output 2

3

If you give two banknotes of value 100, 1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

Sample Output 3

243


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.


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


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
-- C
local N = read.n()
local T = {}
local ans = "YES"
for i=1,N do
    local A = read.n()
    T[A] = (T[A] or 0) + 1
    if T[A] == 2 then
        ans = "NO"
    end
end
print(ans)

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


local n, k = io.read("*n", "*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end
local sum = 0
for i = 1, k do
  sum = sum + t[i]
end
local ret = sum
for i = k + 1, n do
  sum = sum + t[i] - t[i - k]
  ret = math.max(ret, sum)
end
print((ret + k) / 2)

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


local mfl = math.floor
local ns = io.read()
local k = io.read("*n")
if k == 1 then
  local a = tonumber(ns:sub(1, 1))
  print(a + 9 * (#ns - 1))
elseif k == 2 then
  if #ns == 1 or ns == "10" then print(0)
  else
    local a = tonumber(ns:sub(1, 1))
    local b, bpos = 0, #ns + 1
    for i = 2, #ns do
      if ns:sub(i, i) ~= "0" then
        b, bpos = tonumber(ns:sub(i, i)), i
        break
      end
    end
    -- use 1st but not max
    local ret = (#ns - 1) * 9 * (a - 1)
    -- don't use 1st
    ret = ret + 81 * mfl((#ns - 1) * (#ns - 2) / 2)
    -- use 1st by max
    if bpos <= #ns then
      ret = ret + b + 9 * (#ns - bpos)
    end
    print(ret)
  end
else
  if #ns < 3 then print(0)
  else
    local a = tonumber(ns:sub(1, 1))
    local b, bpos = 0, #ns + 1
    for i = 2, #ns do
      if ns:sub(i, i) ~= "0" then
        b, bpos = tonumber(ns:sub(i, i)), i
        break
      end
    end
    local c, cpos = 0, #ns + 1
    for i = bpos + 1, #ns do
      if ns:sub(i, i) ~= "0" then
        c, cpos = tonumber(ns:sub(i, i)), i
        break
      end
    end
    -- don't use 1st
    ret = 729 * mfl((#ns - 1) * (#ns - 2) * (#ns - 3) / 6)
    -- use 1st but not max
    ret = ret + (a - 1) * 81 * mfl((#ns - 1) * (#ns - 2) / 2)
    -- use 1st by max
    if bpos <= #ns - 1 then
      -- don't use second
      ret = ret + 81 * mfl((#ns - bpos) * (#ns - bpos - 1) / 2)
      -- use 2nd but not max
      ret = ret + (b - 1) * 9 * (#ns - bpos)
      -- use 2nd by max
      if cpos <= #ns then
        ret = ret + c + 9 * (#ns - cpos)
      end
    end
    print(ret)
  end
end

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


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.T = function(T) local t={} for i=1,T do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
local function factorial(n)
    local a = 1
    for i=1, n do
        a = a * i
    end
    return a
end

local function nCr(n, r)
    if r > n then
        return 0
    end
    if r > n/2 then
        r = n - r
    end
    local a = 1
    for i=n, n-r+1, -1 do
        a = a * i
    end
    return a // factorial(r)
end

local function normal(d, k)
    return nCr(d, k) * math.floor(9^k)
end
-- E
local S = read.l():totable()
local T = {}
for i=1,#S do
    T[i] = tonumber(S[i])
end
local D = #T
local K = read.n()

local function rec(x, remain)
    if remain == 0 then
        return 1
    end
    for i=x, D do
        if T[i] ~= 0 then
            local ans1 = normal(D-i, remain)
            local ans2 = (T[i] - 1) * normal(D-i, remain-1)
            local ans3 = 1 * rec(i+1, remain-1)
            return ans1+ans2+ans3
        end
    end
    return 0
end

local ans = rec(1, K)
print(ans)


local mod = 1000000007
local mfl = math.floor

local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

local function badd(x, y)
  return (x + y) % mod
end

local function bsub(x, y)
  return x < y and x - y + mod or x - y
end
local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local function modinv(src)
  return modpow(src, mod - 2)
end

local r1, c1 = io.read("*n", "*n")
local r2, c2 = io.read("*n", "*n")

local function getSum(r, c)
  local sum = 0
  local cur = 1
  for ir = 0, r do
    cur = bmul(cur, ir + c + 1)
    cur = bmul(cur, modinv(ir + 1))
    sum = badd(sum, cur)
  end
  return sum
end
local z = getSum(r2, c2)
z = bsub(z, getSum(r2, c1 - 1))
z = bsub(z, getSum(r1 - 1, c2))
z = badd(z, getSum(r1 - 1, c1 - 1))
print(z)

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


n = io.read("n")
a = io.read("n")
print((n+a-1) // 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


H=io.read"*n"
N=io.read"*n"
AS=0
for i=1,N do
	AS=AS+io.read"*n"
end
print((
H<=AS
)and"Yes"or"No")

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


n, k = io.read("*n", "*n")
t = {}
for i = 1, n do t[i] = io.read("*n") end
table.sort(t)
s = 0
for i = 1, n - k do s = s + t[i] end
print(s)

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.


local h,n=io.read("n","n")
local a,b={},{}
for i=1,n do
    a[i],b[i]=io.read("n","n")
end

local dp={0}
for i=2,h+1 do
    dp[i]=10^9
end

for i=1,n do
    for j=1,h+1 do
        local k=math.min(j+a[i],h+1)
        dp[k]=math.min(dp[k],dp[j]+b[i])
    end
end
print(dp[h+1])

Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_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 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815


local function meld(a,b)
    if not a then
        return b
    elseif not b then
        return a
    elseif a[1]<b[1] then
        b[4]=a[3]
        a[3]=b
        return a
    else
        a[4]=b[3]
        b[3]=a
        return b
    end
end

local function pair(a)
    local b=a and a[4]
    if not b then
        return a
    end
    local q={}
    while b do
        local c=b[4]
        a[4]=false
        b[4]=false
        table.insert(q,meld(a,b))
        a=c
        b=a and a[4]
    end
    for i=#q,1,-1 do
        a=meld(a,q[i])
    end
    return a
end

local PairingHeap={}

PairingHeap.empty=function(self)
    return not self[1]
end

PairingHeap.push=function(self,k,v)
	if not self[1] then
		self[1]={
            k,
            v,
            false,
            false,
		}
	else
		if self[1][1]<k then
			self[1][3]={
				k,
				v,
				false,
				self[1][3],
			}
		else
			self[1]={
				k,
				v,
				self[1],
				false,
			}
		end
	end
end

PairingHeap.top=function(self)
    return self[1][2]
end

PairingHeap.pop=function(self)
    self[1]=pair(self[1][3])
end

PairingHeap.join=function(self,other)
	if self[1][1]<other[1][1]then
		other[1][4]=self[1][3]
        self[1][3]=other[1]
	else
		self[1][4]=other[1][3]
        other[1][3]=self[1]
        self[1]=other[1]
    end
    return self
end

PairingHeap.new=function(self)
    local obj={}
    setmetatable(obj,{__index=PairingHeap})
    return obj
end

----------

local n,d,a=io.read("n","n","n")
local pq=PairingHeap:new()
for i=1,n do
    local x,h=io.read("n","n")
    pq:push(x,{x,h,0})
end

local atack=0
local total=0
while not pq:empty() do
    local x,h,c=table.unpack(pq:top())
    pq:pop()
    if c==0 then
        if atack*a<h then
            h=h-atack*a
            local count=(h-1)//a+1
            pq:push(x+2*d+0.5,{x+2*d,count,1})
            total=total+count
            atack=atack+count
        end
    else
        atack=atack-h
    end
end
print(total)

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.


local function meld(a,b)
    if not a then
        return b
    elseif not b then
        return a
    elseif a[1]<b[1] then
        b[4]=a[3]
        a[3]=b
        return a
    else
        a[4]=b[3]
        b[3]=a
        return b
    end
end

local function pair(a)
    local b=a and a[4]
    if not b then
        return a
    end
    local q={}
    while b do
        local c=b[4]
        a[4]=false
        b[4]=false
        table.insert(q,meld(a,b))
        a=c
        b=a and a[4]
    end
    for i=#q,1,-1 do
        a=meld(a,q[i])
    end
    return a
end

local PairingHeap={}

PairingHeap.empty=function(self)
    return not self[1]
end

PairingHeap.push=function(self,k,v)
	if not self[1] then
		self[1]={
            k,
            v,
            false,
            false,
		}
	else
		if self[1][1]<k then
			self[1][3]={
				k,
				v,
				false,
				self[1][3],
			}
		else
			self[1]={
				k,
				v,
				self[1],
				false,
			}
		end
	end
end

PairingHeap.top=function(self)
    return self[1][2]
end

PairingHeap.pop=function(self)
    self[1]=pair(self[1][3])
end

PairingHeap.join=function(self,other)
	if self[1][1]<other[1][1]then
		other[1][4]=self[1][3]
        self[1][3]=other[1]
	else
		self[1][4]=other[1][3]
        other[1][3]=self[1]
        self[1]=other[1]
    end
    return self
end

PairingHeap.new=function(self)
    local obj={}
    setmetatable(obj,{__index=PairingHeap})
    return obj
end

----------

local n,d,a=io.read("n","n","n")
local pq=PairingHeap:new()
for i=1,n do
    local x,h=io.read("n","n")
    pq:push(x,{x,h,true})
end

local atack=0
local total=0
while not pq:empty() do
    local x,h,c=table.unpack(pq:top())
    pq:pop()
    if c then
        if atack*a<h then
            h=h-atack*a
            local count=(h-1)//a+1
            pq:push(x+2*d,{x+2*d,count,false})
            total=total+count
            atack=atack+count
        end
    else
        atack=atack-h
    end
end
print(total)


local n,m=io.read("*n","*n")
if n<=m then
  print("Yes")
else
  print("No")
end

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


a=io.read"*n"
b=io.read"*n"
print((
	a<b
)and tostring(a):rep(b)or tostring(b):rep(a))

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


local a,b=io.read("n","n")
local x = a < b and {a,b} or {b,a}
print(string.rep(tostring(x[1]), x[2]))


N=io.read("*n")
counter = 0
min_val = N+1
for i = 1, N do
    P = io.read("*n")
    if P < min_val then
        counter = counter + 1
        min_val = P
    end
end
print(counter)

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


local n = io.read("*n")
local r = 0
local m = n + 1
for i = 1, n do
  local p = io.read("*n")
  if p < m then
    r, m = r + 1, p
  end
end
print(r)


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimensi0n, default_val) assert(type(default_val) ~= 'table') local n=dimensi0n local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----

local N = read.n()

local function first_last(x)
    local s = tostring(x)
    local first = tonumber(s:sub(1,1))
    return first, x%10
end

local t = array(1, 0)
for i=1,N do
    local f, e = first_last(i)
    t[f*10+e] = t[f*10+e] + 1
end

local cnt = 0
for x=1,9 do
    for y=1,9 do
        local cnt_a = t[x*10+y]
        local cnt_b = t[y*10+x]
        cnt = cnt + cnt_a * cnt_b
    end
end
print(cnt)

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


local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimensi0n, default_val) assert(type(default_val) ~= 'table') local n=dimensi0n local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
local function tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end
----

local N = read.n()

local function get(b, e)
    -- number of integer x where:
    --  - begin with b,
    --  - end with e, and
    --  - x <= N
    local c = 0
    -- [1..9]
    if b==e and b <= N then
        c = c + 1
    end
    -- [11..99]
    if b*10+e <= N then
        c = c + 1
    end
    -- [101..999]
    for i=0,9 do
        if b*100+i*10+e <= N then
            c = c + 1
        end
    end
    -- [1001..9999]
    for i=0,9 do
        for j=0,9 do
            if b*1000+i*100+j*10+e <= N then
                c = c + 1
            end
        end
    end
    -- [10001..99999]
    for i=0,9 do
        for j=0,9 do
            for k=0,9 do
                if b*10000+i*1000+j*100+k*10+e <= N then
                    c = c + 1
                end
            end
        end
    end
    -- [100001..199999]
    for i=0,9 do
        for j=0,9 do
            for k=0,9 do
                for l=0,9 do
                    if b*100000+i*10000+j*1000+k*100+l*10+e <= N then
                        c = c + 1
                    end
                end
            end
        end
    end
    return c
end

local cnt = 0
-- A
-- x....y
-- B
-- y....x
for x=1,9 do
    for y=1,9 do
        local cnt_a = get(x, y)
        local cnt_b = get(y, x)
        cnt = cnt + cnt_a * cnt_b
    end
end
print(cnt)


local mfl = math.floor
local n = io.read("*n")
local t = {}
for i = 1, 81 do
  t[i] = 0
end
for i = 1, n do
  local ri = i
  local btm = i % 10
  if btm ~= 0 then
    local top = btm
    if i < 10 then
    end
    while 10 <= i do
      i = mfl(i / 10)
    end
    local top = i
    local idx = (top - 1) * 9 + btm
    t[idx] = t[idx] + 1
  end
end
local ret = 0
for i = 1, 9 do
  local idx = (i - 1) * 9 + i
  -- ret = ret + t[idx] * t[idx]
  for j = 1, 9 do
    local idx1, idx2 = (i - 1) * 9 + j, (j - 1) * 9 + i
    ret = ret + t[idx1] * t[idx2]
  end
end
print(ret)


local mce, mfl, msq, mmi, mma, mab = math.ceil, math.floor, math.sqrt, math.min, math.max, math.abs
local mod = 1000000007

local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end

local function bsub(x, y)
  return x < y and x - y + mod or x - y
end

local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local function modinv(src)
  return modpow(src, mod - 2)
end

local function getprimes(x)
  local primes = {}
  local allnums = {}
  for i = 1, x do allnums[i] = true end
  for i = 2, x do
    if allnums[i] then
      table.insert(primes, i)
      local lim = mfl(x / i)
      for j = 2, lim do
        allnums[j * i] = false
      end
    end
  end
  return primes
end

local function getdivisorparts(x, primes)
  local prime_num = #primes
  local tmp = {}
  local lim = mce(msq(x))
  local primepos = 1
  local dv = primes[primepos]
  while(primepos <= prime_num and dv <= lim) do
    if(x % dv == 0) then
      local asdf = {}
      asdf.p = dv
      asdf.cnt = 1
      x = x / dv
      while(x % dv == 0) do
        x = x / dv
        asdf.cnt = asdf.cnt + 1
      end
      table.insert(tmp, asdf)
      lim = mce(msq(x))
    end
    if(primepos == prime_num) then break end
    primepos = primepos + 1
    dv = primes[primepos]
  end
  if(x ~= 1) then
    local asdf = {}
    asdf.p, asdf.cnt = x, 1
    table.insert(tmp, asdf)
  end
  return tmp
end

local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local primes = getprimes(1000)
local alldvs = {}
local dvs = {}
for i = 1, n do
  dvs[i] = getdivisorparts(a[i], primes)
  for j = 1, #dvs[i] do
    local p, c = dvs[i][j].p, dvs[i][j].cnt
    if not alldvs[p] then
      alldvs[p] = c
    else
      alldvs[p] = mma(alldvs[p], c)
    end
  end
end
local ret = 0
local mul = 1
for p, c in pairs(alldvs) do
  mul = bmul(mul, modpow(p, c))
end
for i = 1, n do
  local z = 1
  for j = 1, #dvs[i] do
    local p, c = dvs[i][j].p, dvs[i][j].cnt
    z = bmul(z, modpow(p, c))
  end
  ret = badd(ret, bmul(mul, modinv(z)))
end
print(ret)

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


local mfl, mce, mmi = math.floor, math.ceil, math.min

local function getor(x, y)
  local ret = 0
  local mul = 1
  while 0 < x + y do
    ret = ret + mul * (1 - (1 - x % 2) * (1 - y % 2))
    x, y, mul = mfl(x / 2), mfl(y / 2), mul * 2
  end
  return ret
end

local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n = io.read("*n")
local edge = {}
local asked = {}
local cpos = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  asked[i] = false
  cpos[i] = 0
  len[i] = 0
end
len[n + 1] = 100
local line = {}
for i = 1, n - 1 do
  local x, y = io.read("*n", "*n")
  table.insert(edge[x], y)
  table.insert(edge[y], x)
  line[i] = {x, y}
end

local tasks = {1}
local posinv = {}
for i = 1, n do
  posinv[i] = 0
end
local eulers = {}
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  asked[src] = true
  table.insert(eulers, src)
  if posinv[src] == 0 then
    posinv[src] = #eulers
  end
  while cpos[src] < #edge[src] do
    cpos[src] = cpos[src] + 1
    local dst = edge[src][cpos[src]]
    if not asked[dst] then
      len[dst] = len[src] + 1
      table.insert(tasks, src)
      table.insert(tasks, dst)
      break
    end
  end
end
local lca = {}
for i = 1, #eulers do
  lca[i] = {}
  local prv = eulers[i]
  lca[i][1] = prv
  for j = i + 1, #eulers do
    lca[i][j - i + 1] = len[prv] < len[eulers[j]] and prv or eulers[j]
    prv = lca[i][j - i + 1]
  end
end
local stLine = SegTree.new(2 * n - 2, function(a, b) return a + b end, 0)

local m = io.read("*n")
local mtot = 2^m
local mbox = {}
for i = 1, mtot do
  mbox[i] = 0LL
end
mbox[1] = 1LL
local posu, posv, posp = {}, {}, {}
for i = 1, m do
  local u, v = io.read("*n", "*n")
  local pu, pv = posinv[u], posinv[v]
  if pv < pu then pu, pv = pv, pu end
  local p = lca[pu][pv - pu + 1]
  posu[i], posv[i], posp[i] = pu, pv, posinv[p]
end
local linepos = {}
for il = 1, n - 1 do
  linepos[il] = {}
  local lx, ly = line[il][1], line[il][2]
  for j = 1, #eulers - 1 do
    if lx == eulers[j] and ly == eulers[j + 1] then
      table.insert(linepos[il], j)
    elseif ly == eulers[j] and lx == eulers[j + 1] then
      table.insert(linepos[il], j)
    end
  end
end

for il = 1, n - 1 do
  local lx, ly = line[il][1], line[il][2]
  local p1, p2 = linepos[il][1], linepos[il][2]
  stLine:setValue(p1, 1)
  stLine:setValue(p2, -1)
  local mul = 1
  local actsum = 0
  for im = 1, m do
    local pu, pv, pp = posu[im], posv[im], posp[im]
    if pv < pp then
      activate = 0 ~= stLine:getRange(pv, pp - 1)
    elseif pp < pv then
      activate = 0 ~= stLine:getRange(pp, pv - 1)
    end
    if not activate then
      if pu < pp then
        activate = 0 ~= stLine:getRange(pu, pp - 1)
      elseif pp < pu then
        activate = 0 ~= stLine:getRange(pp, pu - 1)
      end
    end
    if activate then
      actsum = actsum + mul
    end
    mul = mul * 2
  end
  for im = mtot, 1, -1 do
    local dst = getor(im - 1, actsum)
    mbox[dst + 1] = mbox[dst + 1] + mbox[im]
  end
  stLine:setValue(p1, 0)
  stLine:setValue(p2, 0)
end
local str = tostring(mbox[mtot]):gsub("LL", "")
print(str)

Score : 600 points

Problem Statement

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

Consider painting each of these edges white or black. There are 2^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following M restrictions?

The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black.

Constraints

2 \leq N \leq 50

1 \leq a_i,b_i \leq N

The graph given in input is a tree.

1 \leq M \leq \min(20,\frac{N(N-1)}{2})

1 \leq u_i < v_i \leq N

If i \not= j, either u_i \not=u_j or v_i\not=v_j

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M

Output

Print the number of ways to paint the edges that satisfy all of the M conditions.

Sample Input 1

3
1 2
2 3
1
1 3

Sample Output 1

3

The tree in this input is shown below:

All of the M restrictions will be satisfied if Edge 1 and 2 are respectively painted (white, black), (black, white), or (black, black), so the answer is 3.

Sample Input 2

2
1 2
1
1 2

Sample Output 2

1

The tree in this input is shown below:

All of the M restrictions will be satisfied only if Edge 1 is painted black, so the answer is 1.

Sample Input 3

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

Sample Output 3

9

The tree in this input is shown below:

Sample Input 4

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

Sample Output 4

62

The tree in this input is shown below:


local mfl, mce, mmi = math.floor, math.ceil, math.min

local function getor(x, y)
  if x == 0 then return y end
  if y == 0 then return x end
  local ret = 0
  local mul = 1
  while 0 < x or 0 < y do
    if 1 <= (x % 2) + (y % 2) then
      ret = ret + mul
    end
    x, y, mul = mfl(x / 2), mfl(y / 2), mul * 2
  end
  return ret
end
-- print(getor(1, 7)) os.exit()

local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    for j = 1, self.cnt[i] do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.cnt, self.stage, self.size = {1}, {{}}, {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.cnt[stagenum], self.stage[stagenum] = mul, {}
  end
  for i = 1, stagenum do self.size[i] = self.cnt[stagenum + 1 - i] end
  self.stagenum = stagenum
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.getRange = function(self, left, right)
  if left == right then return self.stage[self.stagenum][left] end
  local start_stage = 1
  while right - left + 1 < self.size[start_stage] do
    start_stage = start_stage + 1
  end
  local ret = self.emptyvalue
  local t1, t2, t3 = {start_stage}, {left}, {right}
  while 0 < #t1 do
    local stage, l, r = t1[#t1], t2[#t1], t3[#t1]
    table.remove(t1) table.remove(t2) table.remove(t3)
    local sz = self.size[stage]
    if (l - 1) % sz ~= 0 then
      local newr = mmi(r, mce((l - 1) / sz) * sz)
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, newr)
      l = newr + 1
    end
    if sz <= r + 1 - l then
      ret = self.func(ret, self.stage[stage][mce(l / sz)])
      l = l + sz
    end
    if l <= r then
      table.insert(t1, stage + 1) table.insert(t2, l) table.insert(t3, r)
    end
  end
  return ret
end
SegTree.setValue = function(self, idx, value, silent)
  self.stage[self.stagenum][idx] = value
  if not silent then
    for i = self.stagenum - 1, 1, -1 do
      local dst = mce(idx / 2)
      local rem = dst * 4 - 1 - idx
      self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
      idx = dst
    end
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n = io.read("*n")
local edge = {}
local asked = {}
local cpos = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  asked[i] = false
  cpos[i] = 0
  len[i] = 0
end
len[n + 1] = 100
local line = {}
for i = 1, n - 1 do
  local x, y = io.read("*n", "*n")
  table.insert(edge[x], y)
  table.insert(edge[y], x)
  line[i] = {x, y}
end

local st = SegTree.new(2 * n - 1, function(a, b)
  return len[a] < len[b] and a or b end,
  n + 1
)

local tasks = {1}
local posinv = {}
for i = 1, n do
  posinv[i] = 0
end
local eulers = {}
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  asked[src] = true
  table.insert(eulers, src)
  st:setValue(#eulers, src)
  if posinv[src] == 0 then
    posinv[src] = #eulers
  end
  while cpos[src] < #edge[src] do
    cpos[src] = cpos[src] + 1
    local dst = edge[src][cpos[src]]
    if not asked[dst] then
      len[dst] = len[src] + 1
      table.insert(tasks, src)
      table.insert(tasks, dst)
      break
    end
  end
end
local stLine = SegTree.new(2 * n - 2, function(a, b) return a + b end, 0)

local m = io.read("*n")
local mtot = 2^m
local mbox = {}
for i = 1, mtot do
  mbox[i] = 0
end
mbox[1] = 1
local mu, mv = {}, {}
for i = 1, m do
  mu[i], mv[i] = io.read("*n", "*n")
end
local linepos = {}
for il = 1, n - 1 do
  linepos[il] = {}
  local lx, ly = line[il][1], line[il][2]
  for j = 1, #eulers - 1 do
    if lx == eulers[j] and ly == eulers[j + 1] then
      table.insert(linepos[il], j)
    elseif ly == eulers[j] and lx == eulers[j + 1] then
      table.insert(linepos[il], j)
    end
  end
end

for il = 1, n - 1 do
  local lx, ly = line[il][1], line[il][2]
  local p1, p2 = linepos[il][1], linepos[il][2]
  stLine:setValue(p1, 1)
  stLine:setValue(p2, -1)
  local mul = 1
  local actsum = 0
  for im = 1, m do
    local u, v = mu[im], mv[im]
    local pu, pv = posinv[u], posinv[v]
    if pv < pu then pu, pv = pv, pu end
    local parent = st:getRange(pu, pv)
    local activate = false
    local pp = posinv[parent]
    if pv < pp then
      activate = 1 == stLine:getRange(pv, pp - 1)
    elseif pp < pv then
      activate = 1 == stLine:getRange(pp, pv - 1)
    end
    if not activate then
      if pu < pp then
        activate = 1 == stLine:getRange(pu, pp - 1)
      elseif pp < pu then
        activate = 1 == stLine:getRange(pp, pu - 1)
      end
    end
    if activate then
      actsum = actsum + mul
    end
    mul = mul * 2
  end
  if 0 < actsum then
  end
  -- print("IL", il, actsum)
  for im = mtot, 1, -1 do
    local src = im - 1
    local dst = getor(src, actsum)
    -- print("a " .. src .. " " .. dst)
    mbox[dst + 1] = mbox[dst + 1] + mbox[src + 1]
  end
  stLine:setValue(p1, 0)
  stLine:setValue(p2, 0)
  -- print(table.concat(mbox, " "))
end
-- print(table.concat(mbox, " "))
print(mbox[mtot])


local h, w, n = io.read("*n", "*n", "*n")
print(math.min(math.ceil(n / w), math.ceil(n / h)))

Score : 100 points

Problem Statement

We have a grid with H rows and W columns, where all the squares are initially white.

You will perform some number of painting operations on the grid.
In one operation, you can do one of the following two actions:

Choose one row, then paint all the squares in that row black.

Choose one column, then paint all the squares in that column black.

At least how many operations do you need in order to have N or more black squares in the grid?
It is guaranteed that, under the conditions in Constraints, having N or more black squares is always possible by performing some number of operations.

Constraints

1 \leq H \leq 100

1 \leq W \leq 100

1 \leq N \leq H \times W

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H
W
N

Output

Print the minimum number of operations needed.

Sample Input 1

3
7
10

Sample Output 1

2

You can have 14 black squares in the grid by performing the "row" operation twice, on different rows.

Sample Input 2

14
12
112

Sample Output 2

8

Sample Input 3

2
100
200

Sample Output 3

2