QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#287642#7754. Rolling For DaysMax_s_xaMWA 1ms9852kbC++144.6kb2023-12-20 21:02:182023-12-20 21:02:19

Judging History

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

  • [2023-12-20 21:02:19]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:9852kb
  • [2023-12-20 21:02:18]
  • 提交

answer

#include <iostream>
#include <algorithm>

typedef long long ll;
typedef double lf;

// #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 = 1010, MAXM = (1 << 12) + 5, mod = 998244353;

int n, m, q, a[15], b[15];

int suma[MAXM], sumb[MAXM];
ll fac[MAXN], inv[MAXN], sinv[MAXN];
ll f[MAXN][MAXM], fs[MAXN][MAXM], g[MAXN][MAXM], gs[MAXN][MAXM];
inline ll C(int n, int m) {return (m < 0 ? 1 : fac[n] * inv[n - m] % mod * inv[m] % mod);}

int main()
{
    //freopen("I.in", "r", stdin);
    //freopen("I.out", "w", stdout);
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    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;
    sinv[1] = 1;
    for (int i = 2; i < MAXN; i++) sinv[i] = (sinv[i - 1] + inv[i]) % mod, inv[i] = inv[i - 1] * inv[i] % mod;
    Read(n), Read(m);
    for (int i = 1; i <= m; i++) Read(a[i]), suma[1 << i - 1] = a[i];
    for (int i = 1; i <= m; i++) Read(b[i]), sumb[1 << i - 1] = b[i];
    int nm = 0, wst = 0;
    for (int i = 1; i <= m; i++)
        if (b[i]) a[++nm] = a[i], b[nm] = b[i], q += b[i];
        else wst += a[i];
    m = nm;
    for (int s = 1; s < (1 << m); s++)
        suma[s] = suma[s ^ (s & -s)] + suma[s & -s], sumb[s] = sumb[s ^ (s & -s)] + sumb[s & -s];
        
    for (int i = 0; i <= q; i++) g[i][0] = inv[n - wst], gs[i][0] = sinv[n - wst] * inv[n - wst];
    // cout << q << " " << wst << "\n";
    for (int s = 1; s < (1 << m); s++)
    {
        for (int i = 1; i <= q; i++)
        {
            if (i < sumb[s]) continue;
            // cout << i << " " << s << " ";
            for (int x = 1; x <= m; x++)
                if (s & (1 << x - 1))
                {
                    int ss = s ^ (1 << x - 1);
                    ll G = (gs[i - 1][ss] - g[i - 1][ss] * sinv[n - i - suma[ss] + sumb[ss] - wst] % mod) * (suma[ss] - sumb[ss] + wst) % mod;
                    ll P = C(i - sumb[ss] - 1, b[x] - 1) % mod * fac[a[x]] % mod * inv[a[x] - b[x]] % mod * fac[n - i - suma[ss] + sumb[ss] - wst] % mod;
                    // cout << G << " " << P << "\n";
                    // cout << gs[i - 1][ss] << "\n";
                    f[i][s] = (f[i][s] + (fs[i - 1][ss] + G) * P) % mod;
                    g[i][s] = (g[i][s] + g[i - 1][ss] * P) % mod;
                }
            // cout << f[i][s] << " " << g[i][s] << "\n";
            fs[i][s] = (fs[i - 1][s] + f[i][s] * inv[n - i - suma[s] + sumb[s] - wst]) % mod;
            gs[i][s] = (gs[i - 1][s] + g[i][s] * sinv[n - i - suma[s] + sumb[s] - wst] % mod * inv[n - i - suma[s] + sumb[s] - wst]) % mod;
            g[i][s] = (g[i - 1][s] + g[i][s] * inv[n - i - suma[s] + sumb[s] - wst]) % mod;
        }
    }
    cout << (f[q][(1 << m) - 1] + mod + q) % mod << "\n";
    return 0;
}

詳細信息

Test #1:

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

input:

2 2
1 1
1 1

output:

2

result:

ok answer is '2'

Test #2:

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

input:

4 2
2 2
2 1

output:

582309210

result:

ok answer is '582309210'

Test #3:

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

input:

5 5
1 1 1 1 1
0 0 0 0 1

output:

5

result:

ok answer is '5'

Test #4:

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

input:

4 4
1 1 1 1
1 1 1 0

output:

831870299

result:

ok answer is '831870299'

Test #5:

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

input:

5 2
4 1
2 1

output:

598946616

result:

ok answer is '598946616'

Test #6:

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

input:

5 2
3 2
3 1

output:

482484776

result:

ok answer is '482484776'

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 7796kb

input:

5 5
1 1 1 1 1
0 1 1 1 0

output:

887328318

result:

wrong answer expected '665496242', found '887328318'