QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#320513#8211. Enumerating Substringsucup-team2112#AC ✓71ms22804kbC++205.4kb2024-02-03 17:29:412024-02-03 17:29:42

Judging History

你现在查看的是最新测评结果

  • [2024-02-03 17:29:42]
  • 评测
  • 测评结果:AC
  • 用时:71ms
  • 内存:22804kb
  • [2024-02-03 17:29:41]
  • 提交

answer

#include <bits/stdc++.h>

const int mod = 1e9 + 7;
using ll = long long;

int norm(int x) {
    if (x < 0) {
        x += mod;
    }
    if (x >= mod) {
        x -= mod;
    }
    return x;
}
 
template<class T>
T power(T a, int b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}
 
struct mint {
    int x;
    mint(int x = 0) : x(norm(x)) {}
    int val() const {
        return x;
    }
    mint operator-() const {
        return mint(norm(mod - x));
    }
    mint inv() const {
        assert(x != 0);
        return power(*this, mod - 2);
    }
    mint &operator*=(const mint &rhs) {
        x = ll(x) * rhs.x % mod;
        return *this;
    }
    mint &operator+=(const mint &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    mint &operator-=(const mint &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    mint &operator/=(const mint &rhs) {
        return *this *= rhs.inv();
    }
    friend mint operator*(const mint &lhs, const mint &rhs) {
        mint res = lhs;
        res *= rhs;
        return res;
    }
    friend mint operator+(const mint &lhs, const mint &rhs) {
        mint res = lhs;
        res += rhs;
        return res;
    }
    friend mint operator-(const mint &lhs, const mint &rhs) {
        mint res = lhs;
        res -= rhs;
        return res;
    }
    friend mint operator/(const mint &lhs, const mint &rhs) {
        mint res = lhs;
        res /= rhs;
        return res;
    }
    friend std::istream &operator>>(std::istream &is, mint &a) {
       ll v;
       is >> v;
       a = mint(v);
       return is;
   }
   friend std::ostream &operator<<(std::ostream &os, const mint &a) {
       return os << a.val();
   }
};


struct Comb {
    int n;
    std::vector<mint> _fac;
    std::vector<mint> _invfac;
    std::vector<mint> _inv;
    
    Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
    Comb(int n) : Comb() {
        init(n);
    }
    
    void init(int m) {
        if (m <= n) return;
        _fac.resize(m + 1);
        _invfac.resize(m + 1);
        _inv.resize(m + 1);
        
        for (int i = n + 1; i <= m; i++) {
            _fac[i] = _fac[i - 1] * i;
        }
        _invfac[m] = _fac[m].inv();
        for (int i = m; i > n; i--) {
            _invfac[i - 1] = _invfac[i] * i;
            _inv[i] = _invfac[i] * _fac[i - 1];
        }
        n = m;
    }
    
    mint fac(int m) {
        if (m > n) init(2 * m);
        return _fac[m];
    }
    mint invfac(int m) {
        if (m > n) init(2 * m);
        return _invfac[m];
    }
    mint inv(int m) {
        if (m > n) init(2 * m);
        return _inv[m];
    }
    mint binom(int n, int m) {
        if (n < m || m < 0) return 0;
        return fac(n) * invfac(m) * invfac(n - m);
    }
} comb;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int n, m, k;
    std::cin >> n >> m >> k;
    mint res = 0;
    std::vector<mint> pw(n + 1);
    pw[0] = 1;
    for (int i = 1; i <= n; i += 1) {
        pw[i] = pw[i - 1] * mint(k);
    }
    std::vector f(m + 1, std::vector<mint>(m + 1));
    f[0][0] = 1;
    for (int i = 1; i <= m; i += 1) {
        for (int j = 0; j <= m; j += 1) {
            if (j + 1 <= m) {
                f[i][j + 1] += f[i - 1][j];
            }
            if (j + 2 <= m) {
                f[i][j + 2] += f[i - 1][j] * comb.invfac(2);
            } 
        }
    }

    {
        mint tmp = 1;
        for (int c = 0; c <= m; c += 1) {
            res += f[c][m] * comb.fac(m) * tmp * mint(n - m + 1) * power(mint(k), n - m);
            tmp = tmp * (k - c) * comb.inv(c + 1);
        }
    }
    
    {
        mint tmp = 1;
        std::vector<mint> dp(m);
        for (int i = 1; i <= m; i += 1) {
            tmp *= k - i + 1;
            if (i + i <= m) {
                mint coef = 1;
                for (int c = 0; c <= m - 2 * i; c += 1) {
                    // std::cerr << i << " " << c << " " << f[c][m - 2 * i] * comb.fac(m - 2 * i) << " " << tmp << " " << coef << "\n";
                    dp[i] += f[c][m - 2 * i] * comb.fac(m - 2 * i) * tmp * coef;
                    coef = coef * (k - i - c) * comb.inv(c + 1);
                }
            }
            // else {
            //     int x = i - (m - i + 1) + 1;
            //     if (x + x >= i) {
            //         mint coef = 1;
            //         for (int c = 0; c <= i - 2 * x; c += 1) {
            //             dp[2 * m - x] += f[c][m - 2 * x] * comb.fac(m - 2 * x) * tmp;
            //             coef = coef * (k - x - c) * comb.inv(c + 1);
            //         }
            //     }
            // }
            // for (int len = 0; len <= 2 * m; len += 1) {
            //     res -= dp[len] * (n - len + 1) * power(mint(k), n - len); 
            // }
        }
        for (int len = 0; len < m; len += 1) {
            int sign = 0;
            for (int x = 2; x * m - (x - 1) * len <= n; x += 1) {
                int c = x * m - (x - 1) * len;
                if (sign) res += dp[len] * (n - c + 1) * pw[n - c]; 
                else res -= dp[len] * (n - c + 1) * pw[n - c]; 
                sign ^= 1;
            }
        }
    }
    std::cout << res << "\n";
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3844kb

input:

4 2 3

output:

228

result:

ok 1 number(s): "228"

Test #2:

score: 0
Accepted
time: 71ms
memory: 22804kb

input:

999999 1999 12345678

output:

52352722

result:

ok 1 number(s): "52352722"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3588kb

input:

7 4 2

output:

182

result:

ok 1 number(s): "182"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3880kb

input:

4 3 4

output:

480

result:

ok 1 number(s): "480"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3876kb

input:

3 1 1

output:

3

result:

ok 1 number(s): "3"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3632kb

input:

5 5 1

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3580kb

input:

7 4 3

output:

5784

result:

ok 1 number(s): "5784"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3684kb

input:

5 2 4

output:

3932

result:

ok 1 number(s): "3932"

Test #9:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

8 2 2

output:

1522

result:

ok 1 number(s): "1522"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

8 1 2

output:

2048

result:

ok 1 number(s): "2048"

Test #11:

score: 0
Accepted
time: 0ms
memory: 3876kb

input:

7 5 3

output:

2430

result:

ok 1 number(s): "2430"

Test #12:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

10 4 3

output:

272004

result:

ok 1 number(s): "272004"

Test #13:

score: 0
Accepted
time: 27ms
memory: 7376kb

input:

675978 614 2

output:

0

result:

ok 1 number(s): "0"

Test #14:

score: 0
Accepted
time: 6ms
memory: 4148kb

input:

244613 38 1

output:

0

result:

ok 1 number(s): "0"

Test #15:

score: 0
Accepted
time: 24ms
memory: 12332kb

input:

186293 1462 1

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 7ms
memory: 6388kb

input:

24867 886 1

output:

0

result:

ok 1 number(s): "0"

Test #17:

score: 0
Accepted
time: 41ms
memory: 10904kb

input:

976164 1014 2

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 0ms
memory: 3920kb

input:

179356 2 716844809

output:

577866092

result:

ok 1 number(s): "577866092"

Test #19:

score: 0
Accepted
time: 19ms
memory: 5716kb

input:

621001 130 310625363

output:

892869197

result:

ok 1 number(s): "892869197"

Test #20:

score: 0
Accepted
time: 28ms
memory: 8748kb

input:

678862 850 754662812

output:

582264789

result:

ok 1 number(s): "582264789"

Test #21:

score: 0
Accepted
time: 30ms
memory: 9424kb

input:

650845 978 348443366

output:

825425732

result:

ok 1 number(s): "825425732"

Test #22:

score: 0
Accepted
time: 26ms
memory: 6500kb

input:

669914 402 87448112

output:

318098088

result:

ok 1 number(s): "318098088"

Test #23:

score: 0
Accepted
time: 39ms
memory: 8320kb

input:

998593 530 681228665

output:

408255654

result:

ok 1 number(s): "408255654"

Test #24:

score: 0
Accepted
time: 42ms
memory: 19680kb

input:

369361 1954 125266115

output:

509912384

result:

ok 1 number(s): "509912384"

Test #25:

score: 0
Accepted
time: 48ms
memory: 14172kb

input:

900226 1378 424079373

output:

406320917

result:

ok 1 number(s): "406320917"

Test #26:

score: 0
Accepted
time: 28ms
memory: 13344kb

input:

334887 1506 17859926

output:

503264679

result:

ok 1 number(s): "503264679"

Test #27:

score: 0
Accepted
time: 37ms
memory: 8152kb

input:

936048 544 53978328

output:

548647866

result:

ok 1 number(s): "548647866"

Test #28:

score: 0
Accepted
time: 15ms
memory: 10176kb

input:

152789 1264 792983073

output:

839541707

result:

ok 1 number(s): "839541707"

Test #29:

score: 0
Accepted
time: 41ms
memory: 13588kb

input:

714493 1392 91796331

output:

721071046

result:

ok 1 number(s): "721071046"

Test #30:

score: 0
Accepted
time: 11ms
memory: 6848kb

input:

269571 816 830801077

output:

330064211

result:

ok 1 number(s): "330064211"

Test #31:

score: 0
Accepted
time: 41ms
memory: 9856kb

input:

845120 944 424581630

output:

348960190

result:

ok 1 number(s): "348960190"

Test #32:

score: 0
Accepted
time: 20ms
memory: 5844kb

input:

533990 368 163586376

output:

522092095

result:

ok 1 number(s): "522092095"

Test #33:

score: 0
Accepted
time: 29ms
memory: 16668kb

input:

181707 1792 462399634

output:

373795106

result:

ok 1 number(s): "373795106"

Test #34:

score: 0
Accepted
time: 44ms
memory: 19156kb

input:

417349 1920 761212891

output:

587051329

result:

ok 1 number(s): "587051329"

Test #35:

score: 0
Accepted
time: 32ms
memory: 12456kb

input:

526583 1344 500217637

output:

108767800

result:

ok 1 number(s): "108767800"

Test #36:

score: 0
Accepted
time: 34ms
memory: 8828kb

input:

867054 769 93998191

output:

239123369

result:

ok 1 number(s): "239123369"

Extra Test:

score: 0
Extra Test Passed