From 5b0b69ef138848ff5ab33a7640bc1c25acb4bb7f Mon Sep 17 00:00:00 2001 From: isaiahliu Date: Fri, 13 Dec 2024 14:49:53 +0800 Subject: [PATCH] 1 --- src/main/kotlin/p32xx/Problem3266.kt | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/kotlin/p32xx/Problem3266.kt diff --git a/src/main/kotlin/p32xx/Problem3266.kt b/src/main/kotlin/p32xx/Problem3266.kt new file mode 100644 index 00000000..bae54941 --- /dev/null +++ b/src/main/kotlin/p32xx/Problem3266.kt @@ -0,0 +1,46 @@ +package p32xx + +import util.expect +import java.util.* + +fun main() { + class Solution { + fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray { + if (multiplier == 1) { + return nums + } + + val m = 1000000007L + var max = 0 + val queue = PriorityQueue(compareBy> { it.first }.thenBy { it.second }); + + for (i in nums.indices) { + max = maxOf(max, nums[i]) + queue.offer(nums[i].toLong() to i) + } + + var tk = k + while (queue.peek().first < max && tk > 0) { + queue.poll().also { (num, index) -> + queue.offer(num * multiplier to index) + } + tk-- + } + + for (numIndex in nums.indices) { + queue.poll().also { (x, y) -> + nums[y] = ((x % m) * multiplier.toBigInteger().modPow((tk / nums.size + (if (numIndex < tk % nums.size) 1 else 0)).toBigInteger(), m.toBigInteger()) + .toLong() % m).toInt() + } + } + + return nums + } + } + + expect { + Solution().getFinalState( + intArrayOf(), 1, 1 + ) + } +}