Advertisement
pb_jiang

lc 1862 tle

Jul 16th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 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.         ll n = v[i];
  12.  
  13.         for (ll l = 1; l <= n; l++) {
  14.             ll d = n / l, r = n / d;
  15.             int lb = lower_bound(v.begin(), v.end(), l) - v.begin();
  16.             int ub = upper_bound(v.begin(), v.end(), r) - v.begin() - 1;
  17.             // printf("lb:%d ub:%d d:%lld l:%lld, r:%lld\n", lb, ub, d, l, r);
  18.             ret += (ub - lb + 1) * d;
  19.             l = r;
  20.         }
  21.         // printf("i:%d ret:%lld\n", i, ret);
  22.         return ret;
  23.     }
  24.  
  25.   public:
  26.     int sumOfFlooredPairs(vector<int> &ns)
  27.     {
  28.         int n = ns.size();
  29.         sort(ns.begin(), ns.end());
  30.         ll ret = 0;
  31.         for (int i = 0; i < n; ++i) {
  32.             ret += get_block_cnt(i, ns);
  33.         }
  34.         return ret;
  35.     }
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement