J title:
Meaning of the questions: to give you a view of uncertain size n, and this figure uncertain degree and out of each point is 1. Then a feature of this map is certainly composed by a number of Euler. Can get recursive formula is n!
Then you look at it there is a feature. It requires that each point must take no more than x can return to their own sides. . . . . . . (N <= 2 * x <2e9)
We certainly can only think from the negative side, it does not meet the conditions are removed as (n <= 2 * x <2e9) This condition may be doomed to think so. . . . . . .
Because p (m) = c (m, n) * (m-1)! * (Nm)! / N! = 1 / m so for each probability is 1 / i (x <i <= n)
Pretreatment what you can, Here is the code:
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <bitset> 5 #include <iostream> 6 #include <algorithm> 7 typedef long long ll; 8 using namespace std; 9 const ll mod=(ll)(1e9+7); 10 const int maxn=(int)(1e6+10); 11 ll sum[maxn]; 12 int t; 13 ll x,n; 14 15 ll fast_pow(ll a,ll b){ 16 ll res=1; 17 while(b){ 18 if(b%2==1) res=(res*a)%mod; 19 a=(a*a)%mod; 20 b=b/2; 21 } 22 return res; 23 } 24 25 void init(){ 26 sum[0]=0; 27 for(ll i=1;i<maxn;i++){ 28 sum[i]=(sum[i-1]+fast_pow(i,mod-2))%mod; 29 } 30 } 31 32 33 34 int main(){ 35 init(); 36 scanf("%d",&t); 37 while(t--){ 38 scanf("%lld%lld",&n,&x); 39 if(x>=n){ 40 printf("1\n"); 41 continue; 42 }else{ 43 ll ans1=1,ans2=1; 44 for(ll i=x+1;i<=n;i++) ans1=(ans1*i)%mod; 45 ans2=(ans1-(ans1*(sum[n]-sum[x]+2*mod))%mod+2*mod)%mod; 46 //cout<<ans1<<" "<<ans2<<endl; 47 printf("%lld\n",ans2*fast_pow(ans1,mod-2)%mod); 48 } 49 } 50 return 0; 51 }