** A, B, C problems ** of ** AtCoder Beginner Contest 180 ** will be explained as carefully as possible with ** Python3 **.
I am aiming to explain a solution that satisfies the following three points, not just a method that can be solved.
--Simple: You don't have to think about anything extra --Easy to implement: I'm glad that mistakes and bugs are reduced ββIt doesn't take long: The performance goes up and you have more time left for later problems.
If you have any questions or suggestions, please leave a comment or Twitter. Twitter: u2dayo
[ABC180 Summary](# abc180-Summary) [A problem "box"](#a problem box) [B problem "Various distances"](#b problem variable-distances) [C problem "Cream puff"](#c problem cream-puff)
Total number of submissions: 5748
Performance | AC | Score | time | Ranking(Within Rated) |
---|---|---|---|---|
200 | A-C--- | 400 | Half an hour | 4393(4250)Rank |
400 | ABC--- | 600 | 51 minutes | 3638(3499)Rank |
600 | ABC--- | 600 | 23 minutes | 3008(2869)Rank |
800 | ABCD-- | 1000 | 115 minutes | 2357(2219)Rank |
1000 | ABCD-- | 1000 | 62 minutes | 1749(1614)Rank |
1200 | ABCD-- | 1000 | 23 minutes | 1241(1110)Rank |
1400 | ABCDE- | 1500 | 85 minutes | 850(724)Rank |
1600 | ABCDE- | 1500 | 60 minutes | 565(441)Rank |
1800 | ABCDE- | 1500 | 44 minutes | 364(251)Rank |
2000 | ABCDE- | 1500 | 33 minutes | 227(131)Rank |
2200 | ABCDE- | 1500 | 26 minutes | 136(63)Rank |
2400 | ABCDE- | 1500 | 17 minutes | 83(29)Rank |
color | Number of people | A | B | C | D | E | F |
---|---|---|---|---|---|---|---|
Ash | 2412 | 99.0 % | 68.4 % | 64.4 % | 12.4 % | 1.2 % | 0.0 % |
tea | 1093 | 99.5 % | 92.5 % | 93.7 % | 45.6 % | 7.0 % | 0.2 % |
Green | 889 | 99.9 % | 97.6 % | 99.0 % | 79.0 % | 31.6 % | 0.1 % |
water | 544 | 100.0 % | 99.8 % | 99.8 % | 95.8 % | 73.2 % | 0.7 % |
Blue | 268 | 100.0 % | 99.6 % | 99.6 % | 97.8 % | 94.0 % | 6.3 % |
yellow | 109 | 98.2 % | 97.2 % | 99.1 % | 98.2 % | 91.7 % | 26.6 % |
orange | 26 | 100.0 % | 100.0 % | 100.0 % | 96.2 % | 96.2 % | 69.2 % |
Red | 8 | 62.5 % | 62.5 % | 62.5 % | 62.5 % | 75.0 % | 100.0 % |
Since I was studying applied information, it is a virtual participation.
** Problem page **: A --box
You don't have to think about the case where trying to retrieve $ A $ is not enough. This is because it is not written in the problem statement and the constraint is $ A \ le {100} \ le {N} $.
Therefore, $ N-A + B $ is the answer.
N, A, B = map(int, input().split())
print(N - A + B)
** Problem page **: B --Various distances
The formula for finding the distance is written in the problem statement. Even if you don't understand the meaning of the distance, you can calculate it exactly as it is written. (By the way, it is impossible to think concretely about the distance beyond the $ 4 $ dimension.)
The meaning of each formula is as follows.
γ
The square root of the sum of "$ x_i $ squared $ {x_i} ^ {2} $". If you square a negative value, it will be positive, so you can remove the absolute value.
It may seem difficult at first glance, but it will be $ \ sqrt {x ^ 2 + y ^ 2} $ in the $ 2 $ dimensional plane and $ \ sqrt {x ^ 2 + y ^ 2 + z ^ 2} $ in the $ 3 $ dimensional space. .. This is a normal distance to learn in middle school or high school math.
γ
Create variables first
, second_temp
, third
with an initial value of $ 0 $. It corresponds to "Manhattan distance", "Euclidean distance square root contents", and "Chebyshev distance" in the order of output.
Look at the elements of the list of point coordinates X
in a for loop, $ 1 $ each, and calculate these $ 3 $.
This is the process performed in the for loop.
The $ 1 $ th "Manhattan distance" is first + = abs (x)
because you can add the absolute values.
The $ 2 $ th "contents of the square root of the Euclidean distance" is second_temp + = x ** 2
because you only need to add the square.
The $ 3 $ th "Chebyshev distance" is third = max (third, abs (x))
because the maximum absolute value can be updated.
Take the second
route to find the" Euclidean distance ". That is, second = second_temp ** 0.5
.
Since all were requested, output in the order of first
, second
, third
, and the end.
You don't have to worry about it in Python, but second_temp
can be as big as $ 10 ^ {15} $. In C ++ etc., you need to be careful about overflow.
N = int(input())
X = list(map(int, input().split()))
first, second_temp, third = 0, 0, 0
for x in X:
first += abs(x)
second_temp += x ** 2
third = max(third, abs(x))
second = second_temp ** 0.5
print(first)
print(second)
print(third)
You don't have to do that, but you can also use the higher-order functions (functions that take a function as arguments) map
and reduce
to find each in a $ 1 $ line.
#Maybe there is a better way to write
N = int(input())
X = list(map(int, input().split()))
print(sum(map(abs, X)))
print((sum(map(lambda x: x ** 2, X))) ** 0.5)
print(max(map(abs, X)))
** Problem page **: C --Cream puff
Output the divisors of $ N $ in ascending order.
Cream puffs can cost up to $ 10 ^ {12} $, so trying everything from $ 1 $ to $ 10 ^ {12} $ is not enough.
By the way, ** "the number of people who can divide cream puffs equally without dividing them" ** means ** "the number divisible by $ N $" **.
** A number that is divisible by an integer $ N $ ** is called a ** divisor of $ N $ **.
** You can enumerate divisors with $ O (\ sqrt {N}) $. ** $ \ sqrt {10 ^ {12}} = 10 ^ 6
The divisors have the property of ** "If you know all the divisors below $ \ sqrt {N} $, you can divide $ N $ by them to find all the divisors above $ \ sqrt {N} $" **. there is.
Taking advantage of this property, ** "Try dividing $ N $ by $ \ sqrt {N} $, and if it is divisible, add $ x $ and $ N / x $ to the divisor list" ** , $ O (\ sqrt {N}) $ can be used to enumerate divisors.
That's because $ N \ div {\ frac {N} {x}} = N \ times {\ frac {x} {N}} = x $. For example, $ 36 = 3 \ times {12} $ is a divisor of both $ 3 $ and $ 12 $.
We will call such $ x $ and $ \ frac {N} {x} $ ** "divisor pairs" **. ** A "divisor pair" is always less than or equal to $ \ sqrt {N} $ and the other is greater than or equal to $ \ sqrt {N} $. ** **
If there is a divisor greater than or equal to ** $ \ sqrt {N} $, then the pair is always less than or equal to $ \ sqrt {N} $. Therefore, you can enumerate the divisors by dividing $ N $ by all the divisors below $ \ sqrt {N} $. ** **
Both are smaller than $ \ sqrt {N} $, that is, $ x \ lt {\ sqrt {N}} $ and $ \ frac {N} {x} \ lt {\ sqrt {N}} $.
Multiplying the inequalities
x\times{\frac{N}{x}} < \sqrt{N}\times{\sqrt{N}}\\
N < N
Will be. This is strange because $ N = N $. In other words, the assumption cannot be wrong.
Similarly, there can be no "both greater than $ \ sqrt {N} $" in the opposite direction of the inequality.
From the above, one of the "divisor pairs" is always $ \ sqrt {N} $ or less, and the other is $ \ sqrt {N} $ or more.
Basically, all you have to do is write the code below.
divs = []
x = 1
while x ** 2 <= N:
if N % x == 0:
divs.append(x)
divs.append(N // x)
x += 1
However, there are $ 2 $ problems with this code.
-** When $ \ sqrt {N} $ becomes a divisor, $ \ sqrt {N} $ is added to the list by $ 2 $ **
The $ 1 $ second problem can be solved by dividing the case where $ \ sqrt {N} $ is a divisor, but it is troublesome.
We will use ** set type ** to avoid duplication. The set type handles "unordered sets" and contains only $ 1 $ of the same element. If you add an element that already exists, nothing happens.
The $ 2 $ problem can be solved by sorting. However, the set type does not have the concept of order and cannot be sorted. Convert to a list and then sort.
N = int(input())
divs = set() #Use set to avoid duplication
#Mathematically x<= N ** 0.Same as 5, but safer to compare with an error-free integer
x = 1
while x ** 2 <= N:
if N % x == 0:
divs.add(x) #Add to set is add, not append
divs.add(N // x)
x += 1
ans = list(divs) #I want to sort in ascending order, so make a list and sort
ans.sort()
for x in ans:
print(x)
Recommended Posts