【UR #1】缩进优化

SOL:

   我们将其映射为 a[x] 表征 有a[x]个x,我们发现ans=ans- 除数*商,

   我们发现暴力统计商的代价O(nlnn)的,所以就好了

#include<bits/stdc++.h>
#define sight(x) ('0'<=x&&x<='9')
#define LL long long
#define maxl 1000001
#define N 7*maxl
using namespace std;
int x,a[N],s[N],*sum=s+1,n;
inline void read(int &x){
    static char c;
    for (c=getchar();!sight(c);c=getchar());
    for (x=0;sight(c);c=getchar())x=x*10+c-48;
}
void write(LL x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);}
inline void writeln(LL x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); }
inline void writel(LL x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); }
LL ans,Su,SS;
signed main () {
    read(n);
    for (int i=1;i<=n;i++)
     read(x),a[x]++,Su+=x;
    for (int i=1;i<N;i++) 
     sum[i]=sum[i-1]+a[i];
    ans=Su;
    for (int i=2;i<maxl;i++) {
        SS=0;
        for (int j=0;j<=maxl;j+=i)  SS+=1ll*(j/i)*(sum[j+i-1]-sum[j-1]);
        ans=min(ans,Su-SS*i+SS);
    }
    writeln(ans);
}

 

posted @ 2018-04-15 20:22  泪寒之雪  阅读(317)  评论(0编辑  收藏  举报