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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值