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
InputThere 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
OutputFor 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
pro: 有N个猴子,M个桃子,其中1号的大王,所以他分得的桃要比其他猴子的都多,问有多少种法。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int Mod=1e9+;
int N,M,fac[maxn],rev[maxn];
int C(int n,int m)
{
if(n<||m<||n<m) return ;
return 1LL*fac[n]*rev[m]%Mod*rev[n-m]%Mod;
}
int qpow(int a,int x){
int res=; while(x){
if(x&) res=1LL*res*a%Mod;
x>>=; a=1LL*a*a%Mod;
} return res;
}
int solve(int x)
{
if(x==M) return ;
if(1LL*(N-)*(x-)<M-x) return ;
int res=;
rep(i,,M/x){
if(M-(i+)*x<) break;
int t=1LL*C(N-,i)*C(M-(i+)*x+N-,N-)%Mod;
if(i&){ res-=t; if(res<) res+=Mod;}
else { res+=t; if(res>=Mod) res-=Mod;}
}
return res;
}
int main()
{
int T,ans;
fac[]=; rev[]=;
rep(i,,) fac[i]=1LL*fac[i-]*i%Mod;
rev[]=qpow(fac[],Mod-);
for(int i=-;i>=;i--) rev[i]=1LL*rev[i+]*(i+)%Mod;
scanf("%d",&T);
while(T--){
scanf("%d%d",&M,&N); ans=;
rep(i,max(M/N,),M) (ans+=solve(i))%=Mod;
printf("%d\n",ans);
}
return ;
}