Fruit Ninja ZOJ - 3638

将所有至少的个数和记为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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值