zoukankan      html  css  js  c++  java
  • Index of super-prime

    题目大意:素数表2,3,5,7,11.....如果一个素数所在的位置还是素数,那么这个素数就是超级素数,比如3在第2位置,那么3就是超级素数.....现在给你一个数,求出来这个数由最少的超级素数的和组成,输出这个超级素数。

    分析:因为给的数字并不大,所以直接用完全背包求出来即可。

    代码如下:

    =================================================================================================================================

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    const int MAXN = 10007;
    const int oo = 1e9+7;
    
    int sup[MAXN], cnt;
    int dp[MAXN];
    
    void superPrime()
    {
        bool used[MAXN]={1,1};
        cnt = 0;
    
        for(int k=0,i=2; i<MAXN; i++)
        {
            if(!used[i])
            {
                k++;
                if(used[k] == 0)
                    sup[++cnt] = i;
                for(int j=i+i; j<MAXN; j+=i)
                    used[j] = true;
            }
        }
    }
    
    int main()
    {
        superPrime();
    
        int N, from[MAXN];
    
        scanf("%d", &N);
    
        for(int i=1; i<=N; i++)
            dp[i] = oo;
    
        for(int i=1; i<=cnt; i++)
        for(int j=sup[i]; j<=N; j++)
        {
            if(dp[j-sup[i]]+1 < dp[j])
            {
                dp[j] = dp[j-sup[i]]+1;
                from[j] = j-sup[i];
            }
        }
    
        if(dp[N] == oo)
            printf("0
    ");
        else
        {
            printf("%d
    ", dp[N]);
            for(int i=N; i!=0; i=from[i])
            {
                printf("%d%c", i-from[i], from[i]?' ':'
    ');
            }
        }
    
        return 0;
    }
  • 相关阅读:
    回调函数设计方法
    C 时间函数总结
    linux多线程全面解析
    从为知笔记收费说起
    C++中strftime()的详细说明
    arguments.callee
    arguments 对象
    学习闭包
    this的call,apply,bind的方法总结--追梦子
    this指向--取自追梦子的文章
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4817412.html
Copyright © 2011-2022 走看看