[2019上海网络赛J题]Stone game
2024-10-07 08:39:36
题目链接 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);
}
}
最新文章
- 扩展JQuery和JS的方法
- jQuery对input select操作小结
- POJ1976A Mini Locomotive(01背包装+连续线段长度)
- adaptive hash index
- Linux目录介绍
- 误用ArrayListMultimap引发的问题
- 【原】Scala学习资料
- JavaScript- The Good Parts CHAPTER 2
- WindowListener中的windowClosed方法不执行的问题。
- 在Win32程序中显示Dos调试窗口,可暂停(AllocConsole,WriteConsole,FreeConsole函数,GetStdHandle函数取得输入句柄)
- OpenWRT推理client线上的数
- 【No JSON object could be decoded】问题解决
- 编译make的出错提示解决方案
- Hystix熔断解决雪崩问题
- 如何用kaldi做孤立词识别二
- docker配置阿里云镜像加速
- pojo类自动生成序列化ID
- java 连接数组
- sex在软件开发中的运用--SIX技术
- 分布式ID生成方法-趋势有序的全局唯一ID