JZOJ4391. 【GDOI2016模拟3.16】装饰

这里写图片描述
这里写图片描述

题解

题目要求两行的方案数,
那假设它存在第三行,
它的颜色就是除了上面两种颜色之外剩下的颜色。

假设知道了第三行的方案数,
那么答案就是这个方案数*2。
考虑如何求出第三行的方案数,
假设开头颜色为X,其数量为x,
那么两两相邻的X可以将这个第三行分成x段或者x-1段(最后是否以X结尾)。

假设第三行被分成了g段,
再枚举一个e,表示长度为偶数的段数。
那么也就是说有g-e段的长度为奇数。
设在这些长度为奇数的段中,开头为Y的有oy个,开头为Z的有oz个。
可以知道它们之间要满足的关系试。
oy+oz=g-e(开头总数等于奇数段数)
y-oy=z-oz(出去开头剩下的Y,Z需要两两配对)
有了这些就可以算现在的方案数:
oy个Y开头放在g-e个长度奇数的段中: C g e o y
e个偶数段在g个段中: C g e
剩下的Y(Z)放在g个段中: C y o y e + g 1 y o y e (隔板问题)
每一个偶数段的开头,为Y或者X: 2 e
把这些全部乘起来就可以了。

code

#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <time.h>
#define ll long long
#define N 1000003
using namespace std;

const int mo=1000000007;
ll jc[N],ny[N],ans;
int m,r,g,b;

ll ksm(ll x,int y)
{
    ll s=1;
    for(;y;y>>=1,x=x*x%mo)
        if(y&1)s=s*x%mo;
    return s;
}

ll C(int x,int y)
{
    if(x>y)return 0;
    return jc[y]*ny[x]%mo*ny[y-x]%mo;
}

void calc(int g,int e,int y,int z)
{
    if(((g-e+y-z)&1) || ((g-e+z-y)&1))return;
    int oy=(g-e+y-z)>>1,oz=(g-e+z-y)>>1,r=y-e-oy;
    if(oy<0 || oz<0)return;
    ans=(ans+C(g-1,r+g-1)*C(e,g)%mo*C(oy,g-e)%mo*ksm(2,e)%mo)%mo;
}

void work(int x,int y,int z)
{
    int g=x;
    for(int e=0;e<=g;e++)
        calc(g,e,y,z);
    g=x-1;
    for(int e=0;e<=g;e++)
        calc(g,e,y,z);
}

int main()
{
    ans=jc[0]=1;
    for(int i=1;i<N;i++)
        jc[i]=jc[i-1]*i%mo;
    ny[N-1]=ksm(jc[N-1],mo-2);
    for(int i=N-1;i;i--)
        ny[i-1]=ny[i]*i%mo;

    scanf("%d%d%d%d",&m,&r,&g,&b);

    r=m-r;g=m-g;b=m-b;

    ans=0;
    work(r,g,b);
    work(g,r,b);
    work(b,g,r);
    printf("%lld",ans*2%mo);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/lijf2001/article/details/81123085