To solve the problem of finding the k-th automorphic number, we need to understand what an automorphic number is. An automorphic number is a number whose square ends with the number itself. For example:
- 5 is automorphic because (5^2 = 25), and it ends with 5.
- 76 is automorphic because (76^2 = 5776), and it ends with 76.
Given the constraint (1 \leq k \leq 36), we need an efficient way to generate and check automorphic numbers until we reach the k-th one.
Approach:
- Incremental Search: Start from 0 and incrementally check each number to see if it's automorphic.
- Check Automorphic: For a number (n), compute (n^2) and check if the last (d) digits of (n^2) match (n), where (d) is the number of digits in (n).
To handle very large numbers (since automorphic numbers can get quite large), we'll use strings to represent numbers and perform the necessary comparisons.
C++ Implementation:
Below is a C++ program that implements the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to compute n squared modulo 10^d, where d is the number of digits in n
string multiply(string num1, string num2) {
int n = num1.size();
int m = num2.size();
vector<int> result(n + m, 0);
// Multiply each digit
for(int i = n-1; i >=0; i--){
for(int j = m-1; j >=0; j--){
int mul = (num1[i]-'0') * (num2[j]-'0');
int sum = result[i+j+1] + mul;
result[i+j+1] = sum % 10;
result[i+j] += sum / 10;
}
}
// Convert the result vector to string
string product = "";
for(auto num : result){
if(!(product.empty() && num == 0)) // Skip leading zeros
product += to_string(num);
}
if(product.empty()) return "0";
return product;
}
int main(){
int k;
cin >> k;
// Initialize variables
// We'll represent numbers as strings to handle large numbers
// Start from 0 upwards
long long count = 0;
// To store the current number, start with 0
// We'll limit the search up to a certain range
// Since k <= 36, it's feasible
// To handle large numbers, we'll use bit manipulation or string
// But here, we'll use string increment
// Start from "0"
// Function to increment string number
auto increment = [](string s) -> string{
int carry = 1;
for(int i = s.size()-1; i >=0 && carry; i--){
int digit = s[i]-'0' + carry;
if(digit >=10){
s[i] = '0';
carry =1;
}
else{
s[i] = digit + '0';
carry =0;
}
}
if(carry) s = "1" + s;
return s;
};
string num = "0";
while(true){
// Compute square
string square = multiply(num, num);
// Now, check if the square ends with num
// To do this, compare the last d digits of square with num
int d = num.size();
if(d > square.size()){
// Not possible to be automorphic
}
else{
string last_d = square.substr(square.size() - d, d);
if(last_d == num){
count++;
if(count == k){
cout << num;
return 0;
}
}
}
// Increment num
num = increment(num);
}
}
Explanation of the Code:
-
Multiplication Function (multiply):
- Multiplies two large numbers represented as strings.
- Handles carry-over during multiplication.
- Returns the product as a string.
-
Main Function:
- Reads the input
k.
- Starts checking from the number
0.
- For each number:
- Computes its square using the
multiply function.
- Checks if the last
d digits of the square match the number itself (d is the number of digits in the number).
- If it matches, increments the count.
- Once the count reaches
k, prints the number and terminates.
- Uses a lambda function
increment to handle string-based number incrementation to avoid integer overflow.
Sample Runs:
Let's verify the program with the provided sample inputs.
-
Sample Input 1:
1
Sample Output 1:
0
-
Sample Input 2:
2
Sample Output 2:
1
-
Sample Input 3:
3
Sample Output 3:
5
-
Sample Input 4:
7
Sample Output 4:
376
Conclusion:
This program efficiently finds the k-th automorphic number by incrementally searching and checking each number. By representing numbers as strings, it can handle very large automorphic numbers without running into integer overflow issues.