Submission #960156

# Submission time Handle Problem Language Result Execution time Memory
960156 2024-04-09T18:35:39 Z Pety Magneti (COCI21_magneti) C++17
110 / 110
246 ms 106580 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1e6+2;
const int mod = 1e9 + 7;
template <class T> T pow(T a, int64_t exp) {
  T res(1);
  while (exp) {
    if (exp % 2)
      res = res * a;
    a = a * a;
    exp /= 2;
  }
  return res;
}

template <decltype(auto) mode> struct ZP;
using ZPP = ZP<mod>;
template <decltype(auto) mod, decltype(auto) R> struct Poly {
  using ZPP = ZP<mod>;

  Poly(ZPP x = 0) : A(0), B(x) {}
  Poly(ZPP A, ZPP B) : A(A), B(B) {}

  Poly operator*(Poly that) const {
    ZPP a = A * that.A;
    ZPP b = A * that.B + B * that.A;
    ZPP c = B * that.B;
    return Poly(b, c + a * R);
  }

  ZPP A, B;
};

template <decltype(auto) mod> struct ZP {
  ZP(int64_t x = 0) : x(x % mod) {
    if (this->x < 0)
      this->x += mod;
  }

  ZP operator*(ZP that) const { return ZP(int64_t(x) * that.x); }
  ZP &operator+=(ZP that) {
    if ((x += that.x) >= mod)
      x -= mod;
    return *this;
  }
  ZP operator-=(ZP that) {
    if ((x -= that.x) < 0)
      x += mod;
    return *this;
  }
  ZP operator*=(ZP that) { return *this = *this * that; }

  ZP operator-(ZP that) const { return ZP(*this) -= that; }
  ZP operator+(ZP that) const { return ZP(*this) += that; }
  ZP operator-() const { return ZP(mod - x); }
  bool operator==(ZP that) const { return x == that.x; }
  bool operator!=(ZP that) const { return x != that.x; }
  friend ZP operator+(int x, ZP that) { return ZP(x) + that; }
  friend ZP operator-(int x, ZP that) { return ZP(x) - that; }
  ZP operator/(ZP that) const { return *this * that.inv(); }

  ZP inv() const { return pow(*this, mod - 2); }
  explicit operator int() const { return x; }
  explicit operator bool() const { return x; }

  friend ostream &operator<<(ostream &stream, ZP that) {
    return stream << that.x;
  }
  bool operator<(ZP that) const { return x < that.x; }

  optional<ZP> sqrt() {
    if (x < 2) {
      return *this;
    }

    if (pow(*this, (mod - 1) / 2) == -1)
      return nullopt;

    static mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

    while (true) {
      ZP z = rnd();
      if (z * z == *this)
        return z;

      static ZP value;
      value = *this;
      Poly<(mod), (value)> P(1, z);
      P = pow(P, mod / 2);
      if (P.A != 0) {
        assert(P.A.inv() * P.A.inv() == *this);
        return P.A.inv();
      }
    }
  }

  int x;
};

ZPP dp[52][52][10002];
ZPP fact[10100];
int n, l, a[52];

ZPP comb (int n, int k) {
  return fact[n] / fact[k] / fact[n - k];
}
 
int main () 
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n >> l;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  fact[0] = 1;
  for (int i = 1; i <= l + n; i++)
    fact[i] = fact[i - 1] * i;
  sort(a + 1, a + n + 1);
  dp[0][0][1] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++)
      for (int k = 0; k <= l; k++) {
        dp[i + 1][j + 1][k] += ZPP (j + 1) * dp[i][j][k];
        if (j && k + a[i + 1] <= l)
          dp[i + 1][j][k + a[i + 1]] += ZPP(2*j)*dp[i][j][k];
        if (j > 1 && k + 2 * a[i + 1] <= l)
          dp[i + 1][j - 1][k + 2 * a[i + 1]] += ZPP(j - 1) * dp[i][j][k];
      }
  }
  ZPP ans = 0;
  for (int d = 1; d <= l; d++) {
    ans += comb(l - d + n, n) * dp[n][1][d];
  }
  cout << ans;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 239 ms 106332 KB Output is correct
2 Correct 31 ms 106332 KB Output is correct
3 Correct 42 ms 106216 KB Output is correct
4 Correct 35 ms 106324 KB Output is correct
5 Correct 41 ms 106328 KB Output is correct
6 Correct 88 ms 106328 KB Output is correct
7 Correct 85 ms 106332 KB Output is correct
8 Correct 31 ms 106160 KB Output is correct
9 Correct 61 ms 106136 KB Output is correct
10 Correct 31 ms 106328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 106328 KB Output is correct
2 Correct 32 ms 106340 KB Output is correct
3 Correct 43 ms 106168 KB Output is correct
4 Correct 34 ms 106324 KB Output is correct
5 Correct 42 ms 106324 KB Output is correct
6 Correct 34 ms 106200 KB Output is correct
7 Correct 41 ms 106332 KB Output is correct
8 Correct 32 ms 106088 KB Output is correct
9 Correct 33 ms 106324 KB Output is correct
10 Correct 35 ms 106576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 106236 KB Output is correct
2 Correct 34 ms 106328 KB Output is correct
3 Correct 33 ms 106324 KB Output is correct
4 Correct 32 ms 106324 KB Output is correct
5 Correct 35 ms 106320 KB Output is correct
6 Correct 31 ms 106324 KB Output is correct
7 Correct 33 ms 106196 KB Output is correct
8 Correct 33 ms 106156 KB Output is correct
9 Correct 32 ms 106116 KB Output is correct
10 Correct 32 ms 106332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 239 ms 106332 KB Output is correct
2 Correct 31 ms 106332 KB Output is correct
3 Correct 42 ms 106216 KB Output is correct
4 Correct 35 ms 106324 KB Output is correct
5 Correct 41 ms 106328 KB Output is correct
6 Correct 88 ms 106328 KB Output is correct
7 Correct 85 ms 106332 KB Output is correct
8 Correct 31 ms 106160 KB Output is correct
9 Correct 61 ms 106136 KB Output is correct
10 Correct 31 ms 106328 KB Output is correct
11 Correct 40 ms 106328 KB Output is correct
12 Correct 32 ms 106340 KB Output is correct
13 Correct 43 ms 106168 KB Output is correct
14 Correct 34 ms 106324 KB Output is correct
15 Correct 42 ms 106324 KB Output is correct
16 Correct 34 ms 106200 KB Output is correct
17 Correct 41 ms 106332 KB Output is correct
18 Correct 32 ms 106088 KB Output is correct
19 Correct 33 ms 106324 KB Output is correct
20 Correct 35 ms 106576 KB Output is correct
21 Correct 33 ms 106236 KB Output is correct
22 Correct 34 ms 106328 KB Output is correct
23 Correct 33 ms 106324 KB Output is correct
24 Correct 32 ms 106324 KB Output is correct
25 Correct 35 ms 106320 KB Output is correct
26 Correct 31 ms 106324 KB Output is correct
27 Correct 33 ms 106196 KB Output is correct
28 Correct 33 ms 106156 KB Output is correct
29 Correct 32 ms 106116 KB Output is correct
30 Correct 32 ms 106332 KB Output is correct
31 Correct 230 ms 106324 KB Output is correct
32 Correct 156 ms 106328 KB Output is correct
33 Correct 226 ms 106308 KB Output is correct
34 Correct 72 ms 106324 KB Output is correct
35 Correct 246 ms 106576 KB Output is correct
36 Correct 43 ms 106324 KB Output is correct
37 Correct 230 ms 106324 KB Output is correct
38 Correct 63 ms 106332 KB Output is correct
39 Correct 228 ms 106324 KB Output is correct
40 Correct 88 ms 106332 KB Output is correct
41 Correct 244 ms 106324 KB Output is correct
42 Correct 32 ms 106276 KB Output is correct
43 Correct 226 ms 106580 KB Output is correct
44 Correct 54 ms 106328 KB Output is correct
45 Correct 225 ms 106324 KB Output is correct
46 Correct 35 ms 106576 KB Output is correct
47 Correct 66 ms 106208 KB Output is correct
48 Correct 46 ms 106332 KB Output is correct
49 Correct 37 ms 106436 KB Output is correct
50 Correct 32 ms 106332 KB Output is correct