Our happy ending
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 229 Accepted Submission(s): 66
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.
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.
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
解题报告: 多校联合训练第四场最后一题,队友比赛前2分钟A了,Orz。基本思路是枚举前i个数能组成的数有哪些,枚举出来的用二进制保存。比如如果前i个数能组成2,3,5,那么用二进制表示就是0,0,1,1,0,1。这样做的原因是避免重复。一路递推下去就好了。
数组开的比较大,所以尽量少memset。代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
const LL inf=1e15;
void work();
int main()
{
#ifdef ACM
//freopen("in.txt", "r", stdin);
#endif // ACM
work();
}
/***************************************************/
const int maxn = bit(21);
const int mod = 1e9+7;
int id;
struct Node
{
int v[maxn];
int h[maxn];
int s[maxn];
int n[maxn];
int size;
void init()
{
++id;
size=0;
}
void push(int ss, int num)
{
int i = ss;
if(v[i] == id)
{
n[h[i]] += num;
if(n[h[i]] >= mod)
n[h[i]] -= mod;
}
else
{
s[size] = ss;
n[size] = num;
h[i] = size++;
v[i] = id;
}
}
} dp[2];
void work()
{
int T;
scanf("%d", &T);
fff(cas, 1, T)
{
int n, k, l;
scanf("%d%d%d", &n, &k, &l);
int now = 1, pre = 0;
dp[now].init();
dp[now].push(1, 1);
LL up = bit(k+1)-1;
int extra = 0;
if(l > k) extra = l - k;
ff(nn, n)
{
swap(now, pre);
dp[now].init();
ff(i, dp[pre].size)
{
int st = dp[pre].s[i];
dff(j, k, 0)
dp[now].push((st << j) & up | st, dp[pre].n[i]);
if(extra)
dp[now].push(st, (LL)dp[pre].n[i] * extra % mod);
}
}
LL sum = 0;
ff(i, dp[now].size) if(dp[now].s[i] & bit(k))
sum += dp[now].n[i];
sum %= mod;
if(sum < 0) sum += mod;
printf("%I64d\n", sum);
}
}