結果

問題 No.2620 Sieve of Coins
ユーザー startcppstartcpp
提出日時 2023-12-15 01:02:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 823 ms / 2,000 ms
コード長 4,145 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 94,672 KB
最終ジャッジ日時 2023-12-15 01:02:52
合計ジャッジ時間 16,379 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
68,224 KB
testcase_01 AC 57 ms
68,224 KB
testcase_02 AC 74 ms
74,080 KB
testcase_03 AC 243 ms
93,520 KB
testcase_04 AC 427 ms
93,776 KB
testcase_05 AC 57 ms
68,224 KB
testcase_06 AC 56 ms
68,224 KB
testcase_07 AC 55 ms
68,224 KB
testcase_08 AC 58 ms
68,224 KB
testcase_09 AC 82 ms
77,912 KB
testcase_10 AC 84 ms
77,912 KB
testcase_11 AC 98 ms
78,164 KB
testcase_12 AC 81 ms
77,908 KB
testcase_13 AC 65 ms
73,032 KB
testcase_14 AC 90 ms
78,036 KB
testcase_15 AC 57 ms
68,224 KB
testcase_16 AC 60 ms
70,948 KB
testcase_17 AC 87 ms
78,380 KB
testcase_18 AC 205 ms
93,232 KB
testcase_19 AC 205 ms
93,520 KB
testcase_20 AC 60 ms
70,948 KB
testcase_21 AC 208 ms
93,520 KB
testcase_22 AC 225 ms
93,520 KB
testcase_23 AC 191 ms
93,520 KB
testcase_24 AC 187 ms
93,520 KB
testcase_25 AC 174 ms
93,520 KB
testcase_26 AC 182 ms
93,520 KB
testcase_27 AC 232 ms
93,448 KB
testcase_28 AC 58 ms
68,224 KB
testcase_29 AC 56 ms
68,224 KB
testcase_30 AC 62 ms
70,948 KB
testcase_31 AC 288 ms
93,648 KB
testcase_32 AC 208 ms
93,520 KB
testcase_33 AC 184 ms
93,520 KB
testcase_34 AC 205 ms
93,520 KB
testcase_35 AC 200 ms
93,776 KB
testcase_36 AC 208 ms
93,520 KB
testcase_37 AC 225 ms
93,648 KB
testcase_38 AC 428 ms
93,648 KB
testcase_39 AC 374 ms
93,648 KB
testcase_40 AC 373 ms
93,648 KB
testcase_41 AC 317 ms
93,648 KB
testcase_42 AC 397 ms
93,648 KB
testcase_43 AC 591 ms
93,776 KB
testcase_44 AC 580 ms
93,904 KB
testcase_45 AC 64 ms
73,028 KB
testcase_46 AC 231 ms
93,776 KB
testcase_47 AC 231 ms
93,904 KB
testcase_48 AC 360 ms
93,648 KB
testcase_49 AC 288 ms
93,776 KB
testcase_50 AC 221 ms
93,648 KB
testcase_51 AC 462 ms
94,032 KB
testcase_52 AC 272 ms
94,032 KB
testcase_53 AC 285 ms
94,032 KB
testcase_54 AC 633 ms
94,672 KB
testcase_55 AC 819 ms
94,544 KB
testcase_56 AC 823 ms
94,544 KB
testcase_57 AC 799 ms
94,472 KB
権限があれば一括ダウンロードができます

ソースコード

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