1
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?

More than 3 years have passed since last update.

AtCoder ABC 179 Python (A~E)

Last updated at Posted at 2020-09-20

総括

A, Bのみ解けました。
最近パフォーマンスが下がり続けています。

#問題
https://atcoder.jp/contests/abc179

A. Plural Form

image.png

回答

S = input()

if S[-1] == 's':
    answer = S + 'es'
else:
    answer = S + 's'

print(answer)


問題文通り書きます。

B. Go to Jail

image.png

回答

N = int(input())
D = [tuple(map(int, input().split())) for _ in range(N)]

answer = 'No'

count = 0
for i in range(N):
    if D[i][0] != D[i][1]:
        count = 0
    else:
        count += 1
    
    if count == 3:
        answer = 'Yes'
        break

print(answer)

ぞろ目を判定し、count3に達したらYesを返します。

C. A x B + C

image.png

回答

N = int(input())

answer = 0
for a in range(1, N):
    answer += (N-1) // a

print(answer)

制約を考えるとforループは1回しか回せそうにないです。
A * B + C = N を変形してA * B = N - C とするとA * B の組み合わせの数を数える問題とよみかえられます。

D. Leaping Tak

image.png

回答

MOD = 998244353
N, K = map(int, input().split()) #Nはマス目の数、Kは区間の数(Kは10以下)
kukan = [tuple(map(int, input().split())) for _ in range(K)]

dp = [0] * (N+1)
dp[1] = 1

sumdp = [0] * (N+1)
sumdp[1] = 1

for i in range(2, N+1):
    for l, r in kukan:
        li = max(i - l, 0)
        ri = max(i - r - 1, 0)

        dp[i] += sumdp[li] - sumdp[ri]
        dp[i] %= MOD

    sumdp[i] = sumdp[i-1] + dp[i]

print(dp[N])

普通にdpをとると間に合わないので、dpと累積和sumdpを使用します。

E. Sequence Sum

image.png

回答

N, X, M = map(int, input().split())
count_memo = [-1] * 10**6 # 回数のメモ
num_memo = [0] # Aのメモ。1インデックスるにする. 

a = X
count = 1
count_memo[a] = count
num_memo.append(a)
for i in range(1, N):
    a = a**2 % M
    if count_memo[a] == -1:
        count += 1
        count_memo[a] = count
        num_memo.append(a)
    else:
        break

if count == N: # サイクルに入る前に終わった場合は全部合計
    answer = sum(num_memo)
else:
    # 周期に入るまでの回数と合計
    count_before_cycle = count_memo[a] - 1
    sum_before_cycle = sum(num_memo[:count_before_cycle+1])
    # 1周期の数と合計
    count_cycle = count - count_before_cycle
    sum_cycle = sum(num_memo[count_before_cycle+1:])
    # 残りのサイクルの数と合計
    cycle_count = (N - count) // count_cycle
    sum_after_cycle = sum_cycle * cycle_count
    # あまりの数と合計
    remain_count =  (N - count) % count_cycle
    sum_remain = sum(num_memo[count_before_cycle+1:count_before_cycle+1 + remain_count])

    answer = sum_before_cycle + sum_cycle + sum_after_cycle + sum_remain

print(answer)

A はどこかでサイクルに入ります。
したがって、

  1. サイクルに入る前
  2. サイクル
  3. サイクルの繰り返し
  4. サイクルの途中で終わる余分な回数
    の4通りに分けて足し合わせます。
1
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
1
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?