一道风骚的DP

时间:2023-03-09 01:17:33
一道风骚的DP

也是校赛学长出的一道题~想穿了很简单。。但我还是听了学长讲才明白。

观察力有待提高。

Problem D: YaoBIG’s extra homework
Time Limit
Memory Limit
1 s
128 MB
Description
One day, a teacher assigned this extra homework to students of UCAS:
"What’s the number of the solution that an n n ladder is covered by exactly n rectangles? Note
that every two rectangles cannot overlap and the rectangles cannot reach out of the ladder."(The "Note"
section gives you an example of the "ladder".)
While YaoBIG was awkward, he still wanted the extra points, so he asked you to help him.
Input
The input contains zero or more test cases and the first line of the input gives the number T of test
cases. For each test case:
There is only one line with an integer n.
Output
For each test case, output the number of the solution that an n n ladder is covered by exactly n
rectangles, which is described above.
Notice that the answer might be too large, so you should output the answer modulo 1000000007.
Limits
0 <=T <=1 000
1 <=n <=1000
Sample input and output
Sample Input Sample Output
1
4
14
Note
The diagram explaining the case where n = 4 is shown below:

一道风骚的DP

大致复盘了一下应该怎么想。

从求n阶阶梯形由n个矩形填满的种类这个问题本身我们大致能嗅到一点DP的味道,并且同时比较容易发现搜索保证最后恰好是n个填满相当困难。

于是找一下有没有最优子结构。

关键在于一个矩形不可能填满两个棱角。所以每个棱角都必然由不同的矩形填充,然后考虑左上角的那个格子由某个矩形填充,左上角的格子和一个棱角格子由一个矩形占据,然后问题就被划分成了两个同理的子问题。

另外有1000个cases,但是只用算一遍然后对每个case输出答案即可。

 #include<cstdio>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define p 1000000007;
using namespace std;
const int MAXN=;
long long int dp[MAXN];
int main()
{
// freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
int n;
dp[]=;dp[]=;
rep(i,,)
{
rep(j,,i)
{
dp[i]+=dp[i-j]*dp[j-];
dp[i]%=p;
}
}
rep(i,,T)
{
scanf("%d",&n);
printf("%lld\n",dp[n]);
}
return ;
}