The Monkey King
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 305 Accepted Submission(s): 100
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.
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.
1≤T≤25
1≤n,m≤100000
In the next T lines, each line contains n and m which is mentioned above.
[Technical Specification]
All input items are integers.
1≤T≤25
1≤n,m≤100000
Output
For each case,the output should occupies exactly one line.
See the sample for more details.
See the sample for more details.
Sample Input
2 2 2 3 5
Sample Output
1 5tti题目大意:倒 萨队题目大意: 给出n个桃子,m个猴子,第一个猴子是猴王,猴王要拿到多于任何其他猴子的桃子数的桃子,问有多少种分桃子的方案 题目分析: 首先考虑在没有限制的情况下,设f(x,y)为x个桃子,y个猴子的情况的方案数,那么f(x,y)=C(x+y-1,y-1)=C(x+y-1,x) 通过排列组合中的插板法可以轻易得到。那么可以考虑排除掉不符合要求的情况,也就是出现小猴子比猴王拿到的桃子数量相等或者多, 那么我们假设A[i]表示至少有i只猴子比猴王拿到的桃子数多,首先枚举猴王拿到的桃子的个数x,那么至少有给定的k个猴子比猴王 拿到的桃子多的方案有F(n-(k+1)*x , m-1 ),再乘上选择k个猴子的方案数,就是A[k]的方案数,那么根据容斥定理得, 那么没有猴子比猴王拿的桃子多的方案数就是: A[0]-A[1]+A[2]+(-1)^m×A[m]; 求解组合数时,可以利用公式n!/(m!(n-m)!),因为取模的数是大质数,所以可以用费马小定理求解逆元,阶乘要预处理出来, 组合数也要预处理打好表, 超难看的AC代码如下:HintFor 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 <cstring> #include <algorithm> #include <cstdio> #define MAX 200007 #define MOD (1000000007LL) using namespace std; int t,n,m; typedef long long LL; LL pro[MAX]; LL inv[MAX]; LL pow ( LL num , LL n ) { LL ans = 1; while ( n ) { if ( n&1 ) ans = (ans*num)%MOD; num = (num*num)%MOD; n >>= 1; } return ans; } void init ( ) { pro[0] = pro[1] = inv[0] = inv[1] = 1; for ( LL i = 2 ; i < MAX ; i ++ ) { pro[i] = (pro[i-1]*i)%MOD; inv[i] = pow ( pro[i] , MOD-2 ); } } LL C ( int n , int m ) { if ( m > n || m < 0 ) return 0; return pro[n]*inv[m]%MOD*inv[n-m]%MOD; } LL F( int n , int m ) { return C ( n+m-1 , n ); } LL calc ( int n , int m ) { LL sum = 0; for ( int i = 1 ; i <= n ; i++ ) for ( int j = 0 , s = i; j < m && s <= n ; j++ , s+=i ) { LL temp = C(m-1,j)*F(n-s , m-1)%MOD; if ( j&1 ) sum = ( sum - temp )%MOD + MOD; else sum += temp; sum %= MOD; } return sum; } int main( ) { scanf ( "%d" , &t ); init ( ); while ( t-- ) { scanf ( "%d%d" , &n , &m ); if ( m == 1 ) { puts ( "1" ); continue; } printf ( "%lld\n" , calc ( n , m ) ); } }