|
1 | 1 | package Solution
|
2 | 2 |
|
3 |
| -func Solution(x bool) bool { |
| 3 | +import ( |
| 4 | + "container/heap" |
| 5 | +) |
| 6 | + |
| 7 | +const mod2818 = 1_000_000_007 |
| 8 | + |
| 9 | +type Pair2818 struct { |
| 10 | + value int |
| 11 | + index int |
| 12 | +} |
| 13 | + |
| 14 | +// PriorityQueue implements a priority queue for pairs based on value |
| 15 | +type PriorityQueue []Pair2818 |
| 16 | + |
| 17 | +func (pq PriorityQueue) Len() int { |
| 18 | + return len(pq) |
| 19 | +} |
| 20 | + |
| 21 | +func (pq PriorityQueue) Less(i, j int) bool { |
| 22 | + return pq[i].value > pq[j].value // Max-heap based on value |
| 23 | +} |
| 24 | + |
| 25 | +func (pq PriorityQueue) Swap(i, j int) { |
| 26 | + pq[i], pq[j] = pq[j], pq[i] |
| 27 | +} |
| 28 | + |
| 29 | +func (pq *PriorityQueue) Push(x interface{}) { |
| 30 | + *pq = append(*pq, x.(Pair2818)) |
| 31 | +} |
| 32 | + |
| 33 | +func (pq *PriorityQueue) Pop() interface{} { |
| 34 | + old := *pq |
| 35 | + n := len(old) |
| 36 | + x := old[n-1] |
| 37 | + *pq = old[0 : n-1] |
4 | 38 | return x
|
5 | 39 | }
|
| 40 | + |
| 41 | +// Helper function to compute the number of distinct prime factors |
| 42 | +func distinctPrimeFactors(x int) int { |
| 43 | + count := 0 |
| 44 | + for i := 2; i*i <= x; i++ { |
| 45 | + if x%i == 0 { |
| 46 | + count++ |
| 47 | + for x%i == 0 { |
| 48 | + x /= i |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + if x > 1 { |
| 53 | + count++ // If x is a prime number larger than sqrt(original x) |
| 54 | + } |
| 55 | + return count |
| 56 | +} |
| 57 | + |
| 58 | +func Solution(nums []int, k int) int { |
| 59 | + n := len(nums) |
| 60 | + primeScores := make([]int, n) |
| 61 | + |
| 62 | + for i := 0; i < n; i++ { |
| 63 | + primeScores[i] = distinctPrimeFactors(nums[i]) |
| 64 | + } |
| 65 | + |
| 66 | + nextDominant := make([]int, n) |
| 67 | + prevDominant := make([]int, n) |
| 68 | + for i := 0; i < n; i++ { |
| 69 | + nextDominant[i] = n |
| 70 | + prevDominant[i] = -1 |
| 71 | + } |
| 72 | + |
| 73 | + stack := []int{} |
| 74 | + |
| 75 | + for i := 0; i < n; i++ { |
| 76 | + for len(stack) > 0 && primeScores[stack[len(stack)-1]] < primeScores[i] { |
| 77 | + topIndex := stack[len(stack)-1] |
| 78 | + stack = stack[:len(stack)-1] |
| 79 | + nextDominant[topIndex] = i |
| 80 | + } |
| 81 | + |
| 82 | + if len(stack) > 0 { |
| 83 | + prevDominant[i] = stack[len(stack)-1] |
| 84 | + } |
| 85 | + |
| 86 | + stack = append(stack, i) |
| 87 | + } |
| 88 | + |
| 89 | + numOfSubarrays := make([]int64, n) |
| 90 | + for i := 0; i < n; i++ { |
| 91 | + numOfSubarrays[i] = int64(nextDominant[i]-i) * int64(i-prevDominant[i]) |
| 92 | + } |
| 93 | + |
| 94 | + pq := &PriorityQueue{} |
| 95 | + heap.Init(pq) |
| 96 | + |
| 97 | + // Push each number and its index onto the priority queue |
| 98 | + for i := 0; i < n; i++ { |
| 99 | + heap.Push(pq, Pair2818{value: nums[i], index: i}) |
| 100 | + } |
| 101 | + |
| 102 | + score := int64(1) |
| 103 | + |
| 104 | + // Process elements while there are operations left |
| 105 | + for k > 0 { |
| 106 | + // Get the element with the maximum value from the queue |
| 107 | + top := heap.Pop(pq).(Pair2818) |
| 108 | + num := top.value |
| 109 | + index := top.index |
| 110 | + |
| 111 | + // Calculate operations to apply on the current element |
| 112 | + operations := int64(k) |
| 113 | + if operations > numOfSubarrays[index] { |
| 114 | + operations = numOfSubarrays[index] |
| 115 | + } |
| 116 | + |
| 117 | + // Update the score |
| 118 | + score = (score * power(num, operations)) % mod2818 |
| 119 | + |
| 120 | + // Reduce the remaining operations count |
| 121 | + k -= int(operations) |
| 122 | + } |
| 123 | + |
| 124 | + return int(score) |
| 125 | +} |
| 126 | + |
| 127 | +// Helper function to compute the power of a number modulo mod2818 |
| 128 | +func power(base int, exponent int64) int64 { |
| 129 | + res := int64(1) |
| 130 | + |
| 131 | + // Calculate the exponentiation using binary exponentiation |
| 132 | + for exponent > 0 { |
| 133 | + if exponent%2 == 1 { |
| 134 | + res = (res * int64(base)) % mod2818 |
| 135 | + } |
| 136 | + base = (base * base) % mod2818 |
| 137 | + exponent /= 2 |
| 138 | + } |
| 139 | + |
| 140 | + return res |
| 141 | +} |
0 commit comments