CF294C Shaass and Lights(排列组合)
题目描述
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(排列组合)的更多相关文章
- CF294C Shaass and Lights
题目大意: 有n盏灯,(0<=n<=1000),有m盏已经点亮,每次只能点亮与已经点亮的灯相邻的灯,求总方案数,答案对1e9+7取模 第一行:两个整数n,m表示灯的总数和已点亮的灯的数目 ...
- 学习sql中的排列组合,在园子里搜着看于是。。。
学习sql中的排列组合,在园子里搜着看,看到篇文章,于是自己(新手)用了最最原始的sql去写出来: --需求----B, C, F, M and S住在一座房子的不同楼层.--B 不住顶层.C 不住底 ...
- .NET平台开源项目速览(11)KwCombinatorics排列组合使用案例(1)
今年上半年,我在KwCombinatorics系列文章中,重点介绍了KwCombinatorics组件的使用情况,其实这个组件我5年前就开始用了,非常方便,麻雀虽小五脏俱全.所以一直非常喜欢,才写了几 ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(三)——笛卡尔积组合
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(二)——排列生成
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(一)—组合生成
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- hdu1521 排列组合(指数型母函数)
题意: 有n种物品,并且知道每种物品的数量ki.要求从中选出m件物品的排数. (全题文末) 知识点: 普通母函数 指数型母函数:(用来求解多重集的排列问题) n个元素,其中a1,a2, ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- 排列组合算法(PHP)
用php实现的排列组合算法.使用递归算法,效率低,胜在简单易懂.可对付元素不多的情况. //从$input数组中取$m个数的组合算法 function comb($input, $m) { if($m ...
随机推荐
- ubuntu 14.04 64bit 安装 oracle 11g r2
参考文章:http://tutorialforlinux.com/2016/03/09/how-to-install-oracle-11g-r2-database-on-ubuntu-14-04-tr ...
- IO流之递归
递归: 递归,指在当前方法内调用自己的这种现象 public void method(){ System.out.println(“递归的演示”); //在当前方法内调用自己 method(); } ...
- [原创]vs2012创建的ado.net模型无法实例化的问题
最近从vs2010升级到vs2012,建立数据模型,发现生成的东西跟以前不一样了,而且也无法实例化使用.百度尝试了n种关键词,终于被我找到解决的方法.在这里记录一下. 1.打开设计器,也就是双击这个 ...
- 原生js的math对象
Math对象方法 //返回最大值 var max=Math.max(95,93,90,94,98); console.log(max); //返回最小值 var min=Math.min(95,93, ...
- vim右键粘贴 等杂
putty连上linux,vim编辑个文件,我去,右键不能用用上下面的命令,就好了. set mouse-=a 今天发现mysql倒入utf-8的文件网站显示出来都是乱码,不过用utf-8的控制台看是 ...
- Android 实现电话录音(窃听)
配置文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=&qu ...
- 【Linux】文件操作函数(系统调用函数)
重点在于学习--思路与方法 举一反三 一.文件描述符 系统分配给文件的数字编号 二.函数学习 P.S.Man命令使用方法 manual 前三个章节 命令:系统调用函数:库函数 man read //r ...
- keras 保存训练的最佳模型
转自:https://anifacc.github.io/deeplearning/machinelearning/python/2017/08/30/dlwp-ch14-keep-best-mode ...
- 64位MATLAB和C混合编程以及联合调试
[环境说明] 电脑操作系统:win 7 旗舰版,64位 MATLAB版本:R2013b VS版本:Microsoft Visual Studio 2010 [操作说明] 1.在原来工程的基础上添加下列 ...
- 使用命令行执行.sql文件
用微软自带的sqlcmd工具,可以导入执行.以SQL Server 2008R版本为例: 第一步:Win+R 键入:cmd 命令,开启命令行工具: 第二步:键入:cd C:\Program Files ...