結果

問題 No.719 Coprime
ユーザー ygd.ygd.
提出日時 2022-09-20 23:59:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,312 bytes
コンパイル時間 694 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 83,140 KB
最終ジャッジ日時 2023-08-23 19:44:06
合計ジャッジ時間 16,151 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 161 ms
80,076 KB
testcase_01 AC 179 ms
80,256 KB
testcase_02 AC 160 ms
80,460 KB
testcase_03 AC 160 ms
80,104 KB
testcase_04 AC 157 ms
80,216 KB
testcase_05 AC 160 ms
80,360 KB
testcase_06 AC 161 ms
80,484 KB
testcase_07 AC 162 ms
80,384 KB
testcase_08 AC 160 ms
80,532 KB
testcase_09 AC 161 ms
80,240 KB
testcase_10 AC 157 ms
80,460 KB
testcase_11 AC 162 ms
80,656 KB
testcase_12 AC 159 ms
80,540 KB
testcase_13 AC 159 ms
80,844 KB
testcase_14 AC 161 ms
80,844 KB
testcase_15 AC 164 ms
80,912 KB
testcase_16 AC 165 ms
81,100 KB
testcase_17 AC 171 ms
81,604 KB
testcase_18 AC 170 ms
81,320 KB
testcase_19 AC 171 ms
81,676 KB
testcase_20 AC 170 ms
81,508 KB
testcase_21 AC 187 ms
82,096 KB
testcase_22 AC 187 ms
82,252 KB
testcase_23 AC 184 ms
82,180 KB
testcase_24 AC 185 ms
81,788 KB
testcase_25 AC 184 ms
81,260 KB
testcase_26 AC 178 ms
81,684 KB
testcase_27 AC 185 ms
82,172 KB
testcase_28 AC 184 ms
82,008 KB
testcase_29 AC 200 ms
82,596 KB
testcase_30 AC 198 ms
82,456 KB
testcase_31 AC 199 ms
82,588 KB
testcase_32 AC 189 ms
82,340 KB
testcase_33 AC 188 ms
82,400 KB
testcase_34 AC 189 ms
82,048 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from re import I
import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
import math
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
from collections import defaultdict 
from collections import deque
import copy #DeepCopy: hoge = [_[:] for _ in hogehoge]
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]
#dx8 = [1,1,0,-1,-1,-1,0,1]
#dy8 = [0,1,1,1,0,-1,-1,-1]

# 素数列挙, 10^7で300ms程度。
def prime_list(n):
    is_prime = [0] * (n + 1) #素数かどうか。1なら素数。
    is_prime[2] = 1
    for i in range(3, n + 1, 2):
        is_prime[i] = 1
    for i in range(2, int(n ** 0.5) + 1):
        if is_prime[i]:
            for j in range(i * i, n + 1, 2 * i):
                is_prime[j] = 0
    #最後に全部並べる。
    d = []
    for j in range(n + 1):
        if is_prime[j] == 1:
            d.append(j)
    return d, is_prime

def main():
    N = int(input())
    if N == 2:
        print(2);exit()

    Primes = [2,3,5,7,11,13,17,19,23,29,31]
    while Primes[-1] > N:
        Primes.pop()
    
    # print(Primes)
    M = len(Primes)

    PL,dummy = prime_list(N)
    # PL = deque(PL)
    # while PL and PL[0] in Primes:
    #     PL.popleft()

    # dp[mask]: Primesの素数の使用状況がmaskの時の最大値
    dp = [0]*(1<<M)
    for prime in PL:
        p = copy.copy(dp)
        p,dp = dp,p
        if prime in Primes:
            idx = Primes.index(prime)
            # print("prime",prime,"idx",idx)
            for mask in range(1<<M):
                if (mask>>idx)&1 == 1: #既にprimeが含まれている。
                    continue
                for j in range(M):
                    # print("J",j)
                    if (mask>>j)&1 == 0: #使われていない
                        if j == idx:
                            keta = 0
                            while prime**(keta+1) <= N:
                                keta += 1
                            dp[mask|1<<j] = max(dp[mask|1<<j], p[mask] + prime**keta)
                        else:
                            kata = 0
                            while (Primes[j]**(kata+1))*prime <= N:
                                kata += 1
                            if kata == 0:
                                continue
                            right = (1<<j) + (1<<idx)
                            # print("right",right,"prime",prime,"VAL",(Primes[j]**kata)*prime)
                            dp[mask|right] = max(dp[mask|right], p[mask] + (Primes[j]**kata)*prime)
        else:
            for mask in range(1<<M):
                for j in range(M):
                    if (mask>>j)&1 == 0:
                        kata = 0
                        while (Primes[j]**(kata+1))*prime <= N:
                            kata += 1
                        if kata == 0:
                            continue
                        # print("right",right,"prime",prime,"VAL",(Primes[j]**kata)*prime)
                        dp[mask|1<<j] = max(dp[mask|1<<j], p[mask] + (Primes[j]**kata)*prime)
        # print(dp)
    
    ans = max(dp)
    print(ans)


    
if __name__ == '__main__':
    main()
0