A - Shaass and Lights

 

A - Shaass and Lights

 CodeForces - 294C 

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

再给定一个开着的灯的数量 M

接着给出开着的灯的位置,范围为[1,N]

要求每次开灯都只能从亮着的灯的旁边打开灯

问把所有灯都点亮 共有多少种方式

思路:

参考:https://blog.csdn.net/qq_38538733/article/details/76409237

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod =1000000007;
const int MAXN = 1005;

int Arr[MAXN];
int N,M;
//组合数打表
ll c_[MAXN][MAXN];
//指数幂打表
ll pow_t[MAXN];

void init(){
	
	pow_t[0] =1;
	c_[0][0] =1;
	for(int i=1;i<MAXN;i++){
		c_[i][0] = 1;
		for(int j=1;j<=i;j++){
            //组合数公式
			c_[i][j] = (c_[i-1][j]+c_[i-1][j-1])%mod; 
		}
		c_[i][i] = 1;
		pow_t[i] = (pow_t[i-1]*2)%mod;
	}
}
int main(){
	init();
	scanf("%d %d",&N,&M);
	for(int i=0;i<M;i++){
		scanf("%d",&Arr[i]);		
	}
	sort(Arr,Arr+M);
	
	int num = N-M;
	ll res = c_[num][Arr[0]-1];	
	
	num-=Arr[0]-1;
	for(int i=1;i<M;i++){
		int x = Arr[i]-Arr[i-1]-1;
		if(x>0) res = ((res*c_[num][x]%mod)*pow_t[x-1])%mod; 
		else res = res*c_[num][x]%mod;
		num-=x;
	}
	res = (res*c_[num][N-Arr[M-1]])%mod;
	
	printf("%lld\n",res);	
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/Willen_/article/details/86736176