题解:P13556 【MX-X15-T3】画圈圈

· · 题解

P13556 题解

前言

本题解使用了较多的新渲染特性,若页面卡顿请移步这里。

题意

::::info[题目大意] 如果一个 n\times m 的网格,满足 i+j\nmid k 的所有格子形成了 \bm{\geq l} 个连通块,那么 n+m 的最小值是多少? ::::

做法

将 k=2 和 k\neq 2 分开讨论。

当 k\neq 2 时

::::warning[注意]{open} 以下的例子(不是结论)均以样例 k=3,l=5 为前提条件。 ::::

先分析样例:

::::info[样例分析] 题目给的解释是这样的(Latex 制作,抽象难懂勿喷):

\square\blacksquare\square\square\blacksquare\square\square\blacksquare \\ \blacksquare\square\square\blacksquare\square\square\blacksquare\square \\ \square\square\blacksquare\square\square\blacksquare\square\square \\ \square\blacksquare\square\square\blacksquare\square\square\blacksquare \\ \blacksquare\square\square\blacksquare\square\square\blacksquare\square \\

但实际上还有一个解是这样的,但它的 n+m 值一样:

\square\blacksquare\square\square\blacksquare\square\square\blacksquare\square \\ \blacksquare\square\square\blacksquare\square\square\blacksquare\square\square \\ \square\square\blacksquare\square\square\blacksquare\square\square\blacksquare \\ \square\blacksquare\square\square\blacksquare\square\square\blacksquare\square \\

当然还可以放在一行考虑,它的 n+m 值仍然一样:

\square\blacksquare\square\square\blacksquare\square\square\blacksquare\square\square\blacksquare\square

::::

我们发现(当然也可以证明),当 k\neq 2 时其实都可以放在一行考虑。

根据我们小学二年级就学过的植树问题可得,这里有 l-1 个这样的黑格,k-1 个白格和黑格形成长度为 k 的周期,最左边的周期少了 1 个白格,但是右边又多出来 1 个白格作为第 l 个正好抵消,长(m)即为 (l-1)k,宽(n)明显为 1,所以答案即为 (l-1)k+1。

注意当 l=1 时明显 n=m=1 时最优,那么答案即为 m+n=1+1=2,需要特判。

当 k=2 时

::::warning[注意]{open} 以下的例子(不是结论)均以样例 k=2,l=4 为前提条件。 ::::

先分析样例:

::::info[样例分析] 我们惊奇地发现这回无法在一行考虑了,否则样例里的答案就会变成 9 而不是 6。

看来我们必须找到一种新办法。

我们很快想到,答案必然可以是一个正方形或是长宽相差为 1 的长方形。

::::error[暴力枚举] 结合 k\neq 2 的情况可得 68pts.

我首先想到的是这种办法。

考虑从 n=1,m=1 开始轮流加 n 和 m,每次答案加 1,1,1,1,2,2,2,2,3,3,3,3\dots,所以我们就得到了第一版代码:

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

void solve() {
    ll k, l;
    cin >> k >> l;
    if (k == 2) {
        ll ans = 2, sum = 0;
        for (int i = 1; ; i++)
            for (int j = 0; j < 4; j++) {
                ans++;
                sum += i;
                if (sum >= l) {
                    printf("%lld\n", ans);
                    return;
                }
            }
    } else
        printf("%lld\n", l == 1 ? 2 : (l - 1) * k + 1);
}


int main() {
    cin.tie(0) -> sync_with_stdio(0);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

::::

::::success[正解] 先只考虑正方形的情况,设边长为 a。

\lfloor \frac{a^2}{2}\rfloor \ge l\\ \frac{a^2}{2} \ge l\\ a^2\ge 2l\\ a\ge \sqrt{2l}\\
可以向上取整 \sqrt{2l} 保证 a^2\ge 2l,然后枚举将边长逐渐 -1,最后看能不能变成长宽相差为 1 的长方形。

AC Code

#include <bits/stdc++.h>
#define squ(a) ((a) * (a))
using namespace std;
typedef long long ll;

void solve() {
    ll k, l;
    cin >> k >> l;
    if (k == 2) {
        ll len = ceil(sqrt(2.0 * l)), ans;
        while (squ(len - 1) >= 2 * l)
            len--;
        ans = 2 * len;
        if (len * (len - 1) >= 2 * l)
            ans -= 1;
        printf("%lld\n", ans);
    } else
        printf("%lld\n", l == 1 ? 2 : (l - 1) * k + 1);
}


int main() {
    cin.tie(0) -> sync_with_stdio(0);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

闲话

本题的最快解法可以做到严格 O(T),代码放在这里,我没有看懂为什么这个解法是对的,有兴趣的同学自行研究,这里就不多赘述了。