思路

这道题一开始是百思不得其解的,还试图去找分解的规律,发现大于4的时候分解为不同的元素往往都是增大的,所以我们的思路是尽量分解,中间还想过不断分解质因数,但是均以失败告终,因为没办法满足分解出的数不同的条件

在看了点赞量前二的题解的时候,才明白这题真正的解法,下面直接是对原题解的搬用

贪心思路:

以2004为例,由于把2004分拆成若干个互不相等的自然数的和的分法只有有限种,因而一定存在一种分
法,使得这些自然数的乘积最大。

若1作因数,则显然乘积不会最大。把2004分拆成若干个互不相等的自然数的和,因数个数越多,乘积越大。为了使因数个数尽可能地多,我们把2004分成2+3..+直到和大于等于2004。

  • 若和比2004大1,则因数个数至少减少1个,为了使乘积最大,应去掉最小的2,并将最后一个数(最大)加上1。
  • 若和比2004大k(k≠1),则去掉等于k的那个数,便可使乘积最大。

1.例如15:s=2+3+4+5+6刚好大于15,s-15=5,所以把5去掉。
2.又例如13:s=2+3+4+5刚好大于13,s-13=1,所以去掉2,并把5加1,即346。

dp思路:

互不相同这个条件可以改为从1,2,⋯,n这n个数中选出一些数,使得它们的和为n。但是,01背包只能处理从a个物品中选出总共b空间的物品,使得它们的价值和最大,可是我们要求乘积呀?

在数学里,对数有一个很好的性质:

lna+lnb=ln(a×b)

假如我们要选出一些数使得它们的乘积最大,就等价于这些数的对数之和最大。

因此题目就很显然了。n个物品,背包体积为n,每个物品体积Ci 为i,价值Wi 为lni……(详情参考源代码)。

有同学有疑惑,为什么背包问题不是求容量C所装的最大价值吗?你怎么保证他能把背包装满?
这里我解释一下,背包一定可以装满,因为最大乘积的序列和就为n。
不知道我有没有有讲清楚,因为背包求出来的是最大价值,如果我们剩下空间K没装,这不就相当于我们之前所说的余数K吗?所以大家认为要不要把K分配出去呢?明显是分配后的结果更大,这也就是n和n+1的分别拆开的数的最大乘积哪个更大,明显是n+1更大,所以空间不存在多余,一定会用完。

说白了就是从2 到 某个数 $k$ 的 01背包,这里可以直接假定 $k = n$ ,遍历每个数选择拿或者不拿,dp数组代表的就是当前的乘积

(客观评价,由于dp要设计到高精度乘法和输出走过的路径,虽然好理解,但是相当不好写且时间复杂度略次。不用高精度的话只能通过3个点)


相信点赞量前3的题解已经讲的相当清楚了,那下面贴上原题解未提供的代码和我个人认为比原题解更直观的代码

难度:中位黄~上位黄

代码

贪心版本

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
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

string jjmul(const string& a,const string& b){
int la = a.size(), lb = b.size();
vector<int> re(la + lb, 0);
for(int i = la - 1; i >= 0 ; i --){
int x = a[i] - '0';
for(int j = lb - 1;j >= 0 ; j --){
int y = b[j] - '0';
int mul = x * y + re[i + j + 1];
re[i + j + 1] = mul % 10;
re[i + j] += mul / 10;
}
}
string ans;
ans.reserve(la + lb);
for(int d : re){
ans.push_back(char(d +'0'));
}
ans.erase(0,ans.find_first_not_of('0'));
return ans;
}

signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
cin >> n;
int sum = 0;
int p = 1;
while(sum < n){
p++;
sum += p;
}
string re = "1";
if(sum == n){
for(int i = 2; i <= p ;i++){
re = jjmul(re,to_string(i));
cout << i << " ";
}
cout << endl;
}
else if((sum - n) == 1){
for(int i = 3 ;i < p ;i++){
re = jjmul(re, to_string(i));
cout << i << " ";
}
re = jjmul(re, to_string(p + 1));
cout << p + 1 << endl;
}
else{
for (int i = 2; i <= p; i++) {
if(i == (sum - n)) continue;
re = jjmul(re, to_string(i));
cout << i << " ";
}
cout << endl;
}
cout << re << endl;
return 0;
}

时间复杂度计算:
因为:
$$
1+2+\cdots+p\geq n\Rightarrow\frac{p(p+1)}2\geq n
$$
所以: $p\approx O(\sqrt{n})$

因为re = 2 × 3 × 4 × ... × p = p!,所以最后的re的长度相当于: $\log_{10}(1)+\log_{10}(2)+\cdots+\log_{10}(p) = \log_{10}(p!)\approx p\log_{10}(p)$

(可以证明 $p! \approx p^{p}$ )

所以,所有乘法的时间复杂度一共是:
$$
O(1\cdot1+2\cdot2+3\cdot3+\cdots+p\cdot p)=\sum_{i=1}^pi^2=O(p^3) \approx O(n^{1.5})
$$

上面的是最坏时间复杂度,由于前面的大多数乘法位数都很小,所以平均时间复杂度大概是: $O(p^2 \log p)$


dp版本

注意,注意,下面这两份代码 都是不能 通过hack的数据的,因为拿不到具体的数据,试了很多次还是通不过

时间复杂度:
瓶颈不在乘法,主要是在dp那里,外层 $O(p)$,内层 $O(n)$,共 $O(n^{1.5})$

乘法版本

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

int n;
int sum = 0, p = 1;
vector<vector<string>> dp;
vector<vector<bool>> choose;

void printpath(){
vector<int> path;
int i = p ,j = n;
while(i > 0 && j > 0){
if(choose[i][j]){
path.push_back(i);
j -= i;
i -= 1;
}
else{
i -= 1;
}
}
reverse(path.begin(),path.end());
for(int k : path) cout << k << " ";
cout << endl;
}

string jjmul(const string &a, const string &b) {
if (a == "0" || b == "0")
return "0";
int la = a.size(), lb = b.size();
vector<int> re(la + lb, 0);
for (int i = la - 1; i >= 0; i--) {
int x = a[i] - '0';
for (int j = lb - 1; j >= 0; j--) {
int y = b[j] - '0';
int mul = x * y + re[i + j + 1];
re[i + j + 1] = mul % 10;
re[i + j] += mul / 10;
}
}
string ans;
ans.reserve(la + lb);
for (int d : re) {
ans.push_back(char(d + '0'));
}
ans.erase(0, ans.find_first_not_of('0'));
if (ans.empty())
ans = "0";
return ans;
}

bool cmpbig(const string &a, const string &b){
int la = a.size(),lb = b.size();
if(la > lb) return true;
else if(la < lb) return false;
else{
return a > b;
}
}

signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
while (sum < n) {
p++;
sum += p;
}
dp.assign(p + 1, vector<string>(n + 1 ,"0"));
choose.assign(p + 1, vector<bool>(n + 1, false));
dp[0][0] = "1";
for(int i = 1; i <= p; i ++){
for(int j = 0; j <= n; j++){
dp[i][j] = dp[i - 1][j];
choose[i][j] = false;
if (i >= 2 && j >= i && dp[i - 1][j - i] != "0") {
string t = jjmul(dp[i - 1][j - i], to_string(i));
if (cmpbig(t, dp[i][j])){
dp[i][j] = t;
choose[i][j] = true;
}
}
}
}
printpath();
cout << dp[p][n] << endl;
return 0;
}

取对数版本

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

int n;
int sum = 0, p = 1;
vector<vector<long double>> dp;
vector<vector<bool>> choose;

vector<int> getpath() {
vector<int> path;
int i = p, j = n;
while (i > 0 && j > 0) {
if (choose[i][j]) {
path.push_back(i);
j -= i;
i -= 1;
} else {
i -= 1;
}
}
reverse(path.begin(), path.end());
return path;
}

string jjmul(const string &a, const string &b) {
if (a == "0" || b == "0")
return "0";
int la = a.size(), lb = b.size();
vector<int> re(la + lb, 0);
for (int i = la - 1; i >= 0; i--) {
int x = a[i] - '0';
for (int j = lb - 1; j >= 0; j--) {
int y = b[j] - '0';
int mul = x * y + re[i + j + 1];
re[i + j + 1] = mul % 10;
re[i + j] += mul / 10;
}
}
string ans;
ans.reserve(la + lb);
for (int d : re) {
ans.push_back(char(d + '0'));
}
ans.erase(0, ans.find_first_not_of('0'));
if (ans.empty())
ans = "0";
return ans;
}

signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
while (sum < n) {
p++;
sum += p;
}
const long double NEG = -1e100L;
dp.assign(p + 1, vector<long double>(n + 1, NEG));
choose.assign(p + 1, vector<bool>(n + 1, false));
dp[0][0] = 0.0L;
for (int i = 1; i <= p; i++) {
for (int j = 0; j <= n; j++) {
dp[i][j] = dp[i - 1][j];
choose[i][j] = false;
if (i >= 2 && j >= i && dp[i - 1][j - i] != NEG) {
long double t = dp[i - 1][j - i] + logl((long double)i); //logl是取自然对数
if (t >
dp[i][j] + 1e-15L) { // 浮点容忍
// 累加对数时,每一次 logl()、加法、本身的二进制表示都会引入一点点舍入误差
dp[i][j] = t;
choose[i][j] = true;
}
}
}
}
vector<int> path = getpath();
for (size_t k = 0; k < path.size(); ++k) {
if (k)
cout << " ";
cout << path[k];
}
cout << endl;
string ans = "1";
for(int i : path){
ans = jjmul(ans ,to_string(i));
}
cout << ans << endl;
return 0;
}