結果

問題 No.458 異なる素数の和
ユーザー jensenjensen
提出日時 2021-01-15 13:52:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,751 ms / 2,000 ms
コード長 1,007 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 11,104 KB
実行使用メモリ 363,972 KB
最終ジャッジ日時 2023-08-17 03:59:30
合計ジャッジ時間 11,862 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,128 KB
testcase_01 AC 492 ms
109,700 KB
testcase_02 AC 680 ms
141,036 KB
testcase_03 AC 147 ms
30,604 KB
testcase_04 AC 176 ms
37,356 KB
testcase_05 AC 1,377 ms
304,236 KB
testcase_06 AC 654 ms
133,484 KB
testcase_07 AC 25 ms
8,944 KB
testcase_08 AC 1,417 ms
306,796 KB
testcase_09 AC 61 ms
15,132 KB
testcase_10 AC 17 ms
8,108 KB
testcase_11 AC 1,751 ms
363,972 KB
testcase_12 AC 17 ms
8,244 KB
testcase_13 AC 16 ms
8,084 KB
testcase_14 AC 17 ms
8,200 KB
testcase_15 AC 16 ms
8,040 KB
testcase_16 AC 101 ms
21,168 KB
testcase_17 AC 17 ms
8,088 KB
testcase_18 AC 17 ms
8,160 KB
testcase_19 AC 17 ms
8,148 KB
testcase_20 AC 17 ms
8,076 KB
testcase_21 AC 17 ms
8,124 KB
testcase_22 AC 17 ms
8,144 KB
testcase_23 AC 17 ms
8,132 KB
testcase_24 AC 17 ms
8,080 KB
testcase_25 AC 17 ms
8,152 KB
testcase_26 AC 17 ms
8,152 KB
testcase_27 AC 624 ms
127,168 KB
testcase_28 AC 1,713 ms
356,192 KB
testcase_29 AC 41 ms
11,484 KB
testcase_30 AC 376 ms
81,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

def is_prime(n):
    if n == 1: return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True
def find(l,iter,a):
    for i in range(iter,len(l)):
        if(l[i]>=a):
            break
    return i
            
        
n=int(input())
pris=[]
prisum=[]

for i in range(2,n+1):
    if(is_prime(i)==True):
        pris.append(i)
suum=0

for i in range(0,len(pris)):
    suum+=pris[i]
    prisum.append(suum)
x=len(pris)


f=[[0]*x for i in range(n)]
if(n==1):
    print(-1)
elif(n==2 or n==3):
    print(1)
elif(n==4):
    print(-1)
elif(n>4):
    f[1][0]=1
    f[2][1]=1
    ite=0
    for i in range(4,n):
        ite=find(prisum,ite,i+1)
        for j in range(ite,min(ite+5,x)):
            if(i+1==pris[j]):
                f[i][j]=1
            else:
                z=max(f[i-pris[j]][:j])
                if(z>0):
                    f[i][j]=z+1
    mv=max(f[n-1])
    if(mv==0):
        print(-1)
    else:
        print(mv)
0