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
| long long helper(long long n) { long long res = 1, mod = 1e9 + 7; for(int i = 1; i <= sqrt(n); i++) { if(n % i) continue; res = (res * i) % mod; if(i * i != n) res = (res * (n/i)) % mod; } return res; }
vector<int> Solution::solve(vector<int> &A, vector<int> &B) { vector<pair<long long,long long>> st; A.push_back(INT_MAX); unordered_map<long long, long long> freq; for(int i = 0; i < A.size(); i++) { int cnt = 1; while(!st.empty() and A[st.back().second] <= A[i]) { auto [c, idx] = st.back(); st.pop_back(); cnt += c; freq[A[idx]] += c * (i - idx); } st.push_back({cnt,i}); } unordered_map<long long, long long> P; for(auto& [n, cnt] : freq) P[helper(n)] += cnt; vector<pair<long long, long long>> S; for(auto& [k,v] : P) S.push_back({k,v}); sort(rbegin(S), rend(S)); vector<long long> psum {0}; for(int i = 0; i < S.size(); i++) { auto [_, cnt] = S[i]; psum.push_back(psum.back() + cnt); } vector<int> res; for(int i = 0; i < B.size(); i++) { int f = B[i] - 1; auto idx = upper_bound(begin(psum), end(psum), f) - begin(psum) - 1; res.push_back(S[idx].first); } return res; }
|