codeforces 294C-Shaass and Lights (组合数学)

There are n lights aligned in a row. These lights are numbered 1 to n 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 (109 + 7).

Input

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

Output

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

Examples
input
3 1
1
output
1
input
4 2
1 4
output
2
input
11 2
4 8
output
6720

【 题解】组合数学,n盏灯,刚开始知道所有灯的关闭情况,要求有多少种开灯方式可以把全部的灯都打开,每次开灯只能打开左或右边至少有一盏灯亮着的。

 首先分析,以样例三为例,1 2 3 4 5 6 7 8 9 10 11,可以发现,4以前的灯都必须从右往左开,8以后的灯只能从左往右开,而这里面也存在顺序问题,而夹在4和8中间的灯,每次都有左右两盏灯备选,所以方案数就是2^n,而先开两端还是中间也存在顺序,这就用插板法来模拟,C[sum][num],sum是当前分段的元素总数,num是当前段元素总数;

 【AC代码】

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1005;
const int inf=1e9+7;
typedef __int64 ll;
int m,n;
ll a[N];
ll c[N][N];
ll vis[N];

void Init()//打表  记得放在while循环外
{
    for(int i=0;i<=1000;i++) c[i][0]=1;

    for(int i=1;i<=1000;++i)
        for(int j=1;j<=1000;++j)
        {
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%inf;
        }
    vis[0]=1;
    for(int i=1;i<=1000;++i) vis[i]=(vis[i-1]*2)%inf;
    return ;
}

int main()
{
    Init();
    while(~scanf("%d%d",&m,&n))
    {
        for(int i=1;i<=n;++i)
            scanf("%I64d",&a[i]);
        sort(a+1,a+n+1);
        int num=m-n;
        ll ans=c[num][a[1]-1];
        num-=a[1]-1;
        for(int i=2;i<=n;++i)
        {
             int x=a[i]-a[i-1]-1;
             if(x) ans=((ans*c[num][x])%inf*vis[x-1])%inf;
             else ans=(ans*c[num][x])%inf;
             num-=x;
        }
        ans=(ans*c[num][m-a[n]])%inf;
        printf("%I64d\n",ans);
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值