-
Notifications
You must be signed in to change notification settings - Fork 1
/
no_3266.rs
101 lines (91 loc) · 2.75 KB
/
no_3266.rs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::cmp::Ordering;
use std::collections::BinaryHeap;
const MOD: u128 = 1000000007;
impl Solution {
pub fn get_final_state(nums: Vec<i32>, k: i32, multiplier: i32) -> Vec<i32> {
if multiplier==1 {
return nums;
}
let mut max_heap: BinaryHeap<Point> = BinaryHeap::new();
let mut maxp = 0;
for i in 0..nums.len() {
if nums[i] > nums[maxp] { maxp = i; }
max_heap.push(Point(i, nums[i] as u128));
}
let mut times = k;
while times > 0 {
let mut v = max_heap.pop().unwrap();
let p = v.0;
v.1 = v.1 * multiplier as u128;
max_heap.push(v);
times -= 1;
if p==maxp { break }
}
let remain_opt = times as usize % nums.len();
times = times / nums.len() as i32;
let multiplier_1 = Self::quick_pow(multiplier as i128, times as i128, MOD as i128) as u128;
let multiplier_2 = Self::quick_pow(multiplier as i128, times as i128 + 1, MOD as i128) as u128;
let mut vec = Vec::from(nums);
for i in 0..vec.len() {
let x = max_heap.pop().unwrap();
let m = if i < remain_opt {
multiplier_2
} else {
multiplier_1
};
vec[x.0] = ((x.1 % MOD * m) % MOD) as i32;
}
vec
}
fn quick_pow(a: i128, b: i128, modd: i128) -> i128 {
if a == 0 { return 0; };
if b == 0 { return 1; };
let mut result = 1;
let mut base = a % modd;
let mut x = b;
while x != 0 {
if x & 1 != 0 {
result = (result * base) % modd;
}
base = (base * base) % modd;
x = x >> 1;
}
return result;
}
}
#[derive(Eq, PartialEq, Debug)]
struct Point(usize, u128);
impl PartialOrd for Point {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Point::cmp(self, other))
}
}
impl Ord for Point {
fn cmp(&self, other: &Self) -> Ordering {
self.1
.cmp(&other.1)
.reverse()
.then_with(|| self.0.cmp(&other.0).reverse())
}
}
#[test]
fn test() {
assert_eq!(
dbg!(Solution::get_final_state(vec![2, 1, 3, 5, 6], 5, 2)),
vec![8, 4, 6, 5, 6]
);
assert_eq!(
dbg!(Solution::get_final_state(vec![2, 2, 2], 2, 2)),
vec![4, 4, 2]
);
assert_eq!(
dbg!(Solution::get_final_state(vec![100000, 2000], 2, 1000000)),
vec![999999307, 999999993]
);
assert_eq!(
Solution::get_final_state(
vec![66307295, 441787703, 589039035, 322281864], 900900704, 641725),
vec![664480092, 763599523, 886046925, 47878852]
);
}
struct Solution {}