loj3119 "CTS2019 | CTSC2019." Random Cube

Topic links: loj3119

luogu5400

See "exactly \ (k \) th" almost can guess is related to the inclusion-exclusion

Note \ (F_i \) is at least \ (I \) a great number of the program number, \ (Ans_i \) represents exactly (I \) \ a number of programs, there are
\ [f_k = \ sum_ { i = k} ^ {min (
n, m, l)} \ dbinom {k} {i} Ans_i \] by the binomial inversion to obtain
\ [Ans_k = \ sum_ {i = k} ^ {min (n, m, l)} (- 1
) ^ {ik} \ dbinom {k} {} f_i \] consider computing \ (F_i \) , for \ (I \) a great number, they can influence the position there is at least one of them with the same one-dimensional coordinate position, the remaining positions can be freely arranged, so we can write \ (F_i \) calculation formula
\ [f_i = \ dbinom {nml } {g_i} b_ih_i ( G_i-NML)! \]
\ (G_i \) represents \ (I \) a great number of points having at least the same number of one-dimensional coordinate

\ (b_i \) represents elect (i \) \ program a great number of number

\ (H_i \) shows a \ (G_i \) lattice Insert \ (G_i \) number of the number of programs

Consider how the three values ​​determined above

\ (g_i \) is relatively simple calculation, taking into account all the coordinates are not the great number to the same number, the last \ (g_i = nml- (ni) (mi) (li) \)

\ (B_i and H_i \) is calculated slightly more complex d, where we write \ (I \) a great number are \ (A_1, A_2, \ cdots, a_i \) , and \ (a_1 <a_2 < \ cdots <a_k \)

First \ (B_i \) , as we have chosen \ (a_1-a_ {j- 1} \) when, again consider \ (a_j \) , then it and the previous (j-1 \) \ number a dimensional coordinates are the same, then there is \ (b_i = \ prod_ {j = 0} ^ {i-1} (ni) (mi) (li) \)

Followed by \ (H_i \) , we are still in accordance with the order of subscripts to consider the number of fill, assuming we have \ (a_1-a_ {j- 1} \) numbers, and have at least the same point to fill a one-dimensional coordinates thereof into a cube, consider now \ (a_j \) , note that we do not consider \ (a_j \) has at least the same and one-dimensional coordinate position has been to fill in the number, because the definition has to ensure the legitimacy

So we only need to consider those new \ (a_j \) have at least the same point one-dimensional coordinate (and in addition to their own, because it must fill in all of the current maximum number), is \ (g_i-g_ {i- 1} + 1 \) , so \ (h_i = \ frac {( g_i-1)!} {(g_ {i-1}!)} * h_ {i-1} = \ prod_ {j = 1} ^ i \ frac {(g_j-1) !} {(g_ {j-1})!} \)

Them back to the original formula
\ [\ begin {aligned} f_i = & \ frac {(nml)!} {(Nml-g_i)! G_i!} B_i \ prod_ {j = 1} ^ i \ frac {(g_j- 1)!} {g_ {j -1}!} (nml-g_i)! \\ = & \ frac {nml!} {g_i!} b_i \ prod_ {j = 1} ^ i (g_j-1)! \ prod_ {j = 0} ^ { i-1} \ frac {1} {g_j!} \\ = & (nml!) b_i \ prod_ {j = 1} ^ i \ frac {1} {g_j} \ end { aligned} \]
Finally, since the probability is seeking, directly off about \ (nml! \)

The rest is a communication disorder Well, pay attention to the process \ (g_j \) quickly power can not be violent when the inverse, and inverse factorial method similar elements should be taken to solve the specific details, see the code

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define fir first
#define sec second
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define maxd 998244353
#define eps 1e-6
typedef long long ll;
const int N=5000000;
const double pi=acos(-1.0);
int n,m,l,k;
ll fac[5005000],invfac[5005000],f[5005000],g[5005000],invg[5005000];

int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

ll qpow(ll x,ll y)
{
    ll ans=1;
    while (y)
    {
        if (y&1) ans=ans*x%maxd;
        x=x*x%maxd;y>>=1;
    }
    return ans;
}

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

int main()
{
    fac[0]=1;invfac[0]=1;
    rep(i,1,N) fac[i]=fac[i-1]*i%maxd;
    invfac[N]=qpow(fac[N],maxd-2);
    per(i,N-1,1) invfac[i]=invfac[i+1]*(i+1)%maxd;
    int T=read();
    while (T--)
    {
        n=read();m=read();l=read();k=read();
        int p=min(min(n,m),l);
        if (p<k) {puts("0");continue;}
        ll tot=1ll*n*m%maxd*l%maxd;
        f[0]=1;
        rep(i,0,p-1) f[i+1]=f[i]*(n-i)%maxd*(m-i)%maxd*(l-i)%maxd;
        ll prog=1;
        rep(i,1,p)
        {
            g[i]=(tot-1ll*(n-i)*(m-i)%maxd*(l-i)%maxd+maxd)%maxd;
            prog=prog*g[i]%maxd;
        }
        invg[p]=qpow(prog,maxd-2);
        per(i,p-1,0) invg[i]=invg[i+1]*g[i+1]%maxd;
        ll ans=0;
        rep(i,k,p)
        {
            if ((i-k)&1) ans=(ans+maxd-C(i,k)*f[i]%maxd*invg[i]%maxd)%maxd;
            else ans=(ans+C(i,k)*f[i]%maxd*invg[i]%maxd)%maxd;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/encodetalker/p/10958215.html