UOJ#273. 【清华集训2016】你的生命已如风中残烛

问题相当于有和为0的m个数 a i ,求 i 有多少种排列使得任意一个前缀和都>=0
先给序列末尾加上一个-1,变成m+1个和为-1的数,要求前m个前缀和>=0

发现对于这m+1个数的每一种圆排,在他能够旋转得到的m+1种排列中,只有以最小前缀和第一次出现的位置的后一位为开头的是合法的,也就是说每一个圆排对应有且仅有一个合法方案,目前有 m ! 种合法排列

然后考虑去掉我们加上的这个-1后有多少种排列
我们得到的 m ! 种排列最后一位一定是-1,但不一定是我们加上去的-1,如果不是,我们定义他和 将我们加上去的-1与结尾的-1交换位置后的排列 等价,因为一共有m-n+1个-1,故每个等价类有m-n+1个元素
所以答案是 m ! m n + 1

code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

const int maxn = 55;
const int mod  = 998244353;

int pw(int x,int k)
{
    int re=1;
    for(;k;k>>=1,x=(ll)x*x%mod) if(k&1)
        re=(ll)re*x%mod;
    return re;
}
int inv(int x){ return pw(x,mod-2); }

int n,m;


int main()
{
    //freopen("tmp.in","r",stdin);
    //freopen("tmp.out","w",stdout);

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x; scanf("%d",&x);
        m+=x;
    }
    int re=1;
    for(int i=2;i<=m;i++) re=(ll)re*i%mod;
    printf("%lld\n",(ll)re*inv(m-n+1)%mod);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/l_0_forever_lf/article/details/80329157