11.11 is a remarkable Festival. Let's take part in a contest. Please enjoy it.
UNICORN Programming Contest 2022(AtCoder Beginner Contest 269) (2022.09.17)
A - Anyway Takahashi
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 100 points
Problem Statement
You are given integers a, b, c, and d. Print two lines as follows.
The first line should contain the result of calculating (a+b)×(c−d) as an integer.
The second line should contain Takahashi, regardless of the input.
Constraints
−100≤a,b,c,d≤100
a, b, c, and d are integers.
Input
The input is given from Standard Input in the following format:
a b c d
Output
Print two lines according to the Problem Statement.
My solution
a,b,c,d = gets.split(" ").map(&:to_i)
puts (a+b)*(c-d)
puts "Takahashi"
B - Rectangle Detection
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 200 points
Problem Statement
Takahashi generated 10 strings S1 ,S2 ,…,S10 as follows.
First, let Si (1≤i≤10)= .......... (10 .s in a row).
Next, choose four integers A, B, C, and D satisfying all of the following.
1≤A≤B≤10.
1≤C≤D≤10.
Then, for every pair of integers (i,j) satisfying all of the following, replace the j-th character of Si with #.
A≤i≤B.
C≤j≤D.
You are given S1 ,S2 ,…,S10 generated as above. Find the integers
A, B, C, and D Takahashi chose.It can be proved that such integers A, B, C, and D uniquely exist (there is just one answer) under the Constraints.
Constraints
S1 ,S2,…,S10 are strings, each of length 10, that can be generated according to the Problem Statement.
Input
The input is given from Standard Input in the following format:
S1
S2
⋮
S10
Output
Print the answer in the following format:
A B
C D
My solution
num = []
10.times do
num.push(gets.chomp)
end
a,b,c,d = 10,10,10,10
for i in 0...10
for j in 0...10
if num[i][j] == "#" && a == 10
a = i+1
end
if num[i][j] == "#" && c == 10
c = j+1
end
if num[i][j] == "." && j - 1 >= 0 && num[i][j-1] == "#" && d == 10
d = j
end
if num[i][j] == "." && i - 1 >= 0 && num[i-1][j] == "#" && b == 10
b = i
end
end
end
puts [a,b].join(" ")
puts [c,d].join(" ")
C - Submask
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 300 points
Problem Statement
You are given a non-negative integer N. Print all non-negative integers x that satisfy the following condition in ascending order.
The set of the digit positions containing 1 in the binary representation of x is a subset of the set of the digit positions containing
1 in the binary representation of N.
That is, the following holds for every non-negative integer k: if the digit in the 2**k s place of x is 1, the digit in the 2**k s place of N is 1.
Constraints
N is an integer.
0≤N<2**60
In the binary representation of N, at most 15 digit positions contain 1.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer as decimal integers in ascending order, each in its own line.
My solution
n = gets.to_i
n2 = n.to_s(2)
len2 = n2.length
h = {}
n2.chars.each_with_index do |x,i|
if x == "1"
h[len2-i-1] = 1
end
end
ans = [0]
t = h.length
z = h.keys
for i in 1..t
y = z.combination(i).to_a.map {|x| x.map {|j| 2**j}.sum}
ans.concat(y)
end
ans.sort!
ans.each do |a|
puts a
end