fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. const int mod=1000000007;
  6. vector<ll> fact(1e5+1, 1);
  7.  
  8. ll modInv(ll a, ll e){
  9. ll res=1;
  10. while(e>0){
  11. if (e%2){
  12. res=(res*a)%mod;
  13. e--;
  14. }
  15. else{
  16. a=(a*a)%mod;
  17. e/=2;
  18. }
  19. }
  20. return res;
  21. }
  22. ll nCr(ll n, ll r){
  23. ll invR = modInv(fact[r], mod-2);
  24. ll invNR = modInv(fact[n-r], mod-2);
  25. ll denom = (invR * invNR) % mod;
  26. return (fact[n] * denom) % mod;
  27. }
  28. int main() {
  29. for(int i=1; i<=1e5; i++) fact[i]=(fact[i-1]*i)%mod;
  30. int t; cin>>t;
  31. while(t--){
  32. ll n, k; cin>>n>>k;
  33. ll p=0LL;
  34. for(int i=1; i<=n; i++){
  35. int nxt=k-i;
  36. if (nxt>=1 && nxt<i) p++;
  37. }
  38. ll trail=1, res=0LL;
  39. for(ll i=0; i<p; i++){
  40. ll d = (i % 2) ? -1 : 1;
  41. trail = (trail * 2) % mod;
  42. ll ways = nCr(p, i + 1);
  43. ll term = (ways * trail) % mod;
  44. term = (term * fact[n - i - 1]) % mod;
  45. term = (d * term + mod) % mod;
  46. res = (res + term) % mod;
  47. }
  48. cout << res << endl;
  49. }
  50.  
  51. }
  52.  
Success #stdin #stdout 0.01s 5284KB
stdin
10
3 3
5 4
7 4
8 10
9 16
10 19
10 20
30 40
100 120
1000 1000
stdout
4
48
1440
22560
80640
725760
0
422027771
488057398
656420449