QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#609371#6157. 组合数问题lwm7708100 ✓5ms3952kbC++173.8kb2024-10-04 12:33:352024-10-04 12:33:35

Judging History

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

  • [2024-10-04 12:33:35]
  • 评测
  • 测评结果:100
  • 用时:5ms
  • 内存:3952kb
  • [2024-10-04 12:33:35]
  • 提交

answer

#include <array>
#include <cstdint>
#include <ios>
#include <iostream>
#include <utility>
#include <vector>

template <typename T, typename U>
T exponentiate(T base, U pow, const T& id = T(1)) {

    T res = id;

    while (pow > 0) {
        if (pow & 1) {
            res *= base;
        }
        pow >>= 1;
        base *= base;
    }

    return res;

}

template <typename T>
std::array<T, 3> extended_gcd(T m, T n) {

    T a = 1;
    T a_in = 0;
    T b = 0;
    T b_in = 1;

    while (n) {
        const T q = m / n;
        a_in = std::exchange(a, a_in) - a_in * q;
        b_in = std::exchange(b, b_in) - b_in * q;
        n = std::exchange(m, n) - n * q;
    }

    return std::array<T, 3>({m, a, b});

}

template <std::int32_t>
class dynamic_modular_integer {

private:

    static inline std::int32_t modulus;

public:

    static std::int32_t get_modulus() {

        return modulus;

    }

    static void set_modulus(std::int32_t mod) {

        modulus = mod;

    }

    std::int32_t val;

    explicit dynamic_modular_integer() : dynamic_modular_integer(0) {}

    explicit dynamic_modular_integer(std::int64_t val) : val(val % get_modulus()) {

        if (this->val < 0) {
            this->val += get_modulus();
        }

    }

    dynamic_modular_integer operator-() const {

        return dynamic_modular_integer(-val);

    }

    void operator++() {

        *this += dynamic_modular_integer(1);

    }

    void operator--() {

        *this -= dynamic_modular_integer(1);

    }

    void operator+=(dynamic_modular_integer other) {

        if (other.val >= get_modulus() - val) {
            val -= get_modulus();
        }

        val += other.val;

    }

    void operator-=(dynamic_modular_integer other) {

        val -= other.val;

        if (val < 0) {
            val += get_modulus();
        }

    }

    void operator*=(dynamic_modular_integer other) {

        val = (std::int64_t(val) * other.val) % get_modulus();

    }

    void operator/=(dynamic_modular_integer other) {

        *this *= dynamic_modular_integer(extended_gcd(other.val, get_modulus())[1]);

    }

    friend dynamic_modular_integer operator+(
        dynamic_modular_integer lhs, dynamic_modular_integer rhs
    ) {

        lhs += rhs;

        return lhs;

    }

    friend dynamic_modular_integer operator-(
        dynamic_modular_integer lhs, dynamic_modular_integer rhs
    ) {

        lhs -= rhs;

        return lhs;

    }

    friend dynamic_modular_integer operator*(
        dynamic_modular_integer lhs, dynamic_modular_integer rhs
    ) {

        lhs *= rhs;

        return lhs;

    }

    friend dynamic_modular_integer operator/(
        dynamic_modular_integer lhs, dynamic_modular_integer rhs
    ) {

        lhs /= rhs;

        return lhs;

    }

};

void solve() {

    using num_t = dynamic_modular_integer<0>;

    std::int32_t m;
    std::int32_t n;
    std::int32_t p;
    std::int32_t x;

    std::cin >> n >> x >> p >> m;

    num_t::set_modulus(p);

    std::vector<num_t> dp;
    num_t sum;

    for (std::int32_t i = n - m; i <= n; ++i) {
        const std::int32_t sz = i - (n - m) + 1;
        std::vector<num_t> n_dp(sz);
        n_dp[0] = exponentiate(num_t(x + 1), i);
        for (std::int32_t j = 1; j < sz; ++j) {
            n_dp[j] = (n_dp[j - 1] - dp[j - 1]) * num_t(i);
        }
        dp = std::move(n_dp);
    }

    for (std::int32_t i = 0; i <= m; ++i) {
        std::int32_t a;
        std::cin >> a;
        sum += num_t(a) * dp[i];
    }

    std::cout << sum.val << '\n';

}

int main() {

    std::cin.tie(nullptr);

    std::ios_base::sync_with_stdio(false);

    solve();

    return 0;

}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 4ms
memory: 3684kb

input:

1000 643189585 831941236 1000
621444125 324708497 619770455 112615814 800909697 461794889 380815971 872773343 314541774 472606930 636420887 783552328 794270098 539204140 23007446 139105268 406306323 866123427 218553206 804461683 30473072 117201808 568299563 477252115 324883663 342559946 1371621 5042...

output:

692083420

result:

ok 1 number(s): "692083420"

Test #2:

score: 5
Accepted
time: 4ms
memory: 3716kb

input:

1000 503195138 513597392 1000
722484910 931359038 343065489 981840058 521519661 226445599 530176715 770414408 102322476 673203111 507109164 694874631 741867637 279337383 424160545 294330405 82544373 48857855 815232637 669279005 52910855 214526933 849319059 181639810 172299266 889272850 596357056 666...

output:

318265902

result:

ok 1 number(s): "318265902"

Test #3:

score: 5
Accepted
time: 4ms
memory: 3724kb

input:

1000 906011316 702916476 1000
426378198 448666835 928428034 473805142 373675324 45261623 502895126 736498316 231472510 191865836 242418084 544276235 444104055 477445639 356224464 953908838 306690394 584707561 294817458 752251380 852722174 469468566 503286490 871710527 705395858 404720545 938309838 1...

output:

186621654

result:

ok 1 number(s): "186621654"

Test #4:

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

input:

100000 677812091 277 0
807583683

output:

71

result:

ok 1 number(s): "71"

Test #5:

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

input:

100000 981163973 25889 0
973863137

output:

12731

result:

ok 1 number(s): "12731"

Test #6:

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

input:

100000 835568175 7401731 0
906329707

output:

3064835

result:

ok 1 number(s): "3064835"

Test #7:

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

input:

1000000000 790947092 627519428 0
60710186

output:

272191242

result:

ok 1 number(s): "272191242"

Test #8:

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

input:

1000000000 650690291 731646919 0
877581470

output:

15529275

result:

ok 1 number(s): "15529275"

Test #9:

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

input:

1000000000 797047750 618934290 3
961481438 877843421 701730260 298899

output:

420699238

result:

ok 1 number(s): "420699238"

Test #10:

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

input:

1000000000 864318301 584940599 3
633737140 774981490 625591778 241085537

output:

198331859

result:

ok 1 number(s): "198331859"

Test #11:

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

input:

1000000000 848364348 882312605 4
170103640 631151726 271592928 185866004 334973069

output:

52190000

result:

ok 1 number(s): "52190000"

Test #12:

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

input:

1000000000 632003439 731204527 5
528931262 239172080 749520006 43675943 695916953 6562492

output:

614687549

result:

ok 1 number(s): "614687549"

Test #13:

score: 5
Accepted
time: 5ms
memory: 3652kb

input:

1000000000 1 766455601 1000
558055814 109246028 38835513 947196933 242586123 147273834 256441677 15594213 98941360 755103145 463954471 169688646 98806912 642927659 621211739 713955885 391698389 163712447 588561728 647301220 39465644 800390415 324148500 871577287 613523228 342178732 36750461 26345791...

output:

657791066

result:

ok 1 number(s): "657791066"

Test #14:

score: 5
Accepted
time: 4ms
memory: 3656kb

input:

1000000000 1 873712850 1000
278171983 164635568 206054551 341466272 947345526 592513430 491563677 341146136 935528892 632574736 565810285 515879482 994979438 164263680 189449944 440718937 492084059 497263419 573679235 699086245 659495381 975774703 586571408 481989509 489369682 258500183 253174606 94...

output:

357599008

result:

ok 1 number(s): "357599008"

Test #15:

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

input:

1000000000 1 801809934 1000
326406661 169791972 761656767 15280008 186815446 186117411 265403224 308467755 392051233 725260841 341791121 418439521 777844152 419969954 730027819 343615863 568299464 475588222 277121423 761476244 202455418 960116443 484950749 866814884 28710946 434807477 996165424 7697...

output:

762344818

result:

ok 1 number(s): "762344818"

Test #16:

score: 5
Accepted
time: 4ms
memory: 3652kb

input:

1000000000 1 605325981 1000
300766868 885995664 277533356 168649814 892768488 291744630 646838712 856019873 59320837 572912057 690027481 880187770 374756433 903619161 122904149 844522459 95811969 772786363 239314288 992117811 387289152 145852010 767518940 830315681 766920764 440439657 93192393 76897...

output:

58895801

result:

ok 1 number(s): "58895801"

Test #17:

score: 5
Accepted
time: 4ms
memory: 3952kb

input:

1000000000 957733823 809081300 1000
533363221 638929575 443006622 286508611 691905309 228596664 175904171 410178708 451046889 127797916 415935615 570618723 800660185 563213999 903523847 668436995 933625911 861427774 755608174 362042286 314119712 895203661 417507948 153273866 167248092 956338547 3406...

output:

674756796

result:

ok 1 number(s): "674756796"

Test #18:

score: 5
Accepted
time: 4ms
memory: 3684kb

input:

1000000000 610363186 847371151 1000
665297781 786213968 431288588 909998294 800117192 27065236 692646918 881650665 265579161 44215444 99633849 582605709 402479473 972764887 466468004 538706661 251804078 454620935 67255325 783865759 693395798 583174943 305918899 503101381 981809034 107696 736321655 4...

output:

634262459

result:

ok 1 number(s): "634262459"

Test #19:

score: 5
Accepted
time: 4ms
memory: 3652kb

input:

1000000000 534756891 543527658 1000
702340808 598102723 853329752 572034620 975329351 617733109 230679944 829852779 503128018 355746866 443120420 51627950 127579068 106567561 79644532 703345646 306545813 302226744 726078059 118130342 68034745 998675817 12288173 368910305 490224052 374647055 27100878...

output:

149744150

result:

ok 1 number(s): "149744150"

Test #20:

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

input:

1000000000 909107105 657355356 1000
90722426 58432815 680303429 656802978 821714069 81032229 404049008 508394045 815033128 537356713 111806335 80898026 295991237 78868790 550351734 549534112 766404863 359634541 285022912 417815753 284604425 900505912 411877558 12589604 66352005 634143645 90909857 16...

output:

236596968

result:

ok 1 number(s): "236596968"

Extra Test:

score: 0
Extra Test Passed