题目描述

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. 使用java applet通过签名访问客户端串口

    前端时间公司有需求要访问客户端串口读取电子称的数据,通过网上资料,决定使用applet通过电子签名的形式实现. 1.先写applet:这里我是使用RXRTcomm.jar LocalFileApple ...

  2. Android开发之EditText利用键盘跳转到下一个输入框

    以前做项目的时候,从来没考虑过这些.这段时间公司内部用的一款APP,就出现了这个问题,在登录或者注册的时候,点击键盘的回车按钮,可以跳到下一个输入框的功能,这个属性一直也没记住,今天就把自己一直没记过 ...

  3. CSS基础语法与选择器

    CSS基础 语法 : <head> <style type="text/css"> 选择器(即修饰对象){ 修饰属性:属性值; 修饰属性:属性值; } &l ...

  4. Sde各类命令详解(sdemon 、sdelayer、sdeservice、sdetable、sdeconfig、SdeExport_SdeImport)

      Sdemon命令详解: http://wenku.baidu.com/view/3b53e8ec0975f46527d3e1c2.html 1.重建空间索引       D:\Program Fi ...

  5. 操作系统页面置换算法之FIFO,LRU

    #include<iostream> #include<unistd.h> #include<vector> #include<wait.h> #inc ...

  6. IIS环境搭建

    IIS环境搭建 IIS环境搭建首先是建立在一个干净的.无毒的系统上,再进行相应操作.本文用到的是windows 2003的镜像文件,有条件的用户也可以使用windows的系统安装光盘. 下面进入操作步 ...

  7. Locust性能测试2 分布式运行

    locust分布式可以是本机多进程,也可以是本机作为master,其他机器作slave. 试一下本机的多进程运行: 1  控制台输入 locust -f 脚本路径 --master 2  打开另一个控 ...

  8. 【转载】#443 - An Interface Cannot Contain Fields

    An interface can contain methods, properties, events or indexers. It cannot contain fields. interfac ...

  9. hdu-2136 Largest prime factor---巧用素数筛法

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2136 题目大意: 每个素数在素数表中都有一个序号,设1的序号为0,则2的序号为1,3的序号为2,5的 ...

  10. Android——Activity生命周期

    启动: 触发 onCreate()   onStart()   onResume() Home键: 触发 onPause()    onStop() back键退出: 触发 onPause()   o ...