题目大意
给定一个正整数 n,将其划分为若干正整数的和,求这些数的积的最大值,输出答案位数和其前 100 位数字。
1⩽n⩽31,000
1⩽ans⩽5,000 (答案位数)
题目链接
【SCOI 2006】整数划分 - Luogu 4157
题解
若不考虑「划分为整数」的条件,则答案为 (xn)x,对 y=(xn)x 两侧同时取 ln,对原单调性不影响,有 lny=x(lnn−lnx),求导有:
(lny)′=lnn−lnx−1
令右侧等于 0,得 x=en,即分为若干个 e,由于 e 四舍五入为 3,则答案为划分为尽量多的 3 和几个 2。
要高精度(用 Menci 的代码更新了一下版子)。
另外,那个式子是隔壁【HNOI 2012】矿场搭建的理论最大答案。。。(应该)
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <cstdio> #include <vector> #include <algorithm> struct BigInt { std::vector<char> v; BigInt(int x = 0) { *this = x; } BigInt &operator=(int x) { v.clear(); do v.push_back(x % 10); while (x /= 10); return *this; } BigInt &operator=(const BigInt &x) { v.resize(x.v.size()); for (int i = 0; i < v.size(); i++) v[i] = x.v[i]; return *this; } }; BigInt operator*(const BigInt &a, const BigInt &b) { BigInt res; res.v.resize(a.v.size() + b.v.size()); for (int i = 0; i < a.v.size(); i++) for (int j = 0; j < b.v.size(); j++) { res.v[i + j] += a.v[i] * b.v[j]; res.v[i + j + 1] += res.v[i + j] / 10; res.v[i + j] %= 10; } int size = res.v.size(); while (size > 1 && res.v[size - 1] == 0) size--; res.v.resize(size); return res; } BigInt &operator*=(BigInt &a, const BigInt &b) { return a = a * b; } BigInt pow(int a, int n) { BigInt res(1), x(a); for (; n; n >>= 1, x *= x) if (n & 1) res *= x; return res; } int main() { int n; scanf("%d", &n); BigInt ans; if (n % 3 == 0) ans = pow(3, n / 3); else if (n % 3 == 1) ans = pow(3, (n - 4) / 3) * 4; else ans = pow(3, (n - 2) / 3) * 2; printf("%d\n", (int) ans.v.size()); for (int i = ans.v.size() - 1; i >= std::max(0, (int) ans.v.size() - 100); i--) putchar(ans.v[i] + '0'); puts(""); return 0; }
|