Submission #1073596

# Submission time Handle Problem Language Result Execution time Memory
1073596 2024-08-24T16:25:50 Z coolboy19521 Energetic turtle (IZhO11_turtle) C++17
100 / 100
19 ms 1680 KB
// Thanks Benq
 
#include "bits/stdc++.h"
#define ll long long
#define int ll
 
using namespace std;
 
const int sz = 6e5 + 1;
const int sm = 25;
 
vector<pair<int,int>> vp;
vector<int> ps;
ll wz[sm][sm];
ll dp[sm][sm];
bool sv[sz];
 
ll bpw(ll x, ll b, ll z) {
    ll r = 1;
    while (b) {
        if (b & 1)
            (r *= x) %= z;
        (x *= x) %= z;
        b >>= 1;
    }
    return r;
}
 
ll nck(ll n, ll k, ll z) {
    if (n < k || n < 0) return 0;
    if (k == 0 || n == k) return 1;
	if (k == 1 || k == n - 1) return n;
    ll r = 1;
    k = min(k, n - k);
    for (ll p : ps) {
        if (p > n) break;
        int e = 0;
        for (ll c = p; c <= n; c *= p) {
            if (c <= k) e -= k / c;
            if (c <= n - k) e -= (n - k) / c;
            e += n / c;
        }
        (r *= bpw(p, e, z)) %= z;
    }
    return r;
}
 
ll gt(pair<int,int>& f, pair<int,int>& s, int z) {
    int xds = s.first - f.first;
    int yds = s.second - f.second;
    return nck(xds + yds, yds, z);
}
 
ll sb(ll a, ll b, ll z) {
    return (a + z - b) % z;
}
 
ll ml(ll a, ll b, ll z) {
    return a * b % z;
}
 
ll ad(ll a, ll b, ll z) {
    return (a + b) % z;
}
 
signed main() {
    ll n, m, k, t, z;
    cin >> n >> m >> k >> t >> z;
    int sd = n + m + 2;
 
    for (ll i = 2; i < sd; i ++) {
        if (sv[i]) continue;
        ps.push_back(i);
        sv[i] = true;
        for (ll j = i * i; j < sd; j += i)
            sv[j] = true;
    }
 
    for (int i = 0; i < k; i ++) {
        int x, y;
        cin >> x >> y;
        vp.push_back(make_pair(x, y));
    }

    vp.push_back(make_pair(0, 0));
    vp.push_back(make_pair(n, m));
 
    sort(begin(vp), end(vp));
    k = vp.size();
 
    for (int i = 1; i < k; i ++) {
        vector<int> an(k + 2);
        for (int j = i - 1; 0 <= j; j --) {
            if (vp[j].first > vp[i].first || vp[j].second > vp[i].second) continue;
            wz[j][i] = an[j] = gt(vp[j], vp[i], z);
            for (int l = j + 1; l < i; l ++)
                wz[j][i] = sb(wz[j][i], ml(wz[j][l], an[l], z), z);
        }
    }

    dp[0][1] = 1;
 
    for (int i = 1; i < k; i ++)
        for (int j = 0; j < i; j ++)
            for (int l = 2; l <= min(i + 1, t + 2); l ++)
                dp[i][l] = ad(dp[i][l], ml(dp[j][l - 1], wz[j][i], z), z);
 
    ll r = 0;
 
    for (int i = 1; i <= t + 2; i ++)
        r = ad(r, dp[k - 1][i], z);
 
    cout << r << '\n';
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 6 ms 860 KB Output is correct
12 Correct 15 ms 1680 KB Output is correct
13 Correct 8 ms 1496 KB Output is correct
14 Correct 8 ms 924 KB Output is correct
15 Correct 8 ms 860 KB Output is correct
16 Correct 15 ms 1492 KB Output is correct
17 Correct 13 ms 1496 KB Output is correct
18 Correct 19 ms 1560 KB Output is correct
19 Correct 17 ms 1496 KB Output is correct
20 Correct 15 ms 1492 KB Output is correct