QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#350237#8211. Enumerating Substringswillow#AC ✓24ms21548kbC++173.1kb2024-03-10 15:46:022024-03-10 15:46:03

Judging History

This is the latest submission verdict.

  • [2024-03-10 15:46:03]
  • Judged
  • Verdict: AC
  • Time: 24ms
  • Memory: 21548kb
  • [2024-03-10 15:46:02]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2005;

namespace ModCalculator {
    const int MOD = 1e9 + 7;
    inline void Inc(int &x, int y) {
        x += y; if (x >= MOD) x -= MOD;
    }
    inline int Add(int x, int y) {
        Inc(x, y); return x;
    }
    inline void Dec(int &x, int y) {
        x -= y; if (x < 0) x += MOD;
    }
    inline int Sub(int x, int y) {
        Dec(x, y); return x;
    }
    inline int Mul(int x, int y) {
        return 1LL * x * y % MOD;
    }
    inline int Ksm(int x, int y) {
        int ret = 1;
        for (; y; y >>= 1) {
            if (y & 1) ret = Mul(ret, x);
            x = Mul(x, x);
        }
        return ret;
    }
}
using namespace ModCalculator;

int n, m, k, C[N][N], fac[N], finv[N], pw2[N], pwk[1000005], dnk[N];
int dp[N];

void solve() {
    scanf("%d %d %d", &n, &m, &k);
    if (k * 2 < m) {
        puts("0");
        return;
    }
    pwk[0] = 1;
    for (int i = 1; i <= n; ++i) {
        pwk[i] = Mul(pwk[i - 1], k);
    }
    dnk[0] = 1;
    for (int i = 1; i <= m; ++i) {
        dnk[i] = Mul(dnk[i - 1], k - i + 1);
    }
    fac[0] = fac[1] = finv[0] = finv[1] = 1;
    pw2[0] = 1; pw2[1] = Ksm(2, MOD - 2);
    for (int i = 2; i <= m; ++i) {
        fac[i] = Mul(fac[i - 1], i);
        finv[i] = Mul(finv[i - 1], Ksm(i, MOD - 2));
        pw2[i] = Mul(pw2[i - 1], pw2[1]);
    }
    for (int i = 0; i <= m; ++i) {
        C[i][0] = 1;
        for (int j = 1; j <= i; ++j) {
            C[i][j] = Add(C[i - 1][j], C[i - 1][j - 1]);
        }
    }

    for (int i = 0; i <= m / 2 && i <= k; ++i) {
        for (int j = 0; j * 2 <= m - i * 2; ++j) {
            int l = m - i * 2 - j * 2;
            int cur = Mul(dnk[i + j + l], finv[i + j + l]);
            cur = Mul(cur, Mul(C[i + j + l][i], fac[i]));
            cur = Mul(cur, Mul(C[j + l][j], fac[m - i * 2]));
            cur = Mul(cur, pw2[j]);
            Inc(dp[i], cur);
        }
    }
    for (int i = 1; i <= m / 2; ++i) {
        Dec(dp[0], dp[i]);
    }
    /*
    for (int i = m / 2 - 1; i >= 0; --i) {
        for (int j = m / 2; j > i; --j) {
            Dec(dp[i], dp[j]);
        }
    }*/
    int ans = Mul(dp[0], Mul(n - m + 1, pwk[n - m]));
    for (int i = 1; i <= m / 2; ++i) {
        for (int j = 1, len = m; len <= n; ++j, len += m - i) {
            int x = n - len, y = m - i - 1, cur = 0;
            if (y >= x) {
                cur = Mul(x + 1, pwk[x]);
            } else if (y * 2 >= x) {
                cur = Mul(y * 2 - x + 1, pwk[x]);
                Inc(cur, Mul(x + 1 - y * 2 + x - 1, Sub(pwk[x], pwk[x - y - 1])));
            } else { //y * 2 < x
                cur = Mul((y + 1) * 2, Sub(pwk[x], pwk[x - y - 1]));
                Inc(cur, Mul(x + 1 - (y + 1) * 2, Sub(Add(pwk[x], pwk[x - (y + 1) * 2]), Mul(2, pwk[x - y - 1]))));
            }
            //cerr << j << ' ' << x << ' ' << y << ' ' << cur << endl;
            Inc(ans, Mul(Mul(dp[i], cur), (j + 1) / 2));
        } 
    }
    printf("%d\n", ans);
}

int main() {
    solve();
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 2 3

output:

228

result:

ok 1 number(s): "228"

Test #2:

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

input:

999999 1999 12345678

output:

52352722

result:

ok 1 number(s): "52352722"

Test #3:

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

input:

7 4 2

output:

182

result:

ok 1 number(s): "182"

Test #4:

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

input:

4 3 4

output:

480

result:

ok 1 number(s): "480"

Test #5:

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

input:

3 1 1

output:

3

result:

ok 1 number(s): "3"

Test #6:

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

input:

5 5 1

output:

0

result:

ok 1 number(s): "0"

Test #7:

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

input:

7 4 3

output:

5784

result:

ok 1 number(s): "5784"

Test #8:

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

input:

5 2 4

output:

3932

result:

ok 1 number(s): "3932"

Test #9:

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

input:

8 2 2

output:

1522

result:

ok 1 number(s): "1522"

Test #10:

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

input:

8 1 2

output:

2048

result:

ok 1 number(s): "2048"

Test #11:

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

input:

7 5 3

output:

2430

result:

ok 1 number(s): "2430"

Test #12:

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

input:

10 4 3

output:

272004

result:

ok 1 number(s): "272004"

Test #13:

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

input:

675978 614 2

output:

0

result:

ok 1 number(s): "0"

Test #14:

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

input:

244613 38 1

output:

0

result:

ok 1 number(s): "0"

Test #15:

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

input:

186293 1462 1

output:

0

result:

ok 1 number(s): "0"

Test #16:

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

input:

24867 886 1

output:

0

result:

ok 1 number(s): "0"

Test #17:

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

input:

976164 1014 2

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 3ms
memory: 4640kb

input:

179356 2 716844809

output:

577866092

result:

ok 1 number(s): "577866092"

Test #19:

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

input:

621001 130 310625363

output:

892869197

result:

ok 1 number(s): "892869197"

Test #20:

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

input:

678862 850 754662812

output:

582264789

result:

ok 1 number(s): "582264789"

Test #21:

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

input:

650845 978 348443366

output:

825425732

result:

ok 1 number(s): "825425732"

Test #22:

score: 0
Accepted
time: 10ms
memory: 8476kb

input:

669914 402 87448112

output:

318098088

result:

ok 1 number(s): "318098088"

Test #23:

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

input:

998593 530 681228665

output:

408255654

result:

ok 1 number(s): "408255654"

Test #24:

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

input:

369361 1954 125266115

output:

509912384

result:

ok 1 number(s): "509912384"

Test #25:

score: 0
Accepted
time: 18ms
memory: 16224kb

input:

900226 1378 424079373

output:

406320917

result:

ok 1 number(s): "406320917"

Test #26:

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

input:

334887 1506 17859926

output:

503264679

result:

ok 1 number(s): "503264679"

Test #27:

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

input:

936048 544 53978328

output:

548647866

result:

ok 1 number(s): "548647866"

Test #28:

score: 0
Accepted
time: 8ms
memory: 12560kb

input:

152789 1264 792983073

output:

839541707

result:

ok 1 number(s): "839541707"

Test #29:

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

input:

714493 1392 91796331

output:

721071046

result:

ok 1 number(s): "721071046"

Test #30:

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

input:

269571 816 830801077

output:

330064211

result:

ok 1 number(s): "330064211"

Test #31:

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

input:

845120 944 424581630

output:

348960190

result:

ok 1 number(s): "348960190"

Test #32:

score: 0
Accepted
time: 10ms
memory: 7644kb

input:

533990 368 163586376

output:

522092095

result:

ok 1 number(s): "522092095"

Test #33:

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

input:

181707 1792 462399634

output:

373795106

result:

ok 1 number(s): "373795106"

Test #34:

score: 0
Accepted
time: 12ms
memory: 18728kb

input:

417349 1920 761212891

output:

587051329

result:

ok 1 number(s): "587051329"

Test #35:

score: 0
Accepted
time: 12ms
memory: 14684kb

input:

526583 1344 500217637

output:

108767800

result:

ok 1 number(s): "108767800"

Test #36:

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

input:

867054 769 93998191

output:

239123369

result:

ok 1 number(s): "239123369"

Extra Test:

score: 0
Extra Test Passed