CF294C Shaass and Lights(组合数学:阶乘的逆元)

题目描述

There are nn lights aligned in a row. These lights are numbered 11 to nn from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on.

He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007 (10^{9}+7)1000000007 (109+7) .

输入格式

The first line of the input contains two integers nn and mm where nn is the number of lights in the sequence and mm is the number of lights which are initially switched on, (1<=n<=1000,1<=m<=n)(1<=n<=1000,1<=m<=n) . The second line contains mm distinct integers, each between 11 to nn inclusive, denoting the indices of lights which are initially switched on.

输出格式

In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (10^{9}+7)1000000007 (109+7) .

题意翻译

有 nn 盏灯,(0<=n<=1000),有 m盏已经点亮,每次只能点亮与已经点亮的灯相邻的灯,求点亮所有灯的总方案数,答案对1e9+7取模

第一行:两个整数 n,m 表示灯的总数和已点亮的灯的数目 第二行m个数,表示已点亮的灯的编号

感谢@ztz11 提供的翻译

输入输出样例

输入 #1复制

3 1
1

输出 #1复制

1

输入 #2复制

4 2
1 4

输出 #2复制

2

输入 #3复制

11 2
4 8

输出 #3复制

6720

思路:

首先,所有关着的的灯可根据开着的分成若干块,利用组合数学的思想,可表示为从n-m个灭着的中选b个连续的灭着的构成一个分区。即:C(n-m,b),每次都从总的剩余灭着的灯中选连续的bk个,k从1到cnt,所以总的选法方案为:

C(sum,b[1])*C(sum-b[1],b[2])*...*C(sum-b[1]-b[2]-..-b[k-1],b[k])

可进一步化简为:sum!/(b[1]!*b[2]!*..*b[k]!)         sum=n-m  当然也可以无视这步,根据上边的原式直接算

对于每一块连续的灭着的段:

若处于中间位置(即:两边都有亮着的,形如01110,1表示灭的,0表示亮的,连续的灭的长度len=3):这时,每次可以选最靠近两边亮着的灯(即最左边的或最右边的)点亮,每次点亮其中一盏,剩下的都是从两边最靠近亮着的两个位置选一个,直到最后只剩一个位置固定,所以点亮这一分区的方案数即为:2的len-1次方。

特殊的,位于两头的连续的灭的段,若最边上的位置为0,只能从前往后或从后往前一次点亮,所以方案数为1

所以根据乘法原理,每次选出一段再乘其方案数,乘起来即为答案:

即:ans=C(sum,b[1])*qpow(2,b[1]-1)*C(sum-b[1],b[2])*qpow(2,b[2]-1)*...*C(sum-b[1]-b[2]-..-b[k-1],b[k])*qpow(2,b[k]-1)

代码:

//ans=C(sum,b[1])*qpow(2,b[1]-1)*C(sum-b[1],b[2])*qpow(2,b[2]-1)*...*C(sum-b[1]-b[2]-..-b[k-1],b[k])*qpow(2,b[k]-1)
//=[sum!/(b[1]!*b[2]!*..*b[k]!)]*qpow(2,b[2]-1)*..*qpow(2,b[k]-1) sum=n-m
#include <iostream>
#include <algorithm>
#include <cstring>

#define int long long

using namespace std;

const int maxn=1e3+8,mod=1e9+7;

int n,m,b[maxn],fac[maxn],p[maxn],cnt,a[maxn];

void init()//预处理除阶乘和2的i次幂
{
    p[0]=fac[0]=1;
    for(int i=1;i<maxn;i++){
        fac[i]=fac[i-1]*i%mod;
        p[i]=p[i-1]*2%mod;
    }
}

int qpow(int x,int y)//快速幂
{
    int res=1%mod;
    while(y){
        if(y&1) res=res*x%mod;
        x=x*x%mod;
        y>>=1;
    }
    return res;
}

int C(int n,int m)
{
    int res=1;
    res=res*fac[n]%mod;
    int inv=qpow(fac[m]*fac[n-m]%mod,mod-2)%mod;//fac[m]*fac[n-m]的逆元
    return res*inv%mod;
}

signed main()
{
    cin>>n>>m;
    init();
    for(int i=1;i<=m;i++){
        int x;
        cin>>x;
        a[x]=1;
    }
    int sum=0,i=1;
    while(i<=n){
        int j;
        for(j=i;j<=n&&!a[j];j++);
        sum+=j-i;
        if(j-i>0) b[++cnt]=j-i;
        i=j+1;
    }
   // for(int i=1;i<=cnt;i++) cout<<b[i]<<endl;
  //  cout<<sum<<endl;
    int ans=1;
    for(int i=1;i<=cnt;i++){
        int t=p[b[i]-1];
        if((i==1&&a[1]==0)||(i==cnt&&a[n]==0)) t=1;
        ans=(ans*(C(sum,b[i])*t%mod))%mod;
        sum-=b[i];
    }
    cout<<ans<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值