There is a field divided into $$$n$$$ rows and $$$m$$$ columns. Some cells are empty (denoted as E), other cells contain robots (denoted as R).You can send a command to all robots at the same time. The command can be of one of the four types:  move up;  move right;  move down;  move left. When you send a command, all robots at the same time attempt to take one step in the direction you picked. If a robot tries to move outside the field, it explodes; otherwise, every robot moves to an adjacent cell in the chosen direction.You can send as many commands as you want (possibly, zero), in any order. Your goal is to make at least one robot reach the upper left corner of the field. Can you do this without forcing any of the robots to explode?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. Each test case starts with a line containing two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 5$$$) — the number of rows and the number of columns, respectively. Then $$$n$$$ lines follow; each of them contains a string of $$$m$$$ characters. Each character is either E (empty cell} or R (robot). Additional constraint on the input: in each test case, there is at least one robot on the field.

If it is possible to make at least one robot reach the upper left corner of the field so that no robot explodes, print YES. Otherwise, print NO.

6

1 3

ERR

2 2

ER

RE

2 2

ER

ER

1 1

R

4 3

EEE

EEE

ERR

EER

3 3

EEE

EER

REE

NoteExplanations for test cases of the example:  in the first test case, it is enough to send a command to move left.  in the second test case, if you try to send any command, at least one robot explodes.  in the third test case, it is enough to send a command to move left.  in the fourth test case, there is already a robot in the upper left corner.  in the fifth test case, the sequence "move up, move left, move up" leads one robot to the upper left corner;  in the sixth test case, if you try to move any robot to the upper left corner, at least one other robot explodes.

def get_top_left_robot(a)
  a.each_with_index do |sub, r|
    sub.each_char.with_index do |v, c|
      if v == 'R'
        return r, c
      end
    end
  end
end

def move(a, n, m)
  row, col = get_top_left_robot(a)

  (row+1..n-1).each do |r|
    (0..col-1).each do |c|
      return "NO" if a[r][c] == 'R'
    end
  end

  "YES"
end

t = gets.chomp.to_i
ans = []

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

  ans << move(a, n, m)
end

puts ans

gets.to_i.times do
  n, m = gets.split.map(&:to_i)
  xxs = n.times.map { gets.chomp }
  i, j = xxs.filter_map.with_index do |xs, i|
    if j = xs.index("R")
      [i, j]
    end
  end.first
  if xxs[...i].join.include?("R") || xxs.map { |xs| xs[...j] }.join.include?("R")
    puts "NO"
  else
    puts "YES"
  end
end

def solve(n,m,a)
  0.upto(n-1) do |i|
    0.upto(m-1) do |j|
      if a[i][j] == 'R' then
        c = 0
        0.upto(n-1) do |k|
          0.upto(m-1) do |l|
            if a[k][l] == 'R' and ((k<i) or (l<j))
              c += 1
            end
          end
        end
        #puts "#{i} #{j} #{c}"
        return "YES" if c == 0
      end
    end
  end
  return "NO"
end

gets.to_i.times do
  n,m = gets.chomp.split.map(&:to_i)
  a = []
  n.times do |i|
    a[i] = gets.chomp.split('')
  end
  puts solve(n,m,a)
end


You are given an array $$$a$$$ consisting of $$$n$$$ integers.In one move, you can choose two indices $$$1 \le i, j \le n$$$ such that $$$i \ne j$$$ and set $$$a_i := a_j$$$. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose $$$i$$$ and $$$j$$$ and replace $$$a_i$$$ with $$$a_j$$$).Your task is to say if it is possible to obtain an array with an odd (not divisible by $$$2$$$) sum of elements.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2000$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ ($$$\sum n \le 2000$$$).

For each test case, print the answer on it — "YES" (without quotes) if it is possible to obtain the array with an odd sum of elements, and "NO" otherwise.

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map &:to_i
	sum = a.inject(:+)
	if sum % 2 == 1
		puts "YES"
	else
		odd = a.count{|i| i % 2 == 1}
		even = n - odd
		puts odd > 0 && even > 0 ? "YES" : "NO"
	end
end

gets.to_i.times {
  gets
  counts = Array.new(2, 0)
  gets.split.map { |v|
    v = v.to_i
    counts[v % 2] += 1
  }
  if counts[1] == 0 or counts[0] == 0 and counts[1] % 2 == 0
    puts "NO"
  else
    puts "YES"
  end
}

# coding: utf-8

# if __FILE__ == $0
# end

t = gets.to_i
t.times {
    n = gets.to_i
    a = gets.split.map(&:to_i)
    evens, odds = a.count(&:even?), a.count(&:odd?)
    if odds.odd?
        puts "YES"
    else
        if evens > 0 && odds > 0
            puts "YES"
        else
            puts "NO"
        end
    end
}

1.upto(gets.to_i) do
  gets
  a = gets.split(' ').map(&:to_i)

  result = a.inject(&:+).odd? || (a.any?(&:odd?) && a.any?(&:even?))

  puts result ? 'YES' : 'NO'
end

t = gets.to_i
(1).upto(t) do
  n = gets.to_i
  a = gets.split(' ').map(&:to_i)
  odd=false
  even = false
  a.each do |i|
    odd = true if(i&1)==1
    even = true if(i&1)==0
  end
  if odd and even
    puts "YES"
  elsif odd and (n&1)==1
    puts"YES"
  else
    puts "NO"
  end
end

# coding: utf-8

# if __FILE__ == $0
# end

t = gets.to_i
t.times {
    n = gets.to_i
    a = gets.split.map(&:to_i)
    evens, odds = a.count(&:even?), a.count(&:odd?)
    if odds.odd?
        puts "YES"
    else
        puts "NO"
    end
}


Ringo found a string $$$s$$$ of length $$$n$$$ in his yellow submarine. The string contains only lowercase letters from the English alphabet. As Ringo and his friends love palindromes, he would like to turn the string $$$s$$$ into a palindrome by applying two types of operations to the string. The first operation allows him to choose $$$i$$$ ($$$2 \le i \le n-1$$$) and to append the substring $$$s_2s_3 \ldots s_i$$$ ($$$i - 1$$$ characters) reversed to the front of $$$s$$$.The second operation allows him to choose $$$i$$$ ($$$2 \le i \le n-1$$$) and to append the substring $$$s_i s_{i + 1}\ldots s_{n - 1}$$$ ($$$n - i$$$ characters) reversed to the end of $$$s$$$.Note that characters in the string in this problem are indexed from $$$1$$$.For example suppose $$$s=$$$abcdef. If he performs the first operation with $$$i=3$$$ then he appends cb to the front of $$$s$$$ and the result will be cbabcdef. Performing the second operation on the resulted string with $$$i=5$$$ will yield cbabcdefedc.Your task is to help Ringo make the entire string a palindrome by applying any of the two operations (in total) at most $$$30$$$ times. The length of the resulting palindrome must not exceed $$$10^6$$$It is guaranteed that under these constraints there always is a solution. Also note you do not have to minimize neither the number of operations applied, nor the length of the resulting string, but they have to fit into the constraints.

The only line contains the string $$$S$$$ ($$$3 \le |s| \le 10^5$$$) of lowercase letters from the English alphabet.

The first line should contain $$$k$$$ ($$$0\le k \le 30$$$)  — the number of operations performed. Each of the following $$$k$$$ lines should describe an operation in form L i or R i. $$$L$$$ represents the first operation, $$$R$$$ represents the second operation, $$$i$$$ represents the index chosen. The length of the resulting palindrome must not exceed $$$10^6$$$.

NoteFor the first example the following operations are performed:abac $$$\to$$$ abacab $$$\to$$$ abacabaThe second sample performs the following operations: acccc $$$\to$$$ cccacccc $$$\to$$$ ccccaccccThe third example is already a palindrome so no operations are required.

S = gets.chomp
n = S.size
ans = []

if S[0] != S[2]
  ans << "L 2"
  n += 1
end

ans << "R 2"
n += n - 2
ans << "R #{n - 1}"

puts ans.size
puts ans

S = gets.chomp
n = S.size

if S[0] != S[2]
  puts "L 2"
  n += 1
end

puts "R 2"
n += n - 2
puts "R #{n - 1}"


You are given two integers $$$l$$$ and $$$r$$$, $$$l\le r$$$. Find the largest possible value of $$$a \bmod b$$$ over all pairs $$$(a, b)$$$ of integers for which $$$r\ge a \ge b \ge l$$$.As a reminder, $$$a \bmod b$$$ is a remainder we get when dividing $$$a$$$ by $$$b$$$. For example, $$$26 \bmod 8 = 2$$$.

Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ $$$(1\le t\le 10^4)$$$, denoting the number of test cases. Description of the test cases follows. The only line of each test case contains two integers $$$l$$$, $$$r$$$ ($$$1\le l \le r \le 10^9$$$).

For every test case, output the largest possible value of $$$a \bmod b$$$ over all pairs $$$(a, b)$$$ of integers for which $$$r\ge a \ge b \ge l$$$.

NoteIn the first test case, the only allowed pair is $$$(a, b) = (1, 1)$$$, for which $$$a \bmod b = 1 \bmod 1 = 0$$$.In the second test case, the optimal choice is pair $$$(a, b) = (1000000000, 999999999)$$$, for which $$$a \bmod b = 1$$$.

t = gets.to_i
t.times do

l, r = gets.split.map!(&:to_i)

if l * 2 <= r
  puts r.pred / 2
else
  puts r - l
end

end


You are given N points on a plane. Write a program which will find the sum of squares of distances between all pairs of points.

The first line of input contains one integer number N (1 ≤ N ≤ 100 000) — the number of points. Each of the following N lines contain two integer numbers X and Y ( - 10 000 ≤ X, Y ≤ 10 000) — the coordinates of points. Two or more points may coincide.

The only line of output should contain the required sum of squares of distances between all pairs of points.

def solve(x,n)
    sum=0
    ans=0
    x.each{|i| sum+=i}
    x.each{|i|
        ans+=(n-1)*i*i-(sum-i)*i
    }
    ans
end

n=gets.to_i
x=Array.new(n,0)
y=Array.new(n,0)
n.times{|i|
    input=gets.split.map(&:to_i)
    x[i]=input[0]
    y[i]=input[1]
}

p solve(x,n)+solve(y,n)

#Infinity
#geti,getia
#Object # dcopy
#
#String # to_a
#   "abc".to_a #=> ["a","b","c"]
#String # to_ia
#   "1 2 3".to_ia #=> [1,2,3]
#
#Array # rsort,rsort!
#   [1,3,2,4].rsort #=> [4,3,2,1]
#
#Integer # divisor
#   24.divisor #=> [1,2,3,4,6,8,12,24]
#
include Math
require "prime"
require "bigdecimal"
require "bigdecimal/math"
require "matrix"
require "pp"
require "benchmark"

Infinity = 1/0.0
class Matrix;def inspect;empty? ? "Matrix.empty(#{row_size}, #{column_size})":"Matrix[\n#{@rows.map(&:inspect).join("\n")}\n]";end;end
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_a;split("");end;end
class String;def to_ia;split.map(&:to_i);end;end
def getia;gets.to_ia;end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end
class Array;def rsort;sort.reverse;end;def rsort!;sort!.reverse!;end;end

n = geti
px = []
py = []
res = 0
n.times{
    p = getia
    res += (p[0]**2 + p[1]**2) * (n - 1)
    px.push p[0]
    py.push p[1]
}
xs = px.inject(&:+)
xx = 0
px.each{|x| xx += x * (xs - x) }
ys = py.inject(&:+)
yy = 0
py.each{|y| yy += y * (ys - y) }

p res - xx - yy

n=gets.to_i
sigx1,sigx2,sigy1,sigy2=[0,0,0,0]
n.times do
    a,b=gets.split.map(&:to_i)
    sigx1+=a
    sigx2+=a*a
    sigy1+=b
    sigy2+=b*b
end
p n*(sigx2+sigy2)-sigx1**2-sigy1**2


Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder.

The first line contains an integer n (1 ≤ n ≤ 1000).

On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any.

n = gets.to_i
dx = Array.new(n).map{|v| v = Array.new(n, 0)}
for i in 0...n do
  for j in 0...n do
    if i % 2 == 0
      dx[i][j] = (j % 2 == 0 ? "C" : ".")
    else
      dx[i][j] = (j % 2 == 0 ? "." : "C")
    end
  end
end
cnt = 0
for i in 0...n do
  for j in 0...n do
    cnt += 1 if dx[i][j] == 'C'
  end
end
puts cnt
for i in 0...n do
  puts dx[i].join()
end

n = STDIN.gets.to_i
v1 = (n.to_f / 2).floor
v2 = (n.to_f / 2).ceil
puts v1*v1 + v2*v2
lineCoder = true
(0...n).each do
  colCoder = lineCoder
  (0...n).each do
    print(colCoder ? 'C' : '.')
    colCoder = !colCoder
  end
  puts
  lineCoder = !lineCoder
end

n = gets.chomp().to_i
if (n % 2 == 0)
	puts(n*n/2)
else
	puts((n-1)*n/2 + (n+1)/2)
end
for i in 1..n
	for j in 1..n
		if i%2 == j%2
			print 'C'
		else
			print '.'
		end
	end
	puts
end

n = gets.to_i
res =  n ** 2 / 4  + (n + 1) / 2 * (n / 2)
puts n.even? ? res : res + 1
n.times do |i|
  if i.even?
    puts ("C." * ((n + 1) / 2)).slice(0..n - 1)
  else
    puts (".C" * ((n + 1) / 2)).slice(0..n - 1)
  end
end

n = gets.to_i

ans = []
n.times do |i|
  if i.even?
    ans << "C." * (n/2) + "C" * (n%2)
  else
    ans << ".C" * (n/2) + "." * (n%2)
  end
end
puts n ** 2 / 2 + n % 2
puts ans

n=gets.to_i
if n%2==0 then
	puts n/2*n
else
	puts n*(n/2)+(n/2+1)
end

n.times{|i|
	n.times{|j|
		if (i+j)%2==0 then
			print "C"
		else
			print "."
		end
	}
	puts ""
}

n = gets.to_i
ans = Array.new(n) {Array.new(n) {'.'}}
count = 0
n.times do |i|
    n.times do |j|
        if (i+j)%2==0
            ans[i][j] = 'C'
            count+=1
        end
    end
end
p count
ans.each{|x| puts x.join('')}

n = gets.to_i
a = ['C', '.']
s = []
(n / 2 + 1).times do
  s << a
end
s = s.join("").chars
puts (n * n + 1) / 2
n.times do |i|
  puts s[(i % 2)...(i % 2 + n)].join("")
end

n = gets.to_i
puts ((n + 1)/2)**2 + (n/2)**2
n.times do |i|
  n.times do |j|
    print i%2 == j%2 ? 'C' : '.'
  end
  puts
end

n = gets.chomp.to_i

number = 0

(0...n).each do |row|
	(0...n).each do |col|
		if row % 2 == 0
			number += 1 if col % 2 == 0
		else
			number += 1 if col % 2 == 1
		end
	end
end

puts number

(0...n).each do |row|
	(0...n).each do |col|
		if row % 2 == 0
			print 'C' if col % 2 == 0
			print '.' if col % 2 == 1
		else
			print 'C' if col % 2 == 1
			print '.' if col % 2 == 0
		end
	end

	puts ''
end

n = gets.to_i
puts (n*n+1)/2
(1..n).each_with_index do |x,id|
  if x % 2 == 1
    puts "C." * (n/2) + "C" * (n%2)
  else
    puts ".C" * (n/2) + "." * (n%2)
  end
end

n = gets.to_i
dx = Array.new(n).map{|v| v = Array.new(n, 0)}
for i in 0...n do
  for j in 0...n do
    if i % 2 == 0
      dx[i][j] = (j % 2 == 0 ? "C" : ".")
    else
      dx[i][j] = (j % 2 == 0 ? "." : "C")
    end
  end
end
cnt = 0
for i in 0...n do
  for i in 0...n do
    cnt += 1 if dx[i][j] == 'C'
  end
end
puts cnt
for i in 0...n do
  puts dx[i].join()
end

n = STDIN.gets.to_i
v1 = (n / 2).floor
v2 = (n / 2).ceil
puts v1*v1 + v2*v2
line_coder = true
(0...n).each do
  col_coder = line_coder
  (0...n).each do
    print(col_coder ? 'C' : '.')
    col_coder = !col_coder
  end
  puts
  line_coder = !line_coder
end

n = gets.to_i
n.times do |i|
  if i.even?
    puts ("C." * ((n + 1) / 2)).slice(0..n - 1)
  else
    puts (".C" * ((n + 1) / 2)).slice(0..n - 1)
  end
end

n=gets.to_i
puts n
n.times{|i|
	n.times{|j|
		if i==j then
			print "C"
		else
			print "."
		end
	}
	puts ""
}

n = gets.to_i
ans = Array.new(n) {Array.new(n) {'.'}}
n.times do |i|
    n.times do |j|
        if (i+j)%2==0
            ans[i][j] = 'C'
        end
    end
end
ans.each{|x| puts x.join('')}

n = gets.chomp.to_i

if n == 1
	puts "1"
end

number = 0

(0...n).each do |row|
	(0...n).each do |col|
		if row % 2 == 0
			number += 1 if col % 2 == 0
		else
			number += 1 if col % 2 == 1
		end
	end
end

puts number

(0...n).each do |row|
	(0...n).each do |col|
		if row % 2 == 0
			print 'C' if col % 2 == 0
			print '.' if col % 2 == 1
		else
			print 'C' if col % 2 == 1
			print '.' if col % 2 == 0
		end
	end

	puts ''
end

n = gets.to_i
(1..n).each_with_index do |x,id|
  if x % 2 == 1
    puts "C." * (n/2) + "C" * (n%2)
  else
    puts ".C" * (n/2) + "." * (n%2)
  end
end


This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with:  1 if $$$a \oplus c>b \oplus d$$$.  0 if $$$a \oplus c=b \oplus d$$$.  -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$.

To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer.

NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer.

a = b = 0
puts "? 0 0"
STDOUT.flush
s = gets.to_i
l = s > 0 ? true : false
f = false
f = true if s == 0
29.downto(0) do |i|
    break if f
    puts "? #{a + (1 << i)} #{b + (1 << i)}"
    STDOUT.flush
    s = gets.to_i
    if s == 0
        f = true
    elsif ((s == -1) && l) || ((s == 1) && !l)
        if s == -1
            a += 1 << i
        else
            b += 1 << i
        end
        puts "? #{a} #{b}"
        STDOUT.flush
        s = gets.to_i
        l = s > 0 ? true : false
        f = true if s == 0
    end
end
29.downto(0) do |i|
    next if a[i] == 1 || b[i] == 1
    puts "? #{a} #{b+(1 << i)}"
    STDOUT.flush
    s = gets.to_i
    if (s == 1)
        a += 1 << i
        b += 1 << i
    end
end
puts "! #{a} #{b}"

ahyp = "X"*30
bhyp = "X"*30
checkedi = 0

puts "? " + "0" + " " + "0"
STDOUT.flush
ans = gets.chomp.to_i

if ans==1
  aisgreater = true
  theyareequal = false
elsif ans==-1
  aisgreater = false
  theyareequal = false
else
  aisgreater = false
  theyareequal = true
end

preva = ""
prevb = ""

61.times do

#  p ahyp
#  p bhyp

  if aisgreater and not theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      end
    elsif res==-1
      puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "0"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end
    #UPDATE PREVSTRINGS, INCREASE ITERVAR

  elsif (not aisgreater) and (not theyareequal)
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    elsif res==-1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i
      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "1"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end

  elsif theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "0"
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "1"
    end
    theyareequal = false
    #DEAL WITH EQUALITY
  end

  preva = ahyp[0..checkedi]
  prevb = bhyp[0..checkedi]
  checkedi = checkedi+1

  if (not ahyp.include?("X"))
    puts "! " + ahyp.to_i(2).to_s + " " + bhyp.to_i(2).to_s
    exit
  end

  #UPDATE PREVSTRINGS, INCREASE ITERVAR
end

a = b = 0
puts "? 0 0"
STDOUT.flush
s = gets.to_i
l = s > 0 ? true : false
f = false
f = true if s == 0
30.downto(0) do |i|
    break if f
    puts "? #{a + (1 << i)} #{b + (1 << i)}"
    STDOUT.flush
    s = gets.to_i
    if s == 0
        f = true
    elsif ((s == -1) && l) || ((s == 1) && !l)
        if s == -1
            a += 1 << i
        else
            b += 1 << i
        end
        puts "? #{a} #{b}"
        STDOUT.flush
        s = gets.to_i
        l = s > 0 ? true : false
        f = true if s == 0
    end
end
30.downto(0) do |i|
    next if a[i] == 1 || b[i] == 1
    puts "? #{a} #{b+(1 << i)}"
    STDOUT.flush
    s = gets.to_i
    if (s == 1)
        a += 1 << i
        b += 1 << i
    end
end
puts "! #{a} #{b}"

a = b = 0
puts "? 0 0"
STDOUT.flush
s = gets.to_i
l = s > 0 ? true : false
f = false
f = true if s == 0
30.downto(0) do |i|
    break if f
    puts "? #{a + (1 << i)} #{b + (1 << i)}"
    STDOUT.flush
    s = gets.to_i
    if s == 0
        f = true
    elsif (s == -1) && l
        a += 1 << i
        puts "? #{a} #{b}"
        STDOUT.flush
        s = gets.to_i
        l = s > 0 ? true : false
        f = true if s == 0
    elsif (s == 1) && !l
        b += 1 << i
    end
end
30.downto(0) do |i|
    next if a[i] == 1 || b[i] == 1
    puts "? #{a} #{b+(1 << i)}"
    STDOUT.flush
    s = gets.to_i
    if (s == 1)
        a += 1 << i
        b += 1 << i
    end
end
puts "! #{a} #{b}"

ahyp = "X"*30
bhyp = "X"*30
checkedi = 0

puts "? " + "0" + " " + "0"
STDOUT.flush
ans = gets.chomp.to_i

if ans==1
  aisgreater = true
  theyareequal = true
elsif ans==-1
  aisgreater = false
  theyareequal = true
else
  aisgreater = false
  theyareequal = true
end

preva = ""
prevb = ""

61.times do

  if aisgreater and not theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      end
    elsif res==-1
      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "0"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end
    #UPDATE PREVSTRINGS, INCREASE ITERVAR

  elsif (not aisgreater) and (not theyareequal)
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    elsif res==-1
      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "1"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end

  elsif theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "0"
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "1"
    end
    theyareequal = true
    #DEAL WITH EQUALITY
  end

  preva = ahyp[0..checkedi]
  preva = bhyp[0..checkedi]
  checkedi = checkedi+1

  if (not ahyp.include?("X"))
    puts "! " + ahyp.to_i(2).to_s + " " + bhyp.to_i(2).to_s
    exit
  end

  #UPDATE PREVSTRINGS, INCREASE ITERVAR
end

ahyp = "X"*30
bhyp = "X"*30
checkedi = 0

puts "? " + "0" + " " + "0"
STDOUT.flush
ans = gets.chomp.to_i

if ans==1
  aisgreater = true
  theyareequal = false
elsif ans==-1
  aisgreater = false
  theyareequal = false
else
  aisgreater = false
  theyareequal = true
end

preva = ""
prevb = ""

61.times do

#  p ahyp
#  p bhyp

  if aisgreater and not theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      end
    elsif res==-1
      puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "0"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end
    #UPDATE PREVSTRINGS, INCREASE ITERVAR

  elsif (not aisgreater) and (not theyareequal)
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    elsif res==-1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i
      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "1"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end

  elsif theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==-1
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "0"
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "1"
    end
    theyareequal = false
    #DEAL WITH EQUALITY
  end

  preva = ahyp[0..checkedi]
  preva = bhyp[0..checkedi]
  checkedi = checkedi+1

  if (not ahyp.include?("X"))
    puts "! " + ahyp.to_i(2).to_s + " " + bhyp.to_i(2).to_s
    exit
  end

  #UPDATE PREVSTRINGS, INCREASE ITERVAR
end

ahyp = "X"*30
bhyp = "X"*30
checkedi = 0

puts "? " + "0" + " " + "0"
STDOUT.flush
ans = gets.chomp.to_i

if ans==1
  aisgreater = true
  theyareequal = false
elsif ans==-1
  aisgreater = false
  theyareequal = false
else
  aisgreater = false
  theyareequal = true
end

preva = ""
prevb = ""

61.times do

#  p ahyp
#  p bhyp

  if aisgreater and not theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      end
    elsif res==-1
      puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "0"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end
    #UPDATE PREVSTRINGS, INCREASE ITERVAR

  elsif (not aisgreater) and (not theyareequal)
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    elsif res==-1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i
      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "1"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end

  elsif theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==-1
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "0"
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "1"
    end
    theyareequal = false
    #DEAL WITH EQUALITY
  end

  preva = ahyp[0..checkedi]
  prevb = bhyp[0..checkedi]
  checkedi = checkedi+1

  if (not ahyp.include?("X"))
    puts "! " + ahyp.to_i(2).to_s + " " + bhyp.to_i(2).to_s
    exit
  end

  #UPDATE PREVSTRINGS, INCREASE ITERVAR
end

ahyp = "X"*30
bhyp = "X"*30
checkedi = 0

puts "? " + "0" + " " + "0"
STDOUT.flush
ans = gets.chomp.to_i

if ans==1
  aisgreater = true
  theyareequal = false
elsif ans==-1
  aisgreater = false
  theyareequal = false
else
  aisgreater = false
  theyareequal = true
end

preva = ""
prevb = ""

61.times do

  p ahyp
  p bhyp

  if aisgreater and not theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      end
    elsif res==-1
      puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "0"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end
    #UPDATE PREVSTRINGS, INCREASE ITERVAR

  elsif (not aisgreater) and (not theyareequal)
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    elsif res==-1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i
      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "1"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end

  elsif theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==-1
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "0"
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "1"
    end
    theyareequal = false
    #DEAL WITH EQUALITY
  end

  preva = ahyp[0..checkedi]
  prevb = bhyp[0..checkedi]
  checkedi = checkedi+1

  if (not ahyp.include?("X"))
    puts "! " + ahyp.to_i(2).to_s + " " + bhyp.to_i(2).to_s
    exit
  end

  #UPDATE PREVSTRINGS, INCREASE ITERVAR
end

ahyp = "X"*30
bhyp = "X"*30
checkedi = 0

puts "? " + "0" + " " + "0"
STDOUT.flush
ans = gets.chomp.to_i

if ans==1
  aisgreater = true
  theyareequal = false
elsif ans==-1
  aisgreater = false
  theyareequal = false
else
  aisgreater = false
  theyareequal = true
end

preva = ""
prevb = ""

61.times do

  p ahyp
  p bhyp

  if aisgreater and not theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = true
        #ALAT > BLAT
      end
    elsif res==-1
      puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "0"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end
    #UPDATE PREVSTRINGS, INCREASE ITERVAR

  elsif (not aisgreater) and (not theyareequal)
    puts "? " + (preva+"0"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"1"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i

      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = true
        #ALAT>BLAT
      elsif newres==-1
        ahyp[checkedi] = "1"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    elsif res==-1
      puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
      STDOUT.flush
      newres = gets.to_i
      if newres ==1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "0"
        aisgreater = false
        #ALAT<BLAT
      elsif newres==-1
        ahyp[checkedi] = "0"
        bhyp[checkedi] = "1"
        aisgreater = false
        #ALAT<BLAT
      end
    else
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "1"
      theyareequal = true
      #DEAL WITH EQUALITY HERE
    end

  elsif theyareequal
    puts "? " + (preva+"1"+"0"*(29-checkedi)).to_i(2).to_s + " " + (prevb+"0"+"0"*(29-checkedi)).to_i(2).to_s
    STDOUT.flush
    res = gets.to_i

    if res==-1
      ahyp[checkedi] = "0"
      bhyp[checkedi] = "0"
    else
      ahyp[checkedi] = "1"
      bhyp[checkedi] = "1"
    end
    theyareequal = false
    #DEAL WITH EQUALITY
  end

  preva = ahyp[0..checkedi]
  preva = bhyp[0..checkedi]
  checkedi = checkedi+1

  if (not ahyp.include?("X"))
    puts "! " + ahyp.to_i(2).to_s + " " + bhyp.to_i(2).to_s
    exit
  end

  #UPDATE PREVSTRINGS, INCREASE ITERVAR
end


Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.

The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.

If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that:   the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company,  the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company,  the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,  Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.

6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC


You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y < x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.

For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.

NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.

gets.to_i.times do
	x, y, n = gets.split.map(&:to_i)
	if n % x >= y
		puts n / x * x + y
	else
		puts (n / x - 1) * x + y
	end
end

t = gets.to_i
t.times do
	x,y,n = gets.split.map(&:to_i)
	ans = x * (n/x) + y
	if ans > n
		ans -= x
	end
	puts ans
end

def run
  x, y, n = gets.chomp.split(" ").map(&:to_i)

  l = n % x

  # puts [x, y, n, l].inspect

  if l == y
    puts n
  elsif l > y
    puts n - (l - y)
  else
    puts n - (x - (y - l))
  end
end

def main
  t = gets.chomp.to_i

  t.times do
    run
  end
end

main

n = gets.to_i
n.times do
    x ,y, n = gets.chomp.split(" ").map(&:to_i)
    first = n - y
    kari = first / x
    ans = kari * x + y
    puts ans
end

gets.to_i.times do
  x, y, n = gets.split.map(&:to_i)
  puts (n - y) / x * x + y
end

def answer(n, x, y)
  return answer(n - n.modulo(x) - 1, x, y) if n.modulo(x) < y

  return n - (n.modulo(x) - y)
end

gets.to_i.times do
  x, y, n = gets.split(' ').map(&:to_i)

  puts answer(n, x, y)
end


You have one chip and one chance to play roulette. Are you feeling lucky?

Print your bet. Your chip must be placed entirely within some square (not on an edge or a corner shared by adjacent squares).


This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: "? a b" where $$$1 \le a, b \le 10^{18}$$$ and $$$a \neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\max(a, b) > n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query "? a b", no matter how many such queries. Note that the "? b a" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.

NoteIn the first example, the graph could look like this  The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$.   The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$.  With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$.  In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$.

(2..26).each do |i|
  puts("? 1 #{i}")
  $stdout.flush
  a = gets.to_i
  puts("? #{i} 1")
  $stdout.flush
  b = gets.to_i
  if a != b
    puts("! #{a + b}")
    break
  elsif a == -1
    puts("! #{i - 1}")
    break
  end
end

(2..26).each do |i|
  puts("? 1 #{i}")
  $stdout.flush
  a = gets.to_i
  puts("? #{i} 1")
  $stdout.flush
  b = gets.to_i
  if a != b
    puts("! #{a + b}")
    break
  elsif a == -1
    puts("! #{a - 1}")
    break
  end
end

puts("? 1 2")
$stdout.flush
a = gets.to_i
b = 0
49.times do
  puts("? 2 1")
  $stdout.flush
  b = gets.to_i
  break if a != b
end
puts("! #{a + b}")

puts("? 1 2")
$stdout.flush
a = gets.to_i
b = 0
49.times do
  puts("? 1 2")
  $stdout.flush
  b = gets.to_i
  break if a != b
end
puts("! #{a + b}")

puts("? 1 2")
$stdout.flush
a = gets.to_i
b = 0
50.times do
  puts("? 1 2")
  $stdout.flush
  b = gets.to_i
  break if a != b
end
puts("! #{a + b}")

puts("? 1 2")
$stdout.flush
a = gets.to_i
loop do
  puts("? 1 2")
  $stdout.flush
  b = gets.to_i
  if a != b
    puts("! #{a + b}")
    break
  end
end

l = 3
r = 10**18 + 1
c = 50
while l < r && c > 0
  m = (l + r) / 2
  puts("? 1 #{m}")
  $stdout.flush
  s = gets.to_i
  if s == -1
    r = m
  elsif l < s
    l = s + 1
  end
  c -= 1
end
puts("! #{l}")

l = 1
r = 10**18 + 1
c = 50
while l < r && c > 0
  m = (l + r) / 2
  puts("? 1 #{m}")
  $stdout.flush
  s = gets.to_i
  if s == -1
    r = m
  elsif l < s
    l = s + 1
  end
  c -= 1
end
puts("! #{l}")


3R2 - Standby for ActionOur dear Cafe's owner, JOE Miller, will soon take part in a new game TV-show "1 vs. $$$n$$$"!The game goes in rounds, where in each round the host asks JOE and his opponents a common question. All participants failing to answer are eliminated. The show ends when only JOE remains (we assume that JOE never answers a question wrong!).For each question JOE answers, if there are $$$s$$$ ($$$s > 0$$$) opponents remaining and $$$t$$$ ($$$0 \le t \le s$$$) of them make a mistake on it, JOE receives $$$\displaystyle\frac{t}{s}$$$ dollars, and consequently there will be $$$s - t$$$ opponents left for the next question.JOE wonders what is the maximum possible reward he can receive in the best possible scenario. Yet he has little time before show starts, so can you help him answering it instead?

The first and single line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), denoting the number of JOE's opponents in the show.

Print a number denoting the maximum prize (in dollars) JOE could have. Your answer will be considered correct if it's absolute or relative error won't exceed $$$10^{-4}$$$. In other words, if your answer is $$$a$$$ and the jury answer is $$$b$$$, then it must hold that $$$\frac{|a - b|}{max(1, b)} \le 10^{-4}$$$.

NoteIn the second example, the best scenario would be: one contestant fails at the first question, the other fails at the next one. The total reward will be $$$\displaystyle \frac{1}{2} + \frac{1}{1} = 1.5$$$ dollars.

s,k=0.0,1.0
a=gets.to_i
while k<=a
	s+=1/k
	k+=1
end
puts (s)

n = gets.to_i
ans = 0
(1..n).each do |x|
	ans += 1 / x.to_f
end
puts ans.to_f


You are given three strings $$$a$$$, $$$b$$$ and $$$c$$$ of the same length $$$n$$$. The strings consist of lowercase English letters only. The $$$i$$$-th letter of $$$a$$$ is $$$a_i$$$, the $$$i$$$-th letter of $$$b$$$ is $$$b_i$$$, the $$$i$$$-th letter of $$$c$$$ is $$$c_i$$$.For every $$$i$$$ ($$$1 \leq i \leq n$$$) you must swap (i.e. exchange) $$$c_i$$$ with either $$$a_i$$$ or $$$b_i$$$. So in total you'll perform exactly $$$n$$$ swap operations, each of them either $$$c_i \leftrightarrow a_i$$$ or $$$c_i \leftrightarrow b_i$$$ ($$$i$$$ iterates over all integers between $$$1$$$ and $$$n$$$, inclusive).For example, if $$$a$$$ is "code", $$$b$$$ is "true", and $$$c$$$ is "help", you can make $$$c$$$ equal to "crue" taking the $$$1$$$-st and the $$$4$$$-th letters from $$$a$$$ and the others from $$$b$$$. In this way $$$a$$$ becomes "hodp" and $$$b$$$ becomes "tele".Is it possible that after these swaps the string $$$a$$$ becomes exactly the same as the string $$$b$$$?

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a string of lowercase English letters $$$a$$$. The second line of each test case contains a string of lowercase English letters $$$b$$$. The third line of each test case contains a string of lowercase English letters $$$c$$$. It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $$$100$$$.

Print $$$t$$$ lines with answers for all test cases. For each test case: If it is possible to make string $$$a$$$ equal to string $$$b$$$ print "YES" (without quotes), otherwise print "NO" (without quotes). You can print either lowercase or uppercase letters in the answers.

4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim

NoteIn the first test case, it is impossible to do the swaps so that string $$$a$$$ becomes exactly the same as string $$$b$$$.In the second test case, you should swap $$$c_i$$$ with $$$a_i$$$ for all possible $$$i$$$. After the swaps $$$a$$$ becomes "bca", $$$b$$$ becomes "bca" and $$$c$$$ becomes "abc". Here the strings $$$a$$$ and $$$b$$$ are equal.In the third test case, you should swap $$$c_1$$$ with $$$a_1$$$, $$$c_2$$$ with $$$b_2$$$, $$$c_3$$$ with $$$b_3$$$ and $$$c_4$$$ with $$$a_4$$$. Then string $$$a$$$ becomes "baba", string $$$b$$$ becomes "baba" and string $$$c$$$ becomes "abab". Here the strings $$$a$$$ and $$$b$$$ are equal.In the fourth test case, it is impossible to do the swaps so that string $$$a$$$ becomes exactly the same as string $$$b$$$.

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

next_int.times do |_i|
  a = next_token
  b = next_token
  c = next_token
  ok = true
  a.size.times do |i|
    next if a[i] == c[i] || b[i] == c[i]

    ok = false
  end
  if ok
    puts 'YES'
  else
    puts 'NO'
  end
end

T = gets.to_i

T.times do
  a = gets.chomp
  b = gets.chomp
  c = gets.chomp
  n = a.size

  success = true

  n.times do |i|
    if a[i] != c[i] && b[i] != c[i]
      success = false
    end
  end

  if success
    puts 'YES'
  else
    puts 'NO'
  end
end

n = gets.to_i
n.times {
  a = gets.chomp
  b = gets.chomp
  c = gets.chomp
  flag = true
  for i in (0..a.length - 1)
    if a[i] != c[i] and b[i] != c[i]
      flag = false
    end
  end
  puts flag ? "YES" : "NO"
}

n = Integer gets
for jj in 0...n
s1, s2, s3 = gets.chop!, gets.chop!, gets.chop!
flg = "YES"
for i in 0...s3.size
  if s3[i] != s1[i] && s3[i] != s2[i] then flg = "NO" end
end
puts flg
end

gets.to_i.times{
    a = gets.chomp
    b = gets.chomp
    c = gets.chomp
    x = true
    (0...a.length).each do |i|
        if (a[i] == b[i] && a[i] != c[i] || a[i] != b[i] && b[i] != c[i] && a[i] != c[i])
            x = false
            break
        end
    end
    puts x ? "YES" : "NO"
}

gets.to_i.times{
    a = gets.chomp
    b = gets.chomp
    c = gets.chomp
    x = true
    (0...a.length).each do |i|
        if !((a[i] == c[i]) ^ (b[i] == c[i]) ^ (a[i] == b[i]))
            x = false
            break
        end
    end
    puts x ? "YES" : "NO"
}

gets.to_i.times{
    a = gets.chomp
    b = gets.chomp
    c = gets.chomp
    x = true
    (0...a.length).each do |i|
        if !((a[i] == c[i]) ^ (b[i] == c[i]))
            x = false
            break
        end
    end
    puts x ? "YES" : "NO"
}


Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \leq x_i \leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d$$$).

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$  — the number of test cases.  The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \leq n \leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \leq b < a \leq 1000, 0 \leq d < c \leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).

For each test case given in the input print "Yes", if the information about the weights is not inconsistent, and print "No" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.

NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.

t = gets.to_i
t.times do
  n, a, b, c, d = gets.split.map(&:to_i)
  ok = true
  if (a - b) * n > (c + d) || (a + b) * n < (c - d)
    ok = false
  end
  puts ok ? "Yes" : "No"
end

t = gets.chomp.to_i
t.times do
	n, a, b, c, d = gets.chomp.split(" ").map(&:to_i)
	flag = false
	# (n*(a-b)..n*(a+b)).each do |ngo|
	# 	#print ngo.to_s + " "
	# 	if c - d <= ngo && ngo <= c + d then
	# 		flag = true
	# 		break
	# 	end
	# end
	nab1 = n*(a-b)
	nab2 = n*(a+b)
	puts (nab1 <= c-d && c + d <= nab2) || (c-d<=nab1 && nab1<=c+d) || (c-d<=nab2&&nab2<=c+d) ? "Yes" : "No"
end

# Task 1341A
# require 'byebug'

t = gets.chomp.to_i
answers = []
t.times do
    # Reading input
    str = gets.chomp
    n, a, b, c, d = str.split(" ").map{ |n| n.to_i }

    min_weight = a - b
    max_weight = a + b
    min_total = c - d
    max_total = c + d

    if (max_weight * n) < min_total || max_total < (min_weight * n)
        answers << "No"
    else
        answers << "Yes"
    end
end
puts answers

class Range
  def crosses?(x)
    include?(x.first) || include?(x.last) || x.include?(first) || x.include?(last)
  end
end

1.upto(gets.to_i) do
  n, a, b, c, d = gets.split(' ').map(&:to_i)

  puts ((c-d)..(c+d)).crosses?((n*(a-b))..(n*(a+b))) ? 'Yes' : 'No'
end

def solve n, a, b, c, d
  return "Yes" if n * (a-b) <= c+d && n * (a+b) >= c-d
  "No"
end

t = STDIN.gets.chomp.to_i

(1..t).each do |i|
  n, a, b, c, d = STDIN.gets.chomp.split.map {|i| i.to_i}
  res = solve n, a, b, c, d
  puts res
end

t = gets.to_i
t.times do
	n, a, b, c, d = gets.strip.split.map(&:to_i)
	min_grains = (a - b) * n
	max_grains = (a + b) * n
	min_pack = c - d
	max_pack = c + d
	if max_grains < min_pack || max_pack < min_grains
		puts "No"
	else
		puts "Yes"
	end
end

gets.to_i.times {
  n, a, b, c, d = gets.split.map { |v| v.to_i }
  min_sum = n * (a - b)
  max_sum = n * (a + b)
  if min_sum > c + d or max_sum < c - d
    puts "No"
  else
    puts "Yes"
  end
}

1.upto(gets.chomp.to_i) do
  n,a,b,c,d = gets.chomp.split.map(&:to_i)
  if (n * (a-b) <= (c+d) && n*(a+b) >= (c-d))
    puts 'Yes'
  else
    puts 'No'
  end
end

t = gets.chomp.to_i
t.times do
	n, a, b, c, d = gets.chomp.split(" ").map(&:to_i)
	flag = false
	(a-b..a+b).each do |ngo|
		if c - d <= n * ngo && n * ngo <= c + d then
			puts "Yes"
			flag = true
			break
		end
	end
	puts "No" if flag == false
end

# Task 1341A
# require 'byebug'

t = gets.chomp.to_i
answers = []
t.times do
    # Reading input
    str = gets.chomp
    n, a, b, c, d = str.split(" ").map{ |n| n.to_i }

    min_weight = a - b
    max_weight = a + b
    min_total = c - d
    max_total = c + d

    if (min_weight * n) >= min_total || ((max_weight * n) <= max_total && (max_weight * n) >= min_total)
        answers << "Yes"
    else
        answers << "No"
    end
end
puts answers

def solve n, a, b, c, d
  tot = [c-d, c+d]
  ((a-b)..(a+b)).each do |i|
    return "Yes" if n * i >= tot[0] && n * i <= tot[1]
    return "Yes" if n * i >= tot[0] && n * i <= tot[1]
  end
  "No"
end

t = STDIN.gets.chomp.to_i

(1..t).each do |i|
  n, a, b, c, d = STDIN.gets.chomp.split.map {|i| i.to_i}
  res = solve n, a, b, c, d
  puts res
end

def solve n, a, b, c, d
  tot = [c-d, c+d]
  return "Yes" if n * (a-b) >= tot[0] && n * (a-b) <= tot[1]
  return "Yes" if n * (a+b) >= tot[0] && n * (a+b) <= tot[1]
  "No"
end

t = STDIN.gets.chomp.to_i

(1..t).each do |i|
  n, a, b, c, d = STDIN.gets.chomp.split.map {|i| i.to_i}
  res = solve n, a, b, c, d
  puts res
end

t = gets.to_i
t.times do
	n, a, b, c, d = gets.strip.split.map(&:to_i)
	min_grains = (a - b) * n
	max_grains = (a + b) * n
	min_pack = c - d
	max_pack = c + d
	if (min_pack <= min_grains && max_pack >= min_grains) || (min_pack <= max_grains && max_pack >= max_grains)
		puts "Yes"
	else
		puts "No"
	end
end

t = gets.to_i
t.times do
	n, a, b, c, d = gets.strip.split.map(&:to_i)
	min_grains = (a - b) * n
	max_grains = (a + b) * n
	min_pack = c - d
	max_pack = c + d
	if (min_pack <= min_grains && min_grains <= max_pack) || (min_pack <= max_grains && max_grains <= max_pack)
		puts "Yes"
	else
		puts "No"
	end
end


An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons — 8 blocks in total, each block has four hexadecimal digits. Here is an example of the correct record of a IPv6 address: "0124:5678:90ab:cdef:0124:5678:90ab:cdef". We'll call such format of recording an IPv6-address full.Besides the full record of an IPv6 address there is a short record format. The record of an IPv6 address can be shortened by removing one or more leading zeroes at the beginning of each block. However, each block should contain at least one digit in the short format. For example, the leading zeroes can be removed like that: "a56f:00d3:0000:0124:0001:f19a:1000:0000"  →  "a56f:d3:0:0124:01:f19a:1000:00". There are more ways to shorten zeroes in this IPv6 address.Some IPv6 addresses contain long sequences of zeroes. Continuous sequences of 16-bit zero blocks can be shortened to "::". A sequence can consist of one or several consecutive blocks, with all 16 bits equal to 0. You can see examples of zero block shortenings below:  "a56f:00d3:0000:0124:0001:0000:0000:0000"  →  "a56f:00d3:0000:0124:0001::";  "a56f:0000:0000:0124:0001:0000:1234:0ff0"  →  "a56f::0124:0001:0000:1234:0ff0";  "a56f:0000:0000:0000:0001:0000:1234:0ff0"  →  "a56f:0000::0000:0001:0000:1234:0ff0";  "a56f:00d3:0000:0124:0001:0000:0000:0000"  →  "a56f:00d3:0000:0124:0001::0000";  "0000:0000:0000:0000:0000:0000:0000:0000"  →  "::". It is not allowed to shorten zero blocks in the address more than once. This means that the short record can't contain the sequence of characters "::" more than once. Otherwise, it will sometimes be impossible to determine the number of zero blocks, each represented by a double colon.The format of the record of the IPv6 address after removing the leading zeroes and shortening the zero blocks is called short.You've got several short records of IPv6 addresses. Restore their full record.

The first line contains a single integer n — the number of records to restore (1 ≤ n ≤ 100). Each of the following n lines contains a string — the short IPv6 addresses. Each string only consists of string characters "0123456789abcdef:". It is guaranteed that each short address is obtained by the way that is described in the statement from some full IPv6 address.

For each short IPv6 address from the input print its full record on a separate line. Print the full records for the short IPv6 addresses in the order, in which the short records follow in the input.

gets.to_i.times do
  bb = gets.strip.gsub('::', ':zero:').split(':')
  seq_len = 8 - bb.size + 1
  blocks = []
  bb.each do |b|
    if b == 'zero'
      seq_len.times{ blocks << '0000' }
    else
      blocks << '0' * (4 - b.size) + b
    end
  end

  puts blocks.join(':')
end

n = gets.to_i
n.times do
  c = false
  ip = gets.strip.split(":")
  if ip[0] == "" then ip = ip.drop(1) end
  l = ip.count
  for i in 0...l
    if ip[i] == ""
      if i > 0 then print ":" end
      print "0000"
      (8 - l).times do
        print ":0000"
      end
      c = true
    else
      if i > 0 then print ":" end
      (4 - ip[i].length).times do print "0" end
      print ip[i]
    end
  end
  if l != 8 && !c
    if l > 0 then print ":" end
    print "0000"
    (7 - l).times do
      print ":0000"
    end
  end
  puts ""
end

gets.to_i.times {
  short = gets.strip.gsub('::', ':zero:').split(':')
  seq_len = 8 - short.size + 1
  blocks = []
  short.each { |b|
    if b == 'zero'
      seq_len.times{ blocks << '0000' }
    else
      blocks << '0' * (4 - b.size) + b
    end
  }

  puts blocks.join(':')
}

#require 'colorize'

def recoveryPart(part)
  zones = part.split(':')
  fullPart = String.new

  zones.each { |zone|
    fullPart += ('0' * (4 - zone.length)) + zone + ':'

  }

  fullPart[0..-2]
end

def recoveryIpv6(shortIpv6)

  part = shortIpv6.split('::')

  case part.length
    when 0
      result = ('0000:'*8)[0..-2]
    when 1
      if (shortIpv6[shortIpv6.length-2..shortIpv6.length] == '::')
        result = recoveryPart(part[0]) + ':' + ('0000:' * (8 - part[0].split(':').length))
      else
        result = recoveryPart(shortIpv6)
      end
    when 2
      result = recoveryPart(part[0]) + ':' + ('0000:' * (8 - (part[0].split(':').length + part[1].split(':').length)))  + recoveryPart(part[1])
  end

  if (result[0..0] == ':')
    result = result[1..result.length]
  end

  if (result[result.length-1..result.length] == ':')
    result = result[0..result.length-2]
  end
  result

end

count = gets.chomp.to_i;

count.times do
  puts recoveryIpv6(gets.chomp)
end

#
# TEST
#

#if recoveryIpv6('a56f:d3:0:0124:01:f19a:1000:00') == 'a56f:00d3:0000:0124:0001:f19a:1000:0000'
#  puts 'OK'.green
#else
#  puts 'FAIL'.red
#end
#
#if recoveryIpv6('a56f:00d3:0000:0124:0001::') == 'a56f:00d3:0000:0124:0001:0000:0000:0000'
#  puts 'OK'.green
#else
#  puts 'FAIL'.red
#end
#
#if recoveryIpv6('::a56f:00d3:0000:0124:0001') == '0000:0000:0000:a56f:00d3:0000:0124:0001'
#  puts 'OK'.green
#else
#  puts 'FAIL'.red
#end
#
#if recoveryIpv6('a56f::0124:0001:0000:1234:0ff0') == 'a56f:0000:0000:0124:0001:0000:1234:0ff0'
#  puts 'OK'.green
#else
#  puts 'FAIL'.red
#end
#
#if recoveryIpv6('a56f:0000::0000:0001:0000:1234:0ff0') == 'a56f:0000:0000:0000:0001:0000:1234:0ff0'
#  puts 'OK'.green
#else
#  puts 'FAIL'.red
#end
#
#if recoveryIpv6('::') == '0000:0000:0000:0000:0000:0000:0000:0000'
#  puts 'OK'.green
#else
#  puts 'FAIL'.red
#end
#
#if recoveryIpv6('0ea::4d:f4:6:0') == '00ea:0000:0000:0000:004d:00f4:0006:0000'
#  puts 'OK'.green
#else
#  puts 'FAIL'.red
#end

n=Integer(gets.chomp)

n.times do
s=gets.chomp
a=s.split '::'
if a[0]!=nil
b=a[0].split ':'
else
b=[]
end
if a[1]!=nil
c=a[1].split ':'
else
c=[]
end

a=b+Array.new(8-(b.size+c.size),"0000")+c
a.map!{|i| "0"*(4-i.size)+i}

(a.size-1).times do |i|
print a[i]+':'
end
print a[-1]+"\n"

end

n = gets.to_i
n.times do
  c = false
  ip = gets.strip.split(":")
  l = ip.count
  for i in 0...l
    if ip[i] == ""
      if i > 0 then print ":" end
      print "0000"
      (8 - l).times do
        print ":0000"
      end
      c = true
    else
      if i > 0 then print ":" end
      (4 - ip[i].length).times do print "0" end
      print ip[i]
    end
  end
  if l != 8 && !c
    if l > 0 then print ":" end
    print "0000"
    (7 - l).times do
      print ":0000"
    end
  end
  puts ""
end

n = gets.to_i
n.times do
  c = false
  ip = gets.strip.split(":")
  l = ip.count
  for i in 0...l
    if ip[i] == ""
      if i > 0 then print ":" end
      print "0000"
      (8 - l).times do
        print ":0000"
      end
      c = true
    else
      if i > 0 then print ":" end
      (4 - ip[i].length).times do print "0" end
      print ip[i]
    end
  end
  if !c
    if l > 0 then print ":" end
    print "0000"
    (7 - l).times do
      print ":0000"
    end
  end
  puts ""
end

#!ruby -plr ipaddr
#$_=IPAddr.new($_).to_string
$_=2


Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told "no genome, no degree". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters.Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.

The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length.

Print "YES", if the dwarves belong to the same race. Otherwise, print "NO".

Note  First example: you can simply swap two letters in string "ab". So we get "ba".  Second example: we can't change string "aa" into string "ab", because "aa" does not contain letter "b".

s1 = gets.chomp.split("")
s2 = gets.chomp.split("")

if s1.sort == s2.sort then
    i = 0
    d = 0
    while i < s1.size do
        if s1[i] != s2[i] then
            d += 1
        end
        i += 1
    end
    if d <= 2 then
        puts "YES"
    else
        puts "NO"
    end
else
    puts "NO"
end

s1 = gets.chomp
s2 = gets.chomp
ret = "NO"
r1 = nil
r2 = nil
if s1.length == s2.length
    for i in 0..s1.length-1
        next if s1[i] == s2[i]
        if r1 == nil
            r1 = s1[i]
            r2 = s2[i]
        else
            if ret == "NO"
                if r1 == s2[i] && r2 == s1[i]
                    ret = "YES"
                else
                    break
                end
            else
                ret = "NO"
                break
            end
        end
    end
end
puts ret

require 'set'

def compare_str(a, b)
  return "NO" if a.length != b.length
  a_set = []
  b_set = []
  (0..a.length).each do |i|
    if a[i] != b[i]
      a_set << a[i]
      b_set << b[i]
    end
  end
  return "NO" if a_set.length > 2
  return "YES" if a_set.sort == b_set.sort
  return "NO"
end

a = gets.chomp
b = gets.chomp

puts compare_str(a, b)

def diff(a, b)
  c = 0
  for i in 0..(a.length - 1)
    if a[i] != b[i]
      c += 1
    end
  end
  c
end

a = gets.chomp.split ""
b = gets.chomp.split ""

puts ((a.sort == b.sort) and diff(a, b) == 2) ? "YES" : "NO"

s = STDIN.readline.strip
t = STDIN.readline.strip
if s.length != t.length
	puts 'NO'
	exit
end
differences = []
s.length.times do |i|
	differences.push(i) if s[i] != t[i]
end
if differences.length != 2
	puts 'NO'
	exit
end
a, b = differences
s[a], s[b] = s[b], s[a]
if s == t
	puts 'YES'
else
	puts 'NO'
end

a=gets.chomp.strip
b=gets.chomp.strip
x=[]
if a.length!=b.length
	puts "NO"
	exit
end
for i in 0...a.length
	if a[i]!=b[i]
		x.push(i)
	end
end
if x.length!=2
	puts "NO"
	exit
end
puts (a[x[0]]==b[x[1]] and b[x[0]]==a[x[1]])? "YES" : "NO"

a=gets.chomp.split("")
b=gets.chomp.split("")

if a.length !=b.length
	puts "NO"
elsif a.sort != b.sort
	puts "NO"
elsif a==b
	puts "YES"
elsif a.reverse==b && a.length==2
	puts "YES"
else

k=[]

	0.upto(a.length-1) do|i|
		k[i]=[a[i],b[i]]
	end

k.each{|i| i.uniq!}
k.sort! {|x,y| y.length <=> x.length}
d=0

	0.upto(2) do |i|
		if k[i].length==2
			d+=1
		end
	end
	if d<=1
		puts "YES"
	elsif d>2
		puts "NO"
	else
		if k[0].reverse==k[1]
			puts "YES"
		else
			puts "NO"
		end
	end

end

a = gets.to_s
b = gets.to_s

if a.size != b.size then
	puts "NO"
	exit
end

count = 0
x = 0
y = 0

0.upto (a.size-1) do |i|
	if a[i] != b[i] then
		count += 1
		if count == 1 then
			x = a[i]
			y = b[i]
		elsif count == 2 then
			if (x != b[i]) or (y != a[i]) then
				puts "NO"
				exit
			end
		else
			puts "NO"
			exit
		end
	end
end

if count == 0 then
	d = {}
	0.upto (a.size -1) do |i|
		if d[i] == 1 then
			puts "YES"
			exit
		else
			d[i] = 1
		end
	end
	puts "NO"
	exit
elsif count == 1 then
	puts "NO"
	exit
else
	puts "YES"
	exit
end

first = gets.chomp
second = gets.chomp
ans = "NO"
if first.length == second.length
    diff = []
    act = []
    (0...first.length).each do |i|
        a = first[i]
        b = second[i]
        if a != b
            diff << a
            act << b
        end
    end
    if diff.length == 2
        ans = "YES" if act[0] == diff[1] && act[1] == diff[0]
    end
end
puts ans

a = gets.chomp
b = gets.chomp

def work(a, b)
  return false if a.size != b.size
  d = []
  (0...a.size).each {|x| d << x if a[x] != b[x]}
 # p d.size
  return false if d.size != 0 and d.size != 2

  if d.size == 2
    return true if a[d[0]] == b[d[1]] and a[d[1]] == b[d[0]]
    return false
  end

  hs = Hash.new(0)
  a.each_byte{|x| hs[x] = hs[x] + 1}
  return true if hs.values.any?{|x| x > 1}

  return false
end

if work(a,b)
  puts 'YES'
else
  puts 'NO'
end

a=gets
b=gets
if a.size != b.size
  puts "NO"
else
  s0=[]
  s1=[]
  (a.size-1).times do |i|
    if a[i] != b[i]
      s0 << a[i]
      s1 << b[i]
    end
  end
  if s0.size!=2 || s0[1] != s1[0] || s0[0] != s1[1]
    puts "NO"
  else
    puts "YES"
  end
end

first_count = Hash.new(0)
second_count = Hash.new(0)

a = gets.chomp
b = gets.chomp
ans = false
mismatch_ind = []
if a.length == b.length
	(0..a.length - 1).each do |i|

		a_char = a[i]
		b_char = b[i]

		first_count[a_char] += 1
		second_count[b_char] += 1
		mismatch_ind << i if a_char != b_char
	end

	ans = true if first_count == second_count && ( mismatch_ind.length == 0 || (mismatch_ind.length == 2 && a[mismatch_ind[0]] == b[mismatch_ind[1]] && a[mismatch_ind[1]] == b[mismatch_ind[0]]))

end
puts ans ? "YES" : "NO"

l = []
a = gets
b = gets
if a.size == b.size
	a.split(//).each_with_index do |x, i|
		l << a[i].ord - b[i].ord if a[i].ord - b[i].ord != 0
		if l.size > 2
			break
		end
	end
else
	l << 0
end

if l.size == 0 or (l.size == 2 and l[0] == -l[1])
	puts "YES"
else
	puts "NO"
end

as, bs = gets.chomp.chars.to_a, gets.chomp.chars.to_a
first = true
first_a, first_b = nil, nil
output = false
answer = "NO"
if as.length == bs.length
  as.each_with_index do |a, i|
    b = bs[i]
    if a != b
      if output
        answer = "NO"
        break
      end
      if first
        first_a = a
        first_b = b
        first = false
      else
        answer = "YES" if a == first_b && b == first_a
        output = true
      end
    end
  end

  puts answer
else
  puts "NO"
end

as, bs = gets.chomp.chars.to_a, gets.chomp.chars.to_a

if as.length != bs.length
  puts "NO"
else
  diff = []

  as.zip(bs).each_with_index do |(a, b), i|
    diff << i if a != b
  end

  puts diff.length == 2 && as[diff[0]] == bs[diff[1]] && as[diff[1]] == bs[diff[0]] ? "YES" : "NO"
end

def main
  a = STDIN.gets
  b = STDIN.gets

  return 'NO' if a.length != b.length

  diffs = []
  (0 ... a.length).each do |i|
    diffs.push(i) if a[i] != b[i]
  end

  return 'NO' if diffs.length != 2

  return a[diffs[0]] == b[diffs[1]] && b[diffs[0]] == a[diffs[1]] ? 'YES' : 'NO'
end

puts main

def say_no
  puts 'NO'
  exit
end

g1, g2 = [gets.chomp, gets.chomp]
n = g1.size
say_no if n != g2.size
d = []
for i in 0...n
  say_no if g1[i] != g2[i] && ((t = (d << i).size) > 2 || (t == 2 && (g1[d[0]] != g2[d[1]] || g1[d[1]] != g2[d[0]])))
end
puts [0, 2].any? { |p| p == d.size } ? 'YES' : 'NO'

a = gets
b = gets
ans = false
if a.length == b.length
	s = []
	a.length.times do |i|
		if a[i] != b[i]
			s << i
		end
	end
	ans = s.empty? || ((s.length == 2 and a[s[0]] == b[s[1]] and a[s[1]] == b[s[0]]))
end
puts ans ? "YES" : "NO"

a, b, c, diff  = gets.strip, gets.strip, 0, []

if a.length == b.length
	a.length.times do |i|
		unless a[i] == b[i]
			c += 1
			c < 3 ? diff.push([a[i], b[i]]) : break
		end
	end
	print c == 2 && diff[0] == diff[1].reverse ? :YES : :NO
else
	print :NO
	exit
end

a = gets.chomp
b = gets.chomp

def work(a, b)
  #puts a.size,b.size
  return false if a.size != b.size
  d = []
  (0...a.size).each {|x| d << x if a[x] != b[x]}
 # p d.size
  return false if d.size != 0 and d.size != 2

  if d.size == 2
    return true if a[d[0]] == b[d[1]] and a[d[1]] == b[d[0]]
    return false
  end

  hs = Hash.new(0)
  a.each_byte{|x| hs[x] = hs[x] + 1}
  return true if hs.values.any?{|x| x > 1}

  return false
end

if work(a,b)
  puts 'YES'
else
  puts 'NO'
end

s1 = gets.chomp
s2 = gets.chomp

i = 0
while i < s2.size do
    if s1[i] == s2[i] then
        s1 = s1[0...i] + s1[i+1..-1]
        s2 = s2[0...i] + s2[i+1..-1]
    end
    i += 1
end

s1 = s1.delete(s2)

if s1 == "" then
    puts "YES"
else
    puts "NO"
end

s1 = gets.chomp.split("")
s2 = gets.chomp.split("")

s1 = s1.sort
s2 = s2.sort

if s1 == s2 then
    i = 0
    d = 0
    while i < s1.size do
        if s1[i] != s2[i] then
            d += 1
        end
        i += 1
    end
    if d <= 2 then
        puts "YES"
    else
        puts "NO"
    end
else
    puts "NO"
end

s1 = gets.chomp
s2 = gets.chomp

if s1.size == s2.size then
    i = 0
    while i < s2.size do
        if s1[i] == s2[i] then
            s1 = s1.sub(s1[i], "")
            s2 = s2.sub(s2[i], "")
        end
        i += 1
    end
    if s1.size == 2 then
        s1 = s1.delete(s2)
        if s1 == "" then
            puts "YES"
        else
            puts "NO"
        end
    else
        puts "NO"
    end
else
    puts "NO"
end

s1 = gets.chomp
s2 = gets.chomp

if s1.size == s2.size then
    i = 0
    str = ""
    while i < s2.size do
        if s1[i] == s2[i] then
            str += s1[i]
        end
        i += 1
    end
    s1 = s1.sub(str, "")
    s2 = s2.sub(str, "")
    if s1.size == 2 then
        s1 = s1.delete(s2)
        if s1 == "" then
            puts "YES"
        else
            puts "NO"
        end
    else
        puts "NO"
    end
else
    puts "NO"
end

def diff(a, b)
  c = 0
  for i in 0..(a.length - 1)
    if a[i] == b[i]
      c += 1
    end
  end
  c
end

a = gets.chomp.split ""
b = gets.chomp.split ""

puts ((a.sort == b.sort) and diff(a, b) == 2) ? "YES" : "NO"

def diff(a, b)
  c = 0
  for i in 0..(a.length - 1)
    if a[i] == b[i]
      c += 1
    end
  end
  c
end

a = gets.chomp.split ""
b = gets.chomp.split ""

a.sort!
b.sort!

puts ((a == b) and diff(a, b) == 2) ? "YES" : "NO"

a = gets.chomp.split ""
b = gets.chomp.split ""

a.sort!
b.sort!

puts (a == b) ? "YES" : "NO"

a=gets.chomp.split("")
b=gets.chomp.split("")

if a.length !=b.length
	puts "NO"
elsif a.sort != b.sort
	puts "NO"
elsif a==b
	puts "YES"
elsif a.reverse==b
	puts "YES"
else

k=[]

	0.upto(a.length-1) do|i|
		k[i]=[a[i],b[i]]
	end

k.each{|i| i.uniq!}
k.sort! {|x,y| y.length <=> x.length}
d=0

	0.upto(2) do |i|
		if k[i].length==2
			d+=1
		end
	end

	if d<=1
		puts "YES"
	elsif d>2
		puts "NO"
	else
		if k[0].reverse==k[1]
			puts "YES"
		else
			puts "NO"
		end
	end

end

a=gets
b=gets
if a.size != b.size
  puts "NO"
else
  s0=[]
  s1=[]
  (a.size-1).times do |i|
    puts i
    if a[i] != b[i]
      s0 << a[i]
      s1 << b[i]
    end
  end
  if s0.size!=2 || s0[1] != s1[0] || s0[0] != s1[1]
    puts "NO"
  else
    puts "YES"
  end
end

first_count = Hash.new(0)
second_count = Hash.new(0)

a = gets.chomp
b = gets.chomp

a.each_char do |i|
	first_count[i] += 1
end

b.each_char do |i|
	second_count[i] += 1
end

puts first_count == second_count ? "YES" : "NO"

as, bs = gets.chomp.chars.to_a, gets.chomp.chars.to_a
first = true
first_a, first_b = nil, nil
output = false
as.each_with_index do |a, i|
  b = bs[i]
  if a != b
    if first
      first_a = a
      first_b = b
      first = false
    else
      puts a == first_b && b == first_a ? "YES" : "NO"
      output = true
      break
    end
  end
end

puts "NO" unless output

as, bs = gets.chomp.chars.to_a, gets.chomp.chars.to_a
first = true
first_a, first_b = nil, nil
output = false
answer = "NO"
as.each_with_index do |a, i|
  b = bs[i]
  if a != b
    if output
      answer = "NO"
      break
    end
    if first
      first_a = a
      first_b = b
      first = false
    else
      answer = "YES" if a == first_b && b == first_a
      output = true
    end
  end
end

puts answer

def main
  a = STDIN.gets
  b = STDIN.gets

  return 'NO' if a.length != b.length

  i = 0
  i += 1 while a[i] == b[i]

  return 'YES' if i == a.length

  j = a.length - 1
  j -= 1 while a[j] == b[j]

  return 'YES' if j == i + 1 && a[i] == b[j] && a[j] == b[i]

  return 'NO'
end

print main

def say_no
  puts 'NO'
  exit
end

g1, g2 = [gets.chomp, gets.chomp]
n = g1.size
say_no if n != g2.size
d = []
for i in 0...n
  say_no if g1[i] != g2[i] && ((t = (d << i).size) > 2 || (t == 2 && (g1[d[0]] != g2[d[1]] || g1[d[1]] != g2[d[0]])))
end
puts 'YES'

a , b  = gets.strip, gets.strip
ac, bc = Hash.new(0), Hash.new(0)

a.each_char { |c| ac[c.to_sym] += 1 }
b.each_char { |c| bc[c.to_sym] += 1 }

print ac == bc ? :YES : :NO

a, b, c  = gets.strip, gets.strip, 0
(print :NO; exit) unless a.length == b.length
a.length.times { |i| c += 1 unless a[i] == b[i] }
print c == 2 ? :YES : :NO

a, b = STDIN.read.split("\n")
a1, a2 = a.split('').uniq
b1, b2 = b.split('').uniq

ac1 = a.count(a1)
bc1 = b.count(b1)

ac2 = a2 ? a.count(a2) : 0
bc2 = b2 ? b.count(b2) : 0

print ac1 == bc1 && ac2 == bc2 ? :YES : :NO


Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.

The first input line contains a non-empty string s1. The second input line contains a non-empty string s2. Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.

Print the number of common divisors of strings s1 and s2.

NoteIn first sample the common divisors are strings "abcd" and "abcdabcd".In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa".

def make_fail(str)
    kmp = Array.new(str.length)
    i, j = [1, 0]
    kmp[0] = 0
    while (i < str.length) do
        kmp[i] = j
        while (j > 0 and str[i] != str[j]) do
            j = kmp[j]
        end
        i, j = i + 1, j + 1
    end
    return kmp
end

def get_cycle(str, kmp)
    last = kmp.length - 1
    if kmp[last] == 0 or str[last] != str[kmp[last]]
        return str.length - 1
    else
        if last % (last - kmp[last]) == 0
            return last - kmp[last]
        end
        return last
    end
end

def gcd(a, b)
    if b == 0
        return a
    end
    return gcd(b, a % b)
end

def num_of_divisors(num)
    if num < 2
        return 1
    end
    i, cnt = 1, 0
    while i <= num do
        cnt = cnt + 1 if num % i == 0
        i = i + 1
    end
    return cnt
end

s1 = " " + gets.split.first
s2 = " " + gets.split.first

c1 = get_cycle(s1, make_fail(s1))
c2 = get_cycle(s2, make_fail(s2))
n1 = (s1.length - 1) / c1
n2 = (s2.length - 1) / c2

# puts n1.to_s + " " + n2.to_s

if c1 != c2 or s1[1, c1] != s2[1, c2]
    # puts c1.to_s + " " + c2.to_s
    puts 0
else
    g = gcd(n1, n2)
    puts num_of_divisors(g)
end

# ProblemD

def divisors(n)
  ret = []

  i = 1
  while i * i <= n
    if n % i == 0
      ret << i
      if (ret.index(n / i) == nil)
        ret << (n / i)
      end
    end
    i += 1
  end

  return ret
end

s1 = gets.chomp
s2 = gets.chomp

base = ""
target = ""
if s1.size <= s2.size
  base = s1
  target = s2
else
  base = s2
  target = s1
end

base_size = base.size
target_size = target.size

res = 0

base_divisors = divisors(base_size)
base_divisors.each do |base_divisor|
  tmp_str = base[0..base_divisor-1]
  if (tmp_str * (base_size / base_divisor)) == base
    if target_size % base_divisor == 0
      if (tmp_str * (target_size / base_divisor)) == target
        res += 1
      end
    end
  end
end

puts res

def make_fail(str)
    kmp = Array.new(str.length)
    i, j = [1, 0]
    kmp[0] = 0
    while (i < str.length) do
        kmp[i] = j
        while (j > 0 and str[i] != str[j]) do
            j = kmp[j]
        end
        i, j = i + 1, j + 1
    end
    return kmp
end

def get_cycle(str, kmp)
    last = kmp.length - 1
    if kmp[last] == 0 or str[last] != str[kmp[last]]
        return str.length - 1
    else
        return last - kmp[last]
    end
end

def gcd(a, b)
    if b == 0
        return a
    end
    return gcd(b, a % b)
end

def num_of_divisors(num)
    if num < 2
        return 1
    end
    i, cnt = 1, 0
    while i <= num do
        cnt = cnt + 1 if num % i == 0
        i = i + 1
    end
    return cnt
end

s1 = " " + gets.split.first
s2 = " " + gets.split.first

c1 = get_cycle(s1, make_fail(s1))
c2 = get_cycle(s2, make_fail(s2))
n1 = (s1.length - 1) / c1
n2 = (s2.length - 1) / c2

# puts n1.to_s + " " + n2.to_s

if c1 != c2 or s1[1, c1] != s2[1, c2]
    puts 0
else
    g = gcd(n1, n2)
    puts num_of_divisors(g) # if s1.length < 100
    # puts s1.length.to_s + " " + s2.length.to_s if s1.length > 100
end

def make_fail(str)
    kmp = Array.new(str.length)
    i, j = [1, 0]
    kmp[0] = 0
    while (i < str.length) do
        kmp[i] = j
        while (j > 0 and str[i] != str[j]) do
            j = kmp[j]
        end
        i, j = i + 1, j + 1
    end
    return kmp
end

def get_cycle(str, kmp)
    last = kmp.length - 1
    if kmp[last] == 0 or str[last] != str[kmp[last]]
        return str.length - 1
    else
        return last - kmp[last]
    end
end

def gcd(a, b)
    if b == 0
        return a
    end
    return gcd(b, a % b)
end

s1 = " " + gets.split.first
s2 = " " + gets.split.first

n1 = (s1.length - 1) / get_cycle(s1, make_fail(s1))
n2 = (s2.length - 1) / get_cycle(s2, make_fail(s2))

# puts n1.to_s + " " + n2.to_s

if !(s1.include? s2) and !(s2.include? s2)
    puts 0
else
    g = gcd(n1, n2)
    i = 1
    cnt = 0
    while i * i <= g do
        if g % i == 0
            cnt = cnt + 1
        end
        i = i + 1
    end

    cnt = cnt + 1 if g != 1

    puts cnt
end

def make_fail(str)
    kmp = Array.new(str.length)
    i, j = [1, 0]
    kmp[0] = 0
    while (i < str.length) do
        kmp[i] = j
        while (j > 0 and str[i] != str[j]) do
            j = kmp[j]
        end
        i, j = i + 1, j + 1
    end
    return kmp
end

def get_cycle(str, kmp)
    last = kmp.length - 1
    if kmp[last] == 0 or str[last] != str[kmp[last]]
        return str.length - 1
    else
        return last - kmp[last]
    end
end

def gcd(a, b)
    if b == 0
        return a
    end
    return gcd(b, a % b)
end

def num_of_divisors(num)
    if num < 2
        return 1
    end
    i, cnt = 2, 2
    while i * i <= num do
        cnt = cnt + 1 if num % i == 0
        i = i + 1
    end
    return cnt
end

s1 = " " + gets.split.first
s2 = " " + gets.split.first

n1 = (s1.length - 1) / get_cycle(s1, make_fail(s1))
n2 = (s2.length - 1) / get_cycle(s2, make_fail(s2))

# puts n1.to_s + " " + n2.to_s

if !(s1.include? s2) and !(s2.include? s2)
    puts 0
else
    g = gcd(n1, n2)
    puts num_of_divisors(g) if s1.length < 100
    puts s1.length.to_s + " " + s2.length.to_s if s1.length > 100
end

def make_fail(str)
    kmp = Array.new(str.length)
    i, j = [1, 0]
    kmp[0] = 0
    while (i < str.length) do
        kmp[i] = j
        while (j > 0 and str[i] != str[j]) do
            j = kmp[j]
        end
        i, j = i + 1, j + 1
    end
    return kmp
end

def get_cycle(str, kmp)
    last = kmp.length - 1
    if kmp[last] == 0 or str[last] != str[kmp[last]]
        return str.length - 1
    else
        return last - kmp[last]
    end
end

def gcd(a, b)
    if b == 0
        return a
    end
    return gcd(b, a % b)
end

def num_of_divisors(num)
    if num < 2
        return 1
    end
    i, cnt = 1, 0
    while i <= num do
        cnt = cnt + 1 if num % i == 0
        i = i + 1
    end
    return cnt
end

s1 = " " + gets.split.first
s2 = " " + gets.split.first

n1 = (s1.length - 1) / get_cycle(s1, make_fail(s1))
n2 = (s2.length - 1) / get_cycle(s2, make_fail(s2))

# puts n1.to_s + " " + n2.to_s

if !(s1.include? s2) and !(s2.include? s1)
    puts 0
else
    g = gcd(n1, n2)
    puts num_of_divisors(g) # if s1.length < 100
    # puts s1.length.to_s + " " + s2.length.to_s if s1.length > 100
end

def make_fail(str)
    kmp = Array.new(str.length)
    i, j = [1, 0]
    kmp[0] = 0
    while (i < str.length) do
        kmp[i] = j
        while (j > 0 and str[i] != str[j]) do
            j = kmp[j]
        end
        i, j = i + 1, j + 1
    end
    return kmp
end

def get_cycle(str, kmp)
    last = kmp.length - 1
    if kmp[last] == 0 or str[last] != str[kmp[last]]
        return str.length - 1
    else
        return last - kmp[last]
    end
end

def gcd(a, b)
    if b == 0
        return a
    end
    return gcd(b, a % b)
end

def num_of_divisors(num)
    if num < 2
        return 1
    end
    i, cnt = 1, 0
    while i <= num do
        cnt = cnt + 1 if num % i == 0
        i = i + 1
    end
    return cnt
end

s1 = " " + gets.split.first
s2 = " " + gets.split.first

n1 = (s1.length - 1) / get_cycle(s1, make_fail(s1))
n2 = (s2.length - 1) / get_cycle(s2, make_fail(s2))

# puts n1.to_s + " " + n2.to_s

if !(s1.include? s2) and !(s2.include? s2)
    puts 0
else
    g = gcd(n1, n2)
    puts num_of_divisors(g) # if s1.length < 100
    # puts s1.length.to_s + " " + s2.length.to_s if s1.length > 100
end

# ProblemD

s1 = gets.chomp
s2 = gets.chomp

base = ""
target = ""
if s1.size <= s2.size
  base = s1
  target = s2
else
  base = s2
  target = s1
end

base_size = base.size
target_size = target.size

res = 0
base.size.times do |i|
  tmp_str = base[0..i]
  if target.start_with?(tmp_str)
    n = i + 1
    if (base_size % n == 0) && (target_size % n == 0)
      if tmp_str * (base_size / n) == base && tmp_str * (target_size / n) == target
        puts tmp_str
        res += 1
      end
    end
  end
end

puts res


This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.

The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.

Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".

line = gets
line.strip!
longest = []  # )对应的最长的sequence长度
opn = []
i = 0
line.each_char do |ch|
	if ch == '('
		opn << i
	else
		if opn.empty?
			longest[i] = -1
		else
			relative = opn.pop
			longest[i] = i - relative + 1 # 不是最优，需要向前搜索
			if relative > 0 && line[relative - 1] == ")" && longest[relative - 1] != -1
				longest[i] += longest[relative - 1]
			end
		end
	end
	i += 1
end
max = 0
cnt = 1
# 不能使用.max方法，因为数组中有nil，不能将nil与整数相比较
longest.each do |item|
	if item && (item > max)
		max = item
		cnt = 1
	elsif item == max
		cnt += 1
	end
end
puts "#{max} #{cnt}"

str = gets
c = Array.new
stack = Array.new
i = 0
str.strip!
str.each_char do |s|
  if s=="("
    stack << i
  else
    if stack.empty?
      c[i]=-1
    else
      corr=stack.pop
      c[i]=i-corr+1
      if corr>0 and c[corr-1] and c[corr-1]!=-1
        c[i]+=c[corr-1]
      end
    end
  end
  i+=1
end
max=0
cnt=1
c.each do |cc|
  if cc and cc>max
    max=cc
    cnt=1
  elsif cc==max
    cnt+=1
  end
end
puts "#{max} #{cnt}"

s = gets.chomp
h = Hash.new(0)
count = 0
stk = [-1]
(0...s.length).each { |i|
	a = s[i]
	if a == "("
		stk.push(i)
	else
		stk.pop
		if stk.empty?
			stk.push(i)
		else
			h[i - stk.last] += 1
		end
	end
}
v, c = h.sort_by { |k, v| k }.last
c = 1 if !v || v == 0
puts "#{v.to_i} #{c.to_i}"

s = gets.chomp

diff = 0
match = []
stack = []

(0...(s.size)).each do |i|
	if s[i] == '('
		diff += 1
		stack << i
	elsif s[i] == ')' and diff > 0
		diff -= 1
		index = stack.pop
		match[index] = i
	end
end

time = 0
max = 0
record = Array.new(s.size)
(0...s.size).each do |i|
	total = 0
	if (not match[i].nil?) and record[i].nil?
		record[i] = true
		total += (match[i] - i + 1)
		j = match[i] + 1
		while match[j] != nil
			total += (match[j] - j + 1)
			record[j] = true
			j = match[j] + 1
		end

		if max < total
			max = total
			time = 1
		elsif max == total
			time += 1
		end
	end
end

time = 1 if max == 0

puts "#{max} #{time}"

s = gets.chomp
h = Hash.new(0)
count = 0
stk = []
(0...s.length).each { |i|
	a = s[i]
	if a == "("
		stk.push(a)
	else
		if stk.last == "("
			stk.pop
			count += 2
		else
			count = 0
		end
		h[count] += 1
	end
}
v, c = h.sort_by { |k, v| k }.last
c = 1 if !v
puts "#{v.to_i} #{c.to_i}"

s = gets.chomp
h = Hash.new(0)
count = 0
stk = []
(0...s.length).each { |i|
	a = s[i]
	if a == "("
		stk.push(a)
	else
		if stk.last == "("
			stk.pop
			count += 2
		else
			count = 0
		end
		h[count] += 1
	end
}
v, c = h.sort_by { |k, v| k }.last
c = 1 if !v || v == 0
puts "#{v.to_i} #{c.to_i}"

s = gets.chomp
h = Hash.new(0)
count = 0
stk = []
(0...s.length).each { |i|
	a = s[i]
	if a == "("
		stk.push(a)
	else
		if stk.last == "("
			stk.pop
			count += 2
			h[count] += 1
		else
			count = 0
		end
	end
}
v, c = h.sort_by { |k, v| v }.last
c = 1 if !v
puts "#{v.to_i} #{c.to_i}"

s = gets.chomp

diff = 0
match = []
stack = []

(0...(s.size)).each do |i|
	if s[i] == '('
		diff += 1
		stack << i
	elsif s[i] == ')' and diff > 0
		diff -= 1
		index = stack.pop
		match[index] = i
		match[i] = index
	end
end

max = -1
time = 0
(0...s.size).each do |i|
	next if match[i].nil?

	if max < (i - match[i]).abs
		max = (i - match[i]).abs
		time += 1
	elsif max == (i - match[i]).abs
		time += 1
	end

end

time = 2 if max == -1

puts "#{max + 1} #{time / 2}"


Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000, n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point. It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi < xi + 1, 1 ≤ i < n).

Output a single integer — the minimum number of plants to be replanted.

NoteIn the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.In the second test case, the species are already in the correct order, so no replanting is needed.


A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.You are given an integer $$$n$$$. Your goal is to construct and print exactly $$$n$$$ different regular bracket sequences of length $$$2n$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 50$$$) — the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n \le 50$$$).

For each test case, print $$$n$$$ lines, each containing a regular bracket sequence of length exactly $$$2n$$$. All bracket sequences you output for a testcase should be different (though they may repeat in different test cases). If there are multiple answers, print any of them. It can be shown that it's always possible.

gets.to_i.times do

n = gets.to_i

str = "()" * n
puts str

(1...n).each do |i|
  s = str.dup
  s[2*i-1,2] = "()"
  puts s
end

end