小红的数字分裂(B组)
链接:https://ac.nowcoder.com/acm/contest/72647/C
题目描述

代码题解
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> a(n);
long long g = 0;
// 读入数组 + 计算全局GCD(非递归GCD逻辑内联)
for (int i = 0; i < n; i++) {
cin >> a[i];
// 内联计算gcd(g, a[i]),替代递归函数
long long x = g, y = a[i];
while (y != 0) {
long long temp = x % y;
x = y;
y = temp;
}
g = x;
}
// 计算总操作次数
long long res = 0;
for (auto num : a) {
res += num / g - 1;
}
cout << res << endl;
return 0;
}
重点是找到最小公约数,4和2的最小公约数是2,请问小红最少需要操作多少次才可以使得数组的所有元素都相等。要让所有的数都相等,就代表这个数能拆解成相同的两个数
3676

被折叠的 条评论
为什么被折叠?



