Advertisement
pb_jiang

lc 1862

Jul 16th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. class Solution
  2. {
  3.     using ll = long long;
  4.     static const int MOD = 1e9 + 7;
  5.  
  6.     ll get_block_cnt(int i, const vector<int> &v)
  7.     {
  8.         // ll start = v[i];
  9.         ll ret = 0;
  10.         ll n = v.back();
  11.  
  12.         for (ll l = v[i]; l <= n; l++) {
  13.             ll d = n / l, r = n / d;
  14.             int lb = lower_bound(v.begin(), v.end(), l) - v.begin();
  15.             int ub = upper_bound(v.begin(), v.end(), r) - v.begin() - 1;
  16.             if (i == 0) {
  17.                 printf("lb:%d ub:%d d:%lld l:%lld, r:%lld\n", lb, ub, d, l, r);
  18.             }
  19.             ret += (ub - lb + 1) * d;
  20.             l = r;
  21.         }
  22.         printf("i:%d ret:%lld\n", i, ret);
  23.         return ret;
  24.     }
  25.  
  26.   public:
  27.     int sumOfFlooredPairs(vector<int> &ns)
  28.     {
  29.         int n = ns.size();
  30.         sort(ns.begin(), ns.end());
  31.         ll ret = 0;
  32.         for (int i = 0; i < n; ++i) {
  33.             ret += get_block_cnt(i, ns);
  34.         }
  35.         return ret;
  36.     }
  37. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement