[PYTHON] AtCoder I don't know diary_3 ABC149C, D

Introduction

ABC149 has also participated. I've become a kid. I was stupid, so I misread the question sentence in question A and made a mistake. Also, instead of getting the result in the B problem, I tried to do the operation literally, remembered the amount of calculation and adjusted the direction in a hurry, and what I did two times before last time is a little alive ... ... I feel. Qiita, I want to make this diary a template, but what should I do? That's what programming should do to create some kind of code. I think it's faster to paste it in Notepad and copy and paste it than to open the editor and do something. I feel that there are many programmers and people who try to have fun in this way and do more troublesome things. It's a prejudice.

C - Next Prime Find the smallest prime number greater than or equal to X.

Thoughts

① How long is the prime gap (it seems to be a little over 10 on average) (2) I think we have no choice but to break it down. ③ TLE scary This question sentence is simple and nice. I thought I could use Legendre's conjecture, but I didn't need it.

First time

It's the first time, so it's only once

import math
n=int(input())
#Start the loop with an odd number
if n % 2 == 0 and n != 2:
        n += 1
#Try 20 times with a margin
while n <= n + 20:
    i=3
    #Continue to divide by an odd i smaller than the square root
    while i <= math.sqrt(n):
        #If it breaks, the next n
        if n % i == 0:
            break
        #If it doesn't break, the next i
        else:
            i += 2
    #If everything doesn't break, that's the answer, so it's over
    if i >= math.sqrt(n):
        print(n)
        break
    else:
        n+=2

I passed. Hmmm. I think there is a better way ...

if i >= math.sqrt(n):

However, I think it's a very crappy way to do it. I'm wondering what to do when I've done it without breaking the loop, but I think there's a better way to do the same thing ... Also, regarding prime numbers, I made a loop of 20 times on the assumption that about 20 would be enough at the 10 5 </ sup> level, but I wonder if this is okay ...

Answer of strong man (Tsuyobito)

--Square root, ** 0.5 is fine without math ――Just increase by 1 and divide

It was a surprisingly normal brute force operation. In the first place, I knew that it would end not so many times, so I didn't define the size of i by the while statement, and I should have used the for statement. Similarly, I didn't have to use it twice while. Since I became TLE two times before the last time, I devised to divide it by only odd numbers so as not to put a load, but I thought that it was not necessary unexpectedly. If you can roughly predict the amount of calculation, you will know how much it is. However, I don't know because of that, but the execution time was fast. Also, there weren't many people who used the square root as the division range. However, competitive programming is strong in how fast you write code, so it doesn't have to be that much computational complexity. But I wonder if it will decrease considerably.

I wonder what kind of code is good to see everyone's submissions. Of course, it should be easy to understand, fast and short, but it is difficult to balance them. Writing code that ends in one line is not readable, and at the earliest, such redundant code is not good.

Let's write D before forgetting.

D - Prediction and Restriction

Takahashi decided to play a game called "rock-paper-scissors battle" at an arcade. The rules of this game are as follows.

--The player plays rock-paper-scissors with the chassis N times (in this case, it is also counted as one rock-paper-scissors). ――If the player wins with rock-paper-scissors, the player gets the following score according to the move (Aiko and lose are 0 points). --If you win with Goo, R points ――If you win with choki, S points --P points if you win in par

However, you cannot make the same hand that you made with rock-paper-scissors K times ago. (You can put out your favorite hand with rock-paper-scissors up to the K time.) The housing decides the move to be played in each rock-paper-scissors game before the game starts. Mr. Takahashi, a talented person, read all this before the game started. The information read by Mr. Takahashi is given as the character string T. When the i (1 ≤ i ≤ N) letter of T is r, the housing will give a goo with the i-th rock-paper-scissors, when s, it will give a choki, and when p, it will give a par. Represents. When Takahashi chooses the best move to play in N times of rock-paper-scissors, how many points can he get by the end of the game?

Thoughts

① If you suffer after k times, you will lose, so do not count ② Replace the uncounted characters and count the number of valid rock-paper-scissors at the end. There are many talented people in At Corder. I want to gather everyone and have them fight.

First time

n,k = map(int,input().split())
r,s,p = map(int,input().split())
st = input()
for i in range(len(st)-k):
    if st[i] == st[i + k]:
        st = st[:i + k]+ 'no' + st[i + k + 1:]
win = r*st.count('s') + s*st.count('p') + p*st.count('r')
print(win)

I passed. I couldn't replace the character string, so I cut it into slices and connected them. Tsuka len (st). There are n.

Answer of a strong man (Tsuyobito)

--You can usually receive it in the list and replace it.

Everything else was about the same. The execution time is very long probably because it is a character string. When I tried it on the list, 598ms became 47ms. Quite stupid. There are some bad points, but I was able to do it unexpectedly. I can't do it in time. I want to continue to do my best.

Recommended Posts

AtCoder I don't know diary_3 ABC149C, D
AtCoder I don't know diary_2 ABC148E
AtCoder I don't know diary_4 ABC081B, 087B, 083B (from ABS)
I don't know the value error
I don't understand join