题目描述

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) $ .

题意翻译

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

第一行:

两个整数n,m表示灯的总数和已点亮的灯的数目

第二行:

m个数,表示已点亮的灯的编号

思路:

其实这道题一点儿也不难,就是细节很比较多

给了你这样的一个序列,让你去点灯,我们可以分类讨论一下

如果你点的是两端的区间,那么,对不起,由于你只能点亮相邻的,所以你只能一个一个地点下去

如果你点的是中间的,那么,你会发现,你每次可以点区间的右端点,也可以点区间的左端点(除了最后一次,因为这时左右端点其实是同一个端点)。

这样,每个区间的方案数是(2^(length-1))

但由于你可以交错着取,所以很多时候会有重复问题

所以我们要去重。

而由于模数极大,所以还是逆元吧

(ps:灯的标号给的是无序的,要先排序)

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#define p 1000000007
using namespace std;
long long n,m,a,b,x[],k,kl,ans,ny[];
long long res=;
void qny()
{
ny[]=;
for(register int a=;a<=;a++)
{
ny[a]=(p-(p/a))*ny[p%a]%p;
}
}
long long ksm(long long j,long long k)
{
if(k==)
{
res=res%p;
return res;
}
j=j%p;
if(k%==)
{
res=res*j;
res=res%p;
k--;
ksm(j,k);
}
else
{
k=k/;
j=j*j;
j%=p;
ksm(j,k);
}
}
long long jc(int from,int to)
{
long long sd=;
for(int i=from;i<=to;i++)
{
sd=sd*i;
sd=sd%p;
}
return sd;
}
long long jc1(int from,int to)
{
long long sd=;
for(int i=from;i<=to;i++)
{
sd=sd*ny[i];
sd=sd%p;
}
return sd;
}
int main()
{
qny();
ans=;
x[]=;
cin>>n>>m;
x[m+]=n+;
for(a=;a<=m;a++)
{
cin>>x[a];
}
sort(x+,x+m+);
k=n-m;
ans*=jc(,k);
ans=ans%p;
for(a=;a<=m+;a++)
{
kl=x[a]-x[a-]-;
if(kl==)
{
continue;
}
if(a==||a==m+)
{
ans*=jc1(,kl);
ans%=p;
continue;
}
else
{
res=;
ans*=ksm(,kl-);
ans%=p;
ans*=jc1(,kl);
ans%=p;
} }
cout<<ans;
}

CF294C Shaass and Lights(排列组合)的更多相关文章

  1. CF294C Shaass and Lights

    题目大意: 有n盏灯,(0<=n<=1000),有m盏已经点亮,每次只能点亮与已经点亮的灯相邻的灯,求总方案数,答案对1e9+7取模 第一行:两个整数n,m表示灯的总数和已点亮的灯的数目 ...

  2. 学习sql中的排列组合,在园子里搜着看于是。。。

    学习sql中的排列组合,在园子里搜着看,看到篇文章,于是自己(新手)用了最最原始的sql去写出来: --需求----B, C, F, M and S住在一座房子的不同楼层.--B 不住顶层.C 不住底 ...

  3. .NET平台开源项目速览(11)KwCombinatorics排列组合使用案例(1)

    今年上半年,我在KwCombinatorics系列文章中,重点介绍了KwCombinatorics组件的使用情况,其实这个组件我5年前就开始用了,非常方便,麻雀虽小五脏俱全.所以一直非常喜欢,才写了几 ...

  4. 【原创】开源.NET排列组合组件KwCombinatorics使用(三)——笛卡尔积组合

           本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...

  5. 【原创】开源.NET排列组合组件KwCombinatorics使用(二)——排列生成

           本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...

  6. 【原创】开源.NET排列组合组件KwCombinatorics使用(一)—组合生成

           本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...

  7. hdu1521 排列组合(指数型母函数)

    题意: 有n种物品,并且知道每种物品的数量ki.要求从中选出m件物品的排数.         (全题文末) 知识点: 普通母函数 指数型母函数:(用来求解多重集的排列问题) n个元素,其中a1,a2, ...

  8. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  9. 排列组合算法(PHP)

    用php实现的排列组合算法.使用递归算法,效率低,胜在简单易懂.可对付元素不多的情况. //从$input数组中取$m个数的组合算法 function comb($input, $m) { if($m ...

随机推荐

  1. jeecg308 <t:authFilter />标签失效的问题

    <%--该标签放到body末尾会无效,估计是js冲突,放到body前好用--%><t:authFilter /> <body></body>

  2. 洛谷P1941 飞扬的小鸟(背包 dp)

    题意 题目链接 Sol 很显然的dp,设\(f[i][j]\)表示第\(i\)个位置,高度为\(j\)的最小步数 向上转移的时候是完全背包 向下转移判断一下就可以 #include<bits/s ...

  3. 洛谷P1966 火柴排队(逆序对)

    题意 题目链接 Sol 不算很难的一道题 首先要保证权值最小,不难想到一种贪心策略,即把两个序列中rank相同的数放到同一个位置 证明也比较trivial.假设\(A\)中有两个元素\(a, b\), ...

  4. Python爬虫之requests模块(1)

    一.引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃 ...

  5. 又到圣诞节,让你的网页下起雪(js特效)

    又到圣诞节,让你的网页下起雪(js特效) 在4年多前,我写过一个特效,就是让你的网页下起雨,它的效果就是在你打开的网站,雨点下满你的屏幕,恩,大概效果如下图: 当然这个效果还有一些附带项,比如风速.风 ...

  6. java面试题之----转发(forward)和重定向(redirect)的区别

    阅读目录 一:间接请求转发(Redirect) 二:直接请求转发(Forward) 用户向服务器发送了一次HTTP请求,该请求可能会经过多个信息资源处理以后才返回给用户,各个信息资源使用请求转发机制相 ...

  7. C#使用Process类杀死进程,执行命令等

    c#之process类相关整理 一.根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using Sys ...

  8. SharePoint中遇到Timeout

    使用SharePoint时会遇到不止一种的timeout(即超时)错误. 如果遇到了timeout, 该怎么区分呢? 大致上SharePoint可以控制和影响的timeout地方如下: 1. Shar ...

  9. Visual Studio 快捷键汇总

    常见方法: 强迫智能感知:Ctrl+J.智能感知是Visual Studio最大的亮点之一,选择Visual Studio恐怕不会没有这个原因.  撤销:Ctrl+Z.除非你是天才,那么这个快捷键也是 ...

  10. asp.net超过字数限制用省略号...表示

    显示8个字,多于8个字, 用...表示           <asp:Repeater runat="server" ID="MsgLists">  ...