#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include…
- User
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <float.h>
#define MOD 1000000007
typedef struct { int value; int index; } Element;
int cmp(const void* a, const void* b) { Element* ea = (Element*)a; Element* eb = (Element*)b; if (ea->value != eb->value) return ea->value - eb->value; else return ea->index - eb->index; }
long long mod_pow(long long base, long long exp, long long mod) { long long result = 1; base %= mod; while (exp > 0) { if (exp & 1) result = (result * base) % mod; base = (base * base) % mod; exp >>= 1; } return result; }
int* getFinalState(int* nums, int numsSize, int k, int multiplier, int* returnSize) { returnSize = numsSize; Element elements = (Element*)malloc(numsSize * sizeof(Element)); for (int i = 0; i < numsSize; i++) { elements[i].value = nums[i]; elements[i].index = i; } qsort(elements, numsSize, sizeof(Element), cmp);
long long* t = (long long*)calloc(numsSize, sizeof(long long)); long long k_remain = k; double log_multiplier = log(multiplier); for (int i = 0; i < numsSize && k_remain > 0; i++) { double current_log = log(elements[i].value); double next_log = i + 1 < numsSize ? log(elements[i + 1].value) : DBL_MAX; long long ti = (long long)ceil((next_log - current_log) / log_multiplier); if (ti <= 0) ti = 1; if (ti > k_remain) ti = k_remain; t[i] = ti; k_remain -= ti; }
int* result = (int*)malloc(numsSize * sizeof(int)); for (int i = 0; i < numsSize; i++) { long long updates = t[i]; long long final_value = ((long long)elements[i].value * mod_pow(multiplier, updates, MOD)) % MOD; result[elements[i].index] = (int)final_value; }
free(elements); free(t); return result; }
the output is wrong Wrong Answer Runtime: 0 ms Case 1 Case 2 Case 3 Input nums = [2,1,3,5,6] k = 5 multiplier = 2 Output [4,2,6,10,12] Expected [8,4,6,5,6]
You are given an integer array nums, an integer k, and an integer multiplier.
You need to perform k operations on nums. In each operation:
Find the minimum value x in nums. If there are multiple occurrences of the minimum value, select the one that appears first. Replace the selected minimum value x with x * multiplier. After the k operations, apply modulo 109 + 7 to every value in nums.
Return an integer array denoting the final state of nums after performing all k operations and then applying the modulo.
Example 1:
Input: nums = [2,1,3,5,6], k = 5, multiplier = 2
Output: [8,4,6,5,6]
Explanation:
Operation Result After operation 1 [2, 2, 3, 5, 6] After operation 2 [4, 2, 3, 5, 6] After operation 3 [4, 4, 3, 5, 6] After operation 4 [4, 4, 6, 5, 6] After operation 5 [8, 4, 6, 5, 6] After applying modulo [8, 4, 6, 5, 6] Example 2:
Input: nums = [100000,2000], k = 2, multiplier = 1000000
Output: [999999307,999999993]
Explanation:
- Assistant