結果
| 問題 |
No.458 異なる素数の和
|
| ユーザー |
horiesiniti
|
| 提出日時 | 2018-04-03 07:28:21 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 1,866 ms / 2,000 ms |
| コード長 | 607 bytes |
| コンパイル時間 | 47 ms |
| コンパイル使用メモリ | 7,552 KB |
| 実行使用メモリ | 12,672 KB |
| 最終ジャッジ日時 | 2024-06-26 07:05:42 |
| 合計ジャッジ時間 | 13,973 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
コンパイルメッセージ
Main.rb:42: warning: ambiguous first argument; put parentheses or a space even after `-' operator Syntax OK
ソースコード
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
horiesiniti