62392023-11-08 12:46:21Error42Tömbök előállításacpp17Time limit exceeded 60/1002.049s5556 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;

            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
1Accepted3ms1828 KiB
2Accepted3ms2028 KiB
3Accepted3ms2240 KiB
subtask210/10
4Accepted3ms2456 KiB
5Accepted2ms2552 KiB
6Accepted3ms2648 KiB
subtask310/10
7Accepted2ms2548 KiB
8Accepted3ms2660 KiB
9Accepted3ms2784 KiB
10Accepted3ms2920 KiB
11Accepted3ms3132 KiB
12Accepted3ms3240 KiB
13Accepted3ms3452 KiB
subtask47/7
14Accepted3ms3540 KiB
15Accepted3ms3592 KiB
16Accepted3ms3796 KiB
17Accepted3ms4012 KiB
18Accepted3ms4248 KiB
19Accepted3ms4208 KiB
20Accepted3ms4208 KiB
subtask58/8
21Accepted79ms4224 KiB
22Accepted39ms4344 KiB
23Accepted50ms4560 KiB
24Accepted32ms4652 KiB
25Accepted17ms4740 KiB
26Accepted8ms4868 KiB
27Accepted57ms4964 KiB
28Accepted12ms4964 KiB
29Accepted79ms5068 KiB
30Accepted79ms5200 KiB
subtask625/25
31Accepted17ms5276 KiB
32Accepted17ms5308 KiB
33Accepted8ms5332 KiB
34Accepted8ms5316 KiB
35Accepted6ms5324 KiB
36Accepted8ms5556 KiB
37Accepted3ms5420 KiB
38Accepted3ms5420 KiB
39Accepted10ms5428 KiB
40Accepted13ms5432 KiB
subtask70/40
41Accepted592ms5448 KiB
42Accepted1.904s5544 KiB
43Time limit exceeded2.026s5296 KiB
44Time limit exceeded2.049s5352 KiB
45Time limit exceeded2.046s5432 KiB
46Accepted554ms5460 KiB
47Accepted888ms5388 KiB
48Accepted12ms5368 KiB
49Accepted1.465s5492 KiB
50Accepted683ms5464 KiB