Fabricating Sculptures
Miguel Angelo is a great sculptor, widely recognized for his outdoor sculptures. In his hometown, it is very common to find one of his creations in squares and gardens. People love his sculptures, not only for their beauty, but also because they look like new even after decades.The sculptures do not degrade easily due to the material and technique developed by Miguel and his staff over the years.
To build the sculptures, he first constructs its base by stacking blocks of waterproof plaster(his secret material), forming several stacks of blocks in a straight line. He always uses identical blocks, and each stack has at least one block. To stabilize the structure, he surrounds it by two big glass panes, one behind the stacks and one in front of them. Then he waits for the rain for as long as it takes. If the structure is such that it doesn’t accumulate water during this procedure, Miguel is sure that the base can be used to obtain a piece of long-lasting artwork.Notice that water will accumulate on a block if there are obstacles (other blocks) on both sides(to the left and to the right).
The following picture shows the front view of several different bases. All of them consist of three stacks made of a total of six blocks, with each stack having at least one block as required.However, the eight bases on the left will lead to long-lasting artwork, while the two bases on the right will not.
Miguel Angelo is receiving a lot of sculpture requests. Although he has all the freedom to create the artwork, he wants to be fair and use the same number of stacks and the same number blocks in each of the sculptures. Since he doesn’t want to sell identical sculptures to different clients, he will construct a different base each time.
He worries that he won’t be able to fulfill all the requests. Help him calculate the number of different bases given the number of stacks and the number of blocks that the base must have.
Input
The input consists of a single line that contains two integers S and B (1 ≤ S ≤ B ≤ 5000)indicating respectively the number of stacks and the number of blocks that the base must have.
Output
Output a single line with an integer indicating the number of different bases that don’t accumulate water which Miguel can construct. Because this number can be very large, output the remainder of dividing it by 1e9 + 7.
样例输入1
3 6
样例输出1
8
样例输入2
3 7
样例输出2
12
题意
你现在有 m m m 个方块,要搭建一个以 s s s 为底的一个模型,这个模型是不能储水的结构,即不存在一列,其左边和右边的俩均比它高,问你有多少种搭建的方法。
题解
d
p
[
i
]
[
j
]
dp[i][j]
dp[i][j]表示以
i
i
i个为底,上面有
j
j
j个的方案数,于是:
d
p
[
i
]
[
j
]
=
1
∗
d
p
[
i
]
[
j
−
i
]
+
2
∗
d
p
[
i
−
1
]
[
j
−
i
+
1
]
+
3
∗
d
p
[
i
−
2
]
[
j
−
i
+
2
]
.
.
.
dp[i][j] = 1 ∗ dp[i][j − i] + 2 ∗ dp[i − 1][j − i + 1] + 3 ∗ dp[i − 2][j − i + 2]...
dp[i][j]=1∗dp[i][j−i]+2∗dp[i−1][j−i+1]+3∗dp[i−2][j−i+2]...
d
p
[
i
]
[
j
]
=
∑
k
=
j
s
d
p
[
i
−
j
]
[
k
]
∗
(
k
−
j
+
1
)
dp[i][j]=\sum_{k=j}^{s}{dp[i-j][k]*(k-j+1)}
dp[i][j]=k=j∑sdp[i−j][k]∗(k−j+1)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5005;
const int mod = (int) 1e9 + 7;
int n, m;
// sum maxn个方块的方案数,psum sum前缀和
ll sum[maxn * 2], psum[maxn * 2], dp[maxn][maxn];
int main() {
scanf("%d%d", &n, &m);
m -= n;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= m; j++) {
psum[j] = (psum[j] + sum[j]) % mod;
if (j == 0) {
dp[i][j] = 1;
} else {
dp[i][j] = (dp[i][j] + psum[j]) % mod;
}
sum[i + j] = (sum[i + j] + dp[i][j]) % mod;
}
}
printf("%lld\n", dp[n][m]);
return 0;
}