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__ << "\t" << "Line: " << __LINE__ << "\t :"
#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;
}

转载于:https://www.cnblogs.com/dgklr/p/11301593.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值