hdu 4906 Our happy ending 。神奇的状态转移方程,记录下

本文介绍了一种状态DP算法,用于解决特定条件下的序列组合计数问题。给定一个整数序列,若序列中某些元素之和等于给定值k,则称此序列为好序列。文章探讨了如何计算0到L范围内所有可能的好序列的数量,并提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自http://www.cnblogs.com/20120125llcai/p/3883896.html

Our happy ending

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 194    Accepted Submission(s): 48


Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

Y*wan still remember the day he first meets the devil. Now everything is done and the devil is gone. Y*wan feel very sad and suicide.

You feel guilty after killing so many loli, so you suicide too.

Nobody survive in this silly story, but there is still some hope, because this is just a silly background story during one programming contest!

And the last problem is:

Given a sequence a_1,a_2,...,a_n, if we can take some of them(each a_i can only be used once), and they sum to k, then we say this sequence is a good sequence.

How many good sequence are there? Given that each a_i is an integer and 0<= a_i <= L.

You should output the result modulo 10^9+7.
 

Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains 3 integers n, k, L.

T<=20, n,k<=20 , 0<=L<=10^9.
 

Output
For each cases, output the answer in a single line.
 

Sample Input
  
  
1 2 2 2
 

Sample Output
  
  
6
 

Author
WJMZBMR
 
算是状态dp吧
dp[i] 表示 i 状态下的方案数。


i 化为二进制时 ,第一位为 1 表示有和为1 的存在,0表示没有,第二位为 1 时表示有和为 2的存在


第三位为 1 时表示有和为 3的存在...
转移:sta = i|(1<<(k-1))|(i<<j&m) ; 
表示 i 的状态转移到了 sta状态
假如当前的状态 为 3 二进制就是 11,表示有和为 1,2的存在,
如果加上 3 ,那么 3 的状态就可以达到,也就是 |(1<<(3-1))
还有3可以和1,2加,也就是 原来的加上 3 ,也就是左移 3 位 |(i<<j)&m
因为大于 k的和我们是不在意的,所以最大的状态就是 (1<<k)-1 ;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define maxn 100010
#define mod 1000000007
#define LL long long
using namespace std;
int dp[(1<<20)+10] ;
int main()
{
    int i ,n,m,k,j ;
    int T ,L ;
    cin >> T ;
    while( T-- )
    {
        scanf("%d%d%d",&n,&k,&L) ;
        memset(dp,0,sizeof(dp)) ;
        dp[0]=1 ;
        m = (1<<k)-1 ;
        int tot = 0 ;
        if(L > k)
        {
            tot = L-k ;
            L = k ;
        }
        while(n--)
        {
            for( i = m ; i >= 0 ;i--)
            {
                if(dp[i]==0) continue ;
                j = dp[i] ;
                LL tmp = (LL)tot*dp[i]%mod ;
                for( int u = 1 ; u <= L ;u++)<span style="white-space:pre">		</span>//加上一个小于等于k的数
                {
                    int sta = (i|(1<<(u-1))|((i<<u)&m)) ;
                    dp[sta] += j ;
                    dp[sta] %= mod;
                }
                dp[i] = (dp[i]+tmp)%mod ;<span style="white-space:pre">		</span>//加上一个大于k的数
            }
        }
        LL ans = 0 ;
        for( i = 0 ; i <= m ;i++)
            if((i>>(k-1))&1)
        {
            ans = (ans+dp[i])%mod ;
        }
        cout << ans << endl;
    }
    return 0 ;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值