1127 - Funny Knapsack
1127 - Funny Knapsack
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 }
油!油!you@