# TOJ 4113 Determine X

A[i] % X = A[i] - (A[i] / X) * X;

A[i] %X + (A[i] / X) = A[i] - (A[i]/X) * (X-1);

The portal:http://acm.tju.edu.cn/toj/showp4113.html

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

int  sum[1000010];
int  a[1000010];
int   n;

void Deal_with(){
int  T;
scanf("%d",&T);
while(T--){
memset(sum,0,sizeof(sum));
scanf("%d",&n);
long long t_sum = 0;
int t_max = 0;
for(int  i=0;i<n;i++){
scanf("%d",a+i);
t_sum += (long long)a[i];
if(a[i] > t_max) t_max = a[i];
sum[a[i]]++;
}
for(int  i=1;i<=1000000;i++){
sum[i] = sum[i] + sum[i-1];
}
long long  ans = t_sum;
long long  temp_ans = 0;
//print f("%d\n",t_sum);
int x,y;
for(int  i=2;i<=t_max;i++){
temp_ans = 0;
for(int  j=2;i*(j-1)-1<=t_max;j++){
x = i*j - 1;
y = i * (j-1) - 1;
if((i*j-1) > t_max)x = t_max; // For avoid Runtime Error!It's necessary to add x and y;
if((i*(j-1)-1) > t_max)y = t_max;
temp_ans += (sum[x] - sum[y]) * (long long)(j-1);
}
//print f("temp_ans : %d ",temp_ans);
temp_ans = t_sum - temp_ans * (long long)( i-1 );
//print f("i : %d temp_ans : %d\n",i,temp_ans);
if(temp_ans < ans) ans = temp_ans;
}
printf("%lld\n",ans);
}
}

int main(void){
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：TOJ 4113 Determine X 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)