class Dice

  def initialize(surfaces)
    @surfaces = surfaces.dup
  end

  def top
    @surfaces[0]
  end

  def front
    @surfaces[1]
  end

  def right
    @surfaces[2]
  end

  def N
    tmp = @surfaces[0]
    @surfaces[0] = @surfaces[1]
    @surfaces[1] = @surfaces[5]
    @surfaces[5] = @surfaces[4]
    @surfaces[4] = tmp
    self
  end

  def E
    tmp = @surfaces[0]
    @surfaces[0] = @surfaces[3]
    @surfaces[3] = @surfaces[5]
    @surfaces[5] = @surfaces[2]
    @surfaces[2] = tmp
    self
  end

  def S
    tmp = @surfaces[0]
    @surfaces[0] = @surfaces[4]
    @surfaces[4] = @surfaces[5]
    @surfaces[5] = @surfaces[1]
    @surfaces[1] = tmp
    self
  end

  def W
    tmp = @surfaces[0]
    @surfaces[0] = @surfaces[2]
    @surfaces[2] = @surfaces[5]
    @surfaces[5] = @surfaces[3]
    @surfaces[3] = tmp
    self
  end

  def adjust_front(front)
    4.times do
      self.N
      return if self.front == front
    end

    self.E
    adjust_front(front)
  end

  def equal_surfaces(other)
    (0..5).all?{|n| surface_val(n) == other.surface_val(n) }
  end

  def surface_val(num)
    @surfaces[num]
  end

  def has_value?(v)
    @surfaces.include?(v)
  end
end

dice1 = Dice.new(gets.chomp.split.map(&:to_i))
dice2 = Dice.new(gets.chomp.split.map(&:to_i))

unless dice2.has_value?(dice1.front)
  puts 'No'
  exit
end

dice2.adjust_front(dice1.front)
4.times do
  dice2.E

  if dice1.equal_surfaces(dice2)
    puts 'Yes'
    exit
  end
end

puts 'No'


def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n
end
def array(size,n=1,init=nil) #nに配列の次元数、sizeに配列の大きさ、initに初期値を投げることでその配列を返す
  if n==1
    return Array.new(size){init}
  else
    return Array.new(n).map{Array.new(size){init}}
  end
end

class Dice
  attr_accessor:node,:top,:s,:e,:num
  def initialize(arr)
    @node=Array.new(6){|i| i+1}
    @num=arr
    @top=1
    @s=2
    @e=3
  end

  def reverse_num(x)
    return 7-x
  end

  def S()
    top=@top
    s=@s
    e=@e
    @top=reverse_num(s)
    @s=top
  end

  def E()
    top=@top
    s=@s
    e=@e
    @top=reverse_num(e)
    @e=top
  end

  def W()
    top=@top
    s=@s
    e=@e
    @top=e
    @e=reverse_num(top)
  end

  def N()
    top=@top
    s=@s
    e=@e
    @top=s
    @s=reverse_num(top)
  end
end

n=gets.to_i
dices=Array.new(n)
n.times do|i|
  a=get_i
  dices[i]=Dice.new(a)
end
flag=false
(n-1).times do|i|
  dice1=dices[i]
  (i+1).upto(n-1) do|j|
    dice2=dices[j]
    x=dice1.num.sort
    y=dice2.num.sort
    f=false
    6.times do|k|
      f=true if x[i]!=y[i]
    end
    if f
      next
    end
    4.times do
      4.times do
        break if dice1.num[dice1.s-1]==dice2.num[dice2.s-1]
        dice2.S
      end
      4.times do
        break if dice1.num[dice1.top-1]==dice2.num[dice2.top-1]
        dice2.W
      end

      break if dice1.num[dice1.s-1]==dice2.num[dice2.s-1] and dice1.num[dice1.top-1]==dice2.num[dice2.top-1]
      dice2.S
      dice2.W
    end

    if ((dice1.num[dice1.s-1]==dice2.num[dice2.s-1]) and (dice1.num[dice1.top-1]==dice2.num[dice2.top-1]) and (dice1.num[dice1.e-1]==dice2.num[dice2.e-1]) and (dice1.num[dice1.reverse_num(dice1.s)-1]==dice2.num[dice2.reverse_num(dice2.s)-1]) and (dice1.num[dice1.reverse_num(dice1.top)-1]==dice2.num[dice2.reverse_num(dice2.top)-1]) and (dice1.num[dice1.reverse_num(dice1.e)-1]==dice2.num[dice2.reverse_num(dice2.e)-1]))
      flag=true
      break
    end
  end
  if flag
    break
  end
end
if flag
  puts "No"
else
  puts "Yes"
end

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

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice IV

Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III.

Input

In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III.

Output

Print "Yes" if given dices are all different, otherwise "No" in a line.

Constraints

$2 \leq n \leq 100$

$0 \leq $ the integer assigned to a face $ \leq 100$

Sample Input 1

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

Sample Output 1

No

Sample Input 2

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

Sample Output 2

Yes

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.


X Cubic

Write a program which calculates the cube of a given integer x.

Input

An integer x is given in a line.

Output

Print the cube of x in a line.

Constraints

1 ≤ x ≤ 100

Sample Input 1

2

Sample Output 1

8

Sample Input 2

3

Sample Output 2

27


def deltaPush(x)
	return x*x*x
end

x = gets.to_i
puts deltaPush(x)


MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Watch

Write a program which reads an integer $S$ [second] and converts it to $h:m:s$ where $h$, $m$, $s$ denote hours, minutes (less than 60) and seconds (less than 60) respectively.

Input

An integer $S$ is given in a line.

Output

Print $h$, $m$ and $s$ separated by ':'. You do not need to put '0' for a value, which consists of a digit.

Constraints

$0 \leq S \leq 86400$

Sample Input 1

46979

Sample Output 1

13:2:59


a = STDIN.gets.split

puts "a < b"  if a[0].to_i <  a[1].to_i
puts "a == b" if a[0].to_i == a[1].to_i
puts "a > b"  if a[0].to_i >  a[1].to_i

Small, Large, or Equal

Write a program which prints small/large/equal relation of given two integers a and b.

Input

Two integers a and b separated by a single space are given in a line.

Output

For given two integers a and b, print

a < b

if a is less than b,

a > b

if a is greater than b, and

a == b

if a equals to b.

Constraints

-1000 ≤ a, b ≤ 1000

Sample Input 1

1 2

Sample Output 1

a < b

Sample Input 2

4 3

Sample Output 2

a > b

Sample Input 3

5 5

Sample Output 3

a == b


ab = gets()
ar = ab.split(" ")
a = ar[0].to_i()
b = ar[1].to_i()
if a < b
  print "a < b"+"\n"
else
  if a > b
    print "a > b"+"\n"
  else
    print "a == b"+"\n"
  end
end


# -*- coding:utf-8 -*-

a, b = STDIN.gets.split.map(&:to_i)

if a > b then
    c = '>'
elsif a < b then
    c = '<'
else
    c = '='
end

printf("%d %c %d\n", a, c, b)


while str = STDIN.gets
  n = str.split(" ")
  a = n[0].to_i
  b = n[1].to_i
  c = n[2].to_i
  if a < b && b < c
    print "はい"
  else
    print "いいえ"
  end
end

Range

Write a program which reads three integers a, b and c, and prints "Yes" if a < b < c, otherwise "No".

Input

Three integers a, b and c separated by a single space are given in a line.

Output

Print "Yes" or "No" in a line.

Constraints

0 ≤ a, b, c ≤ 100

Sample Input 1

1 3 8

Sample Output 1

Yes

Sample Input 2

3 8 1

Sample Output 2

No


def max(a, b, c)
    if a < b && b < c
        puts "Yes"
    else
        puts "No"
    end
end

p max(1, 3, 8)
p max(3, 8, 1)


a, b, c = gets.split(" ").map(&:to_i)
if a < b && b < c
  puts "Yes"
else
  puts "No"
end


Sorting Three Numbers

Write a program which reads three integers, and prints them in ascending order.

Input

Three integers separated by a single space are given in a line.

Output

Print the given integers in ascending order in a line. Put a single space between two integers.

Constraints

1 ≤ the three integers ≤ 10000

Sample Input 1

3 8 1

Sample Output 1

1 3 8


W, H, X, Y, R = gets.split.map &:to_i
C = R < X && X+R < W && R < Y && Y+R < H
puts C ? :Yes : :No

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Circle in a Rectangle

Write a program which reads a rectangle and a circle, and determines whether the circle is arranged inside the rectangle. As shown in the following figures, the upper right coordinate $(W, H)$ of the rectangle and the central coordinate $(x, y)$ and radius $r$ of the circle are given.

Input

Five integers $W$, $H$, $x$, $y$ and $r$ separated by a single space are given in a line.

Output

Print "Yes" if the circle is placed inside the rectangle, otherwise "No" in a line.

Constraints

$ -100 \leq x, y \leq 100$

$ 0 < W, H, r \leq 100$

Sample Input 1

5 4 2 2 1

Sample Output 1

Yes

Sample Input 2

5 4 2 4 1

Sample Output 2

No


w, h, x, y, r = gets.split.map(&:to_i)
if x-r >= 0 && y-r >= 0 && x+r <= w && y+r <= h
	puts "Yes"
else
	puts "No"
end


W, H, x, y, r = gets.split(' ').map{|item| item.to_i}

if 0 <= x-r && x+r <= W && 0 <= y-r && y+r <= H
  puts "Yes"
else
    puts "No"
end


a = gets.split.map(&:to_i)
a.each_with_index {
    |x,i|
    i += 1
    if x == 0 then
        break
    end
        puts "Case #{i}: #{x}"
}

Print Test Cases

In the online judge system, a judge file may include multiple datasets to check whether the submitted program outputs a correct answer for each test case. This task is to practice solving a problem with multiple datasets.

Write a program which reads an integer x and print it as is. Note that multiple datasets are given for this problem.

Input

The input consists of multiple datasets. Each dataset consists of an integer x in a line.

The input ends with an integer 0. You program should not process (print) for this terminal symbol.

Output

For each dataset, print x in the following format:

Case i: x

where i is the case number which starts with 1. Put a single space between "Case" and i. Also, put a single space between ':' and x.

Constraints

1 ≤ x ≤ 10000

The number of datasets ≤ 10000

Sample Input

3
5
11
7
8
19
0

Sample Output

Case 1: 3
Case 2: 5
Case 3: 11
Case 4: 7
Case 5: 8
Case 6: 19


x = []
ARGF.each_line do |line|
  x << line
end
x.each_with_index do |str, i|
  puts "case:#{i} str"
end


cnt = 0
while true
    line = gets.lines
    cnt += 1
    if line[0] == "0\n"
        break
    end
        print "Case ",cnt,": ",line[0]

end


i = 0
while true do
	x = gets.to_i
	if x == 0
        break
    end
    i += 1
    puts "Case #{i}: #{x}"
end


i = 1
loop{
  x = gets.chomp.to_i

  x == 0 && break

  puts "Case #{i}: #{x}"
  i += 1
}


i = 1
while true
  x = gets.chomp.to_i
  if x == 0
    break
  end
  puts "Case #{i}: #{x}"
  i += 1
end


while true
	nums = gets.split.map(&:to_i)

	break if (nums[0]==0&&nums[1]==0)

	puts nums.sort.join(" ")
end

Swapping Two Numbers

Write a program which reads two integers x and y, and prints them in ascending order.

Input

The input consists of multiple datasets. Each dataset consists of two integers x and y separated by a single space.

The input ends with two 0 (when both x and y are zero). Your program should not process for these terminal symbols.

Output

For each dataset, print x and y in ascending order in a line. Put a single space between x and y.

Constraints

0 ≤ x, y ≤ 10000

the number of datasets ≤ 3000

Sample Input

3 2
2 2
5 3
0 0

Sample Output

2 3
2 2
3 5


while true
  x,y = gets.split.map(&:to_i)
  if x == 0 && y == 0
    break
  elsif x < y
  puts "#{x} #{y}"
  else
  puts "#{y} #{x}"
 end
end


while true
  x, y = gets.chomp.split(" ").map(&:to_i)
  break if (x == 0 && y == 0)
  if x >= y
    puts "#{x} #{y}"
  else
    puts "#{y} #{x}"
  end
end


for i in 1..3000
    x, y = gets.split
    x = x.to_i;y = y.to_i;
    if x == 0 && y == 0
        break
    end
    if x <= y
        puts "%d %d" %[x, y]
    else
        puts "%d %d" %[y, x]
    end
end


a,b = gets.split.map(&:to_i)
d = a/b
r = a%b
f = a.to_f/b
puts "#{d} #{r} #{f}"

A/B Problem

Write a program which reads two integers a and b, and calculates the following values:

a ÷ b: d (in integer)

remainder of a ÷ b: r (in integer)

a ÷ b: f (in real number)

Input

Two integers a and b are given in a line.

Output

Print d, r and f separated by a space in a line. For f, the output should not contain an absolute error greater than 10-5.

Constraints

1 ≤ a, b ≤ 109

Sample Input 1

3 2

Sample Output 1

1 1 1.50000


a,b=STDIN.gets.split.map(&:to_i)

print a/b,' ', a%b,' ', a/b.to_f, "\n"


a, b = STDIN.gets.split.map(&:to_i)
d=a/b
r=a%b
f=a.to_f/b.to_f

puts sprintf("%d %d %.5f",d,r,f)


init = gets.split.map(&:to_i)
a,b  = init[0], init[1]
puts "#{a/b} #{(a % b)} #{(a/b).to_f.round(5)}"


a,b = gets.chomp.split(" ").map(&:to_i)
puts "#{a/b} #{a%b} #{1.0*a/b}"


r = gets.to_i
pi = Math::PI
a = r ** 2 * pi
s = r * 2 * pi

puts a.to_s + " " + s.to_s

Circle

Write a program which calculates the area and circumference of a circle for given radius r.

Input

A real number r is given.

Output

Print the area and circumference of the circle in a line. Put a single space between them. The output should not contain an absolute error greater than 10-5.

Constraints

0 < r < 10000

Sample Input 1

2

Sample Output 1

12.566371 12.566371

Sample Input 2

3

Sample Output 2

28.274334 18.849556


r = gets.to_f
s = r * r * Math::PI
c = 2 * Math::PI * r
puts [s, c].join(' ')


r=gets.to_f
pi=Math::PI
puts "#{r*r*pi} #{2*r*pi}"


include Math
pi = Math::PI
r=gets.chomp.to_f
printf "%.6f %.6f\n", (pi*r*r), (2*pi*r)


while true
  a, op, b = gets.split(" ")
  a = a.to_i; b = b.to_i
  case op
  when "+"
    puts "#{a + b}"
  when "-"
    puts "#{a - b}"
  when "*"
    puts "#{a * b}"
  when "/"
    puts "#{a / b}"
  when "?"
    break
  end
end

Simple Calculator

Write a program which reads two integers a, b and an operator op, and then prints the value of a op b.

The operator op is '+', '-', '*' or '/' (sum, difference, product or quotient). The division should truncate any fractional part.

Input

The input consists of multiple datasets. Each dataset is given in the following format.

a op b

The input ends with a dataset where op = '?'. Your program should not process for this dataset.

Output

For each dataset, print the value in a line.

Constraints

0 ≤ a, b ≤ 20000

No divisions by zero are given.

Sample Input 1

1 + 2
56 - 18
13 * 2
100 / 10
27 + 81
0 ? 0

Sample Output 1

3
38
26
10
108


loop do
 a,op,b = gets.split()
 case op
  when "+" then puts (a.to_i+b.to_i)
  when "-" then puts (a.to_i-b.to_i)
  when "*" then puts (a.to_i*b.to_i)
  when "/" then puts (a.to_i/b.to_i)
  when "?" then break
 end
end


x, op, y = gets.split
if op != "?" then
  x, y = x.to_i, y.to_f
  puts x.method(op).(y)
end


x = gets
a = gets.split.map(&:to_i)
puts [a.max, a.min, a.inject(:+)]

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Min, Max and Sum

Write a program which reads a sequence of $n$ integers $a_i (i = 1, 2, ... n)$, and prints the minimum value, maximum value and sum of the sequence.

Input

In the first line, an integer $n$ is given. In the next line, $n$ integers $a_i$ are given in a line.

Output

Print the minimum value, maximum value and sum in a line. Put a single space between the values.

Constraints

$0 < n \leq 10000$

$-1000000 \leq a_i \leq 1000000$

Sample Input

5
10 1 5 4 17

Sample Output

1 17 37


gets
nums = gets.split(' ').map(&:to_i)
puts "#{nums.min} #{nums.max} #{nums.sum}"


while true
    h,w=gets.split.map(&:to_i)
    break if h==0 && w==0

    h.times{puts "#"*w}
    puts
end

Print a Rectangle

Draw a rectangle which has a height of H cm and a width of W cm. Draw a 1-cm square by single '#'.

Input

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.

The input ends with two 0 (when both H and W are zero).

Output

For each dataset, print the rectangle made of H × W '#'.

Print a blank line after each dataset.

Constraints

1 ≤ H ≤ 300

1 ≤ W ≤ 300

Sample Input

3 4
5 6
2 2
0 0

Sample Output

####
####
####

######
######
######
######
######

##
##


inputs = []
while str = STDIN.gets
  if str.chomp == "0 0" then break end
  inputs << str.chomp
end

def describe_rectangle(h, w)
  # puts "h = #{h}, w = #{w}"
  output = ""
  for j in 1..h do
    for i in 1..w do
      output += "#"
    end
    output += "\n"
  end
  puts output
end

# puts inputs
for num in 0..inputs.length - 1 do
  rec = inputs[num].split(" ")
  # puts "rec = #{rec}"
  describe_rectangle(rec[0].to_i, rec[1].to_i)
  puts "\n"
end


loop{
  h, w = gets.split.map &:to_i
  break if h == 0 && w == 0
  puts '#' * w
  (h-2).times{|i|
    print '#' if w > 1
    puts '.' * [w-2, 0].max + '#'
  }
  puts '#' * w
  puts
}

Print a Frame

Draw a frame which has a height of H cm and a width of W cm. For example, the following figure shows a frame which has a height of 6 cm and a width of 10 cm.

##########
#........#
#........#
#........#
#........#
##########

Input

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.

The input ends with two 0 (when both H and W are zero).

Output

For each dataset, print the frame made of '#' and '.'.

Print a blank line after each dataset.

Constraints

3 ≤ H ≤ 300

3 ≤ W ≤ 300

Sample Input

3 4
5 6
3 3
0 0

Sample Output

####
#..#
####

######
#....#
#....#
#....#
######

###
#.#
###


while true
h,w =gets.split.map(&:to_i)
break if ( h==0 && w==0 )

h.times{
   w.times{
   if (w%2 == 0 && h%2 == 1) then
       print "."
    elsif print "#"
       end}
puts
       }

end

Print a Chessboard

Draw a chessboard which has a height of H cm and a width of W cm. For example, the following figure shows a chessboard which has a height of 6 cm and a width of 10 cm.

#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#

Note that the top left corner should be drawn by '#'.

Input

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.

The input ends with two 0 (when both H and W are zero).

Output

For each dataset, print the chessboard made of '#' and '.'.

Print a blank line after each dataset.

Constraints

1 ≤ H ≤ 300

1 ≤ W ≤ 300

Sample Input

3 4
5 6
3 3
2 2
1 1
0 0

Sample Output

#.#.
.#.#
#.#.

#.#.#.
.#.#.#
#.#.#.
.#.#.#
#.#.#.

#.#
.#.
#.#

#.
.#

#


loop{
h,w = gets.split.map(&:to_i)
if h == 0 && w == 0
    break
end
i = 0
h.times{
    i += 1
    if i.odd?
        if w.odd?
            puts "#." * (w / 2) + "#"
        elsif w.even?
            puts "#." * (w / 2)
        end
    elsif i.even?
        if w.odd?
            puts ".#" * (w / 2) + "."
        elsif w.even?
            puts ".#" * (w / 2)
        end
    end
}
puts ""
}


x = gets.to_i
for i in 1..x do
    if i % 3 == 0 then
        print " #{i}"
    elsif i.to_s.include?('3')
        print " #{i}"
    end
end
puts

Structured Programming

In programming languages like C/C++, a goto statement provides an unconditional jump from the "goto" to a labeled statement. For example, a statement "goto CHECK_NUM;" is executed, control of the program jumps to CHECK_NUM. Using these constructs, you can implement, for example, loops.

Note that use of goto statement is highly discouraged, because it is difficult to trace the control flow of a program which includes goto.

Write a program which does precisely the same thing as the following program (this example is wrtten in C++). Let's try to write the program without goto statements.

void call(int n){
int i = 1;
CHECK_NUM:
int x = i;
if ( x % 3 == 0 ){
cout << " " << i;
goto END_CHECK_NUM;
}
INCLUDE3:
if ( x % 10 == 3 ){
cout << " " << i;
goto END_CHECK_NUM;
}
x /= 10;
if ( x ) goto INCLUDE3;
END_CHECK_NUM:
if ( ++i <= n ) goto CHECK_NUM;

cout << endl;
}

Input

An integer n is given in a line.

Output

Print the output result of the above program for given integer n.

Constraints

3 ≤ n ≤ 10000

Sample Input

30

Sample Output

3 6 9 12 13 15 18 21 23 24 27 30

Put a single space character before each element.


n=gets.to_i
mp=gets.split(' ').map(&:to_i).sort!
n.downto(1){ |i|
   if i==1 then
   printf("%d\n",mp[i-1])
   else
   printf("%d ",mp[i-1])
   end
}

Reversing Numbers

Write a program which reads a sequence and prints it in the reverse order.

Input

The input is given in the following format:

n
a1 a2 . . . an

n is the size of the sequence and ai is the ith element of the sequence.

Output

Print the reversed sequence in a line. Print a single space character between adjacent elements (Note that your program should not put a space character after the last element).

Constraints

n ≤ 100

0 ≤ ai < 1000

Sample Input 1

5
1 2 3 4 5

Sample Output 1

5 4 3 2 1

Sample Input 2

8
3 3 4 4 5 8 7 9

Sample Output 2

9 7 8 5 4 4 3 3

Note

      解説


x = gets.to_i
arr = gets.split.collect{|item| item.to_i}

puts arr.reverse.join " "


t = gets.chomp

a = gets.chomp.split

puts a.reverse.join(" ")


gets
ary = []
["S", "H", "C", "D"].each do |a|
  13.times{|i| ary << "#{a} #{i+1}"}
end

ARGF.each_line do |line|
  data = line.chomp
  ary.delete(data) if ary.include?(data)
end
puts ary

Finding Missing Cards

Taro is going to play a card game. However, now he has only n cards, even though there should be 52 cards (he has no Jokers).

The 52 cards include 13 ranks of each of the four suits: spade, heart, club and diamond.

Input

In the first line, the number of cards n (n ≤ 52) is given.

In the following n lines, data of the n cards are given. Each card is given by a pair of a character and an integer which represent its suit and rank respectively. A suit is represented by 'S', 'H', 'C' and 'D' for spades, hearts, clubs and diamonds respectively. A rank is represented by an integer from 1 to 13.

Output

Print the missing cards. The same as the input format, each card should be printed with a character and an integer separated by a space character in a line.
Arrange the missing cards in the following priorities:

Print cards of spades, hearts, clubs and diamonds in this order.

If the suits are equal, print cards with lower ranks first.

Sample Input

47
S 10
S 11
S 12
S 13
H 1
H 2
S 6
S 7
S 8
S 9
H 6
H 8
H 9
H 10
H 11
H 4
H 5
S 2
S 3
S 4
S 5
H 12
H 13
C 1
C 2
D 1
D 2
D 3
D 4
D 5
D 6
D 7
C 3
C 4
C 5
C 6
C 7
C 8
C 9
C 10
C 11
C 13
D 9
D 10
D 11
D 12
D 13

Sample Output

S 1
H 3
H 7
C 12
D 8

Note

      解説


# 4棟3階建て10フロアの配列
building = Array.new(4){ Array.new(3){ Array.new(10, 0)}}

N = gets.chomp.to_i

for i in  1..N
	imput = gets.chomp.split("\s")

	# imputされた文字列をintegerに変換
	for k in 0..3
		imput[k] = imput[k].to_i
	end

	# 数値の代入
	building[imput[0] - 1][imput[1] - 1][imput[2] - 1] += imput[3]
end

# 出力

# aa棟bb階の状態を出力する
for aa in 0..3
	for bb in 0..2

		# 最終行だけは改行を加えない
		if aa == 3 && bb == 2
			print("\s", building[aa][bb].join("\s"))
		else
			print("\s", building[aa][bb].join("\s"), "\n")
		end
	end

	# 最後の段以外は仕切りを入れる
	if aa != 3
		puts "#" * 20
	end
end

Official House

You manage 4 buildings, each of which has 3 floors, each of which consists of 10 rooms. Write a program which reads a sequence of tenant/leaver notices, and reports the number of tenants for each room.

For each notice, you are given four integers b, f, r and v which represent that v persons entered to room r of fth floor at building b. If v is negative, it means that −v persons left.

Assume that initially no person lives in the building.

Input

In the first line, the number of notices n is given. In the following n lines, a set of four integers b, f, r and v which represents ith notice is given in a line.

Output

For each building, print the information of 1st, 2nd and 3rd floor in this order. For each floor information, print the number of tenants of 1st, 2nd, .. and 10th room in this order. Print a single space character before the number of tenants. Print "####################" (20 '#') between buildings.

Constraints

No incorrect building, floor and room numbers are given.

0 ≤ the number of tenants during the management ≤ 9

Sample Input

3
1 1 3 8
3 2 2 7
4 3 8 1

Sample Output

0 0 8 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
####################
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
####################
0 0 0 0 0 0 0 0 0 0
0 7 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
####################
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0


A = Array.new
b = Array.new
n, m = gets.split.map(&:to_i)
n.times {A << gets.split.map(&:to_i)}
m.times {b << gets.to_i}

A.each do |is|
  j = sum = 0
  is.each do |i|
    sum += i * b[j]
    j += 1
  end
  puts sum
end

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Matrix Vector Multiplication

Write a program which reads a $ n \times m$ matrix $A$ and a $m \times 1$ vector $b$, and prints their product $Ab$.

A column vector with m elements is represented by the following equation.

\[
b = \left(
\begin{array}{c}
b_1 \\
b_2 \\
: \\
b_m \\
\end{array}
\right)
\]

A $n \times m$ matrix with $m$ column vectors, each of which consists of $n$ elements, is represented by the following equation.

\[
A = \left(
\begin{array}{cccc}
a_{11} & a_{12} & ... & a_{1m} \\
a_{21} & a_{22} & ... & a_{2m} \\
: & : & : & : \\
a_{n1} & a_{n2} & ... & a_{nm} \\
\end{array}
\right)
\]

$i$-th element of a $m \times 1$ column vector $b$ is represented by $b_i$ ($i = 1, 2, ..., m$), and the element in $i$-th row and $j$-th column of a matrix $A$ is represented by $a_{ij}$ ($i = 1, 2, ..., n,$ $j = 1, 2, ..., m$).

The product of a $n \times m$ matrix $A$ and a $m \times 1$ column vector $b$ is a $n \times 1$ column vector $c$, and $c_i$ is obtained by the following formula:

\[
c_i = \sum_{j=1}^m a_{ij}b_j = a_{i1}b_1 + a_{i2}b_2 + ... + a_{im}b_m
\]

Input

In the first line, two integers $n$ and $m$ are given. In the following $n$ lines, $a_{ij}$ are given separated by a single space character. In the next $m$ lines, $b_i$ is given in a line.

Output

The output consists of $n$ lines. Print $c_i$ in a line.

Constraints

$1 \leq n, m \leq 100$

$0 \leq b_i, a_{ij} \leq 1000$

Sample Input

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

Sample Output

5
6
9


n,m = gets.split.map(&:to_i)
a_vec = []
n.times do
  a_vec << gets.split.map(&:to_i)
end

b_vec =[]
m.times do
  b_vec << gets.to_i
end

a_vec.each do |v|
  sum = 0
  m.times do |i|
    sum += (v[i] * b_vec[i])
  end
  puts sum
end


loop do
  m, f, r = gets.split(" ").map(&:to_i)
    exit if m == -1 and f == -1 and r == -1

  puts "F" if (m == -1 or f == -1 ) or (m + f < 30)
  puts "A" if 80 <= m + f
  puts "B" if 65 <= m + f and m + f < 80
  puts "C" if 50 <= m + f and m + f < 65
  if 30 <= m + f and m + f < 50
    puts r <= 50 ? "C" : "D"
  end
end

Grading

Write a program which reads a list of student test scores and evaluates the performance for each student.

The test scores for a student include scores of the midterm examination m (out of 50), the final examination f (out of 50) and the makeup examination r (out of 100). If the student does not take the examination, the score is indicated by -1.

The final performance of a student is evaluated by the following procedure:

If the student does not take the midterm or final examination, the student's grade shall be F.

If the total score of the midterm and final examination is greater than or equal to 80, the student's grade shall be A.

If the total score of the midterm and final examination is greater than or equal to 65 and less than 80, the student's grade shall be B.

If the total score of the midterm and final examination is greater than or equal to 50 and less than 65, the student's grade shall be C.

If the total score of the midterm and final examination is greater than or equal to 30 and less than 50, the student's grade shall be D. However, if the score of the makeup examination is greater than or equal to 50, the grade shall be C.

If the total score of the midterm and final examination is less than 30, the student's grade shall be F.

Input

The input consists of multiple datasets. For each dataset, three integers m, f and r are given in a line.

The input ends with three -1 for m, f and r respectively. Your program should not process for the terminal symbols.

The number of datasets (the number of students) does not exceed 50.

Output

For each dataset, print the grade (A, B, C, D or F) in a line.

Sample Input

40 42 -1
20 30 -1
0 2 -1
-1 -1 -1

Sample Output

A
C
F


num = []
r, c = gets.split.map(&:to_i)
sum_line = Array.new(c, 0)
r.times do
  num << gets.split.map(&:to_i)
end

num.each_with_index do |m, i|
  puts num[i].join(" ") + " " + num[i].inject(:+).to_s
  sum_line.map!.with_index {|arr, j| arr + num[i][j]}
end
 puts sum_line.join(" ") + " " + sum_line.inject(:+).to_s

Spreadsheet

Your task is to perform a simple table calculation.

Write a program which reads the number of rows r, columns c and a table of r × c elements, and prints a new table, which includes the total sum for each row and column.

Input

In the first line, two integers r and c are given. Next, the table is given by r lines, each of which consists of c integers separated by space characters.

Output

Print the new table of (r+1) × (c+1) elements. Put a single space character between adjacent elements. For each row, print the sum of it's elements in the last column. For each column, print the sum of it's elements in the last row. Print the total sum of the elements at the bottom right corner of the table.

Constraints

1 ≤ r, c ≤ 100

0 ≤ an element of the table ≤ 100

Sample Input

4 5
1 1 3 4 5
2 2 2 4 5
3 3 0 1 1
2 3 4 4 6

Sample Output

1 1 3 4 5 14
2 2 2 4 5 15
3 3 0 1 1 8
2 3 4 4 6 19
8 9 9 13 17 56


m,n = gets.split.map(&:to_i)
mat = []

for i in 1..m
temp = gets.split.map(&:to_i)
temp.push(temp.inject(:+))
mat.push(temp)
puts temp.join(" ")
end

temp = []
for i in 0..n

sum = 0
for j in 0..(m-1)
sum += mat[j][i]
end

temp.push(sum)

end

puts temp.join(" ")


Toggling Cases

Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string.

Input

A string is given in a line.

Output

Print the converted string in a line. Note that you do not need to convert any characters other than alphabetical letters.

Constraints

The length of the input string < 1200

Sample Input

fAIR, LATER, OCCASIONALLY CLOUDY.

Sample Output

Fair, later, occasionally cloudy.


while (n = gets.to_i) != 0
  c = 0
  while n > 0
    c += n%10
    n /= 10
  end
  puts c
end

Sum of Numbers

Write a program which reads an integer and prints sum of its digits.

Input

The input consists of multiple datasets. For each dataset, an integer x is given in a line. The number of digits in x does not exceed 1000.

The input ends with a line including single zero. Your program should not process for this terminal symbol.

Output

For each dataset, print the sum of digits in x.

Sample Input

123
55
1000
0

Sample Output

6
10
1


#入力した文書の中にアルファベット数字をいくつはいっているかをわかるプログラム
text = gets("").downcase    #テキストに小文字をとる

for alphabet in "a".."z"           #sをA から　Zまで２６回繰り返す
  puts "#{alphabet} : #{text.count(alphabet)}"      #まずAからZまで繰り返して　テキストの中のアルファベットをカウントする
end

Counting Characters

Write a program which counts and reports the number of each alphabetical letter. Ignore the case of characters.

Input

A sentence in English is given in several lines.

Output

Prints the number of alphabetical letters in the following format:

a : The number of 'a'
b : The number of 'b'
c : The number of 'c'
.
.
z : The number of 'z'

Constraints

The number of characters in the sentence < 1200

Sample Input

This is a pen.

Sample Output

a : 1
b : 0
c : 0
d : 0
e : 1
f : 0
g : 0
h : 1
i : 2
j : 0
k : 0
l : 0
m : 0
n : 1
o : 0
p : 1
q : 0
r : 0
s : 2
t : 1
u : 0
v : 0
w : 0
x : 0
y : 0
z : 0


MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Ring

Write a program which finds a pattern $p$ in a ring shaped text $s$.

Input

In the first line, the text $s$ is given.

In the second line, the pattern $p$ is given.

Output

If $p$ is in $s$, print Yes in a line, otherwise No.

Constraints

$1 \leq $ length of $p \leq $ length of $s \leq 100$

$s$ and $p$ consists of lower-case letters

Sample Input 1

vanceknowledgetoad
advance

Sample Output 1

Yes

Sample Input 2

vanceknowledgetoad
advanced

Sample Output 2

No


str = gets.chomp!
n = gets.to_i
n.times do
    p,a,b,c= gets.split
    a = a.to_i
    b = b.to_i
    if p == "print"
        puts str[a..b]
    elsif p == "reverse"
        str[a..b] = str[a..b].reverse
    else
        str[a..b] = c
    end
end

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Transformation

Write a program which performs a sequence of commands to a given string $str$. The command is one of:

print a b: print from the a-th character to the b-th character of $str$

reverse a b: reverse from the a-th character to the b-th character of $str$

replace a b p: replace from the a-th character to the b-th character of $str$ with p

Note that the indices of $str$ start with 0.

Input

In the first line, a string $str$ is given. $str$ consists of lowercase letters. In the second line, the number of commands q is given. In the next q lines, each command is given in the above mentioned format.

Output

For each print command, print a string in a line.

Constraints

$1 \leq $ length of $str \leq 1000$

$1 \leq q \leq 100$

$0 \leq a \leq b < $ length of $str$

for replace command, $b - a + 1 = $ length of $p$

Sample Input 1

abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4

Sample Output 1

xaze

Sample Input 2

xyz
3
print 0 2
replace 0 2 abc
print 0 2

Sample Output 2

xyz
abc


n = gets.to_i
array_1 = gets.split(" ").map(&:to_i)
m = gets.to_i
array_2 = gets.split(" ").map(&:to_i)
puts (array_1 & array_2).sort.join("\n") if (array_1 & array_2).size > 0

Set Intersection

Find the intersection of two sets $A = \{a_0, a_1, ..., a_{n-1}\}$ and $B = \{b_0, b_1, ..., b_{m-1}\}$.

Input

The input is given in the following format.

$n$
$a_0 \; a_1 \; ... \; a_{n-1}$
$m$
$b_0 \; b_1 \; ... \; b_{m-1}$

Elements of $A$ and $B$ are given in ascending order respectively. There are no duplicate elements in each set.

Output

Print elements in the intersection in ascending order. Print an element in a line.

Constraints

$1 \leq n, m \leq 200,000$

$0 \leq a_0 < a_1 < ... < a_{n-1} \leq 10^9$

$0 \leq b_0 < b_1 < ... < b_{m-1} \leq 10^9$

Sample Input 1

4
1 2 5 8
5
2 3 5 9 11

Sample Output 1

2
5


def is_prime?(num)
    $primes = []
    return true if num == 2
    return false if num % 2 == 0
    $primes.each do |n|
        return false if num % n == 0
    end
    $primes << num
    return true
end

n = gets.chomp.to_i
nn = n
sub_n = 2
prime_factors = []
while n > 1
	if n % sub_n == 0
      n /= sub_n
	  prime_factors << sub_n
	  next
	else
      break if sub_n * sub_n > n
      if sub_n == 2
        sub_n += 1
      else
        sub_n += 2
        while !is_prime?(sub_n)
          sub_n += 2
        end
      end
	end
end
str = "#{nn}:"
prime_factors.each do |k|
	str += " #{k}"
end
if n == nn
  str << " #{nn}"
end
puts str

Prime Factorization

Factorize a given integer n.

Input

n

An integer n is given in a line.

Output

Print the given integer n and :. Then, print prime factors in ascending order. If n is divisible by a prime factor several times, the prime factor should be printed according to the number of times. Print a space before each prime factor.

Constraints

2 ≤ n ≤ 109

Sample Input 1

12

Sample Output 1

12: 2 2 3

Sample Input 2

126

Sample Output 2

126: 2 3 3 7


require "bigdecimal"
a, b = gets.split.map { |x| BigDecimal(x) }
puts (a + b).floor

Addition of Big Integers

Given two integers $A$ and $B$, compute the sum, $A + B$.

Input

Two integers $A$ and $B$ separated by a space character are given in a line.

Output

Print the sum in a line.

Constraints

$-1 \times 10^{100000} \leq A, B \leq 10^{100000}$

Sample Input 1

5 8

Sample Output 1

13

Sample Input 2

100 25

Sample Output 2

125

Sample Input 3

-1 1

Sample Output 3

0

Sample Input 4

12 -3

Sample Output 4

9


n = gets.to_i
i = 0
n.times do
  a, b = gets.split(" ").map(&:to_i)
  if a == b
    i += 1
  else
    i = 0
  end
  break if i == 3
end

puts (i == 3 ? "Yes" : "No")

Score : 200 points

Problem Statement

Tak performed the following action N times: rolling two dice.
The result of the i-th roll is D_{i,1} and D_{i,2}.

Check if doublets occurred at least three times in a row.
Specifically, check if there exists at lease one i such that D_{i,1}=D_{i,2}, D_{i+1,1}=D_{i+1,2} and D_{i+2,1}=D_{i+2,2} hold.

Constraints

3 \leq N \leq 100

1\leq D_{i,j} \leq 6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
D_{1,1} D_{1,2}
\vdots
D_{N,1} D_{N,2}

Output

Print Yes if doublets occurred at least three times in a row. Print No otherwise.

Sample Input 1

5
1 2
6 6
4 4
3 3
3 2

Sample Output 1

Yes

From the second roll to the fourth roll, three doublets occurred in a row.

Sample Input 2

5
1 1
2 2
3 4
5 5
6 6

Sample Output 2

No

Sample Input 3

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

Sample Output 3

Yes


def divisor(num)
  result = []
  i = 1
  while (i*i) <= num do
    remainder = num % i
    if remainder == 0
      result << i
      if i*i!=num
          result<<(num/i)
      end
    end
    i += 1
  end
  return result
end

@n=gets.to_i
count=0

for i in 1..@n-1
    v=@n-i
    count+=divisor(v).length
end
p count

Score : 300 points

Problem Statement

Given is a positive integer N.
How many tuples (A,B,C) of positive integers satisfy A \times B + C = N?

Constraints

2 \leq N \leq 10^6

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

3

Sample Output 1

3

There are 3 tuples of integers that satisfy A \times B + C = 3: (A, B, C) = (1, 1, 2), (1, 2, 1), (2, 1, 1).

Sample Input 2

100

Sample Output 2

473

Sample Input 3

1000000

Sample Output 3

13969985


n, x, m = gets.split(' ').map{|i| i.to_i}

z = 0
a = x
n.times do |i|
  z += a
  a = a ** 2 % m
  break if a == 0
#  puts "#{i} : #{z}" if i % 100 == 0
end
puts z

Score : 500 points

Problem Statement

Let us denote by f(x, m) the remainder of the Euclidean division of x by m.

Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M).
Find \displaystyle{\sum_{i=1}^N A_i}.

Constraints

1 \leq N \leq 10^{10}

0 \leq X < M \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X M

Output

Print \displaystyle{\sum_{i=1}^N A_i}.

Sample Input 1

6 2 1001

Sample Output 1

1369

The sequence A begins 2,4,16,256,471,620,\ldots Therefore, the answer is 2+4+16+256+471+620=1369.

Sample Input 2

1000 2 16

Sample Output 2

6

The sequence A begins 2,4,0,0,\ldots Therefore, the answer is 6.

Sample Input 3

10000000000 10 99959

Sample Output 3

492443256176507


X = gets.to_i

# $stdout = File.open(File::NULL, "w")

def main
  return (X + 1) % 2
end

STDOUT.puts main

Score : 100 points

Problem Statement

Given is an integer x that is greater than or equal to 0, and less than or equal to 1.
Output 1 if x is equal to 0, or 0 if x is equal to 1.

Constraints

0 \leq x \leq 1

x is an integer

Input

Input is given from Standard Input in the following format:

x

Output

Print 1 if x is equal to 0, or 0 if x is equal to 1.

Sample Input 1

1

Sample Output 1

0

Sample Input 2

0

Sample Output 2

1


Score : 300 points

Problem Statement

How many integer sequences A_1,A_2,\ldots,A_N of length N satisfy all of the following conditions?

0 \leq A_i \leq 9

There exists some i such that A_i=0 holds.

There exists some i such that A_i=9 holds.

The answer can be very large, so output it modulo 10^9 + 7.

Constraints

1 \leq N \leq 10^6

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer modulo 10^9 + 7.

Sample Input 1

2

Sample Output 1

2

Two sequences \{0,9\} and \{9,0\} satisfy all conditions.

Sample Input 2

1

Sample Output 2

0

Sample Input 3

869121

Sample Output 3

2511445


class SegTree
    def update(k); @d[k] = @op.call(@d[2 * k], @d[2 * k + 1]); end

    def initialize(v, e, &op)
        @n = v.size
        @log = (@n - 1).bit_length
        @size = 1 << @log
        @op = op

        @d = Array.new(2 * @size, e) # vector

        @n.times{|i| @d[@size + i] = v[i]}
        (@size - 1).downto(1) {|i| update(i)}
    end

    def set(pos, x)
        pos += @size
        @d[pos] = x
        1.upto(@log) {|i| update(pos >> i)}
    end

    def get(pos); @d[pos + @size]; end

    def prod(l, r)
        sml = smr = @d[0]
        l += @size
        r += @size

        while l < r
            (sml = @op.call(sml, @d[l]); l += 1) if l[0] == 1
            smr = @op.call(@d[r -= 1], smr) if r[0] == 1
            l >>= 1
            r >>= 1
        end
        @op.call(sml, smr)
    end

    def all_prod; @d[1]; end

    def max_right(l, &f)
        return @n if l == @n
        l += @size
        sm = @d[0]

        loop do
            l >>= 1 while l[0] == 0
            x = @op.call(sm, @d[l])
            if !f.call(x)
                while l < @size
                    l <<= 1
                    x = @op.call(sm, @d[l])
                    if f.call(x)
                        sm = x
                        l += 1
                    end
                end
                return l - @size
            end

            sm = x
            l += 1
            break if (l & -l) == l
        end

        @n
    end

    def min_left(r, &f)
        return 0 if r == 0
        r += @size
        sm = @d[0]

        loop do
            r -= 1
            r >>= 1 while r > 1 && r[0] == 1
            x = @op.call(@d[r], sm)
            if !f.call(x)
                while r < @size
                    r = 2 * r + 1
                    x = @op.call(@d[r], sm)
                    if f.call(x)
                        sm = x
                        r -= 1
                    end
                end
                return r + 1 - @size
            end
            sm = x
            break if (r & -r) == r
        end

        0
    end
end

n, q = gets.split.map(&:to_i)
st = SegTree.new(gets.split.map(&:to_i), -1) {|ch1, ch2| ch1 > ch2 ? ch1 : ch2}

q.times do
    t, x, y = gets.split.map(&:to_i)

    if t == 1
        st.set(x-1, y)
    elsif t == 2
        puts st.prod(x-1, y)
    else
        puts st.max_right(x-1){|v| v < y} + 1
    end
end

Score : 100 points

Problem Statement

You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types.

The type of i-th query is represented by T_i.

T_i=1: You are given two integers X_i,V_i. Replace the value of A_{X_i} with V_i.

T_i=2: You are given two integers L_i,R_i. Calculate the maximum value among A_{L_i},A_{L_i+1},\cdots,A_{R_i}.

T_i=3: You are given two integers X_i,V_i. Calculate the minimum j such that X_i \leq j \leq N, V_i \leq A_j. If there is no such j, answer j=N+1 instead.

Constraints

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

0 \leq A_i \leq 10^9

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

1 \leq T_i \leq 3

1 \leq X_i \leq N, 0 \leq V_i \leq 10^9 (T_i=1,3)

1 \leq L_i \leq R_i \leq N (T_i=2)

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \cdots A_N
First query
Second query
\vdots
Q-th query

Each query is given in the following format:

If T_i=1,3,

T_i X_i V_i

If T_i=2,

T_i L_i R_i

Output

For each query with T_i=2, 3, print the answer.

Sample Input 1

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

Sample Output 1

3
3
2
6

First query: Print 3, which is the maximum of (A_1,A_2,A_3,A_4,A_5)=(1,2,3,2,1).

Second query: Since 3>A_2, j=2 does not satisfy the condition．Since 3 \leq A_3, print j=3.

Third query: Replace the value of A_3 with 1. It becomes A=(1,2,1,2,1).

Fourth query: Print 2, which is the maximum of (A_2,A_3,A_4)=(2,1,2).

Fifth query: Since there is no j that satisfies the condition, print j=N+1=6.


D, T, S = gets.split.map(&:to_i)

puts T*S >= D ? 'Yes' : 'No'

Score : 100 points

Problem Statement

Takahashi is meeting up with Aoki.

They have planned to meet at a place that is D meters away from Takahashi's house in T minutes from now.

Takahashi will leave his house now and go straight to the place at a speed of S meters per minute.

Will he arrive in time?

Constraints

1 \leq D \leq 10000

1 \leq T \leq 10000

1 \leq S \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

D T S

Output

If Takahashi will reach the place in time, print Yes; otherwise, print No.

Sample Input 1

1000 15 80

Sample Output 1

Yes

It takes 12.5 minutes to go 1000 meters to the place at a speed of 80 meters per minute. They have planned to meet in 15 minutes so he will arrive in time.

Sample Input 2

2000 20 100

Sample Output 2

Yes

It takes 20 minutes to go 2000 meters to the place at a speed of 100 meters per minute. They have planned to meet in 20 minutes so he will arrive just on time.

Sample Input 3

10000 1 1

Sample Output 3

No

He will be late.


s = gets.chomp.split("")
t = gets.chomp.split("")

min = Float::INFINITY

(0 .. s.size - t.size).each{ |i|
	count = 0

	t.each_with_index{ |tt, j|
		if tt != s[i, t.size][j] then
			count += 1
		end
	}

	if min > count then
		min = count
	end
}

puts min

Score : 200 points

Problem Statement

Given are two strings S and T.

Let us change some of the characters in S so that T will be a substring of S.

At least how many characters do we need to change?

Here, a substring is a consecutive subsequence. For example, xxx is a substring of yxxxy, but not a substring of xxyxx.

Constraints

The lengths of S and T are each at least 1 and at most 1000.

The length of T is at most that of S.

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the minimum number of characters in S that need to be changed.

Sample Input 1

cabacc
abc

Sample Output 1

1

For example, changing the fourth character a in S to c will match the second through fourth characters in S to T.

Since S itself does not have T as its substring, this number of changes - one - is the minimum needed.

Sample Input 2

codeforces
atcoder

Sample Output 2

6


require "set"
n,m = gets.chomp.split(" ").map(&:to_i)

hash = {}
m.times do
  a,b = gets.chomp.split(" ").map(&:to_i)
  a,b = b,a if b < a
  hash[a] ||= Set.new
  hash[a] << a
  hash[b] = hash[a]
  hash[b] << b
end

result = 0
hash.each do |k, v|
  result = v.length if v.length > result
end
puts result

Score : 400 points

Problem Statement

There are N persons called Person 1 through Person N.

You are given M facts that "Person A_i and Person B_i are friends." The same fact may be given multiple times.

If X and Y are friends, and Y and Z are friends, then X and Z are also friends. There is no friendship that cannot be derived from the M given facts.

Takahashi the evil wants to divide the N persons into some number of groups so that every person has no friend in his/her group.

At least how many groups does he need to make?

Constraints

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

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

1\leq A_i,B_i\leq N

A_i \neq B_i

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
\vdots
A_M B_M

Output

Print the answer.

Sample Input 1

5 3
1 2
3 4
5 1

Sample Output 1

3

Dividing them into three groups such as \{1,3\}, \{2,4\}, and \{5\} achieves the goal.

Sample Input 2

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

Sample Output 2

4

Sample Input 3

10 4
3 1
4 1
5 9
2 6

Sample Output 3

3


n = gets.split("").map(&:to_i)
sum = 0
n.each do |i|
  sum+=i
end
puts sum % 9 == 0 ? "Yes" : "No"

Score : 200 points

Problem Statement

An integer N is a multiple of 9 if and only if the sum of the digits in the decimal representation of N is a multiple of 9.

Determine whether N is a multiple of 9.

Constraints

0 \leq N < 10^{200000}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a multiple of 9, print Yes; otherwise, print No.

Sample Input 1

123456789

Sample Output 1

Yes

The sum of these digits is 1+2+3+4+5+6+7+8+9=45, which is a multiple of 9, so 123456789 is a multiple of 9.

Sample Input 2

0

Sample Output 2

Yes

Sample Input 3

31415926535897932384626433832795028841971693993751058209749445923078164062862089986280

Sample Output 3

No


gets
m=s=t = 0
gets.split.each {
  s += a = _1.to_i
  t += (m<a ? m=a : m)
}
p t-s

Score : 300 points

Problem Statement

N persons are standing in a row. The height of the i-th person from the front is A_i.

We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:

Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.

Find the minimum total height of the stools needed to meet this goal.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum total height of the stools needed to meet the goal.

Sample Input 1

5
2 1 5 4 3

Sample Output 1

4

If the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively, their heights will be 2, 2, 5, 5, and 5, satisfying the condition.

We cannot meet the goal with a smaller total height of the stools.

Sample Input 2

5
3 3 3 3 3

Sample Output 2

0

Giving a stool of height 0 to everyone will work.


n = gets.to_i
a = gets.split.map(&:to_i)

ans = 0
h = a[0]
for i in 1..(n-1)
  if h >= a[i]
    ans += h-a[i]
  else
    h = a[i]
  end
end
puts ans


(H, W), (Ch, Cw), (Dh, Dw) = 3.times.map{gets.split.map(&:to_i)}
Ss = [""] + readlines.map{" " + _1.strip}  # 1-indexed

mv = [[1, 0], [-1, 0], [0, 1], [0, -1]]
wp = [*-2 .. 2].repeated_permutation(2).to_a - [[0, 0]] - mv
q1, q2, c = [Ch, Cw], [], 0
d = Array.new(H + 1){Array.new(W + 1, -1)}
until q1.empty? && q2.empty?
  while !q1.empty?
    h, w = q1.shift(2)
    q2.append(h, w)
    mv.map{[_1 + h, _2 + w]}.each do |x, y|
      next if x < 1 || H < x || y < 1 || W < y || Ss[x][y] == '#' || d[x][y] >= 0
      d[x][y] = c
      q1.append(x, y)
    end
  end
  c += 1
  while !q2.empty?
    h, w = q2.shift(2)
    wp.map{[_1 + h, _2 + w]}.each do |x, y|
      next if x < 1 || H < x || y < 1 || W < y || Ss[x][y] == '#' || d[x][y] >= 0
      d[x][y] = c
      q1.append(x, y)
    end
  end
end
puts d[Dh][Dw]

Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

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

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2


H,W = gets.split.map(&:to_i)
SY,SX = gets.split.map { _1.to_i - 1 }
GY,GX = gets.split.map { _1.to_i - 1 }
S = readlines.map { _1.chomp.chars }

MV = [[1,0],[-1,0],[0,1],[0,-1]]
WP = [*-2..2].product([*-2..2]) - [*MV, [0,0]]

dist = Array.new(H) { [-1]*W }
que = []
que2 = []
que.push([SY,SX])
dist[SY][SX] = 0
while !que.empty?

  y,x = que.pop
  d = dist[y][x]

  MV.each do |dy,dx|
    yy,xx = y+dy,x+dx
    next if yy < 0 || xx < 0 || H <= yy || W <= xx
    next if S[yy][xx] == "#"
    next if 0 <= dist[yy][xx] && dist[yy][xx] <= d
    dist[yy][xx] = d
    que.push([yy,xx])
  end

  WP.each do |dy,dx|
    yy,xx = y+dy,x+dx
    next if yy < 0 || xx < 0 || H <= yy || W <= xx
    next if S[yy][xx] == "#"
    next if 0 <= dist[yy][xx] && dist[yy][xx] <= d + 1
    dist[yy][xx] = d + 1
    que2.push([yy,xx])
  end

  if que.empty?
    break if dist[GY][GX] != -1
    que = que2
    que2 = []
  end
end

puts dist[GY][GX]


DY = [-1, 0, 1, 0]
DX = [0, 1, 0, -1]

H, W = gets.split.map(&:to_i)
CH, CW = gets.split.map(&:to_i)
DH, DW = gets.split.map(&:to_i)
MAP = H.times.map { gets.chomp.chars }

visited = Array.new(H) { Array.new(W, false) }
queue = []
queue << [CH - 1, CW - 1, 0]

until queue.empty?
  y, x, cost = queue.shift

  next if MAP[y][x] == '#'
  next if visited[y][x]
  visited[y][x] = true

  if DH - 1 == y && DW - 1 == x
    puts cost
    exit
  end

  4.times do |i|
    ny = y + DY[i]
    nx = x + DX[i]

    next if ny < 0 || nx < 0 || H <= ny || W <= nx
    next if visited[ny][nx]

    queue.unshift([ny, nx, cost])
  end

  -2.upto(2) do |dy|
    -2.upto(2) do |dx|
      ny = y + dy
      nx = x + dx

      next if ny < 0 || nx < 0 || H <= ny || W <= nx
      next if visited[ny][nx]

      queue.push([ny, nx, cost + 1])
    end
  end
end

puts -1


h, w = gets.split(' ').map(&:to_i)
wi, wj = gets.split(' ').map(&:to_i)
di, dj = gets.split(' ').map(&:to_i)

wi -= 1
wj -= 1
di -= 1
dj -= 1

m = h.times.map { gets.chomp! }

DS = [[-1, 0], [1, 0], [0, -1], [0, 1]]

queue = [[wi, wj, 0]]
seen = { wi => { wj => true } }
until queue.empty?
  ci, cj, cd = queue.shift
  if ci == di && cj == dj
    puts cd
    exit
  end

  DS.each do |dir|
    ni = ci + dir[0]
    nj = cj + dir[1]
    next if (! ((0 <= ni && ni < h) && (0 <= nj && nj < w))) || m[ni][nj] == '#'
    seen[ni] ||= {}
    next if seen[ni][nj]
    seen[ni][nj] = true
    queue.unshift([ni, nj, cd])
  end

  (-2..2).each do |pi|
    (-2..2).each do |pj|
      ni = ci + pi
      nj = cj + pj
      next if ni == ci && nj == ci
      next if (! ((0 <= ni && ni < h) && (0 <= nj && nj < w))) || m[ni][nj] == '#'
      seen[ni] ||= {}
      next if seen[ni][nj]
      seen[ni][nj] = true
      queue.push([ni, nj, cd + 1])
    end
  end

end

puts -1


def input_n
  gets.to_i               # 単一整数
end

def input_s
  gets.chomp              # 文字列。chompを付けないと改行文字がついてくる
end

def input_a
  gets.split.map(&:to_i)  # スペースで区切られた複数の整数
end

def input_o(num)
  num.times.map { gets.to_i }  # 縦に並んだ複数の整数。たまにある
end

def output_f(num)
  puts sprintf("%.12f", num)
end

x, k, d = input_a
if x.abs >= d * k
  if x >= 0
    x = x - (k * d)
  else
    x = x + (k * d)
  end
  puts x.abs
  exit
else
  tmp = x.abs / d
  if x >= 0
    x = x - (tmp * d)
  else
    x = x + (tmp * d)
  end
  k = k - tmp
end

xx = -1
k.times do |i|
  if xx == x.abs
    if (k - i) % 2 == 1
      if x >= 0
        x = x - d
      else
        x = x + d
      end
    end
    break
  end
  if i % 2 == 0
    xx = x.abs
  end
  if x >= 0
    x = x - d
  else
    x = x + d
  end
end
puts x.abs

Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.


x, k, d = gets.split(" ").map(&:to_i)
if x.abs >= k * d
  puts x.abs - (k * d)
  exit
end
xx=0
yy=1
flag=false
before_0 = x / d
if (k - before_0) % 2 == 0
  puts (x - (before_0 * d)).abs
  flag=true
  yy+=1
else (k - before_0) % 2 != 0
  puts  (x - (before_0 * d) - d).abs
  flag=false
  xx+=1
end


x, k, d = gets.split(" ").map(&:to_i)

absx = x.abs
r = absx / d
if r > k
  puts absx - k * d
else
  absx = absx - r * d - ((k - r) % 2) * d
  puts absx.abs
end


n,k = gets.chomp.split(' ').map(&:to_i)
ps = gets.chomp.split(' ').map(&:to_i)
cs = gets.chomp.split(' ').map(&:to_i)
ans = -Float::INFINITY

n.times do |i|
    now = i
    score = []
    total = 0
    while true
        now = ps[now] - 1
        score << cs[now]
        total += cs[now]
        if now == i
            break
        end
    end
    t = 0
    score.size.times do |i|
        t += score[i]
        sum = t
        if(i+1>k)
            break
        end
        if(total > 0)
            e = (k-(i+1))/(score.size)
            sum += e*total
        end
        ans = [ans,sum].max
    end

end
puts ans

Score : 400 points

Problem Statement

Takahashi will play a game using a piece on an array of squares numbered 1, 2, \cdots, N. Square i has an integer C_i written on it. Also, he is given a permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.

Now, he will choose one square and place the piece on that square. Then, he will make the following move some number of times between 1 and K (inclusive):

In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.

Help him by finding the maximum possible score at the end of the game. (The score is 0 at the beginning of the game.)

Constraints

2 \leq N \leq 5000

1 \leq K \leq 10^9

1 \leq P_i \leq N

P_i \neq i

P_1, P_2, \cdots, P_N are all different.

-10^9 \leq C_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N

Output

Print the maximum possible score at the end of the game.

Sample Input 1

5 2
2 4 5 1 3
3 4 -10 -8 8

Sample Output 1

8

When we start at some square of our choice and make at most two moves, we have the following options:

If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.

If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.

If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.

If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.

If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.

The maximum score achieved is 8.

Sample Input 2

2 3
2 1
10 -7

Sample Output 2

13

Sample Input 3

3 3
3 1 2
-1000 -2000 -3000

Sample Output 3

-1000

We have to make at least one move.

Sample Input 4

10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719

Sample Output 4

29507023469

The absolute value of the answer may be enormous.


# 5 2           n k
# 2 4 5 1 3     p
# 3 4 -10 -8 8  c
n, k = gets.split.map(&:to_i)
array_p = gets.split.map(&:to_i)
array_c = gets.split.map(&:to_i)

score = -10**9
n.times do |i|
  score_temp = -10**9
  score_temp_temp = 0
  next_p = 0
  k.times do |j|
    if j == 0
      score_temp_temp = array_c[i]
      next_p = array_p[i] - 1
    else
      score_temp_temp += array_c[next_p]
      next_p = array_p[next_p] - 1
    end
    score_temp = [score_temp, score_temp_temp].max
  end
  score = [score, score_temp].max
end

puts score


Score : 500 points

Problem Statement

There are K items placed on a grid of squares with R rows and C columns. Let (i, j) denote the square at the i-th row (1 \leq i \leq R) and the j-th column (1 \leq j \leq C). The i-th item is at (r_i, c_i) and has the value v_i.

Takahashi will begin at (1, 1), the start, and get to (R, C), the goal. When he is at (i, j), he can move to (i + 1, j) or (i, j + 1) (but cannot move to a non-existent square).

He can pick up items on the squares he visits, including the start and the goal, but at most three for each row. It is allowed to ignore the item on a square he visits.

Find the maximum possible sum of the values of items he picks up.

Constraints

1 \leq R, C \leq 3000

1 \leq K \leq \min(2 \times 10^5, R \times C)

1 \leq r_i \leq R

1 \leq c_i \leq C

(r_i, c_i) \neq (r_j, c_j) (i \neq j)

1 \leq v_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R C K
r_1 c_1 v_1
r_2 c_2 v_2
:
r_K c_K v_K

Output

Print the maximum possible sum of the values of items Takahashi picks up.

Sample Input 1

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

Sample Output 1

8

He has two ways to get to the goal:

Visit (1, 1), (1, 2), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 5 = 8.

Visit (1, 1), (2, 1), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 4 = 7.

Thus, the maximum possible sum of the values of items he picks up is 8.

Sample Input 2

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

Sample Output 2

29

We have four items in the 1-st row. The optimal choices are as follows:

Visit (1, 1) (1, 2), (1, 3), (1, 4), (2, 4), and (2, 5), in this order, and pick up all items except the one on (1, 2). Then, the total value of the items he picks up will be 3 + 4 + 2 + 20 = 29.

Sample Input 3

4 5 10
2 5 12
1 5 12
2 3 15
1 2 20
1 1 28
2 4 26
3 2 27
4 5 21
3 5 10
1 3 10

Sample Output 3

142


x = gets.to_i

if x >= 30
  puts 'Yes'
else
  puts 'No'
end

Score : 100 points

Problem Statement

You will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above.

The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner?

Constraints

-40 \leq X \leq 40

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print Yes if you will turn on the air conditioner; print No otherwise.

Sample Input 1

25

Sample Output 1

No

Sample Input 2

30

Sample Output 2

Yes


x = gets.to_i
puts x >= 30 ? "Yes" : "No"


a,b = gets
n=a.to_i
d=b.to_i

for i in 1..n
  m=i
  c,d=gets
  x=c.to_i
  y=d.to_i
  e=x**2 + y**2
  f=e**(1/2.0)
  num=0
  if d>=f
    num+=1
  end
end
puts num

Score : 200 points

Problem Statement

We have N points in the two-dimensional plane. The coordinates of the i-th point are (X_i,Y_i).

Among them, we are looking for the points such that the distance from the origin is at most D. How many such points are there?

We remind you that the distance between the origin and the point (p, q) can be represented as \sqrt{p^2+q^2}.

Constraints

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

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

|X_i|,|Y_i| \leq 2\times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D
X_1 Y_1
\vdots
X_N Y_N

Output

Print an integer representing the number of points such that the distance from the origin is at most D.

Sample Input 1

4 5
0 5
-2 4
3 4
4 -4

Sample Output 1

3

The distance between the origin and each of the given points is as follows:

\sqrt{0^2+5^2}=5

\sqrt{(-2)^2+4^2}=4.472\ldots

\sqrt{3^2+4^2}=5

\sqrt{4^2+(-4)^2}=5.656\ldots

Thus, we have three points such that the distance from the origin is at most 5.

Sample Input 2

12 3
1 1
1 1
1 1
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Sample Output 2

7

Multiple points may exist at the same coordinates.

Sample Input 3

20 100000
14309 -32939
-56855 100340
151364 25430
103789 -113141
147404 -136977
-37006 -30929
188810 -49557
13419 70401
-88280 165170
-196399 137941
-176527 -61904
46659 115261
-153551 114185
98784 -6820
94111 -86268
-30401 61477
-55056 7872
5901 -163796
138819 -185986
-69848 -96669

Sample Output 3

6


K = gets.to_i

t = 7 % K
ans = -1
K.times do |i|
    if t == 0 then
        ans = i+1
        break
    end

    t = (10*t + 7) % K
end

p ans

Score : 300 points

Problem Statement

Takahashi loves the number 7 and multiples of K.

Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots? (Also see Output and Sample Input/Output below.)

If the sequence contains no multiples of K, print -1 instead.

Constraints

1 \leq K \leq 10^6

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

Sample Input 1

101

Sample Output 1

4

None of 7, 77, and 777 is a multiple of 101, but 7777 is.

Sample Input 2

2

Sample Output 2

-1

All elements in the sequence are odd numbers; there are no multiples of 2.

Sample Input 3

999983

Sample Output 3

999982


k = gets.chomp.to_i

count = 0
i = 0
prev = nil
while true do
  i = i * 10 + 1
  i %= k
  count += 1
  if i == 0
    break
  elsif i == prev
    puts -1
    exit
  end
  prev = i
end

puts count


# 整数の入力
X = gets.to_i
sevens = 0

if X % 2 == 0
  puts -1
else
  1.step do |i|
    sevens += 7 * 10**i
    if sevens % X == 0
      puts i
      break
    end
  end
end


_ = gets
c = gets.chomp
r = c.scan('R').length

res = 0
r.times do |i|
  if c[i] != "R"
    res += 1
  end
end
puts res

Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3


N,K = gets.split.map &:to_i
A = gets.split.map &:to_i

p (1..10**9).bsearch{|l| A.inject(0){|total,a| total+(a.to_r/l).ceil - 1} <= K }

Score : 500 points

Problem Statement

We have N logs of lengths A_1,A_2,\cdots A_N.

We can cut these logs at most K times in total. When a log of length L is cut at a point whose distance from an end of the log is t (0<t<L), it becomes two logs of lengths t and L-t.

Find the shortest possible length of the longest log after at most K cuts, and print it after rounding up to an integer.

Constraints

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

0 \leq K \leq 10^9

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print an integer representing the answer.

Sample Input 1

2 3
7 9

Sample Output 1

4

First, we will cut the log of length 7 at a point whose distance from an end of the log is 3.5, resulting in two logs of length 3.5 each.

Next, we will cut the log of length 9 at a point whose distance from an end of the log is 3, resulting in two logs of length 3 and 6.

Lastly, we will cut the log of length 6 at a point whose distance from an end of the log is 3.3, resulting in two logs of length 3.3 and 2.7.

In this case, the longest length of a log will be 3.5, which is the shortest possible result. After rounding up to an integer, the output should be 4.

Sample Input 2

3 0
3 4 5

Sample Output 2

5

Sample Input 3

10 10
158260522 877914575 602436426 24979445 861648772 623690081 433933447 476190629 262703497 211047202

Sample Output 3

292638192


Score: 100 points

Problem Statement

M-kun is a competitor in AtCoder, whose highest rating is X.

In this site, a competitor is given a kyu (class) according to his/her highest rating. For ratings from 400 through 1999, the following kyus are given:

From 400 through 599: 8-kyu

From 600 through 799: 7-kyu

From 800 through 999: 6-kyu

From 1000 through 1199: 5-kyu

From 1200 through 1399: 4-kyu

From 1400 through 1599: 3-kyu

From 1600 through 1799: 2-kyu

From 1800 through 1999: 1-kyu

What kyu does M-kun have?

Constraints

400 \leq X \leq 1999

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the kyu M-kun has, as an integer.
For example, if he has 8-kyu, print 8.

Sample Input 1

725

Sample Output 1

7

M-kun's highest rating is 725, which corresponds to 7-kyu.

Thus, 7 is the correct output.

Sample Input 2

1600

Sample Output 2

2

M-kun's highest rating is 1600, which corresponds to 2-kyu.

Thus, 2 is the correct output.


A, B, C = gets.split.map(&:strip).map(&:to_i)
K = gets.to_i

a, b, c = A, B, C

K.times do
  if a >= b
    b *= 2
    next
  elsif b >= c
    c *= 2
    next
  else
    break
  end
end

if a < b && b < c
  puts 'Yes'
else
  puts 'No'
end

Score: 200 points

Problem Statement

M-kun has the following three cards:

A red card with the integer A.

A green card with the integer B.

A blue card with the integer C.

He is a genius magician who can do the following operation at most K times:

Choose one of the three cards and multiply the written integer by 2.

His magic is successful if both of the following conditions are satisfied after the operations:

The integer on the green card is strictly greater than the integer on the red card.

The integer on the blue card is strictly greater than the integer on the green card.

Determine whether the magic can be successful.

Constraints

1 \leq A, B, C \leq 7

1 \leq K \leq 7

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C
K

Output

If the magic can be successful, print Yes; otherwise, print No.

Sample Input 1

7 2 5
3

Sample Output 1

Yes

The magic will be successful if, for example, he does the following operations:

First, choose the blue card. The integers on the red, green, and blue cards are now 7, 2, and 10, respectively.

Second, choose the green card. The integers on the red, green, and blue cards are now 7, 4, and 10, respectively.

Third, choose the green card. The integers on the red, green, and blue cards are now 7, 8, and 10, respectively.

Sample Input 2

7 4 2
3

Sample Output 2

No

He has no way to succeed in the magic with at most three operations.


n = gets.to_i
a = gets.split.map(&:to_i)

s = 0
e = 1
b = 1000
st = 0

while s < n-1 do
  if a[s] < a[e]
    # 底値からスタートして高値まで待つ
    while e < n-1 && a[e] <= a[e+1] do
      e += 1
    end
    # buy min and sell max
    st, b = b.divmod a[s]
    b = b + st * a[e]
    st = 0
    s = e
    e += 1
  else
    # 底値まで待つ
    while e < n-1 && a[e] >= a[e+1] do
      e += 1
    end
    s = e
    e += 1
  end
end

puts b

Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

Initially, he has 1000 yen and no stocks.

Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.

Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.

Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.

Day 4: Do nothing.

Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.

Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.

Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.


n = gets.to_i
a = gets.split.map(&:to_i)

s = 0
e = 1
b = 1000
st = 0

while s < n-1 do
  if a[s] < a[e]
    # 底値からスタートして高値まで待つ
    while e < n-1 && a[e-1] < a[e] do
      e += 1
    end
    # buy min and sell max
    p [s,e,b,st]
    st, b = b.divmod a[s]
    b = b + st * a[e]
    st = 0
    s = e
    e += 1
  else
    # 底値まで待つ
    while e < n && a[e-1] > a[e] do
      e += 1
    end
    s = e
    e += 1
    p [s,e]
  end
end

puts b


N = gets.to_i

x = 1
y = 1
z = 1

count = 0

(1..N).each do |i|
  count = 0
  (x..10).each do |k|
    (y..11).each do |m|
      (z..12).each do |n|
        if k**2 + m**2 + n**2 + k * m + m * n + k * n == i
          count += 1
        end
      end
    end
  end
  puts count
end

Score : 300 points

Problem Statement

Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions:

1 \leq x,y,z

x^2 + y^2 + z^2 + xy + yz + zx = n

Given an integer N, find each of f(1),f(2),f(3),\ldots,f(N).

Constraints

All values in input are integers.

1 \leq N \leq 10^4

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the value f(i).

Sample Input 1

20

Sample Output 1

0
0
0
0
0
1
0
0
0
0
3
0
0
0
0
0
3
3
0
0

For n=6, only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1.

For n=11, three triples, (1,1,2), (1,2,1), and (2,1,1), satisfy both of the conditions. Thus, f(6) = 3.

For n=17, three triples, (1,2,2), (2,1,2), and (2,2,1), satisfy both of the conditions. Thus, f(17) = 3.

For n=18, three triples, (1,1,3), (1,3,1), and (3,1,1), satisfy both of the conditions. Thus, f(18) = 3.


h=Hash.new 0
$<.map{h[_1.chomp]+=1}
%w(AC WA TLE RE).map{puts"#{_1} x #{h[_1]}"}

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

1 \leq N \leq 10^5

S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.

Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0


def fmt(ac,wa,tle,re)
  puts "AC x #{ac}\nWA x #{wa}\nTLE x #{tle}\nRE x #{re}"
end
N = gets.to_i
s = []
N.times do
  s.push(gets.chomp)
end
a=b=c=d=0
s.each do |val|
  case val
    when "AC"
      a += 1
    when "WA"
      b += 1
    when "TLE"
      c += 1
    when "RE"
      d += 1
  end
end
fmt(a,b,c,d)


h,w,k = gets.split.map(&:to_i)
c = h.times.map{gets.split('').map{|c|c=='#'}}

ans = 0
(2**(h+w)).times{|i|
  s = 2**(h+w)+i
  s = s.to_s(2)[1..-1]
  #p s
  cnt = 0
  h.times{|j|
    w.times{|k|
      if s[j]!='0' && s[h+k]!='0'
        if c[j][k]
          cnt += 1
        end
      end
    }
  }
  #p cnt
  ans += 1 if cnt==k
}

p ans

Score : 300 points

Problem Statement

We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #.

Consider doing the following operation:

Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.

You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.

Constraints

1 \leq H, W \leq 6

1 \leq K \leq HW

c_{i,j} is . or #.

Input

Input is given from Standard Input in the following format:

H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}

Output

Print an integer representing the number of choices of rows and columns satisfying the condition.

Sample Input 1

2 3 2
..#
###

Sample Output 1

5

Five choices below satisfy the condition.

The 1-st row and 1-st column

The 1-st row and 2-nd column

The 1-st row and 3-rd column

The 1-st and 2-nd column

The 3-rd column

Sample Input 2

2 3 4
..#
###

Sample Output 2

1

One choice, which is choosing nothing, satisfies the condition.

Sample Input 3

2 2 3
##
##

Sample Output 3

0

Sample Input 4

6 6 8
..##..
.#..#.
#....#
######
#....#
#....#

Sample Output 4

208


INF = 10 ** 9 + 7

n, k = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i)

pos = []
neg = []
a.each do |x|
  if x < 0
    neg << x
  else
    pos << x
  end
end

pos.sort!
neg.sort!

if pos.empty? && k % 2 != 0
  res = 1
  while k > 0
    res *= neg[n - k]
    res %= INF
    k -= 1
  end
  puts res
  exit
end

i = pos.size
j = - 1
k.times do
  if i - 1 < 0
    j += 1 # 負の数を加える
  elsif j + 1 >= neg.size
    i -= 1 # 正の数を加える
  elsif pos[i - 1].abs >= neg[j + 1].abs
    i -= 1
  else
    j += 1
  end
end

if (j + 1) % 2 != 0 && !((i >= pos.size || j >= neg.size - 1) && (j <= -1 || i <= 0))
  if i >= pos.size || j >= neg.size - 1
    i -= 1 # (1)
    j -= 1
  elsif j <= -1 || i <= 0
    j += 1 # (2)
    i += 1
  elsif neg[j] * neg[j - 1] > pos[i] * pos[i + 1]
    i += 1
    j += 1
  else
    i -= 1
    j -= 1
  end
end

res = 1
while i < pos.size
  res *= pos[i]
  res %= INF
  i += 1
end
while j > 0
  res *= (INF + neg[j])
  res %= INF
  j -= 1
end

puts res

Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

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

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).


Score : 100 points

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

Constraints

1 \leq a \leq 10

a is an integer.

Input

Input is given from Standard Input in the following format:

a

Output

Print the value a + a^2 + a^3 as an integer.

Sample Input 1

2

Sample Output 1

14

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.

Sample Input 2

10

Sample Output 2

1110


s=gets.chomp.split("")
t=gets.chomp.split("")
count = 0
(s.size).times do |index|
  count +=1 if s[index] != t[index]
end
puts count

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

S and T have lengths between 1 and 2\times 10^5 (inclusive).

S and T consists of lowercase English letters.

S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.

Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

First, replace the sixth character c with h.

Second, replace the eighth character f with t.

Third, replace the ninth character f with t.

Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.


input = readlines()
s = input.shift.chomp.split('')
t = input.shift.chomp.split('')

cnt = 0
s.each_with_index do |c, i|
  cnt += 1 unless c == t[i]
end

puts cnt


x = gets
if x == x.upcase
  puts "A"
else
  puts "a"
end

Score : 100 points

Problem Statement

An uppercase or lowercase English letter \alpha will be given as input.
If \alpha is uppercase, print A; if it is lowercase, print a.

Constraints

\alpha is an uppercase (A - Z) or lowercase (a - z) English letter.

Input

Input is given from Standard Input in the following format:

α

Output

If \alpha is uppercase, print A; if it is lowercase, print a.

Sample Input 1

B

Sample Output 1

A

B is uppercase, so we should print A.

Sample Input 2

a

Sample Output 2

a

a is lowercase, so we should print a.


c = gets.chomp

if "QWERTYUIOPASDFGHJKLZXCVBNM".include? c
  puts "A"
else
  puts "a"
end


n = gets.to_i
alps = ("a".."z").to_a
ans = []
i = 10
while true
  if n / (26**i) > 0
    ans << alps[n/(26**i) - 1]
    n -= n/(26**i) * (26**i)
    i = 10
  end
  if i <= 0
    break
  end
  i -= 1
end
puts ans.join

Score : 300 points

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

the dogs numbered 1,2,\cdots,26 were respectively given the names a, b, ..., z;

the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;

the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;

the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;

the dogs numbered 475255,475256,\cdots were respectively given the names aaaaa, aaaab, ...;

and so on.

To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names:

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

Now, Roger asks you:

"What is the name for the dog numbered N?"

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


n = gets.to_i
res = []
while n > 0
  res << ("a".ord + (n % 26) - 1).chr
  n /= 26
end
puts res.join("").reverse


n = gets.to_i
a = gets.split(" ").map(&:to_i)
z = []
a.length.times{ |i|
  z[a[i]] ||= 0
  z[a[i]] += 1
}
sum = a.sum
q = gets.to_i
q.times{
  b, c = gets.split(" ").map(&:to_i)
  if z[b] != nil && z[b] > 0
    sum += (c -b) * z[b]
    z[c] ||= 0
    z[c] += z[b]
    z[b] = 0
  end
  p sum
}

Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}

B_{i} \neq C_{i}

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

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

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

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

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000


Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.

Sample Input 2

1 2 0 4 5

Sample Output 2

3


x,y = gets.split.map(&:to_i)

if 2*x <= y && y <= 4*x && y % 2 == 0
  puts "Yes"
else
  puts "No"
end

Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.

Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.


A = gets.chomp.split " "
B = gets.chomp.split " "
a,v = A[0].to_i, A[1].to_i
b,w = B[0].to_i, B[1].to_i
RESULT = gets.chomp.to_i

time = 1

while true
  now = (b + w*time) - (a + v*time)
  if time >= RESULT
    puts "No"
    return
  end

  if now <= 0
    puts "Yes"
    return;
  end
  time += 1;
end

Score : 200 points

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second.
The other child is now at coordinate B, and she can travel the distance of W per second.

He can catch her when his coordinate is the same as hers.
Determine whether he can catch her within T seconds (including exactly T seconds later).
We assume that both children move optimally.

Constraints

-10^9 \leq A,B \leq 10^9

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

If "it" can catch the other child, print YES; otherwise, print NO.

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


a, v = gets.split.map(&:to_i)
b, w = gets.split.map(&:to_i)
t = gets.to_i

if (a == b)
  puts "YES"
  exit
end

if (v <= w)
  puts "NO"
  exit
end

d = (b - a).abs
e = v - w
if (d / e <= t)
  puts "YES"
else
  puts "NO"
end


a, b = gets.split(" ").map(&:to_i)
puts a * b

Score : 100 points

Problem Statement

Compute A \times B.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the value A \times B as an integer.

Sample Input 1

2 5

Sample Output 1

10

We have 2 \times 5 = 10.

Sample Input 2

100 100

Sample Output 2

10000


require 'prime'

n = gets.chomp.to_i
histories = []
count = 0

if n == 1
  puts 0
elsif Prime.prime?(n)
  puts 1
else
  loop do
    if Prime.prime?(n)
      puts count
      break
    end

    (2..n).each do |z|
      if !histories.include?(z) && n % z == 0
        n = n / z
        histories << z
        count += 1
        break
      end
    end

    if n == 1
      puts count
      break
    end
  end
end

Score : 400 points

Problem Statement

Given is a positive integer N. Consider repeatedly applying the operation below on N:

First, choose a positive integer z satisfying all of the conditions below:

z can be represented as z=p^e, where p is a prime number and e is a positive integer;

z divides N;

z is different from all integers chosen in previous operations.

Then, replace N with N/z.

Find the maximum number of times the operation can be applied.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum number of times the operation can be applied.

Sample Input 1

24

Sample Output 1

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=12.)

Choose z=3 (=3^1). (Now we have N=4.)

Choose z=4 (=2^2). (Now we have N=1.)

Sample Input 2

1

Sample Output 2

0

We cannot apply the operation at all.

Sample Input 3

64

Sample Output 3

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=32.)

Choose z=4 (=2^2). (Now we have N=8.)

Choose z=8 (=2^3). (Now we have N=1.)

Sample Input 4

1000000007

Sample Output 4

1

We can apply the operation once by, for example, making the following choice:

z=1000000007 (=1000000007^1). (Now we have N=1.)

Sample Input 5

997764507000

Sample Output 5

7


require 'prime'

n = gets.to_i

div = Prime.prime_division(n)
div_map = div.to_h

num = div_map.keys
count = div_map.values.sum

result = 0
before_result = 0

(1..100).each{ |i|
	num.repeated_combination(i).each{ |c|
		c_h = c.each_with_object(Hash.new(0)){ |x, h| h[x] += 1}

		flag = true

		c_h.each{ |k,v|
			if div_map[k] < v then
				flag = false
				break
			end
		}

		if flag then
			c_h.each{ |k,v|
				div_map[k] -= v
			}

			count -= i
			result += 1

			if count < i then
				puts result
				exit
			end
		end
	}

	if before_result == result then
		break
	else
		before_result = result
	end

	num = div_map.select{|k,v| v > 0}.keys
}

puts result


n = gets.to_i
x = n.times.map{gets.split.map(&:to_i)}
a = x.map{|a, b| a }.sort
b = x.map{|a, b| b }.sort
if n % 2 == 1
    ma = a[n/2]
    mb = b[n/2]
    puts mb - ma + 1
else
    ma1 = a[n/2]
    ma2 = a[n/2 - 1]
    mb1 = b[n/2]
    mb2 = b[n/2 - 1]
    puts (((mb1 + mb2) / 2.0 - (ma1 + ma2) / 2.0) * 2 + 1).to_i
end

Score : 500 points

Problem Statement

There are N integers X_1, X_2, \cdots, X_N, and we know that A_i \leq X_i \leq B_i.
Find the number of different values that the median of X_1, X_2, \cdots, X_N can take.

Notes

The median of X_1, X_2, \cdots, X_N is defined as follows. Let x_1, x_2, \cdots, x_N be the result of sorting X_1, X_2, \cdots, X_N in ascending order.

If N is odd, the median is x_{(N+1)/2};

if N is even, the median is (x_{N/2} + x_{N/2+1}) / 2.

Constraints

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

1 \leq A_i \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

2
1 2
2 3

Sample Output 1

3

If X_1 = 1 and X_2 = 2, the median is \frac{3}{2};

if X_1 = 1 and X_2 = 3, the median is 2;

if X_1 = 2 and X_2 = 2, the median is 2;

if X_1 = 2 and X_2 = 3, the median is \frac{5}{2}.

Thus, the median can take three values: \frac{3}{2}, 2, and \frac{5}{2}.

Sample Input 2

3
100 100
10 10000
1 1000000000

Sample Output 2

9991


mod = 998244353

n, s = gets.to_s.split.map{|e| e.to_i }
a = gets.to_s.split.map{|e| e.to_i }.sort

dp = [0] * (s+1)
dp[0] = 1

s_max = 0
a.each do |t|
  s_max = s < s_max + t ? s : s_max + t
  s_max.downto(0) do |j|
    dp[j] *= 2
    dp[j] += dp[j-t] if j - t >= 0
    dp[j] %= mod if dp[j] >= mod
  end
end

puts dp[s]

Score : 600 points

Problem Statement

Given are a sequence of N positive integers A_1, A_2, \ldots, A_N and another positive integer S.

For a non-empty subset T of the set \{1, 2, \ldots , N \}, let us define f(T) as follows:

f(T) is the number of different non-empty subsets \{x_1, x_2, \ldots , x_k \} of T such that A_{x_1}+A_{x_2}+\cdots +A_{x_k} = S.

Find the sum of f(T) over all 2^N-1 subsets T of \{1, 2, \ldots , N \}. Since the sum can be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq S \leq 3000

1 \leq A_i \leq 3000

Input

Input is given from Standard Input in the following format:

N S
A_1 A_2 ... A_N

Output

Print the sum of f(T) modulo 998244353.

Sample Input 1

3 4
2 2 4

Sample Output 1

6

For each T, the value of f(T) is shown below. The sum of these values is 6.

f(\{1\}) = 0

f(\{2\}) = 0

f(\{3\}) = 1 (One subset \{3\} satisfies the condition.)

f(\{1, 2\}) = 1 (\{1, 2\})

f(\{2, 3\}) = 1 (\{3\})

f(\{1, 3\}) = 1 (\{3\})

f(\{1, 2, 3\}) = 2 (\{1, 2\}, \{3\})

Sample Input 2

5 8
9 9 9 9 9

Sample Output 2

0

Sample Input 3

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

Sample Output 3

3296


n, s = gets.split(" ").map(&:to_i)
alist = gets.split(" ").map(&:to_i)
m = 998244353

dp = Array.new(n+1) { Array.new(s+1, 0)}
dp[0][0] = 1

for i in 0..n-1 do
    for j in 0..s do
        dp[i+1][j] a= (dp[i+1][j] + (dp[i][j] << 1)) % m
        if j + alist[i] <= s then
            dp[i+1][j + alist[i]] = (dp[i+1][j + alist[i]] + dp[i][j]) % m
        end
    end
end

print dp[n][s]


t = gets.chomp
res = ''
t.each_char do |s|
  if s != '?'
    res << s
  else
    res << 'D'
  end
end
puts res

Score : 200 points

Problem Statement

For a string S consisting of the uppercase English letters P and D, let the doctoral and postdoctoral quotient of S be the total number of occurrences of D and PD in S as contiguous substrings. For example, if S = PPDDP, it contains two occurrences of D and one occurrence of PD as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3.

We have a string T consisting of P, D, and ?.

Among the strings that can be obtained by replacing each ? in T with P or D, find one with the maximum possible doctoral and postdoctoral quotient.

Constraints

1 \leq |T| \leq 2 \times 10^5

T consists of P, D, and ?.

Input

Input is given from Standard Input in the following format:

T

Output

Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each ? in T with P or D.
If there are multiple such strings, you may print any of them.

Sample Input 1

PD?D??P

Sample Output 1

PDPDPDP

This string contains three occurrences of D and three occurrences of PD as contiguous substrings, so its doctoral and postdoctoral quotient is 6, which is the maximum doctoral and postdoctoral quotient of a string obtained by replacing each ? in T with P or D.

Sample Input 2

P?P?

Sample Output 2

PDPD