UVA 1213 Sum of Different Primes(经典dp)

题意:选择k(k<15)个唯一质数,求出和为n(n<1121)的可能数

题解:预处理dp,dp[k][n]表示使用k个素数拼成n的总方案数

   就是三重枚举,枚举k,枚举n,枚举小于n的素数

   但是注意三重循环的顺序与位置,我们要防重防漏

   第一重循环是枚举每个小于n的素数,思路是对于每个素数放入dp里面的位置

   第二重倒叙枚举每个数n,倒序是类似01背包不能让枚举的素数重复加入同一个dp数组中

   第三重正序枚举个数k只能放在最里面,这样才不会出现重复

import java.util.Scanner;

public class Main{

    static int Max = 1200;
    static int Maxk = 15;
    static long[][] dp = new long[Maxk][Max];
    static int[] vis = new int[Max];
    static int[] prm = new int[Max];//存所有素数
    static int coun;

    static {
        for (int i = 2; i < Max; ++i) {
            if (vis[i] == 0) {
                for (int j = i + i; j < Max; j += i) {
                    vis[j] = 1;
                }
            }
        }
        for (int i = 2; i < Max; ++i) {
            if (vis[i] == 0) {
                prm[coun++] = i;
            }
        }
    }

    private static void Init(int n) {
        dp[0][0]=1;
        // 预处理dp
        //注意三重循环位置,用于去重
        for (int k = 0; k < coun; ++k) {//枚举每个素数
            for (int j = Max-1; j >=prm[k]; --j) {//倒叙枚举每个数,类似01背包去重
                for (int i = 1; i < Maxk; ++i) {//枚举个数
                        dp[i][j] += dp[i - 1][j - prm[k]];
                }
            }
        }
    }

    public static void main(String[] args) {
        Init(Max);
        int n, k;
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            n = sc.nextInt();
            k = sc.nextInt();
            if (n + k == 0)
                break;
            System.out.println(dp[k][n]);
        }
    }

}
时间: 2024-11-08 18:13:46

UVA 1213 Sum of Different Primes(经典dp)的相关文章

UVA 1213 - Sum of Different Primes

类似一个背包问题的计数问题.(虽然我也不记得这叫什么背包了 一开始我想的状态定义是:f[n = 和为n][k 个素数]. 递推式呼之欲出: f[n][k] = sigma f[n-pi][k-1]. 但是题目还有一个要求是不同素数之和,为了保证素数不同,那就先枚举素数吧, f[i][n][k] = sigma  f[i-1][n-p][k-1], 然后滚动数组降掉一维就好了. 本地筛一遍发现maxn 范围内素数数量是pi_n = 187. maxn*maxk*pi_n ≍ 1e6. 复杂度也是没

UVA1213 Sum of Different Primes(素数打表+dp)

UVA - 1213 Sum of Different Primes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two

POJ 3132 Sum of Different Primes DP背包

http://poj.org/problem?id=3132 题意: 给定n和k,问用恰好k个不同的质数来表示n的方案数. 分析: n和k都很小.反正就是个背包,选k个物品恰好填满n即可. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 bool dp[1200][15]; 6 int ct[1200][15]; 7 int p[1200]; 8 bool a[1200]; 9 int n, k,

POJ 3132 &amp; ZOJ 2822 Sum of Different Primes(dp)

题目链接: POJ:http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2822 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive int

POJ 1160:Post Office 邮局经典DP

Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17168   Accepted: 9270 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each villa

poj3132 Sum of Different Primes

Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3293   Accepted: 2052 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integer

UVA 1362 - Exploring Pyramids(计数问题+区间DP)

题目链接:1362 - Exploring Pyramids 白书上的例题,思路是对于每个结点,往后遍历分为左右两边子树来考虑,左边的子树为去掉根节点剩下的子树,而右边是要算上根节点的,这样就不会有重复的情况出现,然后根据乘法原理,左右两边情况相乘为总情况数,然后计算这些总和. f[i][j]表示[i,j]结点的情况种数,那么 f[i][j] = sum{f[i + 1][k - 1] * f[k][j]},并且s[i] == s[k] == s[j],因为i, j, k表示的是根节点必须相同.

POJ 3132 Sum of Different Primes ( 满背包问题)

Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3280   Accepted: 2040 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integer

uva 11825 Hackers&#39; Crackdown(状态压缩DP)

Hackers' Crackdown Input: Standard Input Output: Standard Output   Miracle Corporations has a number of system services running in a distributed computer system which is a prime target for hackers. The system is basically a set of N computer nodes wi