【数学水题】【TOJ4113】【 Determine X】

380人阅读 评论(0)

yuebai has a long sequence of integers A1,A2,,AN.
He also has such a function:

F(x)=i=1N(Aix+Aimodx)
x is a positive integer, which determined by yuebai.
Now he wants to know the minimum value of the function.

SB了

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
const int maxn=1000000+5;
int N;
int A[maxn],a[maxn];
int MAX;
long long sum=0;
void input()
{
memset(A,0,sizeof(A));
memset(a,0,sizeof(a));
cin>>N;
MAX=-1;
sum=0;
for(int i=1;i<=N;i++)
{
scanf("%d",&A[i]);
a[A[i]]++;
if(A[i]>MAX) MAX=A[i];
sum+=A[i];
}
for(int i=1;i<=MAX;i++)
{
a[i]=a[i]+a[i-1];
}
}
void solve()
{
long long ans=1LL<<30;
long long tt=0,g;
for(int i=1;i<=MAX;i++)
{
int x=i;tt=0;
for(int t=2*x;t-1<=MAX;t+=x)
{
int p=t-1;
long long k=(long long)(a[p]-a[p-x])*(long long)(p/x);
tt=tt+k;
g=p;
}
tt=tt+(long long)(a[MAX]-a[g])*(long long)(MAX/x);
tt=tt+sum-tt*(long long)x;
if(tt<ans) ans=tt;
}
cout<<ans<<endl;
}
int main()
{
//  freopen("a.in","r",stdin);
int T;
cin>>T;
while(T--)
{
input();
solve();
}
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：218876次
• 积分：5085
• 等级：
• 排名：第5448名
• 原创：298篇
• 转载：5篇
• 译文：0篇
• 评论：45条
评论排行
最新评论
css