HDU 5201 The Monkey King

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<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
using namespace std;
const long long base = 1000000007;
const long long maxn = 200005;
long long T, n, m, in[maxn], f[maxn], ans;

long long mod(const long long a)
{
    return (a % base + base) % base;
}

long long inv(long long x)
{
    long long tot = 1, i = base - 2, ans = x;
    while (i)
    {
        if (i & 1) tot = mod(tot * ans);
        ans = mod(ans * ans);    i >>= 1;
    }
    return mod(tot);
}

long long c(long long m, long long n)
{
    return mod(mod(f[n] * in[m]) * in[n - m]);
}

long long cal(long long x, long long n, long long m)
{
    if (!n) return 1;
    if (!m) return 0;
    long long tot = 0;
    for (int i = 0; x*i <= n&&i <= m; i++)
    {
        long long t = c(i, m)*c(m - 1, n - x * i + m - 1);
        if (i & 1) tot = mod(tot - t);
        else tot = mod(tot + t);
    }
    return tot;
}

int main()
{
    for (int i = f[0] = in[0] = 1; i < maxn; i++)
    {
        f[i] = mod(f[i - 1] * i);
        in[i] = inv(f[i]);
    }
    scanf("%lld", &T);
    while (T--)
    {
        scanf("%lld%lld", &n, &m);
        for (int i = ans = 0; i <= n; i++)
            ans = mod(ans + cal(i, n - i, m - 1));
        printf("%lld\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值