JZOJ 4786. 【NOIP2016提高A组模拟9.17】小a的强迫症

Description

JZOJ 4786. 【NOIP2016提高A组模拟9.17】小a的强迫症_第1张图片

Input

输入

Sample Input

3
2 2 1

Output

输出

Sample Output

3

样例解释:

JZOJ 4786. 【NOIP2016提高A组模拟9.17】小a的强迫症_第2张图片

Data Constraint

数据范围

Solution

  • 首先,我们设之前放了 Sum 个珠子,当前要放 Ai 个珠子。

  • 那么这 Ai 个珠子中要有一个放在那 Sum 个珠子后面。

  • 于是就有 Ai1 个珠子要插入到 Sum+Ai1 个珠子之中。

  • 这,便是一个经典的挡板问题!!

  • 这样答案就是 CAi1Sum+Ai1

Code

#include
using namespace std;
const int N=500001,mo=998244353;
long long ans=1,pre[N];
int sum;
inline int read()
{
    int data=0; char ch=0;
    while(ch<'0' || ch>'9') ch=getchar();
    while(ch>='0' && ch<='9') data=data*10+ch-'0',ch=getchar();
    return data;
}
inline long long ksm(long long x,int y)
{
    long long s=1;
    while(y)
    {
        if(y&1) s=s*x%mo;
        x=x*x%mo;
        y/=2;
    }
    return s;
}
int main()
{
    int n=read();
    for(int i=pre[0]=1;i1]*i%mo;
    while(n--)
    {
        int x=read();
        ans=ans*pre[sum+x-1]%mo*ksm(pre[x-1],mo-2)%mo*ksm(pre[sum],mo-2)%mo;
        sum+=x;
    }
    printf("%lld",ans);
    return 0;
}

你可能感兴趣的:(排列组合,数论,逆元,快速幂,组合数)