---
id: 1892
lang: en
translation_of: index.md
source_sha256: 7b4b80c0b8d1fdbc240b320f5f4174416de1b615cde62fa471e902c285d1df8c
title: "AtCoder Grand Contest 001E - BBQ Hard — Solution"
description: "Rewrite the pairwise binomial sum as a full double sum minus the diagonal, then count lattice paths with a DP on the small plane."
tags: ["OI","Godlike","Counting","Transformation"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Main Idea

This is also a rather clever transformation idea.

Let's write out the expression:

$$
\sum_{i = 1}^n \sum_{j = i + 1}^n {a_i + b_i + a_j + b_j \choose a_i + b_i}
$$

Neither computing it directly nor counting by contribution works, so we consider transforming it into this:

$$
\frac{ \sum_{i = 1}^n \sum_{j = 1}^n {a_i + b_i + a_j + b_j \choose a_i + b_i} - \sum_{i = 1}^n {2(a_i + b_i) \choose a_i + b_i} }{2}
$$

For the first part, we can place the points $(-a_i, -b_i)$ on the plane and then just run a path-counting DP (because the plane is fairly small). The first half is the total number of paths from all the points in the third quadrant to $(x_i, y_i)$, and the second half can just be computed by brute force.

<!--more-->

## Code

```cpp
// AGC001E.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 5e3 + 200, DOM = 2500, mod = 1e9 + 7;

int mp[MAX_N][MAX_N], n, xi[MAX_N * 100], yi[MAX_N * 100], fac[MAX_N * MAX_N];

int quick_pow(int bas, int tim)
{
    int ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ret;
}

int main()
{
    scanf("%d", &n);
    for (int i = fac[0] = 1; i < MAX_N * MAX_N; i++)
        fac[i] = 1LL * fac[i - 1] * i % mod;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d", &xi[i], &yi[i]);
        mp[DOM - xi[i]][DOM - yi[i]]++;
    }
    for (int i = 1; i <= (DOM << 1); i++)
        for (int j = 1; j <= (DOM << 1); j++)
            mp[i][j] = (1LL * mp[i][j] + mp[i - 1][j] + mp[i][j - 1]) % mod;
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int pans = mp[xi[i] + DOM][yi[i] + DOM];
        // get oneself outta here;
        int myself = 1LL * fac[(xi[i] << 1) + (yi[i] << 1)] * quick_pow(fac[xi[i] << 1], mod - 2) % mod * quick_pow(fac[yi[i] << 1], mod - 2) % mod;
        ans = (1LL * ans + (pans + mod - myself) % mod) % mod;
    }
    printf("%lld\n", 1LL * ans * quick_pow(2, mod - 2) % mod);
    return 0;
}
```
