Daily AtCoder # 51 in Python

Introduction

Last time

#51 Problem

** Thoughts ** In the production, I searched $ i and j $ completely and TLE. The amount of calculation is $ O ((2 * 10 ^ 5) ^ {2}) $. The constraint of this problem is that it will not pass unless it is suppressed to about $ O (N) $.

Numbers expressed in decimal numbers can be represented as $ a_i * 10 ^ n + a_ {i-1} * 10 ^ {n-1} \ cdots a_0 * 10 ^ 0 $. If the character string $ S $ of length $ N $ is divided by the $ i $ th from the right and the character string is $ S_i $, then $ S_i $ is $ S_0 + S_1 * 10 ^ 1 \ cdots S_i * 10 ^ { It can be expressed as Ni} $. Since it is not possible to combine characters separately, the character string $ S_ {ij} $ in the interval of $ i, j, i <j $ in $ S $ is $ S_ {ij} = \ frac {S_ {j }-S_ {i}} {10 ^ i} $ can be written. Where $ 2019,10 ^ i $ are relatively prime (greatest common divisor is 1). Therefore, to determine if $ S_ {ij} $ is a multiple of 2019, the denominator of the previous equation ($ \ frac {S_ {j} -S_ {i}} {10 ^ i} $) is a multiple of 2019. It is the same value as the judgment that there is. Also, if you subtract two different numbers that have the same remainder divided by 2019, you get a number that is divisible by 2019. So, record $ S_ {i} mod (2019) $ of $ i $ respectively. I stumbled upon making a list to record the remainder. Of course, the remainder $ m $ divided by 2019 is $ 0 \ leq m <2019 $, but only when the remainder is 0, $ S_i $ alone can be a multiple of 2019. So, add +1 to the 0 term of the list that records the remainder first. This +1 has a character string of length 0, and when it is too 0, it is paired with a character string of length 0 → I feel that it will be a multiple of 2019 by itself. The formula for calculating the combination is $ n \ * (n-1) $ by transforming $ {} _n C _2 $.

s = input()

t, a = 0, 1
mod = [0]*2019
mod[0] = 1 #First element(Remainder 0)Is+1
s = s[::-1] #It's easier to scale if you look from behind, so reverse
n = len(s)
for i in range(n):
    t += int(s[i]) * a
    t %= 2019
    a *= 10
    a %= 2019 #Because they are relatively prime%The answer t does not change even if 2019 is calculated
    mod[t] += 1
ans = 0

for i in range(2019):
    ans += mod[i]*(mod[i]-1)//2 #Calculation of combinations
print(ans)

Should I write t, a = 0, 1 on another line? I couldn't find it in PEP8.

Summary

I wanted to solve it in production. I'm happiest this week because I have two ABCs so I'm going to have a cup of tea. see you.

Recommended Posts

Daily AtCoder # 36 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Daily AtCoder # 53 in Python
Daily AtCoder # 7 in Python
Daily AtCoder # 24 in Python
Daily AtCoder # 37 in Python
Daily AtCoder # 8 in Python
Daily AtCoder # 42 in Python
Daily AtCoder # 21 in Python
Daily AtCoder # 17 in Python
Daily AtCoder # 38 in Python
Daily AtCoder # 54 in Python
Daily AtCoder # 11 in Python
Daily AtCoder # 15 in Python
Daily AtCoder # 13 in Python
Daily AtCoder # 45 in Python
Daily AtCoder # 30 in Python
Daily AtCoder # 40 in Python
Daily AtCoder # 10 in Python
Daily AtCoder # 5 in Python
Daily AtCoder # 28 in Python
Daily AtCoder # 39 in Python
Daily AtCoder # 20 in Python
Daily AtCoder # 19 in Python
Daily AtCoder # 52 in Python
Daily AtCoder # 3 in Python
Daily AtCoder # 14 in Python
Daily AtCoder # 50 in Python
Daily AtCoder # 26 in Python
Daily AtCoder # 4 in Python
Daily AtCoder # 43 in Python
Daily AtCoder # 29 in Python
Daily AtCoder # 22 in Python
Daily AtCoder # 49 in Python
Daily AtCoder # 27 in Python
Daily AtCoder # 1 in Python
Daily AtCoder # 25 in Python
Daily AtCoder # 16 in Python
Daily AtCoder # 12 in Python
Daily AtCoder # 48 in Python
Daily AtCoder # 34 in Python
Daily AtCoder # 51 in Python
Daily AtCoder # 31 in Python
Daily AtCoder # 46 in Python
Daily AtCoder # 35 in Python
Daily AtCoder # 9 in Python
Daily AtCoder # 44 in Python
Daily AtCoder # 41 in Python
atCoder 173 Python
Python Input Note in AtCoder
Atcoder ABC165 A-D in Python
Atcoder ABC166 A-E in Python
Atcoder ABC169 A-E in Python
AtCoder ABC177 A-D in python
Solve Atcoder ABC169 A-D in Python
[Python] Basic knowledge used in AtCoder
Python in optimization
CURL in python
Metaprogramming in Python