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[1005],k,kl,ans,ny[1005];
long long res=1;
void qny()
{
    ny[1]=1;
    for(register int a=2;a<=1001;a++)
    {
        ny[a]=(p-(p/a))*ny[p%a]%p;
    }
}
long long ksm(long long j,long long k)
{
    if(k==0)
    {
        res=res%p;
        return res;
    }
    j=j%p;
    if(k%2==1)
    {
        res=res*j;
        res=res%p;
        k--;
        ksm(j,k);
    }
    else
    {
        k=k/2;
        j=j*j;
        j%=p;
        ksm(j,k);
    }    
}
long long jc(int from,int to)
{
    long long sd=1;
    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=1;
    for(int i=from;i<=to;i++)
    {
        sd=sd*ny[i];
        sd=sd%p;
    }
    return sd;
}
int main()
{
    qny();
    ans=1;
    x[0]=0;
    cin>>n>>m;
    x[m+1]=n+1;
    for(a=1;a<=m;a++)
    {
        cin>>x[a];
    }
    sort(x+1,x+m+1);
    k=n-m;
    ans*=jc(1,k);
    ans=ans%p;
    for(a=1;a<=m+1;a++)
    {
        kl=x[a]-x[a-1]-1;
        if(kl==0)
        {
            continue;
        }
        if(a==1||a==m+1)
        {
            ans*=jc1(1,kl);
            ans%=p;
            continue;
        }
        else
        {
            res=1;
            ans*=ksm(2,kl-1);
            ans%=p;
            ans*=jc1(1,kl);
            ans%=p;
        }
        
    }
    cout<<ans;
}

猜你喜欢

转载自www.cnblogs.com/ztz11/p/9290474.html