SUBSUMS - Subset Sums

Given a sequence of N (1 ≤ N ≤ 34) numbers S1, ..., SN (-20,000,000 ≤ Si ≤ 20,000,000), determine how many subsets of S (including the empty one) have a sum between A and B (-500,000,000 ≤ A ≤ B ≤ 500,000,000), inclusive.

Input

The first line of standard input contains the three integers N, A, and B. The following N lines contain S1 through SN, in order.

Output

Print a single integer to standard output representing the number of subsets satisfying the above property. Note that the answer may overflow a 32-bit integer.

Example

Input:

3 -1 2

1

-2

3

Output:

5

The following 5 subsets have a sum between -1 and 2:

0 = 0 (the empty subset)

1 = 1

1 + (-2) = -1

-2 + 3 = 1

1 + (-2) + 3 = 2

Submit solution!

思路:折半枚举+二分;

复杂度O(\(2^ \frac{n}{2}*log(2^ \frac{n}{2})\))

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<string.h>
#include<map>
typedef long long LL;
using namespace std;
int ans[50];
int aa[20],bb[20];
int a1[200000],a2[200000];
int low(int l,int r,int ask);
int high(int l,int r,int ask);
int main(void)
{
int n,a,b;
scanf("%d %d %d",&n,&a,&b);
for(int i = 1; i <= n; i++)
{
scanf("%d",&ans[i]);
}
int cn = 0;
for(int i = 1; i <= (n/2); i++)
{
aa[cn++] = ans[i];
}
cn = 0;
for(int i = n/2+1; i <= n; i++)
{
bb[cn++] = ans[i];
}
int x1 = n/2,x2 = n-x1;
int cx1 = 0;
for(int i = 0; i < (1<<x1); i++)
{
int sum = 0;
for(int j = 0; j < x1; j++)
{
if(i&(1<<j))
sum+= aa[j];
}
a1[cx1++] = sum;
}
int cx2 = 0;
for(int i = 0; i < (1<<x2); i++)
{
int sum = 0;
for(int j = 0; j < x2; j++)
{
if(i&(1<<j))
sum += bb[j];
}
a2[cx2++] = sum;
}
sort(a1,a1+cx1);
sort(a2,a2+cx2);
LL acc = 0;
for(int i = 0; i < cx1; i++)
{
int asl = a-a1[i];
int asr = b-a1[i];
int ll = low(0,cx2-1,asl);
int rr = high(0,cx2-1,asr);
if(rr >= ll&&ll!=-1&&rr!=-1)
{
acc += (LL)(rr-ll+1);
}
}
printf("%lld\n",acc);
return 0;
}
int low(int l,int r,int ask)
{
int id = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(a2[mid] >= ask)
{
id = mid;
r = mid-1;
}
else l = mid+1;
}
return id;
}
int high(int l,int r,int ask)
{
int id = -1;
while(l <= r)
{
int mid = (l+r)/2;
if(a2[mid] <= ask)
{
id = mid;
l = mid+1;
}
else r = mid-1;
}
return id;
}

spoj-SUBSUMS - Subset Sums的更多相关文章

  1. 洛谷P1466 集合 Subset Sums

    P1466 集合 Subset Sums 162通过 308提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 对于从1到N (1 ...

  2. Project Euler 106:Special subset sums: meta-testing 特殊的子集和:元检验

    Special subset sums: meta-testing Let S(A) represent the sum of elements in set A of size n. We shal ...

  3. Project Euler P105:Special subset sums: testing 特殊的子集和 检验

    Special subset sums: testing Let S(A) represent the sum of elements in set A of size n. We shall cal ...

  4. Project Euler 103:Special subset sums: optimum 特殊的子集和:最优解

    Special subset sums: optimum Let S(A) represent the sum of elements in set A of size n. We shall cal ...

  5. Codeforces348C - Subset Sums

    Portal Description 给出长度为\(n(n\leq10^5)\)的序列\(\{a_n\}\)以及\(m(m\leq10^5)\)个下标集合\(\{S_m\}(\sum|S_i|\leq ...

  6. CodeForces 348C Subset Sums(分块)(nsqrtn)

    C. Subset Sums time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  7. DP | Luogu P1466 集合 Subset Sums

    题面:P1466 集合 Subset Sums 题解: dpsum=N*(N+1)/2;模型转化为求选若干个数,填满sum/2的空间的方案数,就是背包啦显然如果sum%2!=0是没有答案的,就特判掉F ...

  8. SPOJ TSUM Triple Sums(FFT + 容斥)

    题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...

  9. 洛谷 P1466 集合 Subset Sums Label:DP

    题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...

随机推荐

  1. Python查找最长回文暴力方法

    查找最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 例如1: 输入: "babad" 输出: "bab" ...

  2. Pyquery解析库的安装和使用

    Pyquery同样是一个强大的网页解析工具,它提供了和jQuery类似的语法来解析HTML文档,支持CSS选择器,使用非常方便.GitHub:https://github.com/gawel/pyqu ...

  3. C#集合Dictionary中按值的排序

    C#集合Dictionary中按值的降序排列 static void Main(string[] args) {             Dictionary<string, int> d ...

  4. 同步阻塞IO模型

    同步阻塞IO模型 有上篇IO模型中的,同步阻塞IO模型,我们能够知道,用户线程发起请求后就一直阻塞的等待 内核完成准备数据.数据拷贝的工作.并且返回成功的指示. 实现 使用java来实现同步阻塞IO模 ...

  5. C++自定义字符串类

    //header.h #ifndef _HEADER_H #define _HEADER_H #define defaultSize 128 #include<iostream> #inc ...

  6. zabbix之模板制作(memcache redis)

    #:找一台主机安装redis和memcached(记得安装zabbix-agent) root@ubuntu:~# apt install redis root@ubuntu:~# apt insta ...

  7. 【JAVA】【JVM】内存结构

    虽然jvm帮我们做了内存管理的工作,但是我们仍需要了解jvm到底做了什么,下面我们就一起去看一看 jvm启动时进行一系列的工作,其中一项就是开辟一块运行时内存.而这一块内存中又分为了五大区域,分别用于 ...

  8. 【Linux】【Shell】【Basic】数组

    1. 数组:         变量:存储单个元素的内存空间:         数组:存储多个元素的连续的内存空间:             数组名:整个数组只有一个名字:             数组 ...

  9. Thymeleaf+layui+jquery复选框回显

    一.Thymeleaf+layui+jquery复选框回显 基于Thymeleaf模板下的layui+jquery复选框回显,主要是jquery.大致意思是:把数组转成JSON传到前台,再在前台转回数 ...

  10. 【Windows】github无法访问/hosts文件只能另存为txt

    因为我的github访问不了了,搜索解决方案为修改host文件 https://blog.csdn.net/curry10086/article/details/106800184/ 在hosts文件 ...