Gym - 102861H SBC‘s Hangar

A small cargo plane of the System of Binary Cargos (SBC) was designed to transport special and secret products. These products are grouped in boxes with different weights.

The plane has a safety weight range, within which the aircraft is stable. More specifically, there is an interval such that if the total weight of the boxes carried is not in the interval, then it will not be possible to guarantee the security of the flight.

It is known that all boxes have different weights. Moreover, given any two boxes, the heavier one weighs at least twice as much as the lighter box.

Your task is to determine in how many ways you can choose a specified number of these boxes to be transported on the plane without destabilizing it.

Input

The first line of the input contains two integers, NN and KK, representing respectively the number of available boxes and the specified number of boxes to be loaded in the plane.

The second line of the input contains NN integers, separated by a space, and representing the weights of the boxes.

The third line of the entry contains two integers, AA and BB, which specify the weight safety range, which is the (closed) interval [A,B][A,B].

Consider all weights reported in the same unit.

  • 1≤N≤501≤N≤50
  • 1≤K≤501≤K≤50
  • the weight WW of each box is in the interval 1≤W≤10181≤W≤1018
  • 1≤A≤B≤2×10181≤A≤B≤2×1018

Output

The output consists of a single line containing the number of possible ways to choose the specified number of boxes without jeopardizing the flight.

Examples

input

Copy

3 2
10 1 3
4 13

output

Copy

3

input

Copy

4 3
20 10 50 1
21 81

output

Copy

4

input

Copy

6 3
14 70 3 1 6 31
10 74

output

Copy

11

一道相当经典的贪心题。由于N的规模为50,搜索/剪枝搜索n是难以流畅跑完了,这时就需要想想其他方法。注意到题目的特殊条件,对于其中的任意两个元素,较重的那个至少为较轻的两倍,那么我们很自然的就想到了按照重量进行排序。

设排序后的数组各元素为ai,i=1,2,3,……n,a1

an-1+an-2+……+a1<=an*(1/2+(1/2)^2+(1/2)^3+……+(1/2)^n)=an*(1-(1/2)^n)

即:如果an能够加入物品中且不发生超载,那么其余较an小的任意件物品的重量和必然亦不会超载,那么就有以下关系成立:

若:ak可以放入物品组中:总数量+=C(待选物品个数,剩余位置数),并将物品组的上线减去ak

否则,直接进行下一个操作。

由于对于每一个操作最多仅仅计算了一个组合数,可知这个算法的复杂度为O(n)。

代码(GNU G++14):
 

#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll s[55],h[55],n,sl=0;
ll zhs(ll a,ll b){
	if(a>b) return 0;
	if(a==0) return 1;
	if(b==0) return 1;
	ll i,s=1;
	for(i=b-a+1;i<=b;i++){
		s*=i;
		s/=i-b+a;
	}
	return s;
}
ll slsl(ll wz,ll sy,ll xqs){
	if(wz>=n+1) return 1;
	if(s[wz]>sy){
		if(xqs<=n-wz) return slsl(wz+1,sy,xqs);
	}
	else{
		sl+=zhs(xqs,n-wz);
		if(xqs>1) return slsl(wz+1,sy-s[wz],xqs-1);
		else{
			sl+=1;
			return 0;
		}
	}
}
int main(){
	ll i,a,b,k,zc=0,sl1,sl2;
	scanf("%lld%lld",&n,&k);
	for(i=1;i<=n;i++) scanf("%lld",&s[i]);
	scanf("%lld%lld",&a,&b);
	sort(s+1,s+1+n,greater<ll>());
	sl=0;
	slsl(1,a-1,k);
	sl1=sl;
	sl=0;
	slsl(1,b,k);
	sl2=sl;
	printf("%lld",sl2-sl1);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值