题解:P2000 拯救世界

· · 题解

前言

前置知识:

  1. 高精度加法,高精度减法,高精度乘高精度(暴力),高精度除以低精度;
  2. 插板法;
  3. 初一数学。

题解

第一部分:问题转化

我们发现可以把 10 个条件两两对应起来,比如,把 kkk 的「金神石的块数必须是 6 的倍数」和「水神石最多用 5 块」放在一起。

那为什么把它们两个放在一起呢?假设金神石和水神石的个数和为 x,那金神石的个数就是 \lfloor\dfrac x6\rfloor\times 6,水神石的个数就是 x\bmod 6。也就是说我们可以通过 x 确定出金神石和水神石的个数,而 x 可以是任意一个非负整数。

那么我们可以按照下面的方式把 10 个条件对应起来:

然后问题就转变成了求有多少组非负整数 x_1,x_2,x_3,x_4,x_5 使得 x_1+x_2+x_3+x_4+x_5=n,通过插板法得知答案就是:

\binom{n+4}{4}=\dfrac{(n+4)(n+3)(n+2)(n+1)}{24}

第二部分:高精度乘法

题解区大部分题解到这里都是直接用 FFT 或 NTT(又或者不用 C++),然而作者是一个蒟蒻并不会这么高级的做法,于是这里给出一个暴力做法。

考虑压位,我们把一个数的 9 位压进一个 int 里,那么 n 就乘上了一个 \dfrac 19,O(n^2) 的乘法就有一个 \dfrac 1{81} 的常数。

然而到这里还不足以通过,因为时限是 0.5 秒,而且还要计算三次乘法,效率实在太低。

思考常规的高精度乘法是怎么做的,先枚举 a_i,再枚举 b_j,然后让 c_{i+j} 这一项加上 a_ib_j,最后处理一下进位。

这里有一个小技巧,高精度平方是可以带有 \dfrac 12 的常数的,因为 i<j 和 i>j 时的贡献是一样的,因此我们 j 只需要枚举到 i-1,i=j 特殊处理一下即可。这个优化会快非常多。

那我们怎么用到平方优化呢?

于是我们想要化一下式子,令 t=n+4,则上面的式子就变成了 \dfrac{t(t-1)(t-2)(t-3)}{24},再令 y=t(t-3)=t^2-3t,不难发现 (t-1)(t-2)=t^2-3t+2=y+2。

我们惊奇的发现答案的分子部分变成了 y(y+2)=y^2+2y,那么求 y 需要用一次平方,求答案需要用一次平方,我们成功的把三次乘法变成了两次平方!

然后我们就获得了一个带有 \dfrac 1{162} 倍常数的程序:

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

typedef long long ll;
typedef unsigned uint;
const int N = 1e5 + 10;
const int B = 1e9;

// 简单的函数都压成了一行,仅重点展示平方函数
struct node {
    ll a[N], n; 
    node(): n(0) { memset(a, 0, sizeof(a)); }
    node operator + (const node &x) const { node res; res.n = max(n, x.n); for (int i = 0; i <= res.n; i++) { res.a[i] += a[i]+x.a[i]; if (res.a[i] >= B) res.a[i] -= B, res.a[i+1]++; if (res.a[res.n+1]) ++res.n; } return res; }
    node operator - (const node &x) const { node res; res.n = n; for (int i = 0; i <= n; i++) { res.a[i] += a[i]-x.a[i]; if (res.a[i] < 0) res.a[i] += B, res.a[i+1]--; } while (!res.a[res.n]) --res.n; return res; }
    node operator * (int x) const { node res = *this; ll t, c = 0; for (int i = 0; i <= res.n; i++) { t = res.a[i] * x + c; c = t/B, res.a[i] = t-t/B*B; if (res.a[res.n+1]) ++res.n; } return res; }
    node operator / (int x) const { node res = *this; for (int i = n; i >= 0; i--) { if (i) res.a[i-1] += res.a[i]%x*B; res.a[i] /= x; } while (!res.a[res.n]) --res.n; return res; }
    node sq() const {
        node res;
        ll x, t;
        for (int i = 0; i <= n; i++) {
            t = a[i], x = t * t;
            res.a[i+i+1] += x/B, res.a[i+i] += x-x/B*B; // 为了防止溢出 long long 我们提前拆成两位,下面同理
            for (int j = 0; j < i; j++) x = t * a[j], res.a[i+j+1] += x/B*2, res.a[i+j] += (x-x/B*B)*2; // i < j 和 i > j 都有贡献,因此要 *2
        }
        for (int i = 0; i <= n+n+5; i++) if (res.a[i] >= B) res.a[i+1] += res.a[i]/B, res.a[i] %= B; // 处理进位
        for (int i = 0; i <= n+n+5; i++) if (res.a[i]) res.n = i;
        return res;
    }
    void read() { string s; cin >> s; int m = s.size(); n = -1; for (int i = m-1; i >= 0; i -= 9) { ++n; for (int j = max(0, i-8); j <= i; j++) a[n] = a[n] * 10 + s[j] - '0'; } }
    void write() { string out; out.reserve((n+1) * 9); out += to_string(a[n]); char buf[9]; for (int i = n-1; i >= 0; --i) { uint v = a[i]; for (int k = 8; k >= 0; --k) { buf[k] = char('0' + v % 10); v /= 10; } out.append(buf, 9); } fwrite(out.data(), 1, out.size(), stdout); }
};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    node n, t4;
    t4.a[0] = 4;
    n.read();
    n = n + t4;
    node y = n.sq()-n*3; 
    node ans = (y.sq()+y*2)/24;
    ans.write();
    return 0;
}