zoj 2414 - Index of Prime

题目:判断一个数能不能写成素数的和的形式,输出对应的素数大小最小组合。

分析:dp,多重背包。看到整数拆分就是背包了。

由于时间和数据的限制,所以采用打表计算;

每次记录上次使用的 prime然后逆向求解即可。

说明:注意,没有时输出 0,由于没写 WA了好几次。。。(2011-10-03 18:37)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define OO 10001

bool used[ 10001 ];
int  prime[ 1231 ];
int madelist()
{
    memset( used, 0, sizeof( used ) );
    int count = 0;
    for ( int i = 2 ; i < 10000 ; ++ i )
        if ( !used[ i ] ) {
            prime[ ++ count ] = i;
            for ( int j = i<<1 ; j < 10000 ; j += i )
                used[ j ] = 1;
        }
    return count;
}

int f[ 10001 ];
int t[ 10001 ];

int main()
{
    int C = madelist();

    for ( int i = 0 ; i <= 10000 ; ++ i ) {
        f[ i ] = OO;
        t[ i ] = 0;
    }
    f[ 0 ] = 0;

    for ( int i = 1 ; i <= C ; ++ i )
    for ( int j = prime[ i ] ; j <= 10000 ; ++ j )
        if ( f[ j ] > f[ j-prime[ i ] ]+1 ) {
            f[ j ] = f[ j-prime[ i ] ]+1;
            t[ j ] = prime[ i ];
        }

    int n;
    while ( ~scanf("%d",&n) ) {
        if ( t[ n ] ) {
            printf("%d\n%d",f[ n ],t[ n ]);
            int v = n-t[ n ];
            while ( t[ v ] ) {
                printf(" %d",t[ v ]);
                v = v-t[ v ];
            }
        }else printf("0");
        printf("\n");
    }
    return 0;
}
时间: 2024-10-17 20:44:18

zoj 2414 - Index of Prime的相关文章

poj 3518 Prime Gap

Prime Gap 1 #include<cmath> 2 #include<cstdio> 3 #include<algorithm> 4 #include<iostream> 5 #include<string> 6 #include<cstring> 7 #include<vector> 8 using namespace std; 9 #define max 100000 10 int prime[max+5];

HDU 1010 Prime Ring Problem

A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1. Input n (0 < n <

poj 1365 Prime Land 质因数分解

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int primeNUM[] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,

Tree

Tree 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creations: D

hdu 1164 Eddy&#39;s research I

1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 using namespace std; 8 #define MAX 65535 9 int prime[MAX+5]; 10 bool vis[MAX+5]; 11 int index=0; 1

LeetCode(313):Super Ugly Number

Super Ugly Number: Write a program to find the nth super ugly number.Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequenc

hihocoder offer收割编程练习赛12 A 歌德巴赫猜想

思路: 枚举. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAX_N = 1000005; 7 8 int prime[MAX_N]; 9 bool is_prime[MAX_N + 1]; 10 11 int init(int n) 12 { 13 int p = 0; 14 for (int i =

求最大公约数的两种解法(欧几里得算法和素数分解)

最大公约数的两种解法(欧几里得算法和素数分解) 方法一: 欧几里得算法,又称辗转相除法 定理(欧几里得算法):设a和b是正整数,则存在最大求最大公因子d=(a,b)的一种算法,且存在求一组整数s,t使得d = sa+tb 举个例子:求168和60的最大公约数? 168 = 2 * 60 + 48 60  = 1 * 48 +12 48  = 4 * 12 由此得最大公约数为12 关于最大公倍数 C语言程序代码:很简单就不加注释了 #include<stdio.h> #define SWAP(a

查找素数Eratosthenes筛法的mpi程序

思路: 只保留奇数 (1)由输入的整数n确定存储奇数(不包括1)的数组大小: n=(n%2==0)?(n/2-1):((n-1)/2);//n为存储奇数的数组大小,不包括基数1 (2)由数组大小n.进程号id和进程数p,确定每个进程负责的基数数组的第一个数.最后一个数和数组维度: low_value = 3 + 2*(id*(n)/p);//进程的第一个数 high_value = 3 + 2*((id+1)*(n)/p-1);//进程的最后一个数 size = (high_value - lo