結果

問題 No.2620 Sieve of Coins
ユーザー startcppstartcpp
提出日時 2023-12-15 01:04:31
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 4,145 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 33,180 KB
最終ジャッジ日時 2023-12-15 01:04:36
合計ジャッジ時間 5,405 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
17,684 KB
testcase_01 AC 33 ms
10,880 KB
testcase_02 AC 34 ms
10,880 KB
testcase_03 AC 1,100 ms
26,504 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List, Dict, Tuple

def setMebius(n: int) -> List[int]:
    is_prime: List[int] = [True] * (n + 1)
    mebius: List[int] = [1] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, n + 1):
        if is_prime[i]:
            bai: int = 0 if i == 2 else 2
            mebius[i] *= -1
            for j in range(2 * i, n + 1, i):
                is_prime[j] = False
                if bai == 0:
                    mebius[j] = 0
                else:
                    mebius[j] *= -1
                bai += 1
                if bai == i:
                    bai = 0
    return mebius
    
def calc(L: int, noDiv2: bool, noDiv3: bool, mebius: List[int]) -> int:
    ret: int = 0
    i: int = 1
    while i * i <= L:
        keisu: int = 0   # g(i) := L以下の正のi * iの倍数であって「noDiv*=trueなら*の倍数でない」を満たすものの個数
        if i % 2 == 0 and noDiv2:
            keisu = 0
        elif i % 3 == 0 and noDiv3:
            keisu = 0
        else:
            c: int = L // (i * i)
            keisu = c
            if noDiv2:
                keisu -= c // 2
            if noDiv3:
                keisu -= c // 3
            if noDiv2 and noDiv3:
                keisu += c // 6
        # f(i) := L以下の正のi * iの倍数であって「noDiv*=trueなら*の倍数でない」を満たす上で、任意の整数j > iについてj * jの倍数でもないものの個数
        # f(i) = g(i) - f(2 * i) - f(3 * i) - f(4 * i) - ...より、g(i) = sum(i|j, f(j)) (jはiの倍数) 
        # メビウスの反転公式より f(i) = sum(i|j, μ(j/i) g(j)). 求めたいのはf(1)なので、f(1) = sum(j, μ(j)g(j)) が得られる.
        ret += mebius[i] * keisu
        i += 1
    return ret

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