HDU 4906 Our happy ending (DP)

HDU 4906

Our happy ending

Our happy ending

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

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

Source

2014 Multi-University Training Contest 4

题意比较容易理解。   这题的关键点就是n,k比较小,都<= 20 虽然L 比较大,但是 取0 和 取 > k是没有影响的。   使用状态压缩去DP。 使用dp\[j\] 表示前i个数,可以表示的数的状态是j , 有多少种。  第一维不需要增加空间了,滚动数组就可以了。

/* ***
Author :kuangbin
Created Time :2014/7/31 19:00:30
File Name :E:\2014ACM\比赛\2014多校训练\2014多校4\HDU4906.cpp
************************************************ */

#include <stdio.h>

#include <string.h>

#include

#include

#include

#include

#include

#include

#include

#include <math.h>

#include <stdlib.h>

#include <time.h>
using namespace std;
const int MOD = 1e9+7;
int dp[1<<20];
void add(int &a,int b)
{
a += b;
if(a >= MOD)a -= MOD;
}
int two[30];

int main()
{
//freopen(“in.txt”,”r”,stdin);
//freopen(“out.txt”,”w”,stdout);
two[0] = 1;
for(int i = 1;i < 30;i++)
two[i] = 2two[i-1];
int T;
int n,k,L;
scanf(“%d”,&T);
while(T–)
{
scanf(“%d%d%d”,&n,&k,&L);
memset(dp,0,sizeof(dp));
dp[0] = 1;
for(int i = 0;i < n;i++)
for(int j = two[k]-1; j >= 0;j–)
if(dp[j])
{
int tmp = dp[j];
for(int x = 1;x <= min(k,L);x++)
{
int nj = j | two[x-1];
nj |= ((j << x) & (two[k]-1));
add(dp[nj],tmp);
}
if(L > k)add(dp[j],(long long)tmp
(L-k)%MOD);
}
int ans = 0;
for(int i = two[k-1];i < two[k];i++)
add(ans,dp[i]);
printf(“%d\n”,ans);
}
return 0;
}

------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 301.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%