-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3266-final-array-state-after-k-multiplication-operations-ii.cpp
More file actions
51 lines (48 loc) · 1.33 KB
/
3266-final-array-state-after-k-multiplication-operations-ii.cpp
File metadata and controls
51 lines (48 loc) · 1.33 KB
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
class Solution {
public:
long long expo(long long a, long long b, int mod) {
long long res = 1;
while (b > 0) {
if (b & 1) {
res *= a;
res %= mod;
}
a *= a;
a %= mod;
b >>= 1;
}
return res;
}
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
if (multiplier == 1) {
return nums;
}
const int MOD = 1e9 + 7;
int n = (int) nums.size();
long long mx = *max_element(nums.begin(), nums.end());
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
for (int i = 0; i < n; i++) {
pq.push({nums[i], i});
}
while (k--) {
auto [mn, idx] = pq.top(); pq.pop();
long long nxt = mn * multiplier;
pq.push({nxt, idx});
if (nxt > mx) {
break;
}
mx = max(mx, nxt);
}
int q = k / n;
int r = k % n;
while (!pq.empty()) {
auto [cur, idx] = pq.top(); pq.pop();
cur %= MOD;
cur *= expo(multiplier, q + (r > 0), MOD);
cur %= MOD;
r--;
nums[idx] = cur;
}
return nums;
}
};