hdu 4945 2048 (dp+组合的数目)
2048
You are given some numbers. Every time you can choose two numbers of the same value from them and merge these two numbers into their sum. And these two numbers disappear meanwhile.
If we can get 2048 from a set of numbers with this operation, Teacher Mai think this multiset is good.
You have n numbers, A1,...,An. Teacher Mai ask you how many subsequences of A are good.
The number can be very large, just output the number modulo 998244353.
For each test case, the first line contains an integer n (1<=n<=10^5), the next line contains n integers ai (0<=ai<=2048).
4
1024 512 256 256
4
1024 1024 1024 1024
5
1024 512 512 512 1
0
Case #1: 1
Case #2: 11
Case #3: 8HintIn the first case, we should choose all the numbers.
In the second case, all the subsequences which contain more than one number are good.
能够联想的dp+组合。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#include <sstream>
#define maxn 100005
#define MAXN 100005
#define mod 998244353
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-8
typedef long long ll;
using namespace std; int n,m,tot;
int mp[2050],cnt[13],p[13],ed[13];
ll ans,dp[13][2050];
ll inv[100005];
int a[12]= {2048,1024,512,256,128,64,32,16,8,4,2,1}; void scanf_(int&ret)
{
char c;
ret=0;
while((c=getchar())<'0'||c>'9');
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
}
ll pow_mod(ll x,ll n)
{
ll res = 1;
while(n)
{
if(n&1) res = res * x %mod;
x = x * x %mod;
n >>= 1;
}
return res;
}
void egcd(ll a,ll b,ll &x,ll &y)
{
if (b==0)
{
x=1,y=0;
return ;
}
egcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-a/b*x;
}
void solve()
{
int i,j,k;
memset(dp,0,sizeof(dp));
for(i=0; i<=11; i++)
{
ed[i]=min(cnt[i],a[i]-1);
}
dp[0][0]=1;
ll h=1;
for(i=1; i<=ed[0]; i++)
{
h=((h*(cnt[0]-i+1))%mod)*inv[i]%mod;
dp[0][i]=h;
}
for(i=0; i<11; i++)
{
for(j=0; j<a[i]; j++)
{
if(!dp[i][j]) continue ;
h=1;
dp[i+1][j>>1]+=dp[i][j];
dp[i+1][j>>1]%=mod;
for(k=1; k<=ed[i+1]; k++)
{
if((j>>1)+k<a[i+1])
{
h=((h*(cnt[i+1]-k+1))%mod)*inv[k]%mod;
dp[i+1][(j>>1)+k]+=h*dp[i][j];
dp[i+1][(j>>1)+k]%=mod;
}
else break ;
}
}
}
ans=((pow_mod(2,n-tot)-dp[11][0]+mod)%mod)*pow_mod(2,tot);
ans%=mod;
}
int main()
{
int i,j,u,test=0;
for(i=1; i<=100000; i++)
{
ll x,y;
egcd(i,mod,x,y);
x=(x+mod)%mod;
inv[i]=x;
}
memset(mp,-1,sizeof(mp));
p[0]=1;
mp[1]=0;
for(i=1; i<=11; i++)
{
p[i]=p[i-1]*2;
mp[p[i]]=i;
}
while(1)
{
scanf_(n);
if(n==0) break ;
tot=0;
memset(cnt,0,sizeof(cnt));
for(i=1; i<=n; i++)
{
scanf_(u);
if(mp[u]!=-1) cnt[mp[u]]++;
else tot++;
}
solve();
printf("Case #%d: %I64d\n",++test,ans);
}
return 0;
}
/*
11
256 256 256 256 256 256 512 512 1024 1024 2048
*/
版权声明:本文博主原创文章。博客,未经同意不得转载。
hdu 4945 2048 (dp+组合的数目)的更多相关文章
- HDU 4945 2048 DP 组合
思路: 这个题写了一个背包的解法,超时了.搜了下题解才发现我根本不会做. 思路参见这个: 其实我们可以这样来考虑,求补集,用全集减掉不能组成2048的集合就是答案了. 因为只要达到2048就可以了,所 ...
- HDU 4945 2048(DP)
HDU 4945 2048 题目链接 题意:给定一个序列,求有多少个子序列能合成2048 思路:把2,4,8..2048这些数字拿出来考虑就能够了,其它数字不管怎样都不能參与组成.那么在这些数字基础上 ...
- 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 ...
随机推荐
- ZooKeeper场景实践:(2)集中式配置管理
1. 基本介绍 在分布式的环境中,可能会有多个对等的程序读取相同的配置文件,程序能够部署在多台机器上,假设配置採用文件的话,则须要为部署该程序的机器也部署一个配置文件,一旦要改动配置的时候就会很麻烦, ...
- 转:svn命令行操作
原文:http://zhumeng8337797.blog.163.com/blog/static/10076891420115218821912/SVN命令行 -- ::| 分类: CVS |字号 ...
- 2014百度之星第三题Xor Sum(字典树+异或运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- Mac OS X在建筑Python科学计算环境
经验(比如这篇日志:http://blog.csdn.net/waleking/article/details/7578517).他们推荐使用Mac Ports这种软件来管理和安装全部的安装包.依照这 ...
- 用EnableMenuItem不能使菜单变灰的原因
为何不能Disable菜单项 问:我有一个工具button在WM_COMMAND消息是这样做的: CMenu *pMenu = GetMenu(); pMe ...
- CSDN-markdown语法之怎样使用LaTeX语法编写数学公式
文件夹 文件夹 正文 标记公式 行内公式 块级公式 上标和下标 分数表示 各种括号 根号表示 省略号 矢量表示 间隔空间 希腊字母 特殊字符 关系运算符 集合运算符 对数运算符 三角运算符 微积分运算 ...
- phpcms v9框架的目录结构分析
phpcms v9框架的目录结构分析: 了解v9框架的目录结构,有助于帮助我们快速建立起对v9框架的一个整体认识 打开"mycms"项目,有如下文件和目录 使用 ...
- php-GD库函数(三)
<?php //imagefilledellipse — 画一椭圆并填充 /*bool imagefilledellipse ( resource $image , int $cx , int ...
- crontab,想说爱你不easy
悲剧的背景 跑自己主动化脚本的机器连不上toastserver了, 仅仅能自己写个脚本每天跑了. 当然要放在crontab里了. 5 3 * * * sh ~/nosecron.sh 更悲剧的结果 第 ...
- Windows 8 应用开发 - 磁贴
原文:Windows 8 应用开发 - 磁贴 我们开发的应用在Win8 界面中会以磁贴形式展现,默认情况下磁贴会显示应用图标,即项目工程中的Logo.png图片文件.开发人员可按应用的需要使 ...