现在的位置: 首页 > 综合 > 正文

lightoj 1127 Funny Knapsack

2018年02月21日 ⁄ 综合 ⁄ 共 1137字 ⁄ 字号 评论关闭
1127 - Funny Knapsack

Given n integers and a knapsack of weight W,you have to count the number of combinations for which you can add the items inthe 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 linewill containn integers separated by spaces. The integers will be nonnegative and less than109.

Output

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

Sample input

3

1 1

1

1 1

2

3 10

1 2 4

Sample Output

Case 1: 2

Case 2: 1

Case 3: 8

有n(<=30)个整数和一个数W(<=2e9)。问每个数之多用一次并且总和不超过W的情况下有多少种组合数。

所有的情况有2^30种,必然TLE。

将n个数分成两份,在每一份里面统计用这些数能组成多少种整数。然后两种一综合就是答案。

ll a[maxn];
ll b[maxn];
ll t[maxn];
int main(){
	int T;

	cin>>T;
	for(int tt=1;tt<=T;tt++){
		memset(t,0,sizeof(t));
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int n;
		ll w;
		scanf("%d%lld",&n,&w);
		int ta=n>>1;
		int tb=n-ta;
		int l=1<<ta;
		int r=1<<tb;
		for(int i=0;i<n;i++)
			scanf("%lld",&t[i]);

		for(int i=0;i<l;i++){
			for(int j=0;j<ta;j++){
				if(i&(1<<j))
					a[i]+=t[j];
			}
		}
		for(int i=0;i<r;i++){
			for(int j=0;j<tb;j++){
				if(i&(1<<j))
					b[i]+=t[ta+j];
			}
		}
		ll ans=0;
		sort(b,b+r);
		for(int i=0;i<l;i++){
			if(w-a[i]>=0){
				ans+=upper_bound(b,b+r,w-a[i])-b;
			}
		}
		printf("Case %d: %lld\n",tt,ans);
	}
	return 0;
}

抱歉!评论已关闭.