zoukankan      html  css  js  c++  java
  • 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__ << "	" << "Line: " << __LINE__ << "	 :"
    #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;
    }
    
  • 相关阅读:
    【Q&A】pytorch中的worker如何工作的
    【教程】opencv-python+yolov3实现目标检测
    ubuntu使用scrcpy手机投屏-免费神器scrcpy【介绍、安装、使用】
    【从踩坑到教程】win10下ubuntu18.04双系统UEFI模式安装、Nvidia驱动安装
    Python引用与目录结构
    交流总结
    转载-趣图展现程序员职业生涯的11个阶段
    转载-在家工作,10招助你效率、生活两不误
    转载-新年寄望:从小做起,活在当下
    转载-成为明星程序员的10个提示
  • 原文地址:https://www.cnblogs.com/dgklr/p/11301593.html
Copyright © 2011-2022 走看看