您的位置:首页 > 移动开发

hdu 4906 Our happy ending

2014-08-03 14:16 274 查看


Our happy ending

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 700 Accepted Submission(s): 223



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



题意:给你n个数字,每个数字可以是1~L,要求取几个数,使他们的和为k,求方案数

思路:状态压缩。dp[i]表示i这个状态的方法数,使用滚动数组,像背包那样从后往前递推防MLE。

状态转移:令k = 4 ,s = (1<<4) - 1 = 1111(2). 令前一状态为1010(表示能构成2,4的方法数),若当前取1,则下一状态为能构成1,3的方法数(即1,1+2,1+4(去掉))。

所以next = i | ((i<<j) & s) | (1 << (j-1));

对于那些超出K的数,它们都转移会自己。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
//#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF 1e9
#define MAXN 21
#define maxn 105
#define mod 1000000007
#define eps 1e-7
#define pi 3.1415926535897932384626433
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scanll(n) scanf("%I64d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
ll n,k,L;
ll dp[1<<20]; 
void add(ll& a,ll b)
{
	a = a + b;
	if(a >= mod) a %= mod;
	//a = (a + b) % mod;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
#endif  
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%I64d%I64d%I64d",&n,&k,&L);
		ll ty = 0;
		if(L >= k)
		{
			ty = L - k;
			L = k;
		}
		ini(dp);
		int s = (1 << k) - 1;
		dp[0] = 1;
		while(n--)
		{
			for(int i = s; i >= 0; i--)
			{
				ll p = dp[i];
				if(p == 0) continue;
				rep1(j,L)
				{
					int next = i | ((i<<j) & s) | (1 << (j-1)); //下一状态
					add(dp[next],p);
				}
				add(dp[i],p * ty); //有ty个数转移回自己
			}
		}
		ll ans = 0;
		for(int i = 0;i <= s; i++)
		{
			if((1 << (k-1)) & i) //取出状态中有k的相加
			{
				add(ans,dp[i]);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: