QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#519327#6333. Festivals in JOI Kingdom 2Max_s_xaM87 1365ms6828kbC++143.2kb2024-08-14 18:45:522024-08-14 18:45:52

Judging History

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

  • [2024-08-14 18:45:52]
  • 评测
  • 测评结果:87
  • 用时:1365ms
  • 内存:6828kb
  • [2024-08-14 18:45:52]
  • 提交

answer

#include <iostream>
#include <ctime>

typedef long long ll;
typedef double lf;
typedef __int128 LL;

// #define DEBUG 1
struct IO
{
    #define MAXSIZE (1 << 20)
    #define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;
    #if DEBUG
    #else
    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
    #endif
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
    #define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')

    template <typename T>
    void Read(T &x)
    {
        #if DEBUG
        std::cin >> x;
        #else
        bool sign = 0; char ch = gc(); x = 0;
        for (; !isdigit(ch); ch = gc())
            if (ch == '-') sign = 1;
        for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
        if (sign) x = -x;
        #endif
    }
    void Read(char *s)
    {
        #if DEBUG
        std::cin >> s;
        #else
        char ch = gc();
        for (; blank(ch); ch = gc());
        for (; !blank(ch); ch = gc()) *s++ = ch;
        *s = 0;
        #endif
    }
    void Read(char &c) {for (c = gc(); blank(c); c = gc());}

    void Push(const char &c)
    {
        #if DEBUG
        putchar(c);
        #else
        if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        *pp++ = c;
        #endif
    }
    template <typename T>
    void Write(T x)
    {
        if (x < 0) x = -x, Push('-');
        static T sta[35];
        int top = 0;
        do sta[top++] = x % 10, x /= 10; while (x);
        while (top) Push(sta[--top] ^ 48);
    }
    template <typename T>
    void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)

using namespace std;

const int MAXN = 4e4 + 10;

int n, mod;

ll fac[MAXN], inv[MAXN];
inline ll Qpow(ll x, ll y) { ll res = 1; while (y) (y & 1) && (res = res * x % mod), x = x * x % mod, y >>= 1; return res; }

LL f[MAXN], g[MAXN];
inline void add(ll &x, ll y) { (x += y) >= mod && (x -= mod); }

int main()
{
    // freopen("B.in", "r", stdin);
    // freopen("B.out", "w", stdout);
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif

    Read(n), Read(mod);

    fac[0] = fac[1] = inv[0] = inv[1] = 1;
    for (int i = 2; i < MAXN; i++) fac[i] = fac[i - 1] * i % mod, inv[i] = (mod - mod / i) * inv[mod % i] % mod;
    for (int i = 2; i < MAXN; i++) inv[i] = inv[i - 1] * inv[i] % mod;

    f[0] = 0, g[0] = 1;
    int m = n << 1; LL tmp;
    for (int i = 0; i < n; i++)
    {
        f[i] %= mod, g[i] %= mod;
        for (int k = 1; i + k <= n; k++)
        {
            int p = m - ((i << 1) + k + 1), q = p - k + 2;
            if (k > 1)
            {
                tmp = fac[p] * inv[q];
                f[i + k] += (f[i] * k + g[i]) * (k - 1) * tmp;
            }
            tmp = fac[p] * inv[q - 1];
            g[i + k] += (f[i] * k + g[i]) * tmp;
        }
    }
    f[n] %= mod, g[n] %= mod;
    cout << (ll)(fac[m] * inv[n] % mod * Qpow(2, mod - 1 - n) % mod - f[n] - g[n] + 2 * mod) % mod << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

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

input:

1 194903119

output:

0

result:

ok 1 number(s): "0"

Test #2:

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

input:

2 933234047

output:

0

result:

ok 1 number(s): "0"

Test #3:

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

input:

3 277793111

output:

2

result:

ok 1 number(s): "2"

Test #4:

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

input:

4 355321177

output:

28

result:

ok 1 number(s): "28"

Test #5:

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

input:

5 306636893

output:

358

result:

ok 1 number(s): "358"

Subtask #2:

score: 5
Accepted

Dependency #1:

100%
Accepted

Test #6:

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

input:

8 361605653

output:

1236922

result:

ok 1 number(s): "1236922"

Test #7:

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

input:

8 995512643

output:

1236922

result:

ok 1 number(s): "1236922"

Test #8:

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

input:

8 101102801

output:

1236922

result:

ok 1 number(s): "1236922"

Test #9:

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

input:

6 458322727

output:

4894

result:

ok 1 number(s): "4894"

Test #10:

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

input:

7 721691819

output:

73884

result:

ok 1 number(s): "73884"

Test #11:

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

input:

7 370629137

output:

73884

result:

ok 1 number(s): "73884"

Subtask #3:

score: 27
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #12:

score: 27
Accepted
time: 1ms
memory: 5604kb

input:

30 779092367

output:

686412377

result:

ok 1 number(s): "686412377"

Test #13:

score: 27
Accepted
time: 1ms
memory: 5672kb

input:

29 963995171

output:

128570082

result:

ok 1 number(s): "128570082"

Test #14:

score: 27
Accepted
time: 1ms
memory: 5604kb

input:

18 666092701

output:

185922458

result:

ok 1 number(s): "185922458"

Test #15:

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

input:

14 671243719

output:

623913899

result:

ok 1 number(s): "623913899"

Subtask #4:

score: 14
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #16:

score: 14
Accepted
time: 1ms
memory: 5548kb

input:

300 463478027

output:

89265245

result:

ok 1 number(s): "89265245"

Test #17:

score: 14
Accepted
time: 1ms
memory: 5836kb

input:

296 567610679

output:

406342763

result:

ok 1 number(s): "406342763"

Test #18:

score: 14
Accepted
time: 2ms
memory: 5980kb

input:

297 609090359

output:

128986577

result:

ok 1 number(s): "128986577"

Test #19:

score: 14
Accepted
time: 0ms
memory: 6212kb

input:

48 759569383

output:

635573072

result:

ok 1 number(s): "635573072"

Test #20:

score: 14
Accepted
time: 1ms
memory: 6320kb

input:

99 298873033

output:

285340078

result:

ok 1 number(s): "285340078"

Subtask #5:

score: 36
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Test #21:

score: 36
Accepted
time: 32ms
memory: 5932kb

input:

3000 752129633

output:

33058561

result:

ok 1 number(s): "33058561"

Test #22:

score: 36
Accepted
time: 28ms
memory: 5752kb

input:

2993 491173567

output:

343277625

result:

ok 1 number(s): "343277625"

Test #23:

score: 36
Accepted
time: 32ms
memory: 4344kb

input:

2993 783095563

output:

776085006

result:

ok 1 number(s): "776085006"

Test #24:

score: 36
Accepted
time: 2ms
memory: 5664kb

input:

327 399783431

output:

163535283

result:

ok 1 number(s): "163535283"

Test #25:

score: 36
Accepted
time: 6ms
memory: 5588kb

input:

1233 857060207

output:

422139845

result:

ok 1 number(s): "422139845"

Test #26:

score: 36
Accepted
time: 5ms
memory: 5712kb

input:

1114 600227447

output:

598509129

result:

ok 1 number(s): "598509129"

Subtask #6:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Test #27:

score: 13
Accepted
time: 1364ms
memory: 5820kb

input:

20000 221054167

output:

39809956

result:

ok 1 number(s): "39809956"

Test #28:

score: 0
Wrong Answer
time: 1365ms
memory: 6828kb

input:

19997 823974001

output:

585684233

result:

wrong answer 1st numbers differ - expected: '267537750', found: '585684233'