HDU 2019 Multi-University Training Contest 1 杭电2019多校联合训练赛 第一场 1001 Blank (6578)

HDU 2019 Multi-University Training Contest 1 杭电2019暑期多校集训第一场 1001 Blank (6578)

Problem Description

There are N blanks arranged in a row. The blanks are numbered 1,2,…,N from left to right.
Tom is filling each blank with one number in {0,1,2,3}. According to his thought, the following M conditions must all be satisfied. The i-th condition is:
There are exactly xi different numbers among blanks ∈[li,ri].
In how many ways can the blanks be filled to satisfy all the conditions? Find the answer modulo 998244353.

Input

The first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.
In each test case, there are two integers n(1≤n≤100) and m(0≤m≤100) in the first line, denoting the number of blanks and the number of conditions.
For the following m lines, each line contains three integers l,r and x, denoting a condition(1≤l≤r≤n, 1≤x≤4).

Output

For each testcase, output a single line containing an integer, denoting the number of ways to paint the blanks satisfying all the conditions modulo 998244353.

Sample Input

2
1 0
4 1
1 3 3

Sample Output

4
96


题意

有一个长度为n的串,这个串仅由0,1,2,3构成,给定m个条件,条件形式为在l到r区间内有x种数字(1<=x<=4)。问有多少种这样的串,答案模998244353。

思路

DP。定义一个vector记录m个条件。定义一个思维的dp数组,前三维记录每种数字最后出现的位置,最后一种数字出现的位置即为当前字符串长度。第四维为滚动数组,降低空间复杂度。dp时分四种情况进行dp,下一位数字和上一位相同,即直接将dp[i][j][k][last]继承,或者与前三维某个数字相同,将其他数字重新放入这三维进行dp即可。dp完后需判断每种情况是否满足这m个条件,即判断(i>=l)+(j>=l)+(k>=l)+(cur>=l)==x是否成立,若不成立,则将dp值直接刷新为0。最后对dp数组求和即可。

坑点

字符串长度每增加一位就需要将dp数组第四维为now的数组清空。否则就会继续继承上上次长度dp时的dp值。
最后求和得出答案时只需要对第四维为n&1的数组求和。
别忘了模998244353。


代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MOD=998244353;
const int N=105;

ll dp[N][N][N][2];

void init()
{
    memset(dp,0,sizeof(dp));
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        vector<pair<int,int> > G[N];
        init();
        for(int i=0;i<m;i++)
        {
            int l,r,x;
            scanf("%d%d%d",&l,&r,&x);
            G[r].push_back(make_pair(l,x));
        }
        dp[0][0][0][0]=1;
        for(int cur=1;cur<=n;cur++)
        {
            int now=cur&1;
            int last=(!now);
            for(int i=0;i<=cur;i++)
            {
                for(int j=i;j<=cur;j++)
                {
                    for(int k=j;k<=cur;k++)
                    {
                        dp[i][j][k][now]=0;
                    }
                }
            }
            for(int i=0;i<=cur;i++)
            {
                for(int j=i;j<=cur;j++)
                {
                    for(int k=j;k<=cur;k++)
                    {
                        dp[i][j][k][now]=(dp[i][j][k][now]+dp[i][j][k][last])%MOD;
                        dp[i][j][cur-1][now]=(dp[i][j][cur-1][now]+dp[i][j][k][last])%MOD;
                        dp[j][k][cur-1][now]=(dp[j][k][cur-1][now]+dp[i][j][k][last])%MOD;
                        dp[i][k][cur-1][now]=(dp[i][k][cur-1][now]+dp[i][j][k][last])%MOD;
                    }
                }
            }
            for(int i=0;i<=cur;i++)
            {
                for(int j=i;j<=cur;j++)
                {
                    for(int k=j;k<=cur;k++)
                    {
                        for(int o=0;o<(int)G[cur].size();o++)
                        {
                            int l=G[cur][o].first;
                            int r=cur;
                            int x=G[cur][o].second;
                            if((i>=l)+(j>=l)+(k>=l)+(cur>=l)!=x)
                            {
                                dp[i][j][k][now]=0;
                            }
                        }
                    }
                }
            }
        }
        ll ans=0;
        for(int i=0;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                for(int k=j;k<=n;k++)
                {
                    ans=(ans+dp[i][j][k][n&1])%MOD;
                }
            }
        }
        printf("%lld\n",ans%MOD);
    }
    return 0;
}


深夜写博客~

skr~

发布了42 篇原创文章 · 获赞 6 · 访问量 4038

猜你喜欢

转载自blog.csdn.net/qq_43383246/article/details/98855451