zoukankan      html  css  js  c++  java
  • 多重部分和 poj1742

    Description

    People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
    You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

    Input

    The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.

    Output

    For each test case output the answer on a single line.

    Sample Input

    3 10
    1 2 4 2 1 1
    2 5
    1 4 2 1
    0 0
    

    Sample Output

    8
    4
    
    多重部分和问题:
    有n中大小不同的数字,每种c[i]个,判断这些数字之中能否选出若干个使其和为K
    此题是让求K<=m时,有多少个解

    一个一般性的代码如下
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 int n, m;
     9 int a[102];
    10 int c[102];
    11 int dp[102][100002];
    12 
    13 int main(int argc, char const *argv[])
    14 {
    15     //freopen("input.txt","r",stdin);
    16     while(scanf("%d %d",&n,&m) != EOF && (n != 0 && m != 0)) {
    17         for(int i = 0; i < n; i++) {
    18             scanf("%d",&a[i]);
    19         }
    20         for(int i = 0; i < n; i++) {
    21             scanf("%d",&c[i]);
    22         }
    23         memset(dp, 0, sizeof(dp));
    24         dp[0][0] = 1;
    25         for(int i = 0; i < n; i++) {
    26             for(int j = 0; j <= m; j++) {
    27                 for(int k = 0; k <= c[i] && k * a[i] <= j;k++) {
    28                     dp[i+1][j] = dp[i+1][j]| dp[i][j-k*a[i]];
    29                 }
    30             }
    31         }
    32         int ans = 0;
    33         for(int i = 1; i <= m; i++) {
    34             if(dp[n][i] > 0) {
    35                 ans++;
    36             }
    37         }
    38         printf("%d
    ",ans);
    39     }
    40     return 0;
    41 }

    若用dp[i+1][j]表示用前i个数相加和为j时第i种数最多能剩余几个(不能得到和为-1)

    可得代码如下

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 int n, m;
     9 int a[102];
    10 int c[102];
    11 int dp[100002];
    12 
    13 int main(int argc, char const *argv[])
    14 {
    15     //freopen("input.txt","r",stdin);
    16     while(scanf("%d %d",&n,&m) != EOF && (n != 0 && m != 0)) {
    17         for(int i = 0; i < n; i++) {
    18             scanf("%d",&a[i]);
    19         }
    20         for(int i = 0; i < n; i++) {
    21             scanf("%d",&c[i]);
    22         }
    23         memset(dp, -1, sizeof(dp));
    24         dp[0] = 0;
    25         for(int i = 0; i < n; i++) {
    26             for(int j = 0; j <= m; j++) {
    27                 if(dp[j] >= 0) {
    28                     dp[j] = c[i];
    29                 }
    30                 else if(j < a[i] || dp[j - a[i]] <= 0) {
    31                     dp[j] = -1;
    32                 }
    33                 else {
    34                     dp[j] = dp[j - a[i]] - 1;
    35                 }
    36             }
    37         }
    38         int ans = 0;
    39         for(int i = 1; i <= m; i++) {
    40             if(dp[i] >= 0) {
    41                 ans++;
    42             }
    43         }
    44         printf("%d
    ",ans);
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    分区硬盘Lvm 折腾小记
    添加源ubuntu_x64 安装 Adobe Reader
    停止标记NYOJ 一个简单的数学题 南工330停止标记
    读控制台HDU 1788 Chinese remainder theorem again 数论读控制台
    对象方法PHP中魔术方法的用法对象方法
    指针修饰C语言const修饰符探秘指针修饰
    输入整数角谷步数 你听说过角谷猜想吗? 任意的正整数,比如 5, 我们从它开始,如下规则计算: 如果是偶数,则除以2,如果是奇数,则乘以3再加1. 如此循环,最终必会得到“1” !输入整数
    根节点左边POJ 1456 Supermarket根节点左边
    代码功能【OpenGL】初识OpenGL4.0代码功能
    终点节点NYOJ115 城市平乱终点节点
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5790170.html
Copyright © 2011-2022 走看看