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 ...
随机推荐
- 【MSDN】 SqlServer DBCC解析
汇总学习下SqlServer的DBCC指令. DBCC:Transact-SQL 编程语言提供 DBCC 语句以作为 SQL Server 的数据库控制台命令. 数据库控制台命令语句可分为以下类别. ...
- Java线程同步打印ABC
需求: 三个线程,依次打印ABCABCABC.... 方案一: 使用阻塞队列,线程1从队列1获取内容打印,线程2从队列2获取内容打印,线程3从队列3中获取内容打印.线程1把B放到队列3中,线程2把C放 ...
- Backbone事件机制核心源码(仅包含Events、Model模块)
一.应用场景 为了改善酷版139邮箱的代码结构,引入backbone的事件机制,按照MVC的分层思想搭建酷版云邮局的代码框架.力求在保持酷版轻量级的基础上提高代码的可维护性. 二.遗留问题 1.b ...
- form中button特殊功能
描述:写弹窗的时候发现,form中的button,不对它进行什么设置,它会有默认的操作,点击“发送验证码”或者“提交申请”,它都会退出弹窗(取消遮罩层) 解决:button有不同的type属性,只需要 ...
- 洛谷P3177 [HAOI2015]树上染色(树上背包)
题意 题目链接 Sol 比较套路吧,设\(f[i][j]\)表示以\(i\)为根的子树中选了\(j\)个黑点对答案的贡献 然后考虑每条边的贡献,边的两边的答案都是可以算出来的 转移的时候背包一下. # ...
- Elipse plugin or Bundle & OSGI
Develop and register service, lookup and use service! Android Design on service's publish-find-bind ...
- CNN 和RNN 中input 长度不一致问题
转自:https://www.jianshu.com/p/86d667ee3c62,感谢分享! pad_sequences & Masking layer 上面提到,文本数据也可以用CNN来处 ...
- sparkpython
http://blog.csdn.net/ydq1206/article/details/51922148
- Django初步(一):安装和简单演示
参考:Windows下安装Django:http://www.th7.cn/Program/Python/201305/136301.shtml
- GET来传递数据的实例
实例 下面实例是一点典型的使用GET来传递数据的实例: 客户端请求: GET /hello.txt HTTP/1.1 User-Agent: curl/7.16.3 libcurl/7.16.3 Op ...