Problem Statement
Joisino has a lot of red and blue bricks and a large box. She will build a tower of these bricks in the following manner.
First, she will pick a total of N bricks and put them into the box. Here, there may be any number of bricks of each color in the box, as long as there are N bricks in total. Particularly, there may be zero red bricks or zero blue bricks. Then, she will repeat an operation M times, which consists of the following three steps:
Take out an arbitrary brick from the box.
Put one red brick and one blue brick into the box.
Take out another arbitrary brick from the box.
After the M operations, Joisino will build a tower by stacking the 2×M bricks removed from the box, in the order they are taken out. She is interested in the following question: how many different sequences of colors of these 2×M bricks are possible? Write a program to find the answer. Since it can be extremely large, print the count modulo 109+7.
Constraints
1≤N≤3000
1≤M≤3000
题目大意
定义一种操作:
先从箱子里面取出一个球,
然后放入一个红球和一个蓝球,
再取出一个球。
原来箱子里面有n个球,
经过m轮操作,
问最终有多少个不同的序列。
题解
设
fi,j
f
i
,
j
来表示经过i轮操作,初始的红球数量为j的方案数。
但某些合法的序列可能会对应多个初始红球数量,
就用最小的初始红球数量来表示。
最小的初始红球数量肯定会在某个时刻变为0,
所以增加一维0/1来表示是否存在某个时刻,
红球的数量变为了0。
最终的答案就是
∑fm,i,1
∑
f
m
,
i
,
1
code
#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <time.h>
#define ll long long
#define N 3003
#define M 103
#define db double
#define P putchar
#define G getchar
using namespace std;
char ch;
void read(int &n)
{
n=0;
ch=G();
while((ch<'0' || ch>'9') && ch!='-')ch=G();
ll w=1;
if(ch=='-')w=-1,ch=G();
while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
n*=w;
}
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
ll abs(ll x){return x<0?-x:x;}
ll sqr(ll x){return x*x;}
void write(ll x){if(x>9) write(x/10);P(x%10+'0');}
int f[N][N][2],n,m,ans;
void add(int& a,int b)
{
a=(a+b)%1000000007;
}
int main()
{
read(n);read(m);
f[0][0][1]=1;
for(int i=1;i<=n;i++)
f[0][i][0]=1;
for(int i=0;i<m;i++)
for(int j=0;j<=n;j++)
for(int k=0;k<=1;k++)
{
if(j)add(f[i+1][j-1][!(j-1)|k],f[i][j][k]);
if(n-j)add(f[i+1][j+1][k],f[i][j][k]);
if(j)add(f[i+1][j][k|(j==1)],f[i][j][k]);
if(n-j)add(f[i+1][j][k],f[i][j][k]);
}
ans=0;
for(int i=0;i<=n;i++)
add(ans,f[m][i][1]);
write(ans);
return 0;
}