Template AtCoder ABC 179 Python (A ~ E)

Summary

Only A and B were solved. Performance has been declining recently.

problem

https://atcoder.jp/contests/abc179

A. Plural Form image.png

Answer

S = input()

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

print(answer)


Write according to the problem statement.

B. Go to Jail image.png

Answer

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)

It determines the doublet and returns `Yes``` when count``` reaches `3```.

C. A x B + C image.png

Answer

N = int(input())

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

print(answer)

Considering the restrictions, it seems that the for loop can be turned only once. a * b + c = nTransforma * b = n - cThena * b It can be called the problem of counting the number of combinations of.

D. Leaping Tak image.png

Answer

MOD = 998244353
N, K = map(int, input().split()) #N is the number of squares, K is the number of sections(K is 10 or less)
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])

Normally, taking `dp``` is not in time, so use ``` dp``` and the cumulative sum `sumdp```.

E. Sequence Sum image.png

Answer

N, X, M = map(int, input().split())
count_memo = [-1] * 10**6 #Number of notes
num_memo = [0] #A note. 1 index. 

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: #Total if finished before entering the cycle
    answer = sum(num_memo)
else:
    #Number and total until entering the cycle
    count_before_cycle = count_memo[a] - 1
    sum_before_cycle = sum(num_memo[:count_before_cycle+1])
    #Number and total of 1 cycle
    count_cycle = count - count_before_cycle
    sum_cycle = sum(num_memo[count_before_cycle+1:])
    #Number and total of remaining cycles
    cycle_count = (N - count) // count_cycle
    sum_after_cycle = sum_cycle * cycle_count
    #Too many and total
    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)

aEnters the cycle somewhere. Therefore,

  1. Before entering the cycle
  2. Cycle
  3. Repeat cycle
  4. Extra number of times ending in the middle of the cycle Divide into 4 ways and add them together.

Recommended Posts

Template AtCoder ABC 179 Python (A ~ E)
AtCoder ABC 177 Python (A ~ E)
AtCoder ABC 178 Python (A ~ E)
AtCoder ABC 176 Python (A ~ E)
AtCoder ABC 182 Python (A ~ D)
AtCoder ABC 174 Python
AtCoder ABC187 Python
AtCoder ABC188 Python
Solve Atcoder ABC176 (A, B, C, E) in Python
AtCoder ABC 175 Python
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Solve AtCoder ABC168 with python (A ~ D)
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
Solve AtCoder ABC166 with python
Atcoder ABC164 A-C in Python
Solve ABC176 E in Python
Atcoder ABC167 A-D in Python
Atcoder ABC165 A-D in Python
Atcoder ABC166 A-E in Python
Solve AtCoder ABC 186 with Python
Atcoder ABC169 A-E in Python
AtCoder ABC177 A-D in python
Review of atcoder ABC158, up to question E (Python)
Solve ABC163 A ~ C with Python
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
ABC127 A, B, C Explanation (python)
atCoder 173 Python
ABC166 in Python A ~ C problem
Solve ABC168 A ~ C with Python
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
[Python] Now a brown coder ~ [AtCoder]
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
AtCoder ABC176
Solved AtCoder ABC 114 C-755 with Python3
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
ABC128 A, B, C commentary (python)
Solve ABC158 A ~ C with Python
ABC126 A, B, C Explanation (python)
Solve ABC037 A ~ C in Python
AtCoder ABC177
AtCoder ABC168 A case expression solved in Ruby and Python
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
[Python] I'm a green coder ~ [AtCoder]
Solve ABC175 A, B, C in Python
[Python] [Explanation] AtCoder Typical DP Contest: A Contest
Python beginner Atcoder memo @ KEYENCE 2020, ABC problem
AtCoderBeginnerContest154 Participation memo (Python, A ~ E problem)
Solve ABC165 A, B, D in Python
Learn dynamic programming in Python (A ~ E)
Beginner ABC154 (Python)
Beginner ABC156 (Python)
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
Beginner ABC155 (Python)
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
python argparse template
Beginner ABC157 (Python)