1127 - Funny Knapsack

   PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

Given n integers and a knapsack of weight W, you have to count the number of combinations for which you can add the items in the knapsack without overflowing the weight.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 30) and W (1 ≤ W ≤ 2 * 109) and the next line will contain n integers separated by spaces. The integers will be non negative and less than 109.

Output

For each set of input, print the case number and the number of possible combinations.

Sample Input

Output for Sample Input


3

1 1

1

1 1

2

3 10

1 2 4


Case 1: 2

Case 2: 1

Case 3: 8

思路:一个超大背包问题,用折半枚举然后二分查找;

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<queue>
 6 #include<stack>
 7 #include<set>
 8 #include<math.h>
 9 using namespace std;
10 typedef long long LL;
11 LL ans[100];
12 LL ak1[40000];
13 LL ak2[40000];
14 LL bk1[50];
15 LL bk2[50];
16 int main(void)
17 {
18     int i,j,k;
19     scanf("%d",&k);
20     int s;
21     int n;
22     LL m;
23     for(s=1; s<=k; s++)
24     {
25         scanf("%d %lld",&n,&m);
26         for(i=0; i<n; i++)
27         {
28             scanf("%lld",&ans[i]);
29         }
30         for(i=0; i<(n/2); i++)
31         {
32             bk1[i]=ans[i];
33         }
34         for(j=0; i<n; j++,i++)
35         {
36             bk2[j]=ans[i];
37         }
38         int n1=(n/2);
39         int n2=n-n1;
40         for(i=0; i<=(1<<n1)-1; i++)
41         {
42             LL sum=0;
43             for(j=0; j<n1; j++)
44             {
45                 if(i&(1<<j))
46                 {
47                     sum+=bk1[j];
48                 }
49             }
50             ak1[i]=sum;
51         }
52         int num=(1<<n2)-1;
53         for(i=0; i<=(1<<n2)-1; i++)
54         {
55             LL sum=0;
56             for(j=0; j<n2; j++)
57             {
58                 if(i&(1<<j))
59                     sum+=bk2[j];
60             }
61             ak2[i]=sum;
62         }
63         sort(ak2,ak2+num);
64         LL sum=0;
65         for(i=0; i<(1<<n1); i++)
66         {
67             int l=0;
68             int r=(1<<n2)-1;
69             LL ask=m-ak1[i];
70             if(ask>=0)
71             {
72                 int cc=-1;
73                 while(l<=r)
74                 {
75                     int mid=(l+r)/2;
76                     if(ak2[mid]<=ask)
77                     {
78                         cc=mid;
79                         l=mid+1;
80                     }
81                     else r=mid-1;
82                 }
83                 sum+=(cc+1);
84
85             }
86         }
87         printf("Case %d: %lld\n",s,sum);
88     }
89     return 0;
90 }
时间: 2024-08-05 11:08:57

1127 - Funny Knapsack的相关文章

Lightoj 1127 - Funny Knapsack 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1127 题意:有n个物体(n<30)和一个容量为W的容器,问将容器不装满的放置物品的方式有多少种. 思路 : 状态压缩+二分.将前n/2个物体看做一个整体,将剩下的看做一个整体.1<<(n/2)个状态代表前一半的物品使用情况,然后求出每一种状态的总的体积.排序.对于后面的那一半也是.答案只需枚举一半然后在另一半中找和W差的下界即可. 代码: #include <s

1127: 零起点学算法34——继续求多项式

1127: 零起点学算法34--继续求多项式 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 3481  Accepted: 1985[Submit][Status][Web Board] Description 输入1个正整数n, 计算1+(1+2)+(1+2+3)+...+(1+2+3+...+n) Input 输入正整数n(多组数据) Output 输出1+(1+2)+(1+2+3)+...+

hihoCoder #1127 : 二分图二&#183;二分图最小点覆盖和最大独立集

#1127 : 二分图二·二分图最小点覆盖和最大独立集 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上次安排完相亲之后又过了挺长时间,大家好像都差不多见过面了.不过相亲这个事不是说那么容易的,所以Nettle的姑姑打算收集一下之前的情况并再安排一次相亲.所以现在摆在Nettle面前的有2个问题: 1.姑姑想要了解之前所有相亲的情况.对于任一个一次相亲,只要跟参与相亲的两人交流就可以得到这次相亲的情况.如果一个人

Greedy for Fractional Knapsack

部分背包(Fractional Knapsack) 按性价比排好序列 一个一个往进去装直到最后一个不能全装进去计算比例 JAVA public class FractionalKnapsack { public static float[] GREEDY_KNAPSACK(int[] w,int W,int[] v) { int i; int n=w.length;//总共有多少个物品,用w.length/v.length都可以 float[] x=new float[n]; for(i=0;i

FZU 2214 ——Knapsack problem——————【01背包的超大背包】

2214 Knapsack problem Accept: 6    Submit: 9Time Limit: 3000 mSec    Memory Limit : 32768 KB  Problem Description Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total

51Nod - 1127 最短的包含字符串

51Nod - 1127 最短的包含字符串 给出一个字符串,求该字符串的一个子串S,S包含A-Z中的全部字母,并且S是所有符合条件的子串中最短的,输出S的长度.如果给出的字符串中并不包括A-Z中的全部字母,则输出No Solution. Input 第1行,1个字符串.字符串的长度 <= 100000. Output 输出包含A-Z的最短子串长度.如果没有符合条件的子串,则输出No Solution. Input示例 BVCABCDEFFGHIJKLMMNOPQRSTUVWXZYZZ Outpu

题目1127:简单密码

题目描述: Julius Caesar曾经使用过一种很简单的密码. 对于明文中的每个字符,将它用它字母表中后5位对应的字符来代替,这样就得到了密文. 比如字符A用F来代替.如下是密文和明文中字符的对应关系. 密文 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 明文 V W X Y Z A B C D E F G H I J K L M N O P Q R S T U 你的任务是对给定的密文进行解密得到明文. 你需要注意的是,密文中出现的字

TYVJ 1127 计算细胞数(DFS)

计算细胞数 From 陈超锐 描述 Description 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数. 如:阵列 0234500067 1034560500 2045600671 0000000089  有4个细胞 输入格式 InputFormat 第一行 :两个数字M N (1<=M<=50 1<=N<80)表示该阵列有M行N列  从第2行到第M+1行 每行有连续的N个字符 输出格式 Outp

1127 - 咸鱼文章

Time Limit:1s Memory Limit:128MByte Submissions:488Solved:200 DESCRIPTION elttiL moT nwod eht teerts sllac ruo god " ehT peek god " . piZ si a peehs god . tuB nehw moT seirt ot yas " peeS " , ti semoc tuo " peek " . dnA ni a