【Aizu2968】Non-trivial Common Divisor
Non-trivial Common Divisor
题目描述
You are given a positive integer sequence of length . You can remove any numbers from the sequence to make the sequence “friendly". A sequence is called friendly if there exists an integer (>1) such that every number in the sequence is a multiple of . Since the empty sequence is friendly, it is guaranteed that you can make the initial sequence friendly.
You noticed that there may be multiple ways to make the sequence friendly. So you decide to maximize the sum of all the numbers in the friendly sequence. Please calculate the maximum sum of the all numbers in the friendly sequence which can be obtained from the initial sequence.
输入格式
The input consists of a single test case formatted as follows.
The first line consists of a single integer (). The -st line consists of an integer ( for ).
输出格式
Print the maximum sum of all the numbers in the friendly sequence which can be obtained from the initial sequence.
样例输入输出
Input | Output |
---|---|
6 1 2 3 4 5 6 |
12 |
3 173 1733 111733 |
111733 |
4 1 1 1 1 |
0 |
10 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 |
9999999990 |
1 999999999 |
999999999 |
10 28851 8842 9535 2311 25337 26467 12720 10561 8892 6435 |
56898 |
题解
题意:有个数,从中选出个数,使这些数有除以外的公约数,求这个数的和最大为多少。
我们考虑到一个数的因数是“对称”的。
那么,假设最大的数为我们只要枚举公约数到。
但是我们会发现这么枚举,如果所有的数都是质数,那么答案的公约数肯定没有被枚举到,那么我们再枚举所有的数本身为公约数就好了。
ps:结尾不换行会PE
上代码:
#include<bits/stdc++.h>
using namespace std;
int n;
int a[1009];
bool p[40009];
int x[40009],lx;
long long ans;
bool cmp(int x,int y){return x<y;}
void pd(int x){
if(x==1) return;
long long sum=0;
for(int j=1;j<=n;j++)
if(a[j]%x==0) sum+=a[j];
ans=max(ans,sum);
}
int main(){
scanf("%d",&n);
for(int j=1;j<=n;j++)
scanf("%d",&a[j]);
sort(a+1,a+n+1,cmp);
for(int j=2;j*j<=a[n];j++){
if(p[j]==0){
x[++lx]=j;
pd(j);
}
for(int i=1;i<=lx;i++){
if(j*x[i]>40000) continue;
p[j*x[i]]=1;
if(j%x[i]==0) break;
}
}
for(int j=1;j<=n;j++)
pd(a[j]);
printf("%lld\n",ans);
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· 在线客服系统 QPS 突破 240/秒,连接数突破 4000,日请求数接近1000万次,.NET 多
· C# 开发工具Visual Studio 介绍
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程