首页 > 试题广场 >

小红的gcd

[编程题]小红的gcd
  • 热度指数:1211 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小红有一个长度为 n 的数组,她希望数组元素之和越少越好。
她可以进行任意次操作,每次选择数组中的两个元素 a_ia_j ,令 a_i=a_j=\gcd(a_i,a_j)
所有操作结束后,请你输出最小的数组元素之和。

输入描述:
第一行有一个整数 n\ (\ 1 \leq n \leq 10^5\ )
第二行有 n 个整数 a_i\ (\ 1 \leq a_i \leq 10^9\ )


输出描述:
输出一个整数,代表最小的数组元素之和 。
示例1

输入

5
2 4 6 8 10

输出

10
头像 Turgen
发表于 2026-01-30 00:52:46
观察样例大胆猜测:答案是 因为任意两个数的gcd一定有一个因子是总体的gcd,也就是这个数一定是总体的倍数,连续gcd后,最终的数必然都会变成全体的最大公约数,不可能比这个还小,因为gcd最小就能让一个数减少到全体的公约数,不会使得一个数比这个全体的公约数还要小,因此我们简单证明了这个答案是最小的, 展开全文
头像 Silencer76
发表于 2026-01-30 15:00:38
注意到 最少执行 遍题目所给操作,就可以让数组元素最小,和也最小。 import math n=int(input()) a=list(map(int,input().split())) for i in range(1,n): a[i]=math.gcd(a[i-1],a[i]) p 展开全文
头像 Anon不是奶龙
发表于 2026-01-30 11:31:54
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; lon 展开全文
头像 小男娘
发表于 2026-01-30 13:33:14
两行用分号可以一行 from math import gcd print(int(input()) * gcd(*map(int, input().split())))
头像 Eternal_ATRI
发表于 2026-01-30 14:16:48
# 曲线行驶 # 车压道路边缘线,扣100分,考试结束,成绩不合格。请不要下车,等待补考 from functools import reduce from math import gcd n=int( input( ) ) print( n*reduce( 展开全文
头像 刘森煜
发表于 2026-01-30 01:24:50
#include <iostream> #include <vector> using namespace std; long long gcd(long long a, long long b){ while(b!=0){ long long tem 展开全文
头像 bing糖雪狸
发表于 2026-01-30 12:33:59
#include<bits/stdc++.h> #define il inline #define endl '\n' using namespace std; #define pb push_back #define fastio \ ios::sync_with_stdio(fal 展开全文
头像 quchen666
发表于 2026-01-30 12:40:50
#include <bits/stdc++.h> using namespace std; const int N=3e5+10; const int mod = 998244353; typedef long long ll; typedef unsigned long long ul 展开全文
头像 ccl_aurora
发表于 2026-01-30 15:21:13
#include <iostream> #include<vector> #define ll long long using namespace std; ll gcd(ll a,ll b){ ll t; while(a%b!=0){ t= 展开全文
头像 冰原毛豆企鹅
发表于 2026-01-30 16:10:47
#include <stdio.h> long long gcd(long long a,long long b){ while(b!=0){ long long temp=b; b=a%b; a=temp; } 展开全文