Advertisement
tien_noob

VOI - OR - 4 subs

Feb 21st, 2022
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. //Make CSP great again
  2. //You're as beautiful as the day I lost you
  3. #include <bits/stdc++.h>
  4. #define TASK "TESTCODE"
  5. #define Log2(x) 31 - __builtin_clz(x)
  6. using namespace std;
  7. const int mod = 1e9 + 7, N = 1e6, M = 200;
  8. int f[M + 1][1000], dp[N + 1], cnt[N + 1], a[N + 1], fac[N + 1], MaxA, n, L, R, k;
  9. void read()
  10. {
  11.     cin >> n >> k >> L >> R;
  12.     for (int i = 1; i <= n; ++ i)
  13.     {
  14.         cin >> a[i];
  15.         ++cnt[a[i]];
  16.         MaxA = max(MaxA, a[i]);
  17.     }
  18. }
  19. void Sub2()
  20. {
  21.     f[0][0] = 1;
  22.     for (int i = 1; i <= n; ++ i)
  23.     {
  24.         for (int j = k - 1; j >= 0; -- j)
  25.         {
  26.             for (int mask = 0; mask < (1 << 8); ++ mask)
  27.             {
  28.                 f[j + 1][mask | a[i]] += f[j][mask];
  29.                 f[j + 1][mask | a[i]] %= mod;
  30.             }
  31.         }
  32.     }
  33.     int res = 0;
  34.     for (int i = (L + 2)/3 * 3; i <= min(255, R); i += 3)
  35.     {
  36.         res += f[k][i];
  37.         res %= mod;
  38.     }
  39.     cout << res;
  40. }
  41. int Pow(int a, int b)
  42. {
  43.     int ret = 1;
  44.     for (; b > 0; b >>= 1)
  45.     {
  46.         if (b & 1)
  47.         {
  48.             ret = (1LL * ret * a) % mod;
  49.         }
  50.         a = (1LL * a * a) % mod;
  51.     }
  52.     return ret;
  53. }
  54. int C(int k, int n)
  55. {
  56.     if (k > n)
  57.     {
  58.         return 0;
  59.     }
  60.     return (1LL * ((1LL * fac[n] * Pow(fac[n - k], mod - 2)) % mod) * Pow(fac[k], mod - 2)) % mod;;
  61. }
  62. void Sub4()
  63. {
  64.     if (L % 3 != 0)
  65.     {
  66.         cout << 0;
  67.         return ;
  68.     }
  69.     fac[0] = 1;
  70.     for (int i = 1; i <= N; ++ i)
  71.     {
  72.         fac[i] = (1LL * fac[i - 1] * i) % mod;
  73.     }
  74.     for (int i = 0; i <= N; ++ i)
  75.     {
  76.         dp[i] = cnt[i];
  77.     }
  78.     for (int i = 0; i < 20; ++ i)
  79.     {
  80.         for (int mask = (1 << 20) - 1; mask >= 0; -- mask)
  81.         {
  82.             if ((mask >> i) & 1)
  83.             {
  84.                 dp[mask] += dp[mask ^ (1 << i)];
  85.             }
  86.         }
  87.     }
  88.     int res = 0;
  89.     int x = __builtin_popcount(L);
  90.     for (int mask = L; mask > 0; mask = (mask - 1) & L)
  91.     {
  92.         int t = abs(x - __builtin_popcount(mask)) & 1;
  93.         if (t == 0)
  94.         {
  95.             res += C(k, dp[mask]);
  96.             res %= mod;
  97.         }
  98.         else
  99.         {
  100.             res -= C(k, dp[mask]);
  101.             res %= mod;
  102.         }
  103.     }
  104.     cout << (res + mod) % mod;
  105. }
  106. void solve()
  107. {
  108.     if (n <= M && MaxA <= M)
  109.     {
  110.         Sub2();
  111.         return ;
  112.     }
  113.     if (L == R)
  114.     {
  115.         Sub4();
  116.         return;
  117.     }
  118. }
  119. int main()
  120. {
  121.     ios_base::sync_with_stdio(false);
  122.     cin.tie(nullptr);
  123.     if (fopen(TASK".INP", "r"))
  124.     {
  125.         freopen(TASK".INP", "r", stdin);
  126.         //freopen(TASK".OUT", "w", stdout);
  127.     }
  128.     int t = 1;
  129.     bool typetest = false;
  130.     if (typetest)
  131.     {
  132.         cin >> t;
  133.     }
  134.     for (int __ = 1; __ <= t; ++ __)
  135.     {
  136.         //cout << "Case " << __ << ": ";
  137.         read();
  138.         solve();
  139.     }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement