結果

問題 No.2620 Sieve of Coins
ユーザー startcppstartcpp
提出日時 2023-12-15 01:39:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,627 ms / 2,000 ms
コード長 3,386 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 71,300 KB
最終ジャッジ日時 2023-12-15 01:40:06
合計ジャッジ時間 59,034 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 451 ms
43,248 KB
testcase_01 AC 456 ms
43,248 KB
testcase_02 AC 481 ms
43,232 KB
testcase_03 AC 1,142 ms
71,284 KB
testcase_04 AC 1,300 ms
71,284 KB
testcase_05 AC 462 ms
43,232 KB
testcase_06 AC 453 ms
43,232 KB
testcase_07 AC 499 ms
43,248 KB
testcase_08 AC 448 ms
43,248 KB
testcase_09 AC 459 ms
43,232 KB
testcase_10 AC 471 ms
43,232 KB
testcase_11 AC 469 ms
43,248 KB
testcase_12 AC 486 ms
43,248 KB
testcase_13 AC 458 ms
43,232 KB
testcase_14 AC 462 ms
43,232 KB
testcase_15 AC 474 ms
43,232 KB
testcase_16 AC 458 ms
43,232 KB
testcase_17 AC 509 ms
43,232 KB
testcase_18 AC 1,173 ms
70,732 KB
testcase_19 AC 1,104 ms
71,284 KB
testcase_20 AC 479 ms
43,232 KB
testcase_21 AC 1,133 ms
64,324 KB
testcase_22 AC 1,142 ms
64,324 KB
testcase_23 AC 1,127 ms
51,012 KB
testcase_24 AC 1,098 ms
51,012 KB
testcase_25 AC 1,081 ms
51,012 KB
testcase_26 AC 1,109 ms
51,028 KB
testcase_27 AC 1,135 ms
50,996 KB
testcase_28 AC 476 ms
43,232 KB
testcase_29 AC 458 ms
43,232 KB
testcase_30 AC 476 ms
43,232 KB
testcase_31 AC 1,195 ms
71,284 KB
testcase_32 AC 1,114 ms
64,444 KB
testcase_33 AC 1,128 ms
51,012 KB
testcase_34 AC 1,142 ms
51,012 KB
testcase_35 AC 1,137 ms
51,012 KB
testcase_36 AC 1,161 ms
64,324 KB
testcase_37 AC 1,144 ms
61,292 KB
testcase_38 AC 1,321 ms
71,284 KB
testcase_39 AC 1,289 ms
68,820 KB
testcase_40 AC 1,298 ms
71,300 KB
testcase_41 AC 1,269 ms
71,284 KB
testcase_42 AC 1,321 ms
71,284 KB
testcase_43 AC 1,491 ms
71,284 KB
testcase_44 AC 1,452 ms
71,284 KB
testcase_45 AC 469 ms
43,248 KB
testcase_46 AC 1,170 ms
57,300 KB
testcase_47 AC 1,171 ms
51,676 KB
testcase_48 AC 1,270 ms
71,284 KB
testcase_49 AC 1,231 ms
71,284 KB
testcase_50 AC 1,170 ms
56,024 KB
testcase_51 AC 1,360 ms
71,284 KB
testcase_52 AC 1,167 ms
57,320 KB
testcase_53 AC 1,195 ms
57,312 KB
testcase_54 AC 1,526 ms
71,284 KB
testcase_55 AC 1,627 ms
71,284 KB
testcase_56 AC 1,617 ms
71,284 KB
testcase_57 AC 1,617 ms
71,056 KB
権限があれば一括ダウンロードができます

ソースコード

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