Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers…
- User
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 ≤ x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all.
You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 109) — the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero.
Output
In the single line print the result without spaces — the number after the k operations are fulfilled.
Examples
Input
7 4 4727447
Output
4427477
Input
4 2 4478
Output
4478
Note
In the first sample the number changes in the following sequence: 4727447 → 4427447 → 4427477 → 4427447 → 4427477.
In the second sample: 4478 → 4778 → 4478.
Assistant
pythonn, k = map(int, input().split()) d = list(input().strip()) prev_steps = {} current_step = 0 while current_step < k: x = -1 for i in range(n-1): if d[i] == '4' and d[i+1] == '7': x = i + 1 # 1-based index break if x == -1: break # Check for cycle i = x - 1 # convert to 0-based current_d1, current_d2 = d[i], d[i+1] parity = x % 2 key = (x, parity, current_d1, current_d2) if key in prev_steps: cycle_length = current_step - prev_steps[key] if cycle_length > 0: remaining_steps = k - current_step total_cycles = remaining_steps // cycle_length current_step += total_cycles * cycle_length if current_step >= k: break # Reset prev_steps to avoid re-detecting the same cycle prev_steps = {} else: prev_steps[key] = current_step # Perform the operation if x % 2 == 1: d[i] = d[i+1] = '4' else: d[i] = d[i+1] = '7' current_step += 1 print(''.join(d))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.