Advertisement
nnv-nick

Untitled

Nov 27th, 2022
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. #define endl '\n'
  3. #define ll long long
  4. #define double long double
  5. using namespace std;
  6. const ll inf = 1000000000000000000;
  7.  
  8. const ll mod = 998244353;
  9. ll n, x, a[100005], b[100005], kol21, kol6, kolnech, lef, rig, mid, ans, sum_begin_without_21;
  10. bool check(ll k2, ll k3) {
  11.     // First stage
  12.     ll sum = sum_begin_without_21;
  13.     if (k3 <= kolnech) {
  14.         kolnech -= k3;
  15.         sum -= k3 * 3;
  16.         ll twos_needed = (kol21 + (sum - kolnech) / 2 + kolnech);
  17.         return (twos_needed <= k2);
  18.     }
  19.     sum -= kolnech * 3;
  20.     k3 -= kolnech;
  21.     kolnech = 0;
  22.     // Second stage
  23.     if (k3 <= kol6 * 2) {
  24.         if (k3 % 2 == 1) {
  25.             ll used_3 = k3 - 1;
  26.             sum -= used_3 * 3;
  27.             sum -= 3;
  28.             kolnech = 1;
  29.             ll twos_needed = (kol21 + (sum - kolnech) / 2 + kolnech);
  30.             return (twos_needed <= k2);
  31.         } else {
  32.             sum -= k3 * 3;
  33.             ll twos_needed = (kol21 + sum / 2);
  34.             return (twos_needed <= k2);
  35.         }
  36.     }
  37.     k3 -= kol6 * 2;
  38.     sum -= kol6 * 6;
  39.     return (kol21 + (sum / 2) <= k2 + k3);
  40. }
  41.  
  42. int main() {
  43.     freopen("input.txt", "r", stdin);
  44.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  45.     cin >> n >> x;
  46.     for (ll i = 1; i <= n; ++i) {
  47.         cin >> a[i];
  48.         b[i] = a[i];
  49.         if (a[i] <= 2) {
  50.             ++kol21;
  51.         } else {
  52.             kolnech += (a[i] % 2);
  53.             sum_begin_without_21 += a[i];
  54.         }
  55.     }
  56.     for (ll i = 1; i <= n; ++i) {
  57.         if (b[i] >= 3 && b[i] % 2 == 1) {
  58.             b[i] -= 3;
  59.         }
  60.         kol6 += (b[i] / 6);
  61.     }
  62.     for (ll i = 0; i <= x; ++i) {
  63.         if (check(i, 0)) {
  64.             continue;
  65.         }
  66.         lef = 0;
  67.         rig = 1000000000;
  68.         while (rig - lef > 1) {
  69.             mid = lef + (rig - lef) / 2;
  70.             if (check(i, mid)) {
  71.                 rig = mid;
  72.             } else {
  73.                 lef = mid;
  74.             }
  75.         }
  76.         ans += rig;
  77.         ans %= mod;
  78.     }
  79.     cout << ans << endl;
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement