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?

競技プログラミングの鉄則演習問題集を全部解く(A01~A05/B01~B04)

Last updated at Posted at 2025-05-12

はじめに

はじめまして、なすです。
'25年5月12日現在はAtCoderのレートが550(茶色)です。使用言語はPython(PyPy)です。
アルゴリズムの勉強をしたいなあと思って部室の本を漁っていたところ部室にこの本があり、それを知った流れでAtCoderに問題が置いてあると知りました。なので、少しずつ解いていくついでに記事として感想などを残そうと思いこれを書いています。

このシリーズのスタイル

問題を解いて、ACが出た自分の解答を載せる。思ったことがあれば一言書く。以上。

A01

n = int(input())
print(n*n)

A02

n, x = map(int,input().split())
a = list(map(int,input().split()))
if x in a:
  print("Yes")
else:
  print("No")

A03

n, k = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
ans = "No"
for i in range(n):
  if (k - p[i]) in q:
    ans = "Yes"
    break
print(ans)

in演算子を使うと1重ループだけになるので見た目が簡単になって良いです。

A04

n = int(input())
print(f"{int(bin(n)[2:]):010}")

フォーマットを使ってゼロ埋めをしています。また、bin(n)は先頭2文字が"0b"になるのでスライスで除いています。短く書いた分やや読みづらくなっています。

A05

n, k = map(int,input().split())
ans = 0
for i in range(1,n+1):
  for j in range(1,n+1):
    if 1 <= k - i - j <= n:
      ans += 1
print(ans)

1<=N<=3000なので3重ループにするとTLEになります。kから引いた値を調べることで2重ループに抑えられました。

B01

a,b = map(int,input().split())
print(a+b)

B02

a,b = map(int,input().split())
ans = "No"
for i in range(a,b+1):
  if 100 % i == 0:
    ans = "Yes"
print(ans)

B03

n = int(input())
a = list(map(int,input().split()))
ans = "No"
for i in range(n):
  for j in range(i+1,n):
    if 1000 - a[i] - a[j] in (a[0:i]+a[i+1:j]+a[j+1:]):
      ans = "Yes"
      break
  if ans == "Yes":
    break
print(ans)

3<=N<=100で制約が緩いので3重ループを回しても良いですが、inを使うことで2重ループに抑えられました。調べたところリストに対するinはそれほど速くないらしいので、Nの制約によっては3重ループにした方が良いかもしれません。

B04

b = input()
ans = 0
for i in range(len(b)):
  ans = ans*2 + int(b[i])
print(ans)

最高位の数字が欲しいので文字列として入力を受け取るのが良さそうです。

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?