跳到正文

题解

题解:P15933 [TOPC 2021] Eatcoin

I_am_kunzi约 13 分钟
原文
文章信息与版本

文章信息

发布
2026年3月26日
收录
2026年3月28日
更新
2026年11月15日
篇幅
约 13 分钟
题目
P15933

版本

  1. v1当前

P15933 题解

本题首杀。

省流:高精度,妙妙推式子。

题目思路

看到 109910 ^ {99} 这么大的量级一定是要写高精度了。但由于高精度部分代码比较长,所以我把这一部分的详细内容放在后面,这里先说一下其他部分的思路。

第一问让我们求最小的初始 Eatcoin 个数。我们可以模拟整个过程,并假设 Eatcoin 可以为负数个。此时我们要找的答案就是在每天消耗完 pp 个 Eatcoin 后的 Eatcoin 最小值。由于每天的 Eatcoin 消耗量不变,而产出量一直在增加,所以取到 Eatcoin 最小值时,净增长量仍然 <0 < 0,但再往后一天净增长量就会 ≥0 \ge 0,往后两天及之后的天数中净增长量 >0 > 0,否则若往后一天的净增长量仍然 <0 < 0,则当天不可能取到 Eatcoin 最小值。

当模拟到 Eatcoin 数量不少于 00 时,我们就可以停止模拟。此时 Eatcoin 最小值必定在这一天之前,根据上一段的推导,这一天的净增长量一定 ≥0 \ge 0,之后的每一天净增长量一定 >0 > 0,所以后续不可能再出现最小值,无需模拟。

直觉告诉我们模拟次数不会太多,因为每天 Eatcoin 的产出量 q×i5q \times i ^ 5 的增长速度很快。当然也可以理性计算。最坏情况下,每天的 Eatcoin 消耗量可以是 1018 10 ^ {18},而 qq 的值最小时可以取 1 1,此时消耗量最大而产出量最少,则 Eatcoin 非负所需要的天数最多。设需要 xx 天,为了方便列式,只考虑第 xx 天的产出量,而实际的天数一定少于 xx 天。此时可以列出 x×1018≤x5 x \times 10 ^ {18} \le x ^ 5,发现最坏情况下,不超过一万天也可以使 Eatcoin 数量非负,所以直接模拟完全可行。

接下来是第二问。即使 qq 取到 1018 10 ^ {18},想要让 Eatcoin 数量达到 1099 10 ^ {99},需要的天数也极多,完全不可能直接模拟。发现 109910 ^ {99} 是一个很大的数字,如果我们的 Eatcoin 数量达到这个量级,每天消耗的 pp 个 Eatcoin 早就可以忽略不计了,也就是此时净增长量一定 >0 > 0,因此可以二分答案。

Eatcoin 初始值已经在第一问计算过了,假设当前二分到第 nn 天,则 Eatcoin 消耗量也可以直接写出,为 n×p n \times p。现在的关键就是如何快速求出 q×∑i=1ni5 \displaystyle q \times \sum_{i = 1} ^ n i ^ 5。接下来介绍一种通用的推导方法。

初始我们已经知道 ∑i=1ni=n(n+1)2 \displaystyle \sum_{i = 1} ^ n i = \frac {n (n + 1)} 2,这是我们推式子的根基。如果我们能有一种通用的办法从 ∑i=1ni\displaystyle \sum_{i = 1} ^ n i 推导出 ∑i=1ni2 \displaystyle \sum_{i = 1} ^ n i ^ 2,那么多来几次就得到了 ∑i=1ni5 \displaystyle \sum_{i = 1} ^ n i ^ 5。

要解决这个问题,我们可以使用裂项求和,以推导 ∑i=1ni2\displaystyle \sum_{i = 1} ^ n i ^ 2 为例。首先写出 i3−(i−1)3=3i2−3i+1 i ^ 3 - (i - 1) ^ 3 = 3 i ^ 2 - 3 i + 1,直接暴力展开就可以。此时式子中已经有了 i2i ^ 2 一项,所以我们求 i=1∼ni = 1 \sim n 等号左右两侧的和。

∑i=1n[i3−(i−1)3]=3∑i=1ni2−3∑i=1ni+nn3=3∑i=1ni2−32×n(n+1)+n∑i=1ni2=n33+n(n+1)2−n3=2n3+3n(n+1)−2n6=2n3+3n(n+1)−2n6=n(n+1)(2n+1)6\begin{aligned} \displaystyle \sum_{i = 1} ^ n [i ^ 3 - (i - 1) ^ 3] & = 3 \sum_{i = 1} ^ n i ^ 2 - 3 \sum_{i = 1} ^ n i + n \\ n ^ 3 & = 3 \sum_{i = 1} ^ n i ^ 2 - \frac 3 2 \times n (n + 1) + n \\ \sum_{i = 1} ^ n i ^ 2 & = \frac {n ^ 3} 3 + \frac {n (n + 1)} 2 - \frac n 3 \\ & = \frac {2 n ^ 3 + 3 n (n + 1) - 2 n} 6 \\ & = \frac {2 n ^ 3 + 3 n (n + 1) - 2 n} 6 \\ & = \frac {n (n + 1) (2 n + 1)} 6 \end{aligned}

用同样的方法,稍微花一点时间,不难推出以下式子。

∑i=1ni3=n2(n+1)24∑i=1ni4=n(n+1)(2n+1)(3n2+3n−1)30∑i=1ni5=n2(n+1)2(2n2+2n−1)12\begin{aligned} \sum_{i = 1} ^ n i ^ 3 & = \frac {n ^ 2 (n + 1) ^ 2} 4 \\ \sum_{i = 1} ^ n i ^ 4 & = \frac {n (n + 1) (2 n + 1) (3 n ^ 2 + 3 n - 1)} {30} \\ \sum_{i = 1} ^ n i ^ 5 & = \frac {n ^ 2 (n + 1) ^ 2 (2 n ^ 2 + 2 n - 1)} {12} \end{aligned}

然后通项公式的问题就解决了,只剩下高精度了。


从这里往下是高精度的部分。

本题建议不要写压位高精。第一是因为数字位数少,没必要写;第二是因为压位太多可能会在运算过程中爆 long long 而出现不明负数,即使加了很多取模也可能会出现这种情况,调试难度甚至大于不压位高精。

简单来说本题需要高精度的以下操作:

  • 比较大小、判断相等、输出;

  • 高精加单精、高精加高精;

  • 高精减单精(虽然只有在计算 ∑i=1ni5\displaystyle \sum_{i = 1} ^ n i ^ 5 时会用到一次,但复制高精加单精即可);

  • 高精乘单精、高精乘高精;

  • 高精除以单精。

这一部分没什么难的,但由于我不会维护负号,所以需要用一些办法保证减完之后的数是非负数,比如上面求第一问答案的时候,可以通过改变被减数和减数,使负数的 Eatcoin 变成非负数。

还有一点就是运算顺序的问题,比如我定义高精乘单精时是高精在前,单精在后,那么应该写成类似于 i * 2 的形式,而写成 2 * i 则会编译错误。

最后记得及时取模和维护高精度的位数。

题目代码

由于高精度代码比较长,这里用折叠框存一份压行后的代码,不压行的代码可以在这里查看。

P15933 [TOPC 2021] Eatcoin
C++
const long long BASE = 10;
struct node {
    long long a[505] , len;
    node() { memset(a , 0 , sizeof(a)); len = 1; }
    void res() {
        for(int i = 500 ; i >= 1 ; i--) {
            if(a[i]) { len = i; return ; } }
        len = 1; }
    void up() {
        res();
        for(int i = 1 ; i <= len ; i++) {
            a[i + 1] += a[i] / BASE; a[i] %= BASE;
            if(a[i] < 0) { a[i] += BASE; a[i + 1]--; } }
        res(); }
    void print() {
        res();
        if(len == 1 && a[1] == 0) { cout << 0; return ; } cout << a[len];
        for(int i = len - 1 ; i >= 1 ; i--) { printf("%lld" , a[i]); } } };
bool operator < (const node &a , const node &b) {
    if(a.len != b.len) { return a.len < b.len; }
    for(int i = a.len ; i >= 1 ; i--) {
        if(a.a[i] != b.a[i]) { return a.a[i] < b.a[i]; } } }
bool operator > (const node &a , const node &b) { return b < a; }
bool operator == (const node &a , const node &b) {
    if(a.len != b.len) { return 0; }
    for(int i = a.len ; i >= 1 ; i--) {
        if(a.a[i] != b.a[i]) { return 0; } }
    return 1; }
bool operator >= (const node &a , const node &b) { return (a > b) || (a == b); }
bool operator <= (const node &a , const node &b) { return (a < b) || (a == b); }
node operator + (const node &a , long long b)
{ node c = a; c.a[1] += b; c.up(); return c; }
node operator - (const node &a , long long b)
{ node c = a; c.a[1] -= b; c.up(); return c; }
node operator + (const node &a , const node &b) {
    node c;
    for(int i = 1 ; i <= max(a.len , b.len) ; i++)
    { c.a[i] = a.a[i] + b.a[i]; }
    c.up(); return c; }
node operator - (const node &a , const node &b) {
    node c;
    for(int i = 1 ; i <= max(a.len , b.len) ; i++)
    { c.a[i] = a.a[i] - b.a[i]; }
    c.up(); return c; }
node operator * (const node &a , long long b) {
    node c = a;
    for(int i = 1 ; i <= a.len ; i++) { c.a[i] *= b; }
    c.up(); return c; }
node operator / (const node &a , long long b) {
    node c; int mod = 0;
	for(int i = a.len ; i >= 1 ; i--)
	{ mod *= BASE; mod += a.a[i]; c.a[i] = mod / b; mod %= b; }
    c.up(); return c; }
node operator * (const node &a , const node &b) {
    node c;
    for(int i = 1 ; i <= a.len ; i++) {
        for(int j = 1 ; j <= b.len ; j++) {
            c.a[i + j - 1] += a.a[i] * b.a[j];
            c.a[i + j] += c.a[i + j - 1] / BASE;
            c.a[i + j - 1] %= BASE; } }
    c.up(); return c; }
long long a , b;
node p , q , ans1 , ans2;
node day(node i) { return q * i * i * i * i * i; }
node S(node i) { return (q * i * i * (i + 1) * (i + 1) * (i * i * 2 + i * 2 - 1)) / 12; }
node calcmin() {
    node now = p , maxx = p , gain , i; i = i + 1;
    for( ; ; i = i + 1) {
        gain = gain + day(i); now = now + p;
        if(gain >= now) { break; }
        maxx = max(maxx , now - gain); }
    return maxx; }
bool check(node mid) {
    node cost = mid * p , gain = S(mid) , e99;
    e99.a[100] = 1; e99.res();
    return gain + ans1 >= e99 + cost; }
signed main() {
    read(a , b);
    int cnt = 0;
    while(a) { p.a[++cnt] = a % BASE; a /= BASE; } p.res();
	cnt = 0;
    while(b) { q.a[++cnt] = b % BASE; b /= BASE; } q.res();
    ans1 = calcmin(); ans1.print(); print('\n');
    node l , r; r.a[30] = 1; r.res();
    while(l <= r) {
        node mid = (l + r) / 2;
        if(check(mid)) {
            r = mid - 1; ans2 = mid; }
        else { l = mid + 1; } }
    ans2.print(); print('\n');
	return 0; }
原文