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
#include
#include
#include
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;
}

你可能感兴趣的:(ACM__数论)