QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#326412#2576. 麻将luyiming123100 ✓715ms14252kbC++234.4kb2024-02-13 00:22:012024-02-13 00:22:02

Judging History

This is the latest submission verdict.

  • [2024-02-13 00:22:02]
  • Judged
  • Verdict: 100
  • Time: 715ms
  • Memory: 14252kb
  • [2024-02-13 00:22:01]
  • Submitted

answer

# include <bits/stdc++.h>
using namespace std;
const int N = 105, mod = 998244353; 

void Add(int &x, int y) {x = (1ll * x + y) % mod; return; }

int n, buc[N]; 
void chkmax(int &x, int y) { x = max(x, y); return; }

struct info
{
    int dp[3][3]; 
    void Init(void)
    {
        memset(dp, -1, sizeof(dp)); 
        return; 
    }
}; 

info Trans(info &X, int t)
{
    info ans; ans.Init(); 
    for(int i = 0; i <= 2; i++)
    {
        for(int j = 0; j <= 2; j++)
        {
            if(X.dp[i][j] != -1) 
            {
                for(int k = 0; k <= 2; k++)
                {
                    if(t >= i + j + k) chkmax(ans.dp[j][k], min(X.dp[i][j] + i + (t - i - j - k) / 3, 4)); 
                }
            }
        }
    }
    return ans; 
}

void chkmax(info &x, info y)
{
    for(int i = 0; i <= 2; i++)
        for(int j = 0; j <= 2; j++) 
            x.dp[i][j] = max(x.dp[i][j], y.dp[i][j]);
    return; 
}

struct node
{
    info DP[2]; int tot; // 有没有对子;(i-1,i,i+1)个数;(i,i+1,i+2)个数;对子个数
    void Init(void)
    {
        for(int o : {0, 1}) DP[o].Init(); 
        tot = 0; 
        return; 
    }

    void Init0(void)
    {
        Init(); DP[0].dp[0][0] = 0; 
        return; 
    }
    
    bool check(void)
    {
        if(tot >= 7) return 1; 
        for(int i = 0; i <= 2; i++)
            for(int j = 0; j <= 2; j++)
                if(DP[1].dp[i][j] >= 4) return 1; 
        return 0; 
    }
}; 

bool operator < (const node &x, const node &y)
{
    if(x.tot != y.tot) return x.tot < y.tot; 
    for(int i = 0; i <= 1; i++)
        for(int j = 0; j <= 2; j++)
            for(int k = 0; k <= 2; k++)
                if(x.DP[i].dp[j][k] != y.DP[i].dp[j][k]) return x.DP[i].dp[j][k] < y.DP[i].dp[j][k]; 
    return 0; 
}

node Trans(node &X, int t)
{
    node Ans; Ans.Init(); 
    chkmax(Ans.DP[0], Trans(X.DP[0], t)); 
    chkmax(Ans.DP[1], Trans(X.DP[1], t)); 
    if(t >= 2) chkmax(Ans.DP[1], Trans(X.DP[0], t - 2)); 
    Ans.tot = X.tot + (t >= 2); 
    return Ans; 
}

map <node, int> Tong; int idtot = 0; 
int to[5][3005]; 
void bfs(void)
{
    queue <node> Q; node St; St.Init0(); 
    Q.push(St); Tong[St] = ++idtot; 
    while(!Q.empty())
    {
        auto I = Q.front(); Q.pop(); 
        for(int t = 0; t <= 4; t++)
        {
            auto New = Trans(I, t); 

            if(New.check()) to[t][Tong[I]] = 0;
            else 
            {
                if(!Tong.count(New)) Tong[New] = ++idtot, Q.push(New); 
                to[t][Tong[I]] = Tong[New]; 
            }
        } 
    }
    return; 
}

int f[2][N << 2][3005], fac[N << 2], ifac[N << 2]; 

int qpow(int x, int p = mod - 2ll)
{
    int ans = 1ll; 
    while(p) 
    {
        if(p & 1) ans = 1ll * ans * x % mod; 
        x = 1ll * x * x % mod, p >>= 1; 
    }
    return ans; 
}

void Init(int n = 405)
{
    fac[0] = ifac[0] = 1; 
    for(int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod; 
    ifac[n] = qpow(fac[n]); 
    for(int i = n - 1; i >= 1; i--) ifac[i] = ifac[i + 1] * (i + 1ll) % mod; 
    return; 
}

int C(int n, int m)
{
    if(n < m) return 0; 
    return 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod; 
}

signed main(void)
{
    Init(); 
    scanf("%d", &n); 
    for(int i = 1; i <= 13; i++) 
    {
        int w, t; scanf("%d%d", &w, &t); ++buc[w]; 
    }
    memset(to, -1, sizeof(to)); 
    for(int i = 0; i <= 4; i++) to[i][0] = 0; 
    bfs(); 

    f[0][0][1] = 1; 
    for(int i = 0; i <= n; i++)
    {
        int o = (i & 1); 
        memset(f[o ^ 1], 0, sizeof(f[o ^ 1])); 
        for(int j = 0; j <= 4 * i; j++)
        {
            for(int s = 0; s <= idtot; s++)
            {
                for(int t = 0; t <= 4 - buc[i + 1]; t++) // (i + 1) choose t
                {
                    Add(f[o ^ 1][j + t][to[t + buc[i + 1]][s]], 1ll * f[o][j][s] * C(4 - buc[i + 1], t) % mod);
                }
            }
        }
    }

    int Ans = 0; 
    for(int j = 1; j <= 4 * n - 13; j++)
    {
        int sum = 0; 
        for(int s = 1; s <= idtot; s++) // 不胡
        {
            Add(sum, f[n & 1][j][s]); 
        }
        Add(Ans, 1ll * sum * fac[j] % mod * fac[(4 * n - 13) - j] % mod); 
    }
    Ans = 1ll * Ans * ifac[4 * n - 13] % mod; Ans = (Ans + 1ll) % mod;
    printf("%d\n", Ans); 
    return 0; 
}

詳細信息

Test #1:

score: 10
Accepted
time: 10ms
memory: 13996kb

input:

5
1 1
1 2
1 3
1 4
2 1
3 1
3 2
3 3
3 4
5 1
5 2
5 3
5 4

output:

570425346

result:

ok single line: '570425346'

Test #2:

score: 10
Accepted
time: 7ms
memory: 14000kb

input:

5
1 3
2 4
4 3
5 3
3 1
5 4
4 2
1 4
4 4
3 3
3 2
1 1
2 2

output:

713031682

result:

ok single line: '713031682'

Test #3:

score: 10
Accepted
time: 14ms
memory: 13944kb

input:

13
1 3
12 1
12 2
1 4
13 3
13 2
3 2
13 1
1 1
3 4
3 3
1 2
12 3

output:

868236068

result:

ok single line: '868236068'

Test #4:

score: 10
Accepted
time: 16ms
memory: 14052kb

input:

13
1 1
13 1
2 3
1 3
3 1
3 4
2 1
13 4
1 4
12 2
13 3
12 3
3 2

output:

234399821

result:

ok single line: '234399821'

Test #5:

score: 10
Accepted
time: 17ms
memory: 14048kb

input:

13
2 3
2 1
12 2
13 2
2 2
1 2
13 3
12 4
13 4
3 4
3 1
12 3
1 3

output:

892522858

result:

ok single line: '892522858'

Test #6:

score: 10
Accepted
time: 466ms
memory: 14008kb

input:

81
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1

output:

152635587

result:

ok single line: '152635587'

Test #7:

score: 10
Accepted
time: 630ms
memory: 14252kb

input:

94
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1

output:

339459976

result:

ok single line: '339459976'

Test #8:

score: 10
Accepted
time: 644ms
memory: 13972kb

input:

96
1 2
1 3
1 4
1 1
2 2
2 3
2 4
2 1
3 2
3 3
3 4
3 1
4 2

output:

230028597

result:

ok single line: '230028597'

Test #9:

score: 10
Accepted
time: 497ms
memory: 14000kb

input:

83
1 2
1 3
1 4
1 1
2 2
2 3
2 4
2 1
3 2
3 3
3 4
3 1
4 2

output:

967250236

result:

ok single line: '967250236'

Test #10:

score: 10
Accepted
time: 715ms
memory: 13968kb

input:

100
67 1
26 4
12 4
11 4
30 4
29 1
1 4
13 3
30 1
64 2
63 1
20 2
32 4

output:

345132636

result:

ok single line: '345132636'

Extra Test:

score: 0
Extra Test Passed