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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章