• http://ac.nbutoj.com/Problem/view.xhtml?id=1479
  • [1479] How many

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • There are N numbers, no repeat. All numbers is between 1 and 120, and N is no more than 60. then given a number K(1 <= K <= 100). Your task is to find out some given numbers which sum equals to K, and just tell me how many answers totally,it also means find out hwo many combinations at most.
  • 输入
  • There are T test cases. For each case: First line there is a number N, means there are N numbers. Second line there is a number K, means sum is K. Third line there lists N numbers. See range details in describe.
  • 输出
  • Output the number of combinations in total.
  • 样例输入
  • 2
    5
    4
    1,2,3,4,5
    5
    6
    1,2,3,4,5
  • 样例输出
  • 2
    3
  • 01背包||DP代码:
  •  #include <iostream>
    #include <stdio.h>
    #include <math.h>
    #include <string.h> using namespace std;
    int dp[]; int main()
    {
    int t;
    scanf("%d",&t);
    while(t--)
    {
    int n,m;
    scanf("%d%d",&n,&m);
    memset(dp,,sizeof(dp));
    dp[]=;
    int i,j;
    for(i=;i<n;i++)
    {
    int a;
    scanf("%d",&a);
    getchar();
    for(j=m;j>=;j--)
    {
    if(a+j<=m)
    {
    dp[a+j]+=dp[j];
    }
    }
    // for(j=0;j<=m;j++) printf("%d ",dp[j]); putchar(10);
    }
    printf("%d\n",dp[m]);
    }
    return ;
    }

    DFS代码:

     #include <iostream>
    #include <stdio.h>
    #include <string.h> using namespace std; int n,m;
    int a[],mark[];
    int cnt; void dfs(int x,int s){
    if(s==){
    cnt++;
    return;
    }
    int i;
    for(i=x;i<n;i++){
    if(!mark[i]&&s-a[i]>=){
    mark[i]=;
    dfs(i+,s-a[i]);
    mark[i]=;
    }
    }
    } int main()
    {
    int t;
    scanf("%d",&t);
    while(t--){
    scanf("%d%d",&n,&m);
    int i;
    for(i=;i<n;i++){
    scanf("%d",&a[i]);
    getchar();
    }
    memset(mark,,sizeof(mark));
    cnt=;
    dfs(,m);
    printf("%d\n",cnt);
    }
    return ;
    }

noj [1479] How many (01背包||DP||DFS)的更多相关文章

  1. PAT L3-001 凑零钱(01背包dp记录路径)

    韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是 ...

  2. 0-1背包dp|波动数列|2014年蓝桥杯A组10-fishers

    标题:波动数列 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. 栋栋对这种数列很好奇,他想知道长度为 n 和为 s 而且后一项总是比前一项增加a ...

  3. HDU 1203 I NEED A OFFER!(01 背包DP)

    点我看题目 题意 : 中文题不详述. 思路 :类似于01背包的DP,就是放与不放的问题,不过这个要求概率,至少得到一份offer的反面就是一份也得不到,所以先求一份也得不到的概率,用1减掉就可以得到所 ...

  4. (01背包 dp)P1049 装箱问题 洛谷

    题目描述 有一个箱子容量为VV(正整数,0≤V≤20000),同时有nn个物品(0<n≤30,每个物品有一个体积(正整数). 要求nn个物品中,任取若干个装入箱内,使箱子的剩余空间为最小. 输入 ...

  5. HDU 2602 Bone Collector (01背包DP)

    题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...

  6. hiho #1038 : 01背包 (dp)

    #1038 : 01背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励 ...

  7. Bookshelf 2(poj3628,01背包,dp递推)

    题目链接:Bookshelf 2(点击进入) 题目解读: 给n头牛,给出每个牛的高度h[i],给出一个书架的高度b(所有牛的高度相加>书架高度b),现在把一些牛叠起来(每头牛只能用一次,但不同的 ...

  8. 01背包-dp

    一 问题分析 二 代码实现 package Dp_0_1_bag; import java.io.BufferedWriter; import java.io.FileWriter; import j ...

  9. TZOJ 1545 Hurdles of 110m(01背包dp)

    描述 In the year 2008, the 29th Olympic Games will be held in Beijing. This will signify the prosperit ...

随机推荐

  1. 消息系统Flume与Kafka的区别

    首先Flume和Kafka都是消息系统,但是它俩也有着很多不同的地方,Flume更趋向于消息采集系统,而Kafka更趋向于消息缓存系统. [一]设计上的不同 Flume是消息采集系统,它主要解决问题是 ...

  2. ArrayBlockingQueue和LinkedBlockingQueue分析

        JAVA并发包提供三个常用的并发队列实现,分别是:ConcurrentLinkedQueue.LinkedBlockingQueue和ArrayBlockingQueue. Concurren ...

  3. Hadoop MapReduceV2(Yarn) 框架简介

    http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ 对于业界的大数据存储及分布式处理系统来说,Hadoop 是耳熟能详 ...

  4. Android进阶——声波振幅显示

    最近博主想做一个app,中间有一个是录音的功能.于是博主想把UI做的好看一些,想仿照微信或者QQ语音输入时,能够随着声音的大小显示声波振幅.于是查找了一些资料,现在把这个功能的Demo分享给大家,以后 ...

  5. C/C++雷区之内存管理

    C++雷区之内存管理 伟大的Bill Gates 曾经失言: 640K ought to be enough for everybody — Bill Gates 1981 程序员们经常编写内存管理程 ...

  6. HTML——表格table标签,tr或者td

    表格定义和用法 <tr> 标签定义 HTML 表格中的行. tr 元素包含一个或多个 th 或 td 元素. HTML 与 XHTML 之间的差异 在 HTML 4.01 中,tr 元素的 ...

  7. C++例题练习(1)

    环境:Dev-C++( Version:5.6.1) 一.求2个或3个正整数中的最大数,用带有默认参数的函数实现 代码实现: #include <iostream> using names ...

  8. 从零开始部署小型企业级虚拟桌面 -- Vmware Horizon View 6 For Linux VDI -- 结构规划

    环境说明 注,本套环境所用机器全部是64位的. 管理服务器载体:安装win7操作系统,通过VMware Workstation安装4台虚拟机,用作vCenter,Connection Server,D ...

  9. (poj)3020 Antenna Placement 匹配

    题目链接 : http://poj.org/problem?id=3020 Description The Global Aerial Research Centre has been allotte ...

  10. 构造函数为什么不能是虚函数 ( 转载自C/C++程序员之家)

    从存储空间角度,虚函数对应一个指向vtable虚函数表的指针,这大家都知道,可是这个指向vtable的指针其实是存储在对象的内存空间的.问题出来了,如果构造函数是虚的,就需要通过 vtable来调用, ...