2048

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 840    Accepted Submission(s): 199

Problem Description
Teacher Mai is addicted to game 2048. But finally he finds it's too hard to get 2048. So he wants to change the rule:



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.
 
Input
There are multiple test cases, terminated by a line "0".



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).
 
Output
For each test case, output one line "Case #k: ans", where k is the case number counting from 1, ans is the number module 998244353.
 
Sample Input
4
1024 512 256 256
4
1024 1024 1024 1024
5
1024 512 512 512 1
0
 
Sample Output
Case #1: 1
Case #2: 11
Case #3: 8
Hint
In the first case, we should choose all the numbers.
In the second case, all the subsequences which contain more than one number are good.
 
Source



题意:
给你n个数。从中选择一些数能构成一个序列,对于一个序列。能够进行这种操作:选择两个同样的数。将它们替换为它们的和。假设一个序列能够得到2048。那么就说这个序列式good的。问这n个数有多少个子序列是good 的。


思路:
由于每次选择同样的两个数变为他们的和,终于变为2048,所以仅仅有2的幂是有效的,其它的数(tot个)每一个都有两种状态。加或者不加,最后的种数*2^tot。
问题变为给你一些2的幂,问有多少种选取方式终于可变为2048或者2048以上。

能够联想的dp+组合。

dp[i][j]表示处理到2^i,和为[j*2^i,(j+1)*2^i )的个数,枚举选取k个2^(i+1)进行转移就可以。
算2048或者2048以上的状态时间复杂度较高,所以能够算出小于2048的状态。总方案数减去它即可了。

此题时间卡的紧,须要预处理出来逆元。组合数自己递推的时候除法变为乘法。然后还要加输入优化~

代码:
#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+组合的数目)的更多相关文章

  1. HDU 4945 2048 DP 组合

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

  2. HDU 4945 2048(DP)

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

  3. HDU 4945 2048(dp)

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

  4. HDU 4945 (dp+组合数学)

    2048 Problem Description Teacher Mai is addicted to game 2048. But finally he finds it's too hard to ...

  5. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  6. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  7. hdu 3709 数字dp(小思)

    http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...

  8. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. hdu 4283 区间dp

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. A Game of Thrones(6) - Catelyn

    Of all the rooms in Winterfell’s Great Keep, Catelyn’s bedchambers(['bedtʃeɪmbə]卧室,寝室) were the hott ...

  2. java单例模式(线程安全,效率高,双重推断)

    这样的方法,在获取单利的时候,避免了线程锁,导致訪问该方法速度非常慢, 同是,防止了多线程同事房屋该方法就会产生多个实例的问题. 效率高.线程安全. public class TestInstance ...

  3. Java UML描述

      开发Java应用程序时,开发者要想有效地利用统一建模语言(UML),必须全面理解UML元素以及这些元素如何映射到Java.本文重点讨论UML类图中的元素. 类图是最常用的UML图,它用于描述系统的 ...

  4. 局部权重线性回归(Locally weighted linear regression)

    在线性回归中,因为对參数个数选择的问题是在问题求解之前已经确定好的,因此參数的个数不能非常好的确定,假设參数个数过少可能拟合度不好,产生欠拟合(underfitting)问题,或者參数过多,使得函数过 ...

  5. HTML5----响应式(自适应)网页设计

    第一步:在网页代码的头部,加入一行viewport元标签 <meta name="viewport" content="width=device-width, in ...

  6. Java EE (10) - 资源服务器的整合

    加密(Encryption)和数字签名(Digital Signature)通常被用于保护通讯--加密用来防止数据传输过程中的窃听--数字签名用来防止数据传输过程中的篡改 JDBC: 整合关系型数据库 ...

  7. 设计模式之迭代器模式(Iterator)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...

  8. [Unity3D]Unity3D持久性数据的游戏开发PlayerPrefs采用

    大家好,我是秦培,欢迎关注我的博客,我的博客地址">blog.csdn.net/qinyuanpei. 博主今天研究了在Unity3D中的数据持久化问题.数据持久化在不论什么一个开发领 ...

  9. Visual Studio跨平台开发实战(5) - Xamarin Android多页面应用程式开发

    原文 Visual Studio跨平台开发实战(5) - Xamarin Android多页面应用程式开发 前言 大部份的Andr​​oid 都具有实体或虚拟的Back键. 因此在处理多页面应用程式时 ...

  10. Windows Phone开发(2):竖立自信,初试锋茫

    原文:Windows Phone开发(2):竖立自信,初试锋茫 上一篇文章中,我们聊了一些"大炮"话题,从这篇文章开始,我们一起来学习WP开发吧. 一.我们有哪些装备. 安装完VS ...