Advertisement
a53

Potter

a53
Feb 7th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. ifstream f("potter.in");
  6. ofstream g("potter.out");
  7.  
  8. const int NMAX = 2e3 + 1;
  9. const int MOD = 1e9+7;
  10.  
  11. int N, M, T, K;
  12.  
  13. bool Line[NMAX], Column[NMAX];
  14.  
  15. int empty_cells;
  16.  
  17. static inline void Read ()
  18. {
  19. f.tie(nullptr);
  20.  
  21. f >> N >> M >> T >> K;
  22.  
  23. for(int i = 1; i <= T; ++i)
  24. {
  25. int L = 0, C = 0;
  26. f >> L >> C;
  27.  
  28. Line[L] = 1, Column[C] = 1;
  29. }
  30.  
  31. return;
  32. }
  33.  
  34. static inline void Load ()
  35. {
  36. int l = 0, c = 0;
  37.  
  38. for(int i = 1; i <= N; ++i)
  39. if(Line[i])
  40. ++l;
  41.  
  42. for(int i = 1; i <= M; ++i)
  43. if(Column[i])
  44. ++c;
  45.  
  46. empty_cells = N * M - (l * M + c * N - l * c);
  47.  
  48. return;
  49. }
  50.  
  51. static inline int lgput (int n, int p)
  52. {
  53. int ans = 1, aux = n;
  54.  
  55. for(int i = 0; (1 << i) <= p; ++i)
  56. {
  57. if(p & (1 << i))
  58. ans = (1LL * ans * aux) % (1LL * MOD);
  59.  
  60. aux = (1LL * aux * aux) % (1LL * MOD);
  61. }
  62.  
  63. return ans;
  64. }
  65.  
  66. static inline int Comb (int n, int k)
  67. {
  68. if(n < k)
  69. return 0;
  70.  
  71. if(n == k)
  72. return 1;
  73.  
  74. int ans = 1;
  75.  
  76. for(int i = 2; i <= n; ++i)
  77. ans = (1LL * ans * i) % (1LL * MOD);
  78.  
  79. int inv_mod = lgput(ans, MOD - 2);
  80. int last_mod = inv_mod;
  81.  
  82. for(int i = (n - 1); i >= 1; --i)
  83. {
  84. inv_mod = (1LL * last_mod * (i + 1)) % (1LL * MOD);
  85.  
  86. if(i == k)
  87. ans = (1LL * ans * inv_mod) % (1LL * MOD);
  88.  
  89. if(i == (n - k))
  90. ans = (1LL * ans * inv_mod) % (1LL * MOD);
  91.  
  92. last_mod = inv_mod;
  93. }
  94.  
  95. return ans;
  96. }
  97.  
  98. static inline void Solve ()
  99. {
  100. g << Comb(empty_cells - 1 + K, empty_cells - 1) << '\n';
  101.  
  102. return;
  103. }
  104.  
  105. int main()
  106. {
  107. Read();
  108.  
  109. Load();
  110.  
  111. Solve();
  112.  
  113. return 0;
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement