[SGU]116. Index of super-prime

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值