-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSum of Floored Pairs.cpp
53 lines (37 loc) · 1.37 KB
/
Sum of Floored Pairs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Runtime: 687 ms (Top 46.29%) | Memory: 121.4 MB (Top 57.71%)
class Solution {
private:
int MOD = 1e9 + 7;
public:
int sumOfFlooredPairs(vector<int>& nums) {
// first of all, we record the max value
int max_n = INT_MIN;
for(int n : nums) max_n = max(max_n, n);
// then the occurences for each number in [0, max]
vector<int> occs(max_n + 1, 0);
for(int n : nums) occs[n]++;
// prefix sum algorithm to accumulate the occurences
vector<int> occs_acc(max_n + 1, 0);
for(int i = 1; i < max_n + 1; ++i) {
occs_acc[i] = occs[i] + occs_acc[i - 1];
}
// long long needed to prevent overflows
long long ans = 0;
for(int i = 0; i < max_n + 1; ++i) {
// just handle numbers that occur at least once
if(occs[i] != 0) {
int k = 1;
int k_next;
// for each multiple of i
do {
k_next = k + 1;
// "right and left" multipliers in occs_acc
int r = min(k_next * i - 1, max_n);
int l = k * i - 1;
ans += ((long long) occs_acc[r] - (long long) occs_acc[l]) * (long long) occs[i] * k++;
} while(k_next * i - 1 < max_n);
}
}
return ans % MOD;
}
};