62402023-11-08 12:47:28Error42Tömbök előállításacpp17Accepted 100/100967ms5084 KiB
#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

template <ll M>
struct modular {
    ll val;

    // n >= 0
    modular(const ll n) {
        if (n < M)
            val = n;
        else if (n < 2 * M)
            val = n - M;
        else
            val = n % M;
    }

    [[nodiscard]] modular operator +(const modular& rhs) const {
        return val + rhs.val;
    }

    [[nodiscard]] modular operator -(const modular& rhs) const {
        return val + M - rhs.val;
    }

    [[nodiscard]] modular operator *(const modular& rhs) const {
        return val * rhs.val;
    }

    // p >= 0
    [[nodiscard]] modular pow(const ll& p) const {
        if (p == 0)
            return 1;

        if (p % 2 == 0)
            return (*this * *this).pow(p / 2);
        else
            return *this * pow(p - 1);
    }

    [[nodiscard]] modular inv() const {
        return pow(M - 2);
    }

    [[nodiscard]] modular operator /(const modular& rhs) const {
        return *this * rhs.inv();
    }

    modular& operator +=(const modular& rhs) {
        return *this = *this + rhs;
    }

    modular& operator -=(const modular& rhs) {
        return *this = *this - rhs;
    }

    modular& operator *=(const modular& rhs) {
        return *this = *this * rhs;
    }

    modular& operator /=(const modular& rhs) {
        return *this = *this / rhs;
    }

    explicit operator ll() const {
        return val;
    }
};

template <ll M>
ostream& operator<<(ostream& os, const modular<M>& modular) {
    cout << modular.val;
    return os;
}

using mod = modular<1'000'000'007>;

ll n, m, l, r, k;

vector<mod> identity;

vector<mod> combine(vector<mod> const& a, vector<mod> const& b) {
    vector<mod> ret(m, 0);
    
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
            int k = i + j >= m ? i + j - m : i + j;

            ret[k] += a[i] * b[j];
        }
    }

    return ret;
}

vector<mod> calc(ll const n) {
    if (n == 1)
        return identity;
    else if (n % 2 == 1)
        return combine(calc(n - 1), identity);
    else {
        auto const half = calc(n / 2);
        return combine(half, half);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m >> l >> r >> k;

    identity.assign(m, 0);

    for (int i = 0; i < m; i++) {
        identity[i] += (r - i + m) / m;
        identity[i] -= (l - 1 - i + m) / m;
    }

    auto const ans = calc(n);

    cout << ans[k] << "\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1704 KiB
2Accepted3ms1864 KiB
3Accepted3ms2088 KiB
subtask210/10
4Accepted3ms2300 KiB
5Accepted3ms2500 KiB
6Accepted3ms2708 KiB
subtask310/10
7Accepted2ms2816 KiB
8Accepted3ms2920 KiB
9Accepted3ms2944 KiB
10Accepted2ms3008 KiB
11Accepted2ms3112 KiB
12Accepted2ms3108 KiB
13Accepted2ms3104 KiB
subtask47/7
14Accepted3ms3136 KiB
15Accepted3ms3328 KiB
16Accepted2ms3412 KiB
17Accepted2ms3408 KiB
18Accepted2ms3520 KiB
19Accepted3ms3736 KiB
20Accepted3ms3960 KiB
subtask58/8
21Accepted35ms4192 KiB
22Accepted17ms4080 KiB
23Accepted23ms4212 KiB
24Accepted14ms4064 KiB
25Accepted8ms4152 KiB
26Accepted4ms4156 KiB
27Accepted25ms4160 KiB
28Accepted4ms4284 KiB
29Accepted35ms4376 KiB
30Accepted35ms4276 KiB
subtask625/25
31Accepted6ms4472 KiB
32Accepted6ms4472 KiB
33Accepted4ms4472 KiB
34Accepted4ms4572 KiB
35Accepted4ms4568 KiB
36Accepted4ms4472 KiB
37Accepted3ms4476 KiB
38Accepted3ms4484 KiB
39Accepted4ms4484 KiB
40Accepted7ms4612 KiB
subtask740/40
41Accepted268ms4708 KiB
42Accepted855ms5000 KiB
43Accepted967ms5008 KiB
44Accepted961ms5008 KiB
45Accepted930ms5008 KiB
46Accepted250ms4944 KiB
47Accepted393ms4940 KiB
48Accepted4ms5000 KiB
49Accepted661ms5084 KiB
50Accepted305ms4992 KiB