HDU 4945 2048 DP 组合
思路:
这个题写了一个背包的解法,超时了。搜了下题解才发现我根本不会做。
思路参见这个:
其实我们可以这样来考虑,求补集,用全集减掉不能组成2048的集合就是答案了。
因为只要达到2048就可以了,所以求补集会大大减小枚举的次数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; typedef __int64 ll; const int INF = <<;
const int MAXN = 1e5+;
const int MAXM = ;
const ll MOD = ; int cnt[MAXM];
ll dp[][MAXM];
ll fact[MAXN], inv[MAXN];
int n; ll myPow(ll x, int d) {
ll res = ;
while (d>) {
if (d&) res = (res*x)%MOD;
x = (x*x)%MOD;
d >>= ;
}
return res;
} inline ll C(int x, int y) {
return ((fact[x]*inv[y])%MOD * inv[x-y])%MOD;
} inline int lowBit(int x) {
return x&(-x);
} void solve() {
int sum = n;
for (int i = ; i < ; i++) sum -= cnt[<<i]; memset(dp, , sizeof(dp)); for (int j = min(, cnt[]); j >= ; j--) dp[][j] = C(cnt[], j); for (int i = ; i < ; i++) {
int cc = >>i;
for (int k = min(cc, cnt[<<i]); k >= ; k--) { //选k个
ll CC = C(cnt[<<i], k);
for (int j = k; j <= cc; j++) { //
dp[i][j] = (dp[i][j] + (((dp[i-][(j-k)<<]+dp[i-][(j-k)<<|])%MOD)*CC)%MOD )%MOD;
}
}
} ll ans = ((myPow(, n-sum)-dp[][])%MOD+MOD)%MOD;
ans = (ans*myPow(, sum))%MOD;
printf("%I64d\n", ans);
} int main() {
#ifdef Phantom01
freopen("HDU4945.txt", "r", stdin);
#endif //Phantom01 fact[] = ;
for (int i = ; i < MAXN; i++) fact[i] = (fact[i-]*i)%MOD;
inv[MAXN-] = myPow(fact[MAXN-], MOD-);
for (int i = MAXN-; i > ; i--) inv[i-] = (inv[i]*i)%MOD; int T = ; while (scanf("%d", &n)!=EOF && n!=) {
printf("Case #%d: ", T++);
memset(cnt, , sizeof(cnt));
int x;
for (int i = ; i < n; i++) {
scanf("%d", &x);
cnt[x]++;
}
solve();
} return ;
}
HDU 4945 2048 DP 组合的更多相关文章
- HDU 4945 2048(DP)
HDU 4945 2048 题目链接 题意:给定一个序列,求有多少个子序列能合成2048 思路:把2,4,8..2048这些数字拿出来考虑就能够了,其它数字不管怎样都不能參与组成.那么在这些数字基础上 ...
- hdu 4945 2048 (dp+组合的数目)
2048 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- HDU 4945 2048(dp)
题意:给n(n<=100,000)个数,0<=a[i]<=2048 .一个好的集合要满足,集合内的数可以根据2048的合并规则合并成2048 .输出好的集合的个数%998244353 ...
- HDU 4945 (dp+组合数学)
2048 Problem Description Teacher Mai is addicted to game 2048. But finally he finds it's too hard to ...
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
- hdu 4507 数位dp(求和,求平方和)
http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...
- hdu 3709 数字dp(小思)
http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...
- hdu 4352 数位dp + 状态压缩
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4283 区间dp
You Are the One Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- (fields.E304) Reverse accessor for 'UserProfile.groups' clashes with reverse accessor for 'User.groups'.
创建数据库models.py,在进行数据迁移时抛出一下异常: E:\Project\GuoJia>python manage.py makemigrations SystemCheckError ...
- C#追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件
C#追加文件 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); sw ...
- Tensorflow学习笔记----基础(3)
目录: 一.TensorFlow的系统架构 二.TensorFlow的设计理念 三.TensorFlow的运行流程 四.TensorFlow的编程模型:边.节点.图.设备.变量.变量初始化.内核 五. ...
- 小程序--wepy省市区三级联动选择
产品老哥对项目再一次进行关爱, 新增页面, 新增需求, 很完美........ 不多说, 记录一下新增东西中的省市区联动选择, (这里全国地区信息是在本地, 但不建议这么做, 因为js文件太大.. 建 ...
- 洛谷1099 [NOIP2007] 树网的核
链接https://www.luogu.org/problemnew/show/P1099 题目描述 设T=(V,E,W)是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称TTT为 ...
- ansible shell模块
[root@ftp:/root] > ansible ansible01 -u root -k -m shell -a 'hostname' SSH password: ansible01 | ...
- 通过CSS样式隐藏百度版权标志
在JSP中添加: //隐藏所有.anchorBL{ display:none; } //隐藏下方的保留百度地图图片 .BMap_cpyCtrl{ display:none; } 注:维护他人版权, ...
- VUE:路由
VUE:路由 一.说明 1)官方提供的用来实现SPA的vue插件 2)github:https://github.com/vuejs/vue-router 3)中文文档:http://router.v ...
- shell 脚本 helloworld
一.Hello World 脚本代码 #!/bin/sh echo "hello world" /bin/pwd 二.分析脚本 第 1 行:shell 脚本的固定写法 第 2 行: ...
- angular-HTML DOM
ng-disabled用法 <div ng-app="" ng-init="mySwitch=true"> <p> <button ...