题解 | #小红与gcd和sum#(C++)
小红与gcd和sum
https://www.nowcoder.com/practice/67fe6737ef7447e4ab95bfdbb7f6de6f
这题很简单来着
对于每一个可能的固定住的 值,想要权值最大,我们肯定会把所有因数里包含这个值的
全部选上,这样才可能达到最大值
注意到 值最大1e8,复杂度允许
所以我们之间枚举每个 的因数
用
存下来,并让
加上
即可
最后遍历 就可以知道最后得到的最大权值啦
代码如下:
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << '\n';
// #define int long long
#define ctz __builtin_ctzll // 返回二进制表示中末尾连续0的个数
#define clz __builtin_clzll // 返回二进驻表示中先导0的个数
#define count1 __builtin_popcountll // 返回二进制表示中1的个数
// 上面仨不是ll的时候记得调整
#define lowbit(x) (x & -x)
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 1e6 + 100;
const double EPS = 1e-8;
// const int EPS = 0;
using T = long double;
constexpr T PI = 3.1415926535897932384l;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll dir[4][2] = {{1, 0}, {-1, 0}, {0, -1} , {0, 1}};
ll dirr[8][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
void LiangBaiKai()
{
}
void Aiden()
{
ll n;
cin >> n;
vector<ll> a(n + 1);
for (ll i = 1; i <= n;i++)
cin >> a[i];
map<ll, ll> cnt;
for (ll i = 1; i <= n;i++)
{
for (ll j = 1; j * j <= a[i];j++)
{
if (a[i] % j == 0)
{
cnt[j] += a[i];
if (j != a[i] / j)
cnt[a[i] / j] += a[i];
}
}
}
ll ans = -1;
for (auto [k, v] : cnt)
ans = max(ans, k * v);
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
LiangBaiKai();
int _ = 1;
//cin >> _;
while (_--)
Aiden();
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