Solve AtCoder Beginner Contest 170 D --Not Divisible (ABC170D) with python (Eratosthenes sieve)

Hello. During the contest, I made a double loop with this problem and TLE. I don't remember the basic algorithm yet, so I want to be able to use it properly. This problem seems to save a lot of computation using the same principles as the Eratosthenes sieve.

code.py


N = int(input())
A = list(map(int, input().split()))
A.sort()                  #Sort for eratosthenes sieving
Amax = A[-1]
dp = [1]*(Amax+1)         #Make a sieve of Eratosthenes up to the maximum value of A.
ans = 0
for i in range(len(A)-1):
  p = A[i]
  if dp[p] == 1:          #A[i]Is A[j]Check with an Eratosthenes sieve to see if it is a multiple of
    for q in range(Amax//p+1):
      dp[p*q] = 0         #A[i]Set all multiples of
    if A[i] != A[i+1]: 
      ans += 1            #A[i]Count if does not overlap
if dp[Amax] == 1:         #Since the range is over when checking for duplicates, only Amax is judged separately.
  ans += 1
print(ans)

dp is in the initial state dp = [1 1 1 1 1 1 1 ...].

For example If A [0] = 3, the dp after one operation is dp = [0 1 1 0 1 1 0 ...].

Recommended Posts

Solve AtCoder Beginner Contest 170 D --Not Divisible (ABC170D) with python (Eratosthenes sieve)
Solve AtCoder Beginner Contest159 D --Banned K (ABC159D) with python (count is too slow!)
Solve AtCoder ABC168 with python (A ~ D)
AtCoder Beginner Contest: D Question Answers Python
Solve AtCoder 167 with python
Solve AtCoder Beginner Contest 100-102
Solve "AtCoder version! Ant book (beginner)" with Python!
Solve AtCoder ABC166 with python
Solve AtCoder ABC 186 with Python
Atcoder Beginner Contest 152 Kiroku (python)
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solve ABC166 A ~ D with Python
Solve AtCoder Problems Recommendation with python (20200517-0523)
AtCoder Beginner Contest 174 C Problem (Python)
[AtCoder] Solve ABC1 ~ 100 A problem with Python
[At Coder] Beginner Contest 175 ABCD python Solution introduction
AtCoder Beginner Contest 177
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
AtCoder Beginner Contest 179
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
AtCoder Beginner Contest 172
AtCoder Beginner Contest 180
AtCoder Beginner Contest 169, D. Div Game short code
AtCoder Beginner Contest 173
Eratosthenes Sieve (Python)
Atcoder Beginner Contest 153
AtCoder Beginner Contest 176 C Problem "Step" Explanation (Python3, C ++, Java)
[Python] [BFS] At Coder Beginner Contest 168-D [.. Double Dots]
AtCoder Beginner Contest 152 Review
AtCoder Beginner Contest 181 Note
Solve ABC168D in Python
AtCoder Beginner Contest 187 Note
Solve ABC167-D in Python
AtCoder Beginner Contest 160 Review
AtCoder Beginner Contest 178 Review
AtCoder Beginner Contest 180 Note
AtCoder Beginner Contest 166 Review
AtCoder Beginner Contest 167 Review
AtCoder Beginner Contest 182 Note
AtCoder Beginner Contest 164 Review
AtCoder Beginner Contest 169 Review
AtCoder Beginner Contest 181 Review
AtCoder Beginner Contest 171 Review
AtCoder Beginner Contest 182 Review
AtCoder Beginner Contest 180 Review
Solve Sudoku with Python
AtCoder Beginner Contest 156 WriteUp
AtCoder Beginner Contest 177 Review
AtCoder Beginner Contest 168 Review
Solve ABC159-D in Python
AtCoder Beginner Contest 179 Review
AtCoder Beginner Contest 167 Memorandum
AtCoder Beginner Contest 172 Review
AtCoder Beginner Contest 183 Note
AtCoder Beginner Contest 176 Review
AtCoder Beginner Contest 184 Note
AtCoder Beginner Contest 175 Review
AtCoder Beginner Contest 174 Review
AtCoder Beginner Contest 153 Review
AtCoder Beginner Contest 156 Review
AtCoder Beginner Contest 161 Review