0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtCoder Beginner Contest 181をPythonで解く

0
Posted at

AtCoder Beginner Contest 181の問題をPythonで解いてみました。

A - Heavy Rotation

偶数なら「White」、奇数なら「Black」を出力します。

main.py (A - Heavy Rotation)
if int(input()) % 2 == 0:
    print("White")
else:
    print("Black")

B - Trapezoid Sum

$A_i$から$B_i$までの整数の合計は、以下の公式で求められます。

S_i = \frac{\left( B_i + A_i \right) \left( B_i - A_i + 1 \right)}{2}

これを$N$個に対して合計すればよいです。

main.py (B - Trapezoid Sum)
n = int(input())

ans = 0
for i in range(n):
    a, b = map(int, input().split())
    ans += (b + a) * (b - a + 1) // 2

print(ans)

C - Collinearity

3点$(x_1 , y_1)$、$(x_2 , y_2)$、$(x_3 , y_3)$が1直線上にあるためには、2点$(x_1 , y_1)$、$(x_2 , y_2)$を結ぶ直線の傾きが、2点$(x_1 , y_1)$、$(x_3 , y_3)$を結ぶ直線の傾きに等しければよいです。
すなわち、次の式が成り立てばよいです。

\frac{\left( y_2 - y_1 \right)}{\left( x_2 - x_1 \right)} = \frac{\left( y_3 - y_1 \right)}{\left( x_3 - x_1 \right)}

$x_1 = x_2$、$x_1 = x_3$のときにも利用できるように、次のように変形します。

\left( x_3 - x_1 \right) \left( y_2 - y_1 \right) = \left( x_2 - x_1 \right) \left( y_3 - y_1 \right)

この判定を、$N$個の点の組み合わせの数だけ実行すればよいです。

main.py (C - Collinearity)
n = int(input())
data = [list(map(int, input().split())) for _ in range(n)]

ans = False

for i in range(n - 2):
    for j in range(i + 1, n - 1):
        for k in range(j + 1, n):
            if (data[k][0] - data[i][0]) * (data[j][1] - data[i][1]) == (data[k][1] - data[i][1]) * (data[j][0] - data[i][0]):
                ans = True

if ans:
    print("Yes")
else:
    print("No")

D - Hachi

8の倍数かは「下3桁が8で割り切れるか」で判定できます。しかし、愚直に実行するとTLEです。

main.py (D - Hachi (TLE))
s = input().strip()
s_bunkai = [int(s_char) for s_char in s]
n = len(s_bunkai)

is_divisible = False

if n == 1:
    is_divisible = (s_bunkai[0] % 8 == 0)
elif n == 2:
    first = s_bunkai[0] * 10 + s_bunkai[1]
    second = s_bunkai[1] * 10 + s_bunkai[0]
    is_divisible = (first % 8 == 0) or (second % 8 == 0)
else:
    for i in range(n):
        for j in range(n):
            if i == j:
                continue
            for k in range(n):
                if i == k or j == k:
                    continue
                if (s_bunkai[i] * 100 + s_bunkai[j] * 10 + s_bunkai[k]) % 8 == 0:
                    is_divisible = True

if is_divisible:
    print("Yes")
else:
    print("No")

そこで、リストの要素の数え上げができる**collections.Counter()**を利用します。

リストの中で「aが何個、bが何個、……」というデータを得ることができます。
入力した数字からどれか3つを用いて$112$、$120$、$128$、……のどれかを構成できれば、下3桁が8の倍数になるような数字列$S$の並び替えが存在することが分かります。

main.py (D - Hachi)
from collections import Counter

s = input()

if len(s) <= 2:
    if int(s) % 8 == 0 or int(s[::-1]) % 8 == 0:
        print("Yes")
    else:
        print("No")
    exit()

cnt = Counter(s)
for i in range(112, 1000, 8):
    if not Counter(str(i)) - cnt:
        print("Yes")
        exit()

print("No")

参考にしたサイト等

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?