UESTC 1268 Open the lightings

来源:互联网 发布:如何设置数据的有效性 编辑:程序博客网 时间:2024/05/02 10:41

Description

There are n light(s) in a row.These lights are numbered 1 to n from left to right.One of the lights are switched on.I wants to switch all the lights on. 
At each step I can switch a light on(this light should be switched off at that moment)if there's at least one “very close” light which is already switched on. 
More exactly: when No.i light(this light is switched on now),No.j light can be switched on(this light is switched off now) if and only if |i-j|<=2
I knows the initial state of lights and I wonder how many different ways there exist to switch all the lights on.

Please find the required number of ways modulo 1000000007 (10^9+7).

Input

The first line of the input contains two integers n and m where n is the number of lights in the sequence and No.m light are initially switched on,(1<=n<=1000,1<=m<=n).

Output

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).

Sample Input

3 1

Sample Output

2


这题只要把灯拆成左边和右边就行了,左边的情况是没办法影响到右边的,所有两边是独立的。

那么只要单独计算当f[i]前i个灯全量的方案数就行了,这个可以很快找到规律递推出来,然后是两边

的合并,因为互相独立,点i盏灯,右边点j盏灯的全部组合就相当于往i+1个盒子里放j个相同的球的情况

直接预处理出f和组合数就好了。

#include<iostream>  #include<algorithm>#include<cmath>#include<cstdio>#include<vector>#include<cstring>#include<string>using namespace std;typedef long long LL;const int maxn = 1e3 + 5;const int base = 1e9 + 7;const int low(int x){ return x&-x; }int T, n, m;LL f[maxn], c[maxn][maxn];int main(){//scanf("%d", &T);f[0] = f[1] = 1;for (int i = 2; i < 1000; i++) f[i] = ((LL)(i - 1)*f[i - 2] + f[i - 1]) % base;for (int i = 1; i < 1000; i++){c[i][0] = c[i][i] = 1;for (int j = 1; j < i; j++){c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % base;}}while (~scanf("%d%d", &n, &m)){int x = m - 1, y = n - m;printf("%lld\n", c[n - 1][y] * f[x] % base*f[y] % base);}return 0;}


0 0