HDU 4945 2048 (DP + combination)

Source: Internet
Author: User
2048 time limit: 3000/1500 MS (Java/others) memory limit: 65536/65536 K (Java/Others) Total submission (s): 840 accepted submission (s): 199

Problem descriptionteacher 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 and just output the number modulo 998244353.
Inputthere 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 ).
Outputfor 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
41024 512 256 25641024 1024 1024 102451024 512 512 512 10
 
Sample output
Case #1: 1Case #2: 11Case #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. 
 
Source2014 multi-university training contest 8


Question: Give You n numbers and select some numbers to form a sequence. For a sequence, you can perform the following operation: select two identical numbers and replace them with their sum. If a sequence can get 2048, the sequence is good. Ask how many subsequences of N numbers are good.

Idea: because the two numbers selected at a time change to their sum, and finally to 2048, only the power of 2 is valid, and the other numbers (TOT) each instance has two statuses: Add or not add. The final number x 2 ^ tot. The question becomes a power of 2, and you are asked how many options can be changed to 2048 or more. Lenovo DP + combination. DP [I] [J] indicates the number of processed 2 ^ I and [J * 2 ^ I, (J + 1) * 2 ^ I, for enumeration, select K 2 ^ (I + 1) for transfer. It takes a lot of time to calculate the state of 2048 or more, so we can calculate the state of less than 2048, the total number of solutions minus it. Due to the tight schedule of this question, the inverse element needs to be pre-processed. When the combination number is self-recursive, the Division is changed to multiplication, and then the input optimization needs to be added ~
Code:
#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-8typedef 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;}/*11256 256 256 256 256 256 512 512 1024 1024 2048*/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.