CF294C Shaass and Lights

CF294C Shaass and Lights

题意翻译

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

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

感谢@ztz11 提供的翻译

题目描述

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

输入格式

The first line of the input contains two integers n n n and m m m where n n n is the number of lights in the sequence and m m m is the number of lights which are initially switched on, (1<=n<=1000,1<=m<=n) (1<=n<=1000,1<=m<=n) (1<=n<=1000,1<=m<=n) . The second line contains m m m distinct integers, each between 1 1 1 to n n n 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) .

Solution

首先必须发现的是,在一个段了的数贡献是一样的,

那么,我们套一下公式,$$ ans = \frac{n!}{a_1!a_2!~...a_n!} $$ , 显然这里的 $$ n $$ 是(n-m)。$$a_i$$ 是段长。

注意到这里还没有得到答案,还要乘上内部段的方案。

/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG std::cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
#define MOD 1000000007
long long ksm(long long x,long long base){
	long long ret = 1;
	long long xt = x;
	while (base > 0){ 
		if (base & 1)ret = ret * xt % MOD;
		xt = xt * xt % MOD, base /= 2;
	}
	return ret;
}

long long f[1100];
long long invf[1100];
int n,m;
bool a[1100];
int main()
{
	f[0] = 1;
	invf[0] = 1;
	for (int i=1;i<=1010;i++){
		f[i] = f[i-1] * i % MOD;
		invf[i] = ksm(f[i],MOD-2);
	}
	std::cin >> n >> m;
	int MAX = 0;
	int MIN = n;
	for (int i=1;i<=m;i++)
	{
		int tmp;
		std::cin >> tmp;
		MAX = std::max(MAX,tmp);
		MIN = std::min(MIN,tmp);
		a[tmp] = 1;
	}
	long long ans = 1;
	int lft = 0;
	a[n+1] = 1;
	for (int i=1;i<=n+1;i++){
		if (a[i] == 1)
			ans = ans * invf[i-lft-1] % MOD, lft = i;
	}
	lft = m-1;
	for (int i=1;i<=n;i++){
		if (a[i] == 1 && a[i-1] == 1)
			lft --;
	}
	ans = ans * f[n-m] % MOD;
	ans = ans * ksm(2,MAX-MIN+1-m-lft) % MOD;
	std::cout << ans << std::endl;
}
posted @ 2019-08-05 10:43  dgklr  阅读(336)  评论(0编辑  收藏  举报