ABC125_C --GCD on Blackboard [Notes solved in Python]

・ [ABC125_C --GCD on Blackboard] (https://atcoder.jp/contests/abc125/tasks/abc125_c)

N integers A1, A2, .... An are entered. The question of what is the greatest common divisor of N integers when one of them is replaced with an arbitrary integer.

Well, I don't know, but the result of crushing the numbers one by one and turning gcd around to solve it. ・ ....

Bad solution

As a result of implementing it without thinking about anything, the amount of calculation became tremendous by turning gcd from end to end every time I changed the number to be crushed, and I wrote a code that made me crazy and died.

    import sys
    input = sys.stdin.readline
     
    import numpy as np
    import fractions
    from functools import reduce
     
    from copy import deepcopy
     
     
    def gcd_list(nums):
        return reduce(fractions.gcd, nums)
     
    def main():
        N = int(input())
        A = np.array(sorted(list(map(int, input().split()))))
     
        bools = np.ones(N, dtype=bool)
     
        ans = 0
        vals = []
        for i in range(N):
            A_cop = deepcopy(A)
            bl_cop = deepcopy(bools)
     
            fal = [i]
            bl_cop[fal] = False
     
            vals.append(gcd_list(A_cop[bl_cop]))
     
        print(max(vals))
     
    main()

> Sudden TLE <

Good solution

Set the number to be crushed as Ai, and find the greatest common divisor for each of the interval [A0, Ai) and the interval (Ai, An]. Furthermore, by finding the greatest common divisor of those two numbers, the greatest common divisor that can be taken when Ai is crushed can be found, so it is okay if the greatest common divisor (Gestaltzerfall feeling) is taken out after performing this operation on all integers.

If you use this method, you can carry over the value used when calculating the greatest common divisor of the previous Ai when finding the greatest common divisor of the two sections that sandwich Ai, so from the end That you don't have to calculate all the way to the end

It seems to be called cumulative GCD (I haven't even hit the cumulative sum yet)

    import sys
    input = sys.stdin.readline
    import fractions
    
    
    def main():
        N = int(input())
        A = list(map(int, input().split()))
    
        L_gcds = [0] * N
        L_gcd_tmp = L_gcds[0]
        for i in range(1, N):
            L_gcd_tmp = fractions.gcd(L_gcd_tmp, A[i-1])
            L_gcds[i] = L_gcd_tmp
    
        R_gcds = [0] * N
        R_gcd_tmp = R_gcds[0]
        for j in range(1, N):
            R_gcd_tmp = fractions.gcd(R_gcd_tmp, A[N-j])
            R_gcds[j] = R_gcd_tmp
        R_gcds.reverse()
    
        LR_gcds = [0] * N
        for k in range(0, N):
            LR_gcds[k] = fractions.gcd(L_gcds[k], R_gcds[k])
    
        print(max(LR_gcds))
    
    main()

Learn

――Let's see the restrictions properly ――Let's think about the amount of calculation before implementing it.

Recommended Posts

ABC125_C --GCD on Blackboard [Notes solved in Python]
Notes on nfc.ContactlessFrontend () for nfcpy in python
Notes on using code formatter in Python
Solve ABC146-C in Python
Notes on using dict in python [Competition Pro]
Solve ABC098-C in Python
Web scraping notes in python3
[Python] Notes on data analysis
Notes on installing Python on Mac
Get Evernote notes in Python
Notes on imshow () in OpenCV
Notes on installing Python on CentOS
Notes on reading and writing float32 TIFF images in python
Notes on Python and dictionary types
Notes on using MeCab from Python
Atcoder ABC125 C --GCD on Blackboard
Notes on installing Python using PyEnv
Notes on using rstrip with python.
Notes on accessing dashDB from python
Notes using cChardet and python3-chardet in Python 3.3.1.
Notes on PyQ machine learning python grammar
Find files like find on linux in Python
Notes on creating static files in Django
Notes on doing Japanese OCR with Python
Run AzureKinect in Python on Christmas Eve.
Notes on building Python and pyenv on Mac
Notes for using python (pydev) in eclipse
Run Python in C ++ on Visual Studio 2017
Notes on resolving references to packages in Python projects in IntelliJ IDEA (PyCharm)
Potential Outcomes (Potential Outcomes) Causal Reasoning Notes in Python Part 1
Install python package in personal environment on Ubuntu
Notes on installing Python3 and using pip on Windows7
Run Python YOLOv3 in C ++ on Visual Studio 2017
Personal notes to doc Python code in Sphinx
A note on optimizing blackbox functions in Python
Note on encoding when LANG = C in Python
Try working with Mongo in Python on Mac
Notes for implementing simple collaborative filtering in Python
To write to Error Repoting in Python on GAE
Detect golden crosses on stock charts in Python
TensorFlow: Run data learned in Python on Android
[Python] Notes on accelerating genetic algorithms using multiprocessing
Notes on oct2py calling Octave scripts from Python
Quadtree in Python --2
Python in optimization
CURL in python
Python scraping notes
Geocoding in python
SendKeys in Python
Notes on exchanging and multiple assignment of Python variable values ​​learned in quiz format
Python study notes _000
Python learning notes
Meta-analysis in Python
Python on Windows
Unittest in python
Python beginner notes
Python study notes_006
Epoch in Python
Discord in Python
Notes on Flask
Sudoku in Python