結果

問題 No.458 異なる素数の和
ユーザー 👑 horiesinitihoriesiniti
提出日時 2018-04-03 07:28:21
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,802 ms / 2,000 ms
コード長 607 bytes
コンパイル時間 39 ms
コンパイル使用メモリ 11,256 KB
実行使用メモリ 15,628 KB
最終ジャッジ日時 2023-09-08 13:59:47
合計ジャッジ時間 13,294 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
15,080 KB
testcase_01 AC 574 ms
15,312 KB
testcase_02 AC 722 ms
15,448 KB
testcase_03 AC 190 ms
15,492 KB
testcase_04 AC 222 ms
15,304 KB
testcase_05 AC 1,507 ms
15,560 KB
testcase_06 AC 687 ms
15,448 KB
testcase_07 AC 93 ms
15,248 KB
testcase_08 AC 1,531 ms
15,464 KB
testcase_09 AC 118 ms
15,108 KB
testcase_10 AC 88 ms
15,120 KB
testcase_11 AC 1,802 ms
15,464 KB
testcase_12 AC 87 ms
15,196 KB
testcase_13 AC 89 ms
15,116 KB
testcase_14 AC 88 ms
15,336 KB
testcase_15 AC 89 ms
15,320 KB
testcase_16 AC 145 ms
15,368 KB
testcase_17 AC 89 ms
15,080 KB
testcase_18 AC 88 ms
15,108 KB
testcase_19 AC 88 ms
15,336 KB
testcase_20 AC 88 ms
15,292 KB
testcase_21 AC 88 ms
15,316 KB
testcase_22 AC 88 ms
15,520 KB
testcase_23 AC 88 ms
15,088 KB
testcase_24 AC 87 ms
15,160 KB
testcase_25 AC 88 ms
15,156 KB
testcase_26 AC 87 ms
15,300 KB
testcase_27 AC 655 ms
15,628 KB
testcase_28 AC 1,776 ms
15,464 KB
testcase_29 AC 103 ms
15,356 KB
testcase_30 AC 439 ms
15,500 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:42: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

require 'prime'
n=gets.to_i
dp=[0]*(n+2)
x=Prime.each(n).to_a
t=n/2+2
sum=0
while (x.empty? == false) && (x[0]*3<=n)
	e=x.shift
	sum+=e
	sum=n if sum>n
	t=sum-e+1
	t.times{|e2|
		p2=t-e2
		p3=e+p2
		break if p2<0
		next if p3>n
		
		if dp[p2]>0 && dp[p3]<dp[p2]+1
			dp[p3]=dp[p2]+1
		end
	}
	dp[e]=1 if dp[e]==0
end
ans=dp[n]
x.size.times{|i|
	e=x[i]
	p1=n-e
	break if p1<0
	if (dp[p1]>0 || p1==0) && ans<dp[p1]+1
		ans=dp[p1]+1
	end
	((i+1)..(x.size-1)).each{|j|
		e2=x[j]
		p2=n-e-e2
		break if p2<0
		if (dp[p2]>0 || p2==0) && ans<dp[p2]+2
			ans=dp[p2]+2
		end
	}
}
if ans<1
	puts -1
else
	puts ans
end
0