题目链接 CSLnb!

题意是求出给定集合中有多少个合法子集,合法子集的定义为,子集和>=总和-子集和$\& \&$子集和-(子集的子集和)<=总和-子集和。

其实就是很简单的dp,先将集合从大到小排序,dp[i][j]表示以a[i]为子集的最小值时,子集和为j的方案数。因为排序后保证遍历到的a[i]一定为当前最小值,所以暴力统计转移即可。

最后在统计一遍合法答案。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + ;
int a[];
int dp[][];
int sm[];
int add(int a, int b) {
a += b;
if (a >= mod)
a -= mod;
return a;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, sum = ;
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]), sum += a[i];
sort(a + , a + + n, greater<int>());
dp[][] = ;
sm[] = ;
for (int i = ; i <= n; i++) {
for (int j = sum; j >= a[i]; j--) {
dp[i][j] = sm[j - a[i]];
sm[j] = add(sm[j], dp[i][j]);
}
}
int ans = ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= sum; j++) {
if (j >= sum - j && j - a[i] <= sum - j)
ans = add(ans, dp[i][j]);
dp[i][j] = ;
sm[j] = ;
}
}
printf("%d\n", ans);
}
}

最新文章

  1. 扩展JQuery和JS的方法
  2. jQuery对input select操作小结
  3. POJ1976A Mini Locomotive(01背包装+连续线段长度)
  4. adaptive hash index
  5. Linux目录介绍
  6. 误用ArrayListMultimap引发的问题
  7. 【原】Scala学习资料
  8. JavaScript- The Good Parts CHAPTER 2
  9. WindowListener中的windowClosed方法不执行的问题。
  10. 在Win32程序中显示Dos调试窗口,可暂停(AllocConsole,WriteConsole,FreeConsole函数,GetStdHandle函数取得输入句柄)
  11. OpenWRT推理client线上的数
  12. 【No JSON object could be decoded】问题解决
  13. 编译make的出错提示解决方案
  14. Hystix熔断解决雪崩问题
  15. 如何用kaldi做孤立词识别二
  16. docker配置阿里云镜像加速
  17. pojo类自动生成序列化ID
  18. java 连接数组
  19. sex在软件开发中的运用--SIX技术
  20. 分布式ID生成方法-趋势有序的全局唯一ID

热门文章

  1. 获取树莓派ip地址的方法
  2. 对Canvas的研究
  3. 虚拟机使用桥接模式连接网络并且设置静态ip
  4. 使用webuploader组件实现大文件分片上传,断点续传
  5. BZOJ 5004: 开锁魔法II 期望 + 组合
  6. 容器适配器————stack
  7. Ajax异步提交的步骤
  8. qtp识别验证码
  9. [LeetCode]-algorithms-Longest Substring Without Repeating Characters
  10. Yahoo 军规(部分)