Bajtynka dostała od Bajtazara na urodziny nową zabawkę logiczną – zamek cykliczny. Zamek cykliczny
składa się z dwóch przycisków oraz wyświetlacza. Na wyświetlaczu może być wyświetlana dowolnie duża liczba
w systemie dziesiętnym bez zer wiodących. Początkowo wyświetlana jest liczba wybierana przez oprogramowanie zabawki.
Celem gry jest sprawienie, aby wyświetlana była liczba 1. W tym celu można korzystać z wspomnianych
dwóch przycisków. Po naciśnięciu pierwszego przycisku liczba zostaje zwiększona o 1. Drugi przycisk obraca
cyklicznie zapis dziesiętny liczby o jedną pozycję, tak aby najbardziej znacząca cyfra przeniosła się na najmniej
znaczącą pozycję, a następnie usuwa wszystkie zera wiodące.
Na przykład, po naciśnięciu pierwszego przycisku liczba 99 zamienia się w liczbę 100. Po naciśnięciu drugiego przycisku liczba 143 zamienia się w liczbę 431, za to liczba 700523 zamienia się w liczbę 5237.
Teraz Bajtynka zastanawia się, jaka jest minimalna liczba naciśnięć przycisków potrzebna do rozwiązania
zagadki. Twoim zadaniem jest wyznaczenie tej liczby.
Wejście
W pierwszym i jedynym wierszu wejścia znajduje się jedna liczba n oznaczająca aktualnie wyświetlaną liczbę
(1 ≤ n ≤ 10^1 000 000).
Wyjście
Twój program powinien wypisać jedną liczbę oznaczającą minimalną liczbę naciśnięć przycisków potrzebnych
do rozwiązania zagadki.
Przykład
Dla danych wejściowych:
301
poprawnym wynikiem jest:
17
Wyjaśnienie do przykładu: Aby rozwiązać zagadkę w 17 ruchach, należy kolejno:
osiem razy wcisnąć pierwszy przycisk; wówczas zabawka będzie wyświetlać 309,
wcisnąć drugi przycisk; wówczas zabawka będzie wyświetlać 93,
siedem razy wcisnąć pierwszy przycisk; wówczas zabawka będzie wyświetlać 100,
wcisnąć drugi przycisk rozwiązując zagadkę.
Zrób rozwiązanie w C++
Assistant
To solve the problem of determining the minimal number of operations required to transform a given number n to 1 using the two available operations (incrementing by 1 and rotating the digits), we can follow a strategic approach. The key insight is to leverage rotations effectively to reduce the number as much as possible while minimizing the number of increments required.
Strategy Overview:
Rotate to Reduce the Number: Whenever possible, rotate the number to drop the most significant digit, thereby drastically reducing the number's magnitude.
Increment to Reach Rotatable Points: If the number does not have a '0' in the second digit, increment the number until it reaches a point where a rotation will significantly reduce its value.
Special Handling for Single-Digit Numbers: When the number becomes a single digit, handle it separately by incrementing until it reaches 10, then rotating to 1.
Step-by-Step Approach:
Initialization:
Start with the given number n.
Initialize a counter to keep track of the number of operations performed.
Loop Until 1 is Reached:
If n is already 1, terminate.
If n is a single-digit number (other than 1), increment it until it reaches 10, then rotate to 1.
For multi-digit numbers:
Determine a target number (y_shift) by setting the second digit to 0 and the rest to 9 (e.g., from 301 to 309).
If y_shift is greater than or equal to n, increment to reach y_shift and perform a rotation.
If y_shift is less than n, find the next possible number with a 0 in the second digit by incrementing the first digit and setting the rest to 0 (e.g., from 309 to 400).
After each increment or rotation, update the counter accordingly.
Edge Cases:
Handle cases where the number consists of all 9s or when incrementing leads to an increase in the number of digits.
Implementation:
The following C++ code implements the above strategy efficiently, handling large numbers represented as strings:
cpp
#include <bits/stdc++.h>
using namespace std;
// Function to rotate the number: move first digit to end and remove leading zeros
string rotate_number(const string& s) {
string rotated = s.substr(1) + s[0];
// Remove leading zeros
size_t pos = rotated.find_first_not_of('0');
if (pos == string::npos) return "0";
return rotated.substr(pos);
}
// Function to compare two numeric strings
bool is_geq(const string& a, const string& b) {
if (a.size() != b.size()) return a.size() > b.size();
return a >= b;
}
// Function to subtract two numeric strings (a >= b)
string subtract_strings(const string& a, const string& b) {
string result = a;
int carry = 0;
int n = a.size();
for(int i = n-1; i >=0; --i){
int digit_a = a[i] - '0';
int digit_b = (i < (int)b.size()) ? (b[i] - '0') : 0;
int sub = digit_a - digit_b - carry;
if(sub <0){
sub +=10;
carry =1;
}
else{
carry =0;
}
result[i] = sub + '0';
}
// Remove leading zeros
size_t pos = result.find_first_not_of('0');
if(pos == string::npos) return "0";
return result.substr(pos);
}
int main(){
string n;
cin >> n;
long long operations =0;
while(n != "1"){
if(n.size() ==1){
if(n == "1") break;
int num = n[0]-'0';
int x = 10 - num;
operations += x +1;
n = "1";
continue;
}
// Create y_shift
string y_shift = "";
y_shift += n[0];
y_shift += '0';
if(n.size() >2){
y_shift += string(n.size()-2, '9');
}
// Check if y_shift has d2='0' and y_shift >=n
bool has_d2_zero = (y_shift[1] == '0');
bool y_geq_n = is_geq(y_shift, n);
if(has_d2_zero && y_geq_n){
if(y_shift == n){
string rotated = rotate_number(y_shift);
if(rotated < y_shift){
operations +=1;
n = rotated;
continue;
}
}
// Compute x = y_shift - n
string x_str = subtract_strings(y_shift, n);
// Convert x_str to integer
long long x = 0;
for(char c:x_str) x = x*10 + (c - '0');
operations += x +1;
// Rotate y_shift
n = rotate_number(y_shift);
}
else{
// Find y_next = (d1+1) + '0'*(k-1)
string y_next = "";
if(n[0] < '9'){
y_next += char(n[0] +1);
y_next += string(n.size()-1, '0');
}
else{
y_next = "1" + string(n.size(), '0');
}
// Compute x = y_next -n
string x_str = subtract_strings(y_next, n);
// Convert x_str to integer
long long x = 0;
for(char c:x_str) x = x*10 + (c - '0');
operations += x +1;
// Rotate y_next
n = rotate_number(y_next);
}
}
cout << operations;
}
Explanation of the Code:
Rotation Function: The rotate_number function performs the rotation operation by moving the first digit to the end and removing any leading zeros that result from the rotation.
Comparison and Subtraction Functions:
is_geq compares two numeric strings to determine if the first is greater than or equal to the second.
subtract_strings performs subtraction between two numeric strings where the first is guaranteed to be greater than or equal to the second.
Main Logic:
The program repeatedly transforms the number n until it becomes 1.
For multi-digit numbers, it calculates y_shift by setting the second digit to 0 and the rest to 9 to find a suitable rotation point.
Depending on whether y_shift is greater than or equal to n, and whether it needs to rotate or increment, the program updates the count of operations and continues the transformation.
Conclusion:
This approach ensures that we minimize the number of operations by strategically deciding when to rotate and when to increment. The implementation handles very large numbers efficiently by treating them as strings and performing operations accordingly.