题解:P15933 [TOPC 2021] Eatcoin
I_am_kunzi · · 题解
P15933 题解
本题首杀。
省流:高精度,妙妙推式子。
题目思路
看到
第一问让我们求最小的初始 Eatcoin 个数。我们可以模拟整个过程,并假设 Eatcoin 可以为负数个。此时我们要找的答案就是在每天消耗完
当模拟到 Eatcoin 数量不少于
直觉告诉我们模拟次数不会太多,因为每天 Eatcoin 的产出量
接下来是第二问。即使
Eatcoin 初始值已经在第一问计算过了,假设当前二分到第
初始我们已经知道
要解决这个问题,我们可以使用裂项求和,以推导
用同样的方法,稍微花一点时间,不难推出以下式子。
然后通项公式的问题就解决了,只剩下高精度了。
从这里往下是高精度的部分。
本题建议不要写压位高精。第一是因为数字位数少,没必要写;第二是因为压位太多可能会在运算过程中爆 long long 而出现不明负数,即使加了很多取模也可能会出现这种情况,调试难度甚至大于不压位高精。
简单来说本题需要高精度的以下操作:
-
比较大小、判断相等、输出;
-
高精加单精、高精加高精;
-
高精减单精(虽然只有在计算
\displaystyle \sum_{i = 1} ^ n i ^ 5 时会用到一次,但复制高精加单精即可); -
高精乘单精、高精乘高精;
-
高精除以单精。
这一部分没什么难的,但由于我不会维护负号,所以需要用一些办法保证减完之后的数是非负数,比如上面求第一问答案的时候,可以通过改变被减数和减数,使负数的 Eatcoin 变成非负数。
还有一点就是运算顺序的问题,比如我定义高精乘单精时是高精在前,单精在后,那么应该写成类似于 i * 2 的形式,而写成 2 * i 则会编译错误。
最后记得及时取模和维护高精度的位数。
题目代码
由于高精度代码比较长,这里用折叠框存一份压行后的代码,不压行的代码可以在这里查看。
::::success[P15933 [TOPC 2021] Eatcoin]
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; }
::::