缩进优化 [WOJ4371] [数学][有关复杂度的一些计算]

本文探讨了一种针对AI算法的优化方法,通过使用差分数组记录个数并乘以贡献的方式,有效地降低了算法的复杂度。这种方法特别适用于处理大规模数据集,能够显著提高算法的运行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

ans=\sum _{i=1}^n[\frac{a_i}{x}]+(a[i]modx)=\sum _{i=1}^n[\frac{a_i}{x}]+(a[i]-[\frac{a_i}{x}]*x)=sum(n)-(x-1)\sum [\frac{a_i}{x}]

我们发现对于一段ai,ai/x是相同的,我们用差分数组记录个数,乘上贡献就可以了

复杂度有一个比较重要的东西 

\sum _{i=1}^n(N/i) = NlogN


#include<bits/stdc++.h>
#define N 1000050
#define LL long long
#define inf 1000000000000000
using namespace std;

int a[N],Max; LL s,ans;
int n,d[N];
int read(){
	int cnt=0,f=1; char ch=0;
	while(!isdigit(ch)){ch=getchar(); if(ch=='-') f=-1;}
	while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
	return cnt * f;
}
int main(){
	n = read();
	for(int i=1;i<=n;i++){
		a[i] = read();
		s += (LL)a[i];
		Max = max(Max, a[i]);
		d[a[i]]++;
	}
	for(int i=3;i<=Max;i++) d[i] += d[i-1];
	for(int x=2;x<=Max;x++){
		LL tmp = 0;
		for(int l=x;l<=Max;l+=x){
			int r = l + x - 1; if(r > Max) r = Max;
			int val = l/x;
			tmp += (LL)(d[r]-d[l-1]) * (LL)val;
		}
		ans = max(ans, tmp * (LL)(x-1));
	} printf("%lld",s-ans); return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FSYo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值