先是看错题意。。然后知道题意之后写了发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)的更多相关文章

  1. HDU 4945 2048(DP)

    HDU 4945 2048 题目链接 题意:给定一个序列,求有多少个子序列能合成2048 思路:把2,4,8..2048这些数字拿出来考虑就能够了,其它数字不管怎样都不能參与组成.那么在这些数字基础上 ...

  2. bzoj 3851: 2048 dp优化

    3851: 2048 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 22  Solved: 9[Submit][Status] Description T ...

  3. HDU 4945 2048 DP 组合

    思路: 这个题写了一个背包的解法,超时了.搜了下题解才发现我根本不会做. 思路参见这个: 其实我们可以这样来考虑,求补集,用全集减掉不能组成2048的集合就是答案了. 因为只要达到2048就可以了,所 ...

  4. ZOJ3802 Easy 2048 Again (状压DP)

    ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  5. HDU 4945 2048(dp)

    题意:给n(n<=100,000)个数,0<=a[i]<=2048 .一个好的集合要满足,集合内的数可以根据2048的合并规则合并成2048 .输出好的集合的个数%998244353 ...

  6. zoj3802:easy 2048 again(状压dp)

    zoj月赛的题目,非常不错的一个状压dp.. 题目大意是一个一维的2048游戏 只要有相邻的相同就会合并,合并之后会有奖励分数,总共n个,每个都可以取或者不取 问最终得到的最大值 数据范围n<= ...

  7. 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 ...

  8. hdu 4945 2048 (dp+组合的数目)

    2048 Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  9. ZOJ 3802 Easy 2048 Again 像缩进DP

    链接:problemId=5334">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5334 题意:一个长度为5 ...

随机推荐

  1. C 网页压力测试器

    引言 <<独白>> 席慕蓉 节选一 把向你借来的笔还给你吧. 一切都发生在回首的刹那. 我的彻悟如果是缘自一种迷乱,那么,我的种种迷乱不也就只是因为一种彻悟? 在一回首间,才忽 ...

  2. 反射 reflect 初始学习

    类也是对象,本身是java.lang.Class 类的对象. Class的三种表示方式: 1.Class a = A.class: 类名.class的方式.经常在日志记录里面,获取当前类的日志记录生成 ...

  3. [原创]pg_shard使用场景及功能测试

    pg_shard是一个PostgreSQL的sharding extension.可以用于Shards.Replicates tables和高可用.它可以在不修改Applications的情况下无缝分 ...

  4. CoverFlow效果

    1. 成员函数 mCamera是用来做类3D效果处理,比如z轴方向上的平移,绕y轴的旋转等 mMaxRotationAngle是图片绕y轴最大旋转角度,也就是屏幕最边上那两张图片的旋转角度 mMaxZ ...

  5. php中$this、static、final、const、self 等几个关键字的用法

    <?phpclass A { public static function get_self(){ return new self(); } public static function get ...

  6. UIGestureRecongnizer 手势检测 swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  7. hdu 2629 Identity Card (字符串解析模拟题)

    这题是一个字符串模拟水题,给12级学弟学妹们找找自信的,嘿嘿; 题目意思就是要你讲身份证的上的省份和生日解析出来输出就可以了: http://acm.hdu.edu.cn/showproblem.ph ...

  8. Careercup - Microsoft面试题 - 5649647234187264

    2014-05-10 22:17 题目链接 原题: A draw method is given, write a function to draw a chess board. The Draw m ...

  9. nginx简单反向代理和负载均衡(ubuntu)

    nginx简单反向代理与负载均衡 环境:三台ubuntu 12.04.5 虚拟机    均装有nginx 1.1.19 以下u1(192.168.240.129) ,u2(192.168.240.13 ...

  10. 读《我是一只IT小小鸟》有感

          时间太瘦,指缝太宽.一晃一学期过去了,有些迷茫,但也相信未来是美好的.当我看完<我是一只IT小小鸟>这本书之后,心中更是感慨万千.每一个励志的故事都值得欣赏.深思,甚至我还幻想 ...