QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#331657#8211. Enumerating Substrings8BQube#AC ✓134ms42124kbC++203.2kb2024-02-18 16:33:152024-02-18 16:33:16

Judging History

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

  • [2024-02-18 16:33:16]
  • 评测
  • 测评结果:AC
  • 用时:134ms
  • 内存:42124kb
  • [2024-02-18 16:33:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SZ(a) ((int)a.size())

const int MOD = 1e9 + 7;
const int MAXC = 4005;
const int MAXN = 2e6;

void add(int &x, int val) {
    x += val;
    if (x >= MOD) x -= MOD;
}

int pw(int a, int n) {
    int res = 1;
    for (; n; n >>= 1, a = (ll)a * a % MOD)
        if (n & 1)
            res = (ll)res * a % MOD;
    return res;
}

int rC[MAXC][MAXC], fac[MAXC], inv[MAXC], ipw2[MAXC];

int C(int n, int m) { // O(m)
    if (n < m) return 0;
    int res = 1;
    for (int i = 0; i < m; ++i) {
        res = (ll)res * (n - i) % MOD;
        res = (ll)res * inv[i + 1] % MOD;
    }
    return res;
}

int P(int n, int m) { // O(m)
    if (n < m) return 0;
    int res = 1;
    for (int i = 0; i < m; ++i) {
        res = (ll)res * (n - i) % MOD;
    }
    return res;
}

int S[MAXC], L[MAXC][MAXC], len[MAXN];

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    fac[0] = 1;
    for (int i = 1; i < MAXC; ++i)
        fac[i] = (ll)fac[i - 1] * i % MOD;
    inv[1] = 1;
    for (int i = 2; i < MAXC; ++i)
        inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD;
    ipw2[0] = 1;
    for (int i = 1; i < MAXC; ++i)
        ipw2[i] = (ll)ipw2[i - 1] * inv[2] % MOD;
    int n, m, k, ans = 0;
    cin >> n >> m >> k;

    for (int i = 0; i <= m; ++i) {
        rC[i][0] = 1;
        for (int j = 1; j <= m && j <= (k - i); ++j)
            rC[i][j] = (ll)rC[i][j - 1] * (k - i - (j - 1)) % MOD * inv[j] % MOD; 
    }

    auto get_C = [&](int p, int q) {
        if (p < q) return 0;
        return rC[k - p][q];  
    };

    /*for (int d = 0; d <= m / 2; ++d) { // need optimized
        for (int x = 0; x <= m; ++x) {
            for (int i = 0; i <= x / 2; ++i)
                add(L[d][x], (ll)get_C(k - d, i) * get_C(k - i - d, x - 2 * i) % MOD * fac[x] % MOD * ipw2[i] % MOD);
            //cerr << "L " << d << " " << x << " = " << L[d][x] << "\n";
        }
    }*/

    auto get_L = [&](int d, int x) {
        int res = 0;   
        for (int i = 0; i <= x / 2; ++i)
            add(res, (ll)get_C(k - d, i) * get_C(k - i - d, x - 2 * i) % MOD * fac[x] % MOD * ipw2[i] % MOD);
        return res;
    };

    for (int x = 1; x <= m / 2; ++x) {
        S[x] = (ll)P(k, x) * get_L(x, m - 2 * x) % MOD;
        //cerr << "S " << x << " = " << S[x] << "\n";
    }
    for (int x = 1; x <= n; ++x) {
        len[x] = (ll)(n - x + 1) * pw(k, n - x) % MOD;
        //cerr << "len " << x << " = " << len[x] << "\n";
    }

    // case 1
    for (int x = 1; x <= m / 2; ++x) { 
        int sum = 0;
        for (int j = 1; (x + (m - x) * j) <= n; ++j)
            if (j & 1)
                add(sum, len[x + (m - x) * j]);
            else
                add(sum, MOD - len[x + (m - x) * j]);
        add(ans, (ll)S[x] * sum % MOD);
    }
    //cerr << "after case1: " << ans << "\n";

    // case 2
    int sum = get_L(0, m);
    for (int x = 1; x <= m / 2; ++x)
        add(sum, MOD - S[x]);
    add(ans, (ll)len[m] * sum % MOD);

    cout << ans << "\n";
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5660kb

input:

4 2 3

output:

228

result:

ok 1 number(s): "228"

Test #2:

score: 0
Accepted
time: 134ms
memory: 42124kb

input:

999999 1999 12345678

output:

52352722

result:

ok 1 number(s): "52352722"

Test #3:

score: 0
Accepted
time: 1ms
memory: 5776kb

input:

7 4 2

output:

182

result:

ok 1 number(s): "182"

Test #4:

score: 0
Accepted
time: 1ms
memory: 5720kb

input:

4 3 4

output:

480

result:

ok 1 number(s): "480"

Test #5:

score: 0
Accepted
time: 1ms
memory: 5780kb

input:

3 1 1

output:

3

result:

ok 1 number(s): "3"

Test #6:

score: 0
Accepted
time: 1ms
memory: 5804kb

input:

5 5 1

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 1ms
memory: 5708kb

input:

7 4 3

output:

5784

result:

ok 1 number(s): "5784"

Test #8:

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

input:

5 2 4

output:

3932

result:

ok 1 number(s): "3932"

Test #9:

score: 0
Accepted
time: 1ms
memory: 5744kb

input:

8 2 2

output:

1522

result:

ok 1 number(s): "1522"

Test #10:

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

input:

8 1 2

output:

2048

result:

ok 1 number(s): "2048"

Test #11:

score: 0
Accepted
time: 1ms
memory: 5708kb

input:

7 5 3

output:

2430

result:

ok 1 number(s): "2430"

Test #12:

score: 0
Accepted
time: 1ms
memory: 5736kb

input:

10 4 3

output:

272004

result:

ok 1 number(s): "272004"

Test #13:

score: 0
Accepted
time: 61ms
memory: 20244kb

input:

675978 614 2

output:

0

result:

ok 1 number(s): "0"

Test #14:

score: 0
Accepted
time: 16ms
memory: 8660kb

input:

244613 38 1

output:

0

result:

ok 1 number(s): "0"

Test #15:

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

input:

186293 1462 1

output:

0

result:

ok 1 number(s): "0"

Test #16:

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

input:

24867 886 1

output:

0

result:

ok 1 number(s): "0"

Test #17:

score: 0
Accepted
time: 87ms
memory: 24940kb

input:

976164 1014 2

output:

0

result:

ok 1 number(s): "0"

Test #18:

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

input:

179356 2 716844809

output:

577866092

result:

ok 1 number(s): "577866092"

Test #19:

score: 0
Accepted
time: 51ms
memory: 11876kb

input:

621001 130 310625363

output:

892869197

result:

ok 1 number(s): "892869197"

Test #20:

score: 0
Accepted
time: 66ms
memory: 22576kb

input:

678862 850 754662812

output:

582264789

result:

ok 1 number(s): "582264789"

Test #21:

score: 0
Accepted
time: 62ms
memory: 22536kb

input:

650845 978 348443366

output:

825425732

result:

ok 1 number(s): "825425732"

Test #22:

score: 0
Accepted
time: 54ms
memory: 14536kb

input:

669914 402 87448112

output:

318098088

result:

ok 1 number(s): "318098088"

Test #23:

score: 0
Accepted
time: 95ms
memory: 19148kb

input:

998593 530 681228665

output:

408255654

result:

ok 1 number(s): "408255654"

Test #24:

score: 0
Accepted
time: 67ms
memory: 39392kb

input:

369361 1954 125266115

output:

509912384

result:

ok 1 number(s): "509912384"

Test #25:

score: 0
Accepted
time: 105ms
memory: 30348kb

input:

900226 1378 424079373

output:

406320917

result:

ok 1 number(s): "406320917"

Test #26:

score: 0
Accepted
time: 52ms
memory: 30604kb

input:

334887 1506 17859926

output:

503264679

result:

ok 1 number(s): "503264679"

Test #27:

score: 0
Accepted
time: 81ms
memory: 17796kb

input:

936048 544 53978328

output:

548647866

result:

ok 1 number(s): "548647866"

Test #28:

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

input:

152789 1264 792983073

output:

839541707

result:

ok 1 number(s): "839541707"

Test #29:

score: 0
Accepted
time: 87ms
memory: 32364kb

input:

714493 1392 91796331

output:

721071046

result:

ok 1 number(s): "721071046"

Test #30:

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

input:

269571 816 830801077

output:

330064211

result:

ok 1 number(s): "330064211"

Test #31:

score: 0
Accepted
time: 84ms
memory: 24876kb

input:

845120 944 424581630

output:

348960190

result:

ok 1 number(s): "348960190"

Test #32:

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

input:

533990 368 163586376

output:

522092095

result:

ok 1 number(s): "522092095"

Test #33:

score: 0
Accepted
time: 47ms
memory: 36568kb

input:

181707 1792 462399634

output:

373795106

result:

ok 1 number(s): "373795106"

Test #34:

score: 0
Accepted
time: 72ms
memory: 36760kb

input:

417349 1920 761212891

output:

587051329

result:

ok 1 number(s): "587051329"

Test #35:

score: 0
Accepted
time: 65ms
memory: 29020kb

input:

526583 1344 500217637

output:

108767800

result:

ok 1 number(s): "108767800"

Test #36:

score: 0
Accepted
time: 87ms
memory: 23252kb

input:

867054 769 93998191

output:

239123369

result:

ok 1 number(s): "239123369"

Extra Test:

score: 0
Extra Test Passed