考虑每个数在最大值内的倍数都求出来大概只有max(ai)ln(max(ai))个。
先排个序,然后对于每个数ai,考虑哪些数字可以变成ai*k。
显然就是区间[ai*k,ai*(k+1))内的数,这个二分一下就好了。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int a[200010]; 5 6 int main() 7 { 8 int n; 9 scanf("%d", &n); 10 for (int i = 0; i < n; i++) 11 scanf("%d", a + i); 12 sort(a, a + n); 13 long long ans = 0; 14 int last = -1; 15 for (int i = 0; i < n; i++) 16 { 17 if (a[i] == last) 18 continue; 19 long long temp = 0; 20 for (int j = 0; (j - 1) * a[i] <= a[n - 1]; j++) 21 { 22 int down = j * a[i]; 23 int up = (j + 1) * a[i]; 24 int *pup = lower_bound(a, a + n, up); 25 int *pdown = lower_bound(a, a + n, down); 26 temp += 1LL * j * a[i] * (pup - pdown); 27 } 28 ans = max(ans, temp); 29 last = a[i]; 30 } 31 printf("%I64d", ans); 32 return 0; 33 }
时间: 2024-10-28 21:58:25