結果

問題 No.2620 Sieve of Coins
ユーザー startcpp
提出日時 2023-12-15 01:39:05
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,781 ms / 2,000 ms
コード長 3,386 bytes
コンパイル時間 370 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 72,440 KB
最終ジャッジ日時 2024-09-27 05:55:43
合計ジャッジ時間 63,562 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

# numpyを使った高速化
from typing import List, Dict, Tuple
import numpy as np
import math

def setMebius(n: int) -> np.ndarray:
    is_prime = np.ones(n + 1, dtype = np.int32)
    mebius = np.ones(n + 1, dtype = np.int32)
    is_prime[0] = 0
    is_prime[1] = 0
    for i in range(2, n + 1):
        if is_prime[i]:
            mebius[i : n + 1 : i] *= -1
            mebius[i * i : n + 1 : i * i] = 0
            is_prime[2 * i : n + 1 : i] = 0
    return mebius

def calc(L: int, noDiv2: bool, noDiv3: bool, mebius: np.ndarray) -> int:
    sqL: int = int(math.sqrt(L) + 1e-8)
    gs: np.ndarray = np.ones(sqL + 1, dtype = np.int64) * L
    gs[1::] //= np.arange(1, sqL + 1, dtype = np.int64) * np.arange(1, sqL + 1, dtype = np.int64)
    if noDiv2 and noDiv3:
        gs[::2] = 0
        gs[::3] = 0
        gs = gs - gs // 2 - gs // 3 + gs // 6 
    elif noDiv2:
        gs[::2] = 0
        gs = gs - gs // 2
    elif noDiv3:
        gs[::3] = 0
        gs = gs - gs // 3
    return (mebius[1:sqL + 1] * gs[1:]).sum()

def subtask4(L: int, a: List[int], mebius: List[int]) -> int:
    n: int = len(a)
    plot: List[List[bool]] = [[False for _ in range(60)] for _ in range(60)]
    for i in range(n):
        c2: int = 0
        c3: int = 0
        t: int = a[i]
        while t % 2 == 0:
            t //= 2
            c2 += 1
        while t % 3 == 0:
            t //= 3
            c3 += 1
        plot[c2][c3] = True
    
    p2: List[int] = [1]
    p3: List[int] = [1]
    while p2[-1] <= L:
        p2.append(p2[-1] * 2)
    while p3[-1] <= L:
        p3.append(p3[-1] * 3)
    
    ret: int = 0
    i: int = 0
    mp: Dict[Tuple[int, int, int], int] = {}
    while p2[i] <= L:
        j: int = 0
        while p2[i] * p3[j] <= L:
            tmpL: int = L // (p2[i] * p3[j]) # 選ぶ要素のLCM = p2[i] * p3[j]
            for k in range(16):
                max2: int = -1
                min2: int = 1000000
                max3: int = -1
                min3: int = 1000000
                cnt: int = 0
                not_found: bool = False
                for l in range(4):
                    if (k >> l) % 2 == 1:
                        c2: int = i - l // 2
                        c3: int = j - l % 2
                        if c2 < 0 or c3 < 0 or plot[c2][c3] == False:
                            not_found = True
                            break
                        max2 = max(max2, c2)
                        min2 = min(min2, c2)
                        max3 = max(max3, c3)
                        min3 = min(min3, c3)
                        cnt += 1
                if not_found or cnt == 0 or max2 != i or max3 != j:
                    continue
                noDiv2 = (min2 != max2)
                noDiv3 = (min3 != max3)
                key = (tmpL, noDiv2, noDiv3)
                if key not in mp:
                    mp[key] = calc(tmpL, noDiv2, noDiv3, mebius)
                if cnt % 2 == 1:
                    ret += mp[key] * p2[cnt - 1]
                else:
                    ret -= mp[key] * p2[cnt - 1]
            j += 1
        i += 1
    return ret

if __name__ == '__main__':
    L, N = map(int, input().split())
    a = list(map(int, input().split()))
    sqL: int = 1
    while sqL * sqL <= L:
        sqL += 1
    sqL -= 1
    mebius: List[int] = setMebius(sqL)
    ans: int = subtask4(L, a, mebius)
    print(ans)
0