HDU4945 2048(dp)
先是看错题意。。然后知道题意之后写了发dp..无限TLE..实在是不知道怎么优化了,跑了遍数据是对的,就当作理论AC掉好了。。
#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std; #define ll long long
#define maxn 120000
#define mod 998244353 ll mod_pow(ll a, ll n){
ll ret = 1;
while (n){
if (n & 1) ret = ret*a%mod;
a = a*a%mod;
n >>= 1;
}
return ret;
} ll fac[maxn];
ll fac_inv[maxn]; int cnt[2500];
int dp[13][2500];
int two[13] = { 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 };
int two_com[13] = { 0, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 };
int n; inline int getint() {
int ret = 0; bool ok = 0;
for (;;) {
int c = getchar();
if (c >= '0'&&c <= '9')ret = (ret << 3) + ret + ret + c - '0', ok = 1;
else if (ok)return ret;
}
} inline ll comb(int n, int m){
return fac[n] * fac_inv[m] % mod*fac_inv[n - m] % mod;
}
inline void add(int &a, int b){
a += b;
if (a >= mod) a -= mod;
} int main()
{
//freopen("1001.in", "r", stdin);
//freopen("out.txt", "w", stdout);
//double t1 = clock();
fac[0] = fac_inv[0] = 1;
for (int i = 1; i <= 100000; ++i){
fac[i] = fac[i - 1] * i%mod;
}
fac_inv[100000] = mod_pow(fac[100000], mod - 2);
for (int i = 99999; i >= 0; --i){
fac_inv[i] = fac_inv[i + 1] * (i + 1) % mod;
}
int ca = 0;
while (~scanf("%d", &n) && n){
for (int i = 1; i <= 12; ++i) cnt[two[i]] = 0;
int tmp;
for (int i = 0; i < n; ++i) {
tmp = getint();
cnt[tmp]++;
}
int pn = 0;
for (int i = 1; i <= 12; ++i) pn += cnt[two[i]];
memset(dp, 0, sizeof(dp)); dp[0][0] = 1;
ll sum,cb;
for (int i = 1; i <= 12; ++i){
int num = cnt[two[i]];
for (int j = 0; j <= two_com[i - 1]; ++j){
sum = 0;
int k;
for (k = 0; (j >> 1) + k <= two_com[i] && k <= num; ++k){
cb = comb(num, k);
sum = sum + cb; if (sum >= mod) sum -= mod;
add(dp[i][(j >> 1) + k], dp[i - 1][j] * cb%mod);
}
if ((j >> 1) + num > two_com[i]){
ll res = ((mod_pow(2, num) - sum) % mod + mod) % mod;
add(dp[i][two_com[i]], res*dp[i - 1][j] % mod);
}
}
}
ll ans = dp[12][1] * mod_pow(2, n - pn) % mod;
printf("Case #%d: %I64d\n", ++ca, ans);
}
//double t2 = clock();
//cout << t2 - t1 << endl;
return 0;
}
HDU4945 2048(dp)的更多相关文章
- HDU 4945 2048(DP)
HDU 4945 2048 题目链接 题意:给定一个序列,求有多少个子序列能合成2048 思路:把2,4,8..2048这些数字拿出来考虑就能够了,其它数字不管怎样都不能參与组成.那么在这些数字基础上 ...
- bzoj 3851: 2048 dp优化
3851: 2048 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 22 Solved: 9[Submit][Status] Description T ...
- HDU 4945 2048 DP 组合
思路: 这个题写了一个背包的解法,超时了.搜了下题解才发现我根本不会做. 思路参见这个: 其实我们可以这样来考虑,求补集,用全集减掉不能组成2048的集合就是答案了. 因为只要达到2048就可以了,所 ...
- ZOJ3802 Easy 2048 Again (状压DP)
ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- HDU 4945 2048(dp)
题意:给n(n<=100,000)个数,0<=a[i]<=2048 .一个好的集合要满足,集合内的数可以根据2048的合并规则合并成2048 .输出好的集合的个数%998244353 ...
- zoj3802:easy 2048 again(状压dp)
zoj月赛的题目,非常不错的一个状压dp.. 题目大意是一个一维的2048游戏 只要有相邻的相同就会合并,合并之后会有奖励分数,总共n个,每个都可以取或者不取 问最终得到的最大值 数据范围n<= ...
- Easy 2048 Again - ZOJ 3802 像缩进dp
Easy 2048 Again Time Limit: 2 Seconds Memory Limit: 65536 KB Dark_sun knows that on a single-tr ...
- hdu 4945 2048 (dp+组合的数目)
2048 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- ZOJ 3802 Easy 2048 Again 像缩进DP
链接:problemId=5334">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5334 题意:一个长度为5 ...
随机推荐
- SQL基础篇——如何搭建一个数据库
特别提醒:所有的新建数据库,表,行,列都可以通过对象资源管理器操作,下面所讲的为查询操作方法 一.新建数据库 使用CREATE DATABASE语句建立数据库: 新建查询-- CREATE DATAB ...
- AOP在 .NET中的七种实现方法
7Approaches for AOP in .Net AOP在 .NET中的七种实现方法 Here are all the ways that I can think of to add AOPto ...
- python并行迭代
并行迭代:同时并行遍历两个列表 for line1,line2 in zip(line1_list, line2_list): ... 无聊,贴一段刚才的代码: import sys import s ...
- expect脚本
#!/bin/bash # Filename: main.sh usage(){ echo "scp file :./main.sh scp sourcefile destfile" ...
- Node.js 学习(四)Node.js 回调函数
Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了. 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都 ...
- elasticsearch-查询的基本语法
elasticsearch的查询有两部分组成:query and filter. 两者的主要区别在于:filter是不计算相关性的,同时可以cache.因此,filter速度要快于query. 先记录 ...
- 网站中的专题页或者tag聚合页的权重不错
一.据最近的一些观察,觉得网站中的专题页或者tag聚合页的权重不错,因此多给网站制作一些专题页面,不仅有利于聚合站内的文章,更是绝对的原创内容,应该会受到百度的青睐.简评:关于权重的讨论,这篇无疑是很 ...
- 8、android代码优化技术记录
1.length.length().size的优化 举例: int array_one[] = {1,2,3,4,5,6,7,8,9,10}; int array_two[] = {1,2,3,4,5 ...
- 【Permutations】cpp
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- Ombrophobic Bovines - POJ 2391
Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...