本文介绍: Joey 很穷,因此他的朋友 Chandler 想要借给他一些钱。但是 Joey 的自尊心很强,为了不让他的自尊心受挫又能给他钱,Chandler 打算和他玩一个游戏采用贪心的思想,计算一个数字的连乘,并放到第一个位置,其他位置为1,此时可以达到最大。共一行一个整数表示 Joey 最多可以得到的钱乘以 2022 后的值。综上所述, Joey 可以得到的最多的钱即为。即 Joey 所得的钱。,即 Joey 最多可以得到的钱,并输出。因为我们再也见不到它了!最后, Joey 将得到的钱就是。

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:

  1. Take two indices $ i $ and $ j $ ( $ 1 le i < j le n) $ .
  2. Choose two integers $ x $ and $ y $ ( $ x, y ge 1 $ ) such that $ x cdot y = a_i cdot a_j $ .
  3. 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:

  1. 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进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注