Analysis
这道题目只要先处理出所有的super-prime,然后做一个背包就能得到答案了。
Accepted Code
var
bo:array[1..10000] of boolean;
prime,suprime,f:array[0..10000] of longint;
i,j,k,l,n:longint;
function min(a,b:longint):longint;
begin
if a<b then
min:=a
else
min:=b;
end;
begin
fillchar(bo,sizeof(bo),true);
bo[1]:=false;
for i:=2 to 10000 do
begin
if not bo[i] then
continue;
j:=2;
while i*j<=10000 do
begin
bo[i*j]:=false;
inc(j);
end;
end;
j:=0;
for i:=1 to 10000 do
if bo[i] then
begin
inc(j);
prime[j]:=i;
end;
k:=0;
for l:=1 to j do
begin
if prime[l]>j then
break;
inc(k);
suprime[k]:=prime[prime[l]];
end;
readln(n);
for i:=1 to n do
f[i]:=maxlongint shr 1;
f[0]:=0;
for i:=1 to k do
for j:=suprime[i] to n do
f[j]:=min(f[j],f[j-suprime[i]]+1);
if f[n]<>maxlongint shr 1 then
begin
writeln(f[n]);
i:=k;
repeat
while (n<suprime[i]) or (f[n-suprime[i]]<>f[n]-1) do
dec(i);
if n-suprime[i]=0 then
writeln(suprime[i])
else
write(suprime[i],' ');
n:=n-suprime[i];
until n=0;
end
else
writeln(0);
end.