UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#147458#37. 染色mayaming1001605ms1324kbC++111.9kb2022-03-22 21:52:342022-03-22 21:52:35

answer

// http://noi.ac/problem/37

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;
const int MAX_N = 5001;

ull mod[MAX_N];  // n! % p的结果
// 如果m > n,就是m的n次方模p的结果
// a的b次方模p
// 假设b = 11,即1011,那么pow(a, b) = pow(a, 8) * pow(a, 2) * pow(a, 1)
ull fast_pow_mod(ull a, ull b, ull p) {
    ull res = 1;
    ull cur = a % p;
    while (b > 0) {
        if (b & 1) {
            res = res * cur % p;
        }
        cur = cur * cur % p;
        b >>= 1;
    }
    return res;
}

int simple(int n, int m, int p) {
    // 结尾最多有j个各不相同的涂色的方案数
    static ull cur_num[MAX_N];
    static ull prev_num[MAX_N];
    static ull suffix_sum[MAX_N];

    ull res = 0;
    for (int i = 1; i <= n; ++i) {
        int max_j = min(m - 1, i);
        for (int j = 1; j <= max_j; ++j) {
            if (i == 1) {
                cur_num[j] = m;
            }
            else {
                cur_num[j] = prev_num[j-1] * (m - j + 1) % p;
                cur_num[j] = (cur_num[j] + suffix_sum[j]) % p;
            }

            if (i == n) {
                res = (res + cur_num[j]) % p;
            }
        }

        if (i < n) {
            for (int j = 1; j <= max_j; ++j) {
                prev_num[j] = cur_num[j];
            }
            for (int j = max_j; j >= 1; --j) {
                suffix_sum[j] = (j == max_j) ? cur_num[j] : ((suffix_sum[j+1] + cur_num[j]) % p);
            }
        }
    }
    return res;
}

int main() {
    int n, m, p;

    while (cin >>n >>m >>p) {
        if (m == 1) {
            cout <<1 <<endl;
        }
        else if (m > n) {
            cout <<fast_pow_mod(m, n, p) <<endl;
        }
        else {
            cout <<(simple(n, m, p) % p) <<endl;
        }
    }

    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 1208kb

input:

5 2 6

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 5
Accepted
time: 1ms
memory: 1208kb

input:

8 8 562455908

output:

16736896

result:

ok 1 number(s): "16736896"

Test #3:

score: 5
Accepted
time: 0ms
memory: 1208kb

input:

1942 7 873185963

output:

41441893

result:

ok 1 number(s): "41441893"

Test #4:

score: 5
Accepted
time: 1ms
memory: 1212kb

input:

2030 10 187301366

output:

171496026

result:

ok 1 number(s): "171496026"

Test #5:

score: 5
Accepted
time: 0ms
memory: 1208kb

input:

4643 9 998244353

output:

583546139

result:

ok 1 number(s): "583546139"

Test #6:

score: 5
Accepted
time: 0ms
memory: 1196kb

input:

1447 3969 998244353

output:

669602806

result:

ok 1 number(s): "669602806"

Test #7:

score: 5
Accepted
time: 0ms
memory: 1216kb

input:

226 212 998244352

output:

503316480

result:

ok 1 number(s): "503316480"

Test #8:

score: 5
Accepted
time: 0ms
memory: 1220kb

input:

300 292 779230672

output:

95153840

result:

ok 1 number(s): "95153840"

Test #9:

score: 5
Accepted
time: 2ms
memory: 1220kb

input:

269 228 479932618

output:

244481530

result:

ok 1 number(s): "244481530"

Test #10:

score: 5
Accepted
time: 0ms
memory: 1216kb

input:

216 112 207794912

output:

145540448

result:

ok 1 number(s): "145540448"

Test #11:

score: 5
Accepted
time: 2ms
memory: 1224kb

input:

291 261 37264436

output:

28883269

result:

ok 1 number(s): "28883269"

Test #12:

score: 5
Accepted
time: 2ms
memory: 1220kb

input:

273 218 960539553

output:

718739009

result:

ok 1 number(s): "718739009"

Test #13:

score: 5
Accepted
time: 126ms
memory: 1272kb

input:

2991 2318 704783019

output:

517763339

result:

ok 1 number(s): "517763339"

Test #14:

score: 5
Accepted
time: 97ms
memory: 1236kb

input:

4413 790 693631006

output:

634441922

result:

ok 1 number(s): "634441922"

Test #15:

score: 5
Accepted
time: 339ms
memory: 1320kb

input:

4786 4581 2

output:

1

result:

ok 1 number(s): "1"

Test #16:

score: 5
Accepted
time: 376ms
memory: 1320kb

input:

4953 4424 876277826

output:

746769658

result:

ok 1 number(s): "746769658"

Test #17:

score: 5
Accepted
time: 6ms
memory: 1216kb

input:

4921 37 699201223

output:

206663263

result:

ok 1 number(s): "206663263"

Test #18:

score: 5
Accepted
time: 132ms
memory: 1232kb

input:

4991 994 533908193

output:

86765287

result:

ok 1 number(s): "86765287"

Test #19:

score: 5
Accepted
time: 387ms
memory: 1324kb

input:

5000 5000 640462742

output:

518214138

result:

ok 1 number(s): "518214138"

Test #20:

score: 5
Accepted
time: 134ms
memory: 1232kb

input:

4935 1005 961254358

output:

202283451

result:

ok 1 number(s): "202283451"

Extra Test:

score: 0
Extra Test Passed