hdu 5201 容斥原理+组合数取模

The Monkey King

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 207    Accepted Submission(s): 64


Problem Description
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and Fruits. One day, his sons get n peaches. And there are m monkeys (including GoKu), they are numbered from 1 to m , GoKu’s number is 1. GoKu wants to distribute these peaches to themselves. Since GoKu is the King, so he must get the most peach. GoKu wants to know how many different ways he can distribute these peaches. For example n=2, m=3, there is only one way to distribute these peach: 2 0 0.

When given n and m , you are expected to calculate how many different ways GoKu can distribute these peaches. Answer may be very large, output the answer modular 1000000007 instead.
 

Input
There are multiple test cases. In the first line of the input file there is an integer T indicates the number of test cases.

In the next T lines, each line contains n and m which is mentioned above.

[Technical Specification]

All input items are integers.

1T25

1n,m100000
 

Output
For each case,the output should occupies exactly one line.

See the sample for more details.
 

Sample Input
  
  
2 2 2 3 5
 

Sample Output
 
 
1 5
Hint
For the second case, there are five ways. They are 2 1 0 0 0 2 0 1 0 0 2 0 0 1 0 2 0 0 0 1 3 0 0 0 0
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int mxn=200020;
const int mod=1000000007;
#define LL long long
LL fac[mxn], nfac[mxn];

LL qpow(LL x, int k) {
    LL ret = 1;
    while(k) {
        if(k & 1) ret = ret * x % mod;
        x = x * x % mod;
        k >>= 1;
    }
    return ret;
}
void init() {
    fac[0] = fac[1] = nfac[0] = nfac[1] = 1;
    for(int i = 2; i < mxn; ++i) {
        fac[i] = fac[i-1] * i % mod;
        nfac[i] = qpow(fac[i], mod - 2);
    }
}
int n, m;

LL C(int x, int y) {
    return fac[x] * nfac[y] % mod * nfac[x-y] % mod;
}
LL f(int r, int k) {
    return C(r + k - 1, r);
}
LL calc(LL x) {
    LL ret = 0;
    for(int k = 0; k<=m-1&&(k + 1) * x <= n; ++k) {
        LL t = C(m - 1, k) * f(n - (k + 1) * x, m - 1) % mod;
        if(k & 1)
            ret = ((ret - t) % mod + mod) % mod;
        else
            ret = (ret + t) % mod;
    }
    return ret;
}
int main() {
    init();
    int cas;
    scanf("%d", &cas);
    while(cas--) {
        scanf("%d%d", &n, &m);
        if(m == 1) {
            printf("%d\n", 1);
            continue;
        }
        int t = n / m;
        if(n % m == 0) ++t;
        else if(n % m != 1) t += 2;
        LL ans = 0;
        for(int i = max(1, t); i <= n; ++i) {
            ans = (ans + calc(i)) % mod;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值