Advertisement
a53

The Grade

a53
Nov 21st, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cassert>
  4.  
  5. #define MOD 1000000007LL
  6.  
  7. using namespace std;
  8.  
  9. long long fact[1000010], inv[1000010];
  10. int frq[1000010];
  11.  
  12. long long put (long long n, long long p)
  13. {
  14. long long rez = 1LL;
  15. for (long long i = 0LL; (1LL << i) <= p; i += 1LL)
  16. {
  17. if ((1LL << i) & p) rez *= n, rez %= MOD;
  18.  
  19. n *= n;
  20. n %= MOD;
  21. }
  22.  
  23. return rez;
  24. }
  25.  
  26. int main ()
  27. {
  28. // freopen ("input", "r", stdin);
  29. //freopen ("output4", "w", stdout);
  30.  
  31. int q, p;
  32. cin >> q >> p;
  33.  
  34. assert (1 <= q && q <= 1000000);
  35. assert (1 <= p && p <= 100000);
  36.  
  37. fact[0] = inv[0] = 1LL;
  38. for (int i = 1; i <= max (2 * p, q); ++i)
  39. {
  40. fact[i] = 1LL * i * fact[i - 1];
  41. fact[i] %= MOD;
  42.  
  43. inv[i] = put (1LL * fact[i], MOD - 2LL);
  44. }
  45.  
  46. int n = 0;
  47. long long sum = 0LL, perm = 1LL;
  48. for (; q; --q)
  49. {
  50. int op, x;
  51. cin >> op >> x;
  52.  
  53. if (!op)
  54. {
  55. ++n;
  56. sum += 1LL * x;
  57.  
  58. perm *= 1LL * n;
  59. perm %= MOD;
  60.  
  61. if (perm == 0LL)
  62. {
  63. //fprintf (stderr, "1");
  64. return 0;
  65. }
  66.  
  67. perm *= fact[frq[x]];
  68. perm %= MOD;
  69.  
  70. if (perm == 0LL)
  71. {
  72. //fprintf (stderr, "2");
  73. return 0;
  74. }
  75.  
  76. ++frq[x];
  77. perm *= inv[frq[x]];
  78. perm %= MOD;
  79.  
  80. if (perm == 0LL)
  81. {
  82. //fprintf (stderr, "3");
  83. return 0;
  84. }
  85. }
  86.  
  87. else
  88. {
  89. perm *= inv[n];
  90. perm %= MOD;
  91.  
  92. if (perm == 0LL)
  93. {
  94. //fprintf (stderr, "4");
  95. return 0;
  96. }
  97.  
  98. --n;
  99. sum -= 1LL * x;
  100.  
  101. perm *= fact[n];
  102. perm %= MOD;
  103. if (perm == 0LL)
  104. {
  105. //fprintf (stderr, "5");
  106. return 0;
  107. }
  108.  
  109. perm *= fact[frq[x]];
  110. perm %= MOD;
  111. if (perm == 0LL)
  112. {
  113. //fprintf (stderr, "6");
  114. return 0;
  115. }
  116.  
  117. --frq[x];
  118. perm *= inv[frq[x]];
  119. perm %= MOD;
  120. if (perm == 0LL)
  121. {
  122. //fprintf (stderr, "%d", q);
  123. return 0;
  124. }
  125. }
  126.  
  127. if (sum > 1LL * p)
  128. {
  129. cout << "-1\n";
  130. continue;
  131. }
  132.  
  133. long long aux = 1LL * (p + n) - sum;
  134.  
  135. long long rez = fact[aux] * inv[n];
  136. rez %= MOD;
  137.  
  138. rez *= inv[aux - n];
  139. rez %= MOD;
  140.  
  141. rez *= perm;
  142. rez %= MOD;
  143.  
  144. cout << rez << '\n';
  145. }
  146.  
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement