【好题】【题解】[diverta2019-2E] Balanced Piles

Problem

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : \(800\) points

Problem Statement

There are \(N\) squares arranged in a row, numbered \(1\) to \(N\) from left to right. Takahashi will stack building blocks on these squares, on which there are no blocks yet.

He wants to stack blocks on the squares evenly, so he will repeat the following operation until there are \(H\) blocks on every square:

  • Let \(M\) and \(m\) be the maximum and minimum numbers of blocks currently stacked on a square, respectively. Choose a square on which \(m\)blocks are stacked (if there are multiple such squares, choose any one of them), and add a positive number of blocks on that square so that there will be at least \(M\) and at most \(M + D\) blocks on that square.

Tell him how many ways there are to have \(H\) blocks on every square by repeating this operation. Since there can be extremely many ways, print the number modulo \(10^9+7\).

Constraints

  • \(2 \leq N \leq 10^6\)
  • \(1 \leq D \leq H \leq 10^6\)
  • All values in input are integers.

Solution

(官方)题解:定义\((h, x)\)为最高高度为 \(h\),最高块数为 \(x\) 的状态。(假设同一高度的方块有(操作顺)序)

\(N = 4, H = 4, D = 1\) 时,方案数等价于以下图的\((0,4)\to(4,4)\)的路径条数:(用Graphviz生成)

\(N = 4, H = 4, D = 2\) 时,方案数等价于以下图的\((0,4)\to(4,4)\)的路径条数:

然后就做完了……

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int maxn = 1e6+10, MOD = 1e9+7;
int n, h, d;
int a[maxn], sum, b[maxn], s[maxn], ans;

int main() {
	scanf("%d%d%d", &n, &h, &d);
	a[0] = 1;
	for (int i = 1; i <= n; ++i) {
		a[i] = (ll)a[i-1]*i%MOD;
		sum = (ll)(sum+a[i])%MOD;
	}
	for (int i = 1; i <= h; ++i) {
		b[i] = ((s[i-1]-((i-d-1<1)?0:s[i-d-1])+MOD)*(ll)sum+(i<=d))%MOD;
		s[i] = (s[i-1]+b[i])%MOD;
	}
	ans = (ll)b[h]*a[n]%MOD;
	printf("%d\n", ans);
}
posted @ 2021-05-21 11:37  frank3215  阅读(80)  评论(1编辑  收藏  举报