题解:CF2182D Christmas Tree Decoration

· · 题解

思路讲解

首先,不难发现,其实这个顺序,只会影响到最后一轮的情况(只有最后一轮是有可能不完整的,顺序只会影响不完整轮次)。

因此只需要考虑一下最后一轮的情况。最后一轮,为了让更多的数字可以被考虑,我们之前轮次(共 \lfloor \frac{sum}{n}\rfloor 轮,sum 为总装饰数量)希望数字更多的使用自己。

在最后一轮,一共进行 rem\leftarrow sum \bmod n 次操作。在最后一轮中,能够排在前 rem 的数,首先是最后操作下来,还剩一个装饰品的盒子(其数量我们记为 cnt_1),还有就是选一些其他的空盒子,来消耗这个 0 盒子(0 盒子剩余装饰品数量我们记为 A_0)。如果分开计算选 1 的排列和不选 1 的排列,合起来会比较困难,我们不妨先假设我们已经选好了,那么就是 rem! 种排列方法,那我们一共有多少种选法呢?一共有 \binom{n-cnt_1}{A_0} 种选法,剩余的盒子随便排,一共是 (n-rem)! 种排法。

因此最后答案如下。

AC 代码

AC

https://codeforces.com/contest/2182/submission/355784231 ::::info[代码]

/**
 * Problem: D. Christmas Tree Decoration
 * Contest: Educational Codeforces Round 186 (Rated for Div. 2)
 * Judge: Codeforces
 * URL: https://codeforces.com/contest/2182/problem/D
 * Created: 2025-12-29 23:00:52
 * Author: Gospel_rock
 */

#include <bits/stdc++.h>
#include <ranges>
#define all(vec) vec.begin(),vec.end()
#define lson(o) (o<<1)
#define rson(o) (o<<1|1)
#define SZ(a) ((long long) a.size())
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define fsp(x) fixed<<setprecision(x)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using DB = double;
using LD = long double;
// using i128 = __int128;
using CD = complex<double>;

static constexpr ll MAXN = (ll)1e3+10, INF = (1ll<<61)-1;
static constexpr ll mod = 998244353; // (ll)1e9+7; 
static constexpr double eps = 1e-8;
const double pi = acos(-1.0);

ll lT;

/*
 *
 */
namespace Comb{    // 组合数加逆元,依赖于这个MAXN(初始化)
    vector<ll> frac,infrac;
    int ln=1;
    bool initialized=false;
    inline ll binpow(ll a, ll k) {
        ll res = 1;
        a %= mod;
        while (k > 0) {
            if (k & 1) res = (res * a) % mod;
            a = (a * a) % mod;
            k >>= 1;
        }
        return res;
    }

    void Comb(int n) {
        ln = n;
        frac.assign(n + 5, 1);
        infrac.assign(n + 5, 1);
        for (int i = 1; i <= ln; ++i) {
            frac[i] = (frac[i - 1] * i) % mod;
        }
        infrac[ln] = binpow(frac[ln], mod - 2);
        for (int i = ln - 1; i >= 0; --i) {
            infrac[i] = (infrac[i + 1] * (i + 1)) % mod;
        }
    }

    inline ll CC(ll n, ll k) {
        if (k < 0 || k > n) {
            return 0; // Return 0 if k is out of range [0, n]
        }
        if(!initialized){
            Comb(MAXN);
            initialized=true;
        }
        ll res = frac[n];
        res = (res * infrac[n - k]) % mod;
        res = (res * infrac[k]) % mod;
        return res;
    }

    inline ll PP(ll n, ll k) {
        if (k < 0 || k > n) {
            return 0;
        }
        if(!initialized){
            Comb(MAXN);
            initialized=true;
        }
        ll res = frac[n] * infrac[n - k];
        res %= mod;
        return res;
    }
}
using namespace Comb;

void Solve() {
    ll N;
    cin >> N;
    vector<ll> A(N+1);
    for (int i=0;i<=N;++i) {
        cin>>A[i];
    }
    ll sum=accumulate(all(A),0ll);
    ll round=sum/N,rem=sum%N;
    for (int i=1;i<=N;++i) {
        // ll lround=round;
        // if (i<=rem) {
        //     lround++;
        // }
        if (A[0]+A[i]<round) {
            cout<<0<<"\n";
            return;
        }
        ll need_subtract=max(0ll,round-A[i]);
        A[0]-=need_subtract;
        if (A[i]>=round) {
            A[i]-=round;
        }else {
            A[i]=0;
        }
    }
    if (A[0]==rem) {
        cout<<PP(N,N)<<"\n";
        return;
    }
    ll cnt1=0;
    for (int i=1;i<=N;++i) {
        if (A[i]==0) {
            continue;
        }
        if (A[i]>=2) {
            cout<<0<<"\n";
            return;
        }
        if (A[i]==1) {
            ++cnt1;
        }
    }
    if (cnt1+A[0]!=rem) {
        cout<<0<<"\n";
        return;
    }
    ll ans=PP(rem,rem);
    // if (A[0]!=0) {
        ans*=CC(N-cnt1,A[0]);
    // }
    ans%=mod;
    ans*=PP(N-rem,N-rem);
    ans%=mod;
    cout<<ans<<"\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    cin >> lT;
    while(lT--)
        Solve();
    return 0;
}

/*

1
3
1 2 1 0

*/

::::