こんにちは, Shinoryoです.
今回はAtCoder Beginner Contest 347を, Pythonで解いてみた結果を書き連ねていこうと思います.
AtCoder Beginner Contest 347 - AtCoder
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
A - Divisible
空のリストquotients
を用意します. リスト
が で割り切れる場合:quotients
に を追加する. が で割り切れない場合:何もしない.
# 入力 | |
n, k = map(int, input().split()) | |
a = list(map(int, input().split())) | |
# リストaをループして、kで割り切れる要素の商を計算し、quotientsに追加する | |
quotients = [] | |
for a_i in a: | |
if a_i % k == 0: | |
quotients.append(a_i // k) | |
# 出力 | |
print(*quotients) |
B - Substring
# 入力 | |
s = input() | |
# 文字列sの部分文字列を格納するセットを作成 | |
substring_set = set() | |
# 全ての部分文字列を探索し、substring_setに追加する | |
for i in range(len(s)): | |
for j in range(i + 1, len(s) + 1): | |
substring_set.add(s[i:j]) | |
# 出力 | |
print(len(substring_set)) |
C - Ideal Holidays
1週間の何日目に予定があるかが重要なので,
高橋くんの予定が全て休日であるということは, ある予定とその次の予定の間に全ての平日が含まれればよいです.
例えば, 1週間のうち2日が休日で5日が平日の例を考えてみます. 次の表のようになっている場合, 3, 4, 5, 6, 0日目が平日であれば, 高橋くんの予定は全て休日です.
次の表のようになっている場合, 1, 2, 3, 4, 5日目が平日であれば, 高橋くんの予定は全て休日です.
次の表のようになっている場合, 平日をどのように設定しても, 高橋君の予定のどれかが平日になってしまいます.
# 入力 | |
n, a, b = map(int, input().split()) | |
d = list(map(int, input().split())) | |
# 週の長さ | |
week_length = a + b | |
# 予定のある日を1週間の何日目かに変換し、重複を排除する | |
days = [d_i % week_length for d_i in d] | |
distinct_days = list(set(days)) | |
distinct_days.sort() | |
distinct_days_count = len(distinct_days) | |
# 「門番」を加える | |
distinct_days.append(distinct_days[0] + week_length) | |
# 予定のある日の間隔がbを超えるかどうかをチェック | |
for i in range(distinct_days_count): | |
if distinct_days[i + 1] - distinct_days[i] > b: | |
print("Yes") # 予定のある日がb以上離れている場合、Yesを出力して終了 | |
exit() | |
print("No") # 予定のある日がb以上離れていない場合、Noを出力 |
あるいは, 全ての予定が休日の範囲に収まればよいです.
from collections import deque | |
# 入力 | |
n, a, b = map(int, input().split()) | |
d = list(map(int, input().split())) | |
week_length = a + b | |
# 予定のある日を1週間の何日目かに変換し、重複を排除する | |
days = [d_i % week_length for d_i in d] | |
distinct_days = list(set(days)) | |
distinct_days.sort() | |
distinct_days_deque = deque(distinct_days) | |
# 予定のある日の範囲がaを超えないかどうかをチェック | |
for _ in range(n): | |
if distinct_days_deque[-1] - distinct_days_deque[0] < a: | |
print("Yes") # 予定のある日の範囲がa以内の場合、Yesを出力して終了 | |
exit() | |
distinct_days_deque.rotate(-1) | |
distinct_days_deque[-1] += week_length | |
print("No") # 予定のある日の範囲がaを超える場合、Noを出力 |
D - Popcount and XOR
基本的には, 公式解説どおりです.
Editorial - AtCoder Beginner Contest 347
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
が非負である が整数である が の倍数 が非負である が整数である が の倍数 が非負である が整数である が の倍数 が非負である が整数である が の倍数
最終的に,
# 入力 | |
a, b, input_c = map(int, input().split()) | |
c = input_c.bit_count() | |
# a, b, cが条件を満たすかを確認する | |
if (a + b + c) % 2 == 1: | |
print(-1) | |
elif a + b + c > 120: | |
print(-1) | |
elif a > b + c: | |
print(-1) | |
elif b > c + a: | |
print(-1) | |
elif c > a + b: | |
print(-1) | |
# 条件を満たすならば、xとyを計算する | |
else: | |
i = 60 - (a + b + c) // 2 # xのn桁目が0、yのn桁目が0となるようなnの個数 | |
j = (- a + b + c) // 2 # xのn桁目が0、yのn桁目が1となるようなnの個数 | |
k = (a - b + c) // 2 # xのn桁目が1、yのn桁目が0となるようなnの個数 | |
l = (a + b - c) // 2 # xのn桁目が1、yのn桁目が1となるようなnの個数 | |
x = 0 | |
y = 0 | |
# 上の桁から計算する | |
for now_digit in range(59, -1, -1): | |
# input_cのnow_digit桁目が1の場合、xのn桁目かyのn桁目のどちらか一方のみが1である | |
if (input_c >> now_digit) & 1: | |
if j: | |
y |= 1 << now_digit | |
j -= 1 | |
else: | |
x |= 1 << now_digit | |
k -= 1 | |
# input_cのnow_digit桁目が0の場合は、xのn桁目かyのn桁目の両方が0、または両方が1である | |
else: | |
if i: | |
i -= 1 | |
else: | |
x |= 1 << now_digit | |
y |= 1 << now_digit | |
l -= 1 | |
# 出力 | |
print(x, y) |
あるいは, ユーザー解説のようにも実装することができます.
Editorial - AtCoder Beginner Contest 347
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
# 入力 | |
a, b, input_c = map(int, input().split()) | |
x = 0 | |
y = 0 | |
# 下の桁から計算する | |
for now_digit in range(60): | |
# input_cのnow_digit桁目が1の場合、xのn桁目かyのn桁目のどちらか一方のみが1である | |
# aとbの余っている方を優先して消費する | |
if (input_c >> now_digit) & 1: | |
if a > b: | |
x |= 1 << now_digit | |
a -= 1 | |
else: | |
y |= 1 << now_digit | |
b -= 1 | |
# 下の桁から計算する | |
for now_digit in range(60): | |
# input_cのnow_digit桁目が0の場合、xのn桁目かyのn桁目の両方が0、または両方が1である | |
# aとbが両方余っている場合のみ1にする | |
if ((input_c >> now_digit) & 1 == 0) and a > 0 and b > 0: | |
x |= 1 << now_digit | |
y |= 1 << now_digit | |
a -= 1 | |
b -= 1 | |
# 出力 | |
if a == 0 and b == 0: | |
print(x, y) | |
else: | |
print(-1) |
E以降
E以降は私の能力不足故に省略いたします.
参考にしたサイト等
- 「解説 - AtCoder Beginner Contest 347」
Editorial - AtCoder Beginner Contest 347
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
- nkmk様による「Pythonのdequeでキュー, スタック, デック(両端キュー)を扱う」
Pythonのdequeでキュー, スタック, デック(両端キュー)を扱う | note.nkmk.me
Pythonの標準ライブラリcollectionsモジュールのdeque型を使うと, データをキューやスタック, デック(両端キュー)として効率的に扱うことができる. collections.deque --- コンテナデータ型 — Python 3.11.4 ド ...
- nkmk様による「Pythonで2進数の1の数をカウント(int.bit_count)」
Pythonで2進数の1の数をカウント(int.bit_count) | note.nkmk.me
Pythonで, 整数intの2進数表記における1の数(立っているビットの数)をカウントする方法について説明する. このような処理はpopcount(population count)とも呼ばれる. int.bit_count()(Python 3.10以降) bin(s ...
0 件のコメント:
コメントを投稿 (Please feel free to ask me about your questions! You can use Japanese or English in the comments.)