結果

問題 No.458 異なる素数の和
ユーザー jensenjensen
提出日時 2021-01-15 13:54:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,387 ms / 2,000 ms
コード長 1,007 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 366,464 KB
最終ジャッジ日時 2024-05-04 11:11:51
合計ジャッジ時間 9,434 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
11,136 KB
testcase_01 AC 452 ms
112,256 KB
testcase_02 AC 558 ms
143,616 KB
testcase_03 AC 124 ms
33,152 KB
testcase_04 AC 149 ms
39,936 KB
testcase_05 AC 1,079 ms
306,816 KB
testcase_06 AC 537 ms
135,936 KB
testcase_07 AC 32 ms
11,648 KB
testcase_08 AC 1,132 ms
309,248 KB
testcase_09 AC 60 ms
17,664 KB
testcase_10 AC 28 ms
11,008 KB
testcase_11 AC 1,387 ms
366,464 KB
testcase_12 AC 32 ms
11,008 KB
testcase_13 AC 30 ms
11,008 KB
testcase_14 AC 30 ms
11,136 KB
testcase_15 AC 30 ms
11,008 KB
testcase_16 AC 85 ms
23,552 KB
testcase_17 AC 28 ms
11,008 KB
testcase_18 AC 29 ms
11,136 KB
testcase_19 AC 28 ms
11,136 KB
testcase_20 AC 27 ms
11,008 KB
testcase_21 AC 27 ms
11,008 KB
testcase_22 AC 27 ms
11,136 KB
testcase_23 AC 30 ms
11,008 KB
testcase_24 AC 27 ms
11,136 KB
testcase_25 AC 28 ms
11,008 KB
testcase_26 AC 29 ms
11,008 KB
testcase_27 AC 520 ms
129,792 KB
testcase_28 AC 1,377 ms
358,656 KB
testcase_29 AC 45 ms
14,208 KB
testcase_30 AC 308 ms
83,968 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+3,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