---
id: 814
lang: en
translation_of: index.md
source_sha256: 7139735336465f6ac3466adbdc32ecc5ba297e63f87aea5a4a9cde899d0a7daf
title: "POJ1037: A decorative fence — Solution"
description: "A counting DP over plank ranks with a low/high flag, then the m-th fence is rebuilt plank by plank with the trial-filling method."
tags: ["OI","DP","Counting","Counting DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "会经常简单这样的方法 reads like a typo (简单 for 使用, “use”); rendered as “this method is often used”."
  - "试填法 rendered as “the trial-filling method”."
---

## Main Idea

This problem is a really good one among counting DPs.

Let me first briefly describe the “trial-filling method”. Many of the <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">gods</span> who have done digit DP are probably fairly familiar with this kind of trick: trying by subtracting over and over, and tallying up the answer. That is the trial-filling method, and this method is often used, especially in some digit DPs written in recurrence form.

So let's start analyzing this problem. First, let $F[i][j][0/1]$ be the case where, with $i$ planks, the last placed plank has rank $j$ and sits in the low (0) or high (1) position. We can write the recurrence:

$$
\begin{gathered} F[i][j][0] = \sum_{h=j}^{i-1} F[i-1][h][1] \\ F[i][j][1] = \sum_{h = 1}^{j-1} F[i-1][h][0] \end{gathered}
$$

After that, we can try to find the rank of the first plank. Let $last$ be the rank of the previous plank, and $k$ be whether the previous plank is in the high position (0/1). By enumerating the heights and trial-filling:

- When the number of arrangements enumerated is less than the current remaining count, subtract it from the remainder and keep enumerating.
- When the number of arrangements is greater than the remaining count, record the state and break out.

For the subsequent planks $[2,n]$, the reasoning is the same. Note that our enumeration order goes from $n$ to $1$, to guarantee completeness.

## Code

```cpp
// POJ1037.cpp
#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
ll n, m, T, f[25][25][2];
bool vis[25];
void initialize()
{
    f[1][1][0] = f[1][1][1] = 1;
    for (int i = 1; i <= 20; i++)
        for (int j = 1; j <= i; j++)
        {
            for (int k = j; k <= i - 1; k++)
                f[i][j][0] += f[i - 1][k][1];
            for (int k = 1; k <= j - 1; k++)
                f[i][j][1] += f[i - 1][k][0];
        }
}
int main()
{
    scanf("%d", &T);
    initialize();
    while (T--)
    {
        memset(vis, 0, sizeof(vis));
        scanf("%lld%lld", &n, &m);
        // to determine the first condition;
        ll last, k;
        for (int j = 1; j <= n; j++)
        {
            if (f[n][j][1] >= m)
            {
                last = j, k = 1;
                break;
            }
            else
                m -= f[n][j][1];
            if (f[n][j][0] >= m)
            {
                last = j, k = 0;
                break;
            }
            else
                m -= f[n][j][0];
        }
        vis[last] = true;
        printf("%lld", last);
        for (int i = 2; i <= n; i++)
        {
            k ^= 1;
            int j = 0;
            for (int len = 1; len <= n; len++)
            {
                if (vis[len])
                    continue;
                j++;
                if ((k == 1 && len > last) || (k == 0 && len < last))
                    if (f[n - i + 1][j][k] >= m)
                    {
                        last = len;
                        break;
                    }
                    else
                        m -= f[n - i + 1][j][k];
            }
            printf(" %lld", last);
            vis[last] = true;
        }
        puts("");
    }
    return 0;
}
```
