---
id: 392
lang: en
translation_of: index.md
source_sha256: d81373c2a83baf992699aad29dc63940155120189759d347f79ecb03cb9c9e44
title: "P2831: Angry Birds — Solution"
description: "A bitmask DP over which pigs are dead, sped up by transitioning only on the first pig still alive in each state."
tags: ["OI","DP","Bitmask DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name 愤怒的小鸟 rendered as “Angry Birds”, the game's English name."
---

## Approach

This problem is a bitmask DP problem (see the constraints). We compress whether each pig is dead or alive into the state, and the whole program counts from $0$. We can derive the following equations: $f[stat|(1<<i)] = min(f[stat|(1<<i)], f[stat] + 1)$ and $f[stat|dotset[i][j]] = min(f[stat|dotset[i][j]],f[stat]+1)$, where dotset is the set of points on the parabola passing through points $i,j$. This algorithm is $O(Tn^2 2^n)$, which is not good enough. We can use a method to turn $n^2$ into $n$. We can precompute, for every state, the index of the first pig still alive, and then we only need to transition on this one pig: since the later pigs will all be handled anyway, we can drop a dimension directly.

## Code

```cpp
// P2831.cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define pr pair<double, double>
using namespace std;
const double eps = 1e-8;
int T, n, dotset[19][19], dp[(1 << 19)], lbit[(1 << 19)], m;
pr prs[20];
void solve(double &x, double &y, double a1, double b1, double c1, double a2, double b2, double c2)
{
    y = (a1 * c2 - a2 * c1) / (a1 * b2 - a2 * b1);
    x = (c1 - b1 * y) / a1;
}
int main()
{
    for (int i = 0; i < (1 << 18); i++)
    {
        int bit = 0;
        for (; bit < 18 && (i & (1 << bit)) ; bit++)
            ;
        lbit[i] = bit;
    }
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        memset(dp, 0x3f, sizeof(dp)), memset(dotset, 0, sizeof(dotset));
        for (int i = 0; i < n; i++)
            scanf("%lf%lf", &prs[i].first, &prs[i].second);
        // process the curves;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                if (fabs(prs[i].first - prs[j].first) < eps)
                    continue;
                double a, b;
                solve(a, b, prs[i].first * prs[i].first, prs[i].first, prs[i].second,
                      prs[j].first * prs[j].first, prs[j].first, prs[j].second);
                if (a > -eps)
                    continue;
                for (int k = 0; k < n; k++)
                    if (fabs(a * prs[k].first * prs[k].first + b * prs[k].first - prs[k].second) < eps)
                        dotset[i][j] |= (1 << k);
            }
        dp[0] = 0;
        for (int stat = 0; stat < (1 << n); stat++)
        {
            int esspt = lbit[stat];
            dp[stat | (1 << esspt)] = min(dp[stat | (1 << esspt)], dp[stat] + 1);
            for (int k = 0; k < n; k++)
                dp[stat | dotset[esspt][k]] = min(dp[stat | dotset[esspt][k]], dp[stat] + 1);
        }
        printf("%d\n", dp[(1 << n) - 1]);
    }
    return 0;
}
```
