fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29. //* Case 2 : n <= 1e9 && r <= 20
  30.  
  31. //* With Modulo :
  32. //* TC = O(r + logM) SC = O(1)
  33. int nCr(int n, int r){
  34. if(r>n || r<0) return 0;
  35.  
  36. if(r>n-r) r = n-r; //* optimization nCr == nCn-r
  37.  
  38. int res = 1;
  39. for(int i=1; i<=r; i++){
  40. res = (res * (n-i+1)) % M;
  41. }
  42.  
  43. int rFact = 1;
  44. for(int i=1; i<=r; i++){
  45. rFact = (rFact * i)%M;
  46. }
  47.  
  48. res = (res%M * power(rFact, M-2, M) ) % M;
  49.  
  50. return res;
  51. }
  52.  
  53.  
  54.  
  55. //* Without Modulo :
  56. //* TC = O(r) , SC = O(1)
  57. int nCr2(int n, int r){
  58. if(r>n || r<0) return 0;
  59.  
  60. if(r>n-r) r = n-r; //* optimization nCr == nCn-r
  61.  
  62. int res = 1;
  63. for(int i = 1; i <= r; i++){
  64. res = (res * (n - i + 1)) / i;
  65. }
  66.  
  67. return res;
  68. }
  69.  
  70.  
  71.  
  72. //* recursive
  73. //* TC = O(rLogM) SC = O(r)
  74. int nCr3(int n, int r, int M) {
  75. if (r > n || r < 0) return 0;
  76. if (r == 0) return 1;
  77.  
  78. if (r > n - r) r = n - r;
  79.  
  80. int res = (n * nCr3(n-1,r-1, M) ) % M;
  81. res = (res * power(r, M - 2, M)) % M;
  82.  
  83. return res;
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90. int consistency(int n, int r){
  91.  
  92. return nCr(n, r);
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. int practice(int n, int r){
  111.  
  112.  
  113.  
  114. return 0;
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121. void solve() {
  122.  
  123. int n, r;
  124. cin>> n >> r;
  125.  
  126.  
  127. cout << consistency(n, r) << endl;
  128.  
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136. int32_t main() {
  137. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  138.  
  139. int t = 1;
  140. cin >> t;
  141. while (t--) {
  142. solve();
  143. }
  144.  
  145. return 0;
  146. }
Success #stdin #stdout 0.01s 5324KB
stdin
4
5 2
3 5
10 2
4 3
stdout
10
0
45
4