将所有至少的个数和记为sum,对M-sum进行个数有限制的背包
#include<bits/stdc++.h>
using namespace std;
#define fst first
#define sec second
#define sci(num) scanf("%d",&num)
#define scl(num) scanf("%lld",&num)
#define mem(a,b) memset(a,b,sizeof a)
#define cpy(a,b) memcopy(a,b,sizeof b)
typedef long long LL;
typedef pair<int,int> P;
const int MAX_N = 10000107;
const LL mod = 100000007;
int fac[MAX_N];
int les[20],cnt;
char
str[3][100];
char
s[1000];
LL qmod(LL a,LL n,LL MOD) {
LL ans = 1;
while (n > 0) {
if (n & 1) ans = ans * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return ans;
}
LL C(int N,int M) {
if (N < 0 || M <0 ) return 0;
if (N < M) return 0;
return (LL)fac[N] * qmod(fac[M],mod - 2,mod) % mod
* qmod(fac[N - M],mod - 2,mod) % mod;
}
void init() {
fac[0] = fac[1] = 1;
for (LL i = 2;i < MAX_N;i++) {
fac[i] = (LL)fac[i - 1] * i % mod;
}
}
int main() {
init();
int N,M;
while (~scanf("%d%d",&N,&M)) {
if (N == 0 && M == 1) break;
cnt = 0;
mem(les,0);
gets(s);
while(1) {
int num;
if (!gets(s)) break;
if (strlen(s) < 2) break;
sscanf(s,"%s%s%s%d",&str[0],&str[1],&str[2],&num);
if (str[1][0] == 'l') {
les[cnt++] = num;
} else {
M -= num + 1;
}
}
if (M < 0) {
printf("0\n");
} else {
LL ans = 0;
for (int i = 0;i < (1 << cnt);i++) {
int ones = 0;
int temp = 0;
for (int j = 0;j < cnt;j++) {
if (i & (1 << j)) {
ones++;
temp += les[j];
}
}
if (ones & 1)
ans -= C(N + M - 1 - temp,N - 1);
else
ans += C(N + M - 1 - temp,N - 1);
ans = (ans % mod+ mod) % mod;
}
ans = (ans % mod+ mod) % mod;
printf("%lld\n",ans);
}
}
return 0;
}