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
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.
For each test case, the first line contains 3 integers n, k, L.
T<=20, n,k<=20 , 0<=L<=10^9.
數組開的比較大,所以儘量少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);
}
}