Submission #217945

# Submission time Handle Problem Language Result Execution time Memory
217945 2020-03-31T09:40:11 Z Vimmer Zapina (COCI20_zapina) C++14
110 / 110
196 ms 3320 KB
#include <bits/stdc++.h>

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 100005
#define M ll(1e9 + 7)

using namespace std;

typedef long double ld;
typedef long long ll;
typedef short int si;

ll fx(ll a, ll b) {return (a + b) % M;}

ll mx(ll a, ll b) {return (a * b) % M;}

ll binpow(ll a, ll b)
{
    if (b == 0) return 1ll;

    ll s = binpow(a, b / 2);

    if (b % 2) return mx(mx(s, s), a);

    return mx(s, s);
}

ll dp[351][351][2];

ll fact[351], binnom[351][351];

ll calc(ll n, ll k)
{
    if (k == 0) return 1;

    return mx(fact[n], binpow(mx(fact[n - k], fact[k]), M - 2));
}

int main()
{
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n;

    cin >> n;

    fact[0] = 1;

    fact[1] = 1;

    for (ll i = 2; i <= n; i++) fact[i] = mx(fact[i - 1], i);

    for (ll i = 0; i <= n; i++)
      for (ll j = 0; j <= i; j++) binnom[i][j] = calc(i, j);

    dp[0][n][0] = 1;

    for (ll i = 2; i <= n; i++) dp[0][n - i][0] = binnom[n][i];

    dp[0][n - 1][1] = binnom[n][1];

    for (int i = 0; i < n - 1; i++)
      for (int j = 0; j <= n; j++)
        for (int f = 0; f <= 1; f++)
          {
              for (int u = 0; u <= j; u++)
              {
                if (u == i + 2) dp[i + 1][j - u][1] = fx(dp[i + 1][j - u][1], mx(binnom[j][u], dp[i][j][f]));
                     else  dp[i + 1][j - u][f] = fx(dp[i + 1][j - u][f], mx(binnom[j][u], dp[i][j][f]));
              }
        }

    cout << dp[n - 1][0][1] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 416 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 416 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 98 ms 2536 KB Output is correct
18 Correct 6 ms 768 KB Output is correct
19 Correct 27 ms 1664 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 124 ms 2808 KB Output is correct
22 Correct 22 ms 1536 KB Output is correct
23 Correct 6 ms 768 KB Output is correct
24 Correct 33 ms 1828 KB Output is correct
25 Correct 26 ms 1664 KB Output is correct
26 Correct 41 ms 1912 KB Output is correct
27 Correct 180 ms 3192 KB Output is correct
28 Correct 180 ms 3192 KB Output is correct
29 Correct 180 ms 3192 KB Output is correct
30 Correct 184 ms 3320 KB Output is correct
31 Correct 183 ms 3192 KB Output is correct
32 Correct 187 ms 3152 KB Output is correct
33 Correct 185 ms 3192 KB Output is correct
34 Correct 186 ms 3192 KB Output is correct
35 Correct 190 ms 3192 KB Output is correct
36 Correct 186 ms 3192 KB Output is correct
37 Correct 196 ms 3320 KB Output is correct