x=12,;y=1[12,1,1]
综上所述, Joey 可以得到的最多的钱即为
12
+
1
+
1
=
14
12+1+1=14
12+1+1=14 元,所以输出应为
14
×
2022
=
28308
14times 2022 = 28308
14×2022=28308.
题目描述
Joey is low on money. His friend Chandler wants to lend Joey some money, but can’t give him directly, as Joey is too proud of himself to accept it. So, in order to trick him, Chandler asks Joey to play a game.
In this game, Chandler gives Joey an array $ a_1, a_2, dots, a_n $ ( $ n geq 2 $ ) of positive integers ( $ a_i ge 1 $ ).
Joey can perform the following operation on the array any number of times:
- Take two indices $ i $ and $ j $ ( $ 1 le i < j le n) $ .
- Choose two integers $ x $ and $ y $ ( $ x, y ge 1 $ ) such that $ x cdot y = a_i cdot a_j $ .
- Replace $ a_i $ by $ x $ and $ a_j $ by $ y $ .
In the end, Joey will get the money equal to the sum of elements of the final array.
Find the maximum amount of money $ mathrm{ans} $ Joey can get but print $ 2022 cdot mathrm{ans} $ . Why multiplied by $ 2022 $ ? Because we are never gonna see it again!
It is guaranteed that the product of all the elements of the array $ a $ doesn’t exceed $ 10^{12} $ .
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $ t $ ( $ 1 leq t leq 4000 $ ). Description of the test cases follows.
The first line of each test case contains a single integer $ n $ ( $ 2 leq n leq 50 $ ) — the length of the array $ a $ .
The second line contains $ n $ integers $ a_1, a_2, dots, a_n $ ( $ 1 leq a_i leq 10^6 $ ) — the array itself.
It’s guaranteed that the product of all $ a_i $ doesn’t exceed $ 10^{12} $ (i. e. $ a_1 cdot a_2 cdot ldots cdot a_n le 10^{12} $ ).
输出格式
For each test case, print the maximum money Joey can get multiplied by $ 2022 $ .
样例 #1
样例输入 #1
3
3
2 3 2
2
1 3
3
1000000 1000000 1
样例输出 #1
28308
8088
2022000000004044
提示
In the first test case, Joey can do the following:
- He chooses $ i = 1 $ and $ j = 2 $ (so he has $ a[i] cdot a[j] = 6 $ ), chooses $ x = 6 $ and $ y = 1 $ and makes $ a[i] = 6 $ and $ a[j] = 1 $ . $ $KaTeX parse error: Can’t use function ‘$’ in math mode at position 66: …= 2} [6, 1, 2] $̲ $ </li><li> He…$.
solution
采用贪心的思想,计算每一个数字的连乘,并放到第一个位置,其他位置为1,此时可以达到最大
//
// Created by Gowi on 2023/12/2.
//
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n, s = 1;
cin >> n;
for (int i = 0; i < n; ++i) {
long long a;
cin >> a;
s *= a;
}
s = s + n - 1;
cout << 2022 * s << endl;
}
}
原文地址:https://blog.csdn.net/qq_43309286/article/details/134757095
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_27872.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!