📌  相关文章
📜  查找使用数字X和Y形成的第N个偶数长度回文数

📅  最后修改于: 2021-04-24 20:17:23             🧑  作者: Mango

给定一个整数N ,任务是找到第N偶数长度的偶数回文数,该数仅由数字XY组成,其中X,Y> 0

例子:

方法:

  • 使用X&Y的偶数回文数为
    XX, YY, XXXX, XYYX, YXXY, YYYY, XXXXXX, XXYYXX, ...
  • 上面的序列可以观察为:
    XX,       -> Length (L) = 2
    YY,       -> Length (L) = 2
    
    XXXX,     -> Length (L) = 4
    XYYX,     -> Length (L) = 4
    YXXY,     -> Length (L) = 4
    YYYY,     -> Length (L) = 4
    
    XXXXXX,   -> Length (L) = 6
    XXYYXX,   -> Length (L) = 6
    XYXXYX,   -> Length (L) = 6
    XYYYYX,   -> Length (L) = 6
    YXXXXY,   -> Length (L) = 6
    YXYYXY,   -> Length (L) = 6
    YYXXYY,   -> Length (L) = 6
    YYYYYY,   -> Length (L) = 6
    
    XXXXXXXX, -> Length (L) = 8
    ...
    
  • 如果我们将任何一项分为两半,则下半部分与上半部分恰好相反
    例子:
    Taking the term XXYYXX
    
    Dividing this into 2 halves
    XXYYXX = XXY | YXX
    
    So YXX is just the reverse of XXY
    
  • 取项的左半部分,将X = 0和Y = 1得到二进制字符串,可以看到长度为L的数字形成一个从0到(2 L / 2 – 1)的整数序列,作为等级(R) 。因此0≤ R≤ 2升/ 2 – 1

    因此,可以观察到以下顺序:

    L -> Left Half -> Binary String -> Rank (in Decimal) 
    
    2 -> X    -> 0             -> 0
    2 -> Y    -> 1             -> 1
    
    4 -> XX   -> 00            -> 0
    4 -> XY   -> 01            -> 1
    4 -> YX   -> 10            -> 2
    4 -> YY   -> 11            -> 3
    
    6 -> XXX  -> 000           -> 0
    6 -> XXY  -> 001           -> 1
    6 -> XYX  -> 010           -> 2
    6 -> XYY  -> 011           -> 3
    6 -> YXX  -> 100           -> 4
    6 -> YXY  -> 101           -> 5
    6 -> YYX  -> 110           -> 6
    6 -> YYY  -> 111           -> 7
    
    8 -> XXXX -> 0000          -> 0
    ...
    
  • 因此,对于所需项N:
    • 所需的第N个项的长度(L)
    • 所需第N个词的等级(R)
    • 所需的第N个项的前一半= R在L / 2位中的二进制表示,方法是将0替换为X,将1替换为Y
    • 所需的第n项的下半年=前半叶的反

    例子:

    If N = 9,
    -> L = 2(ceil(log2(N + 2)) - 1) = 6
    -> R = N - 2L/2 + 1 = 2
    -> First Half = Binary (2) = 010 = XYX
    -> Second Half = Reverse (First Half) = XYX
    -> Complete Nth term = First Half + Second Half = XYXXYX
    which is the required Nth term.
    

下面是上述方法的实现:

C++
// C++ program to find nth even
// palindromic number of only even
// length composing of 4's and 5's.
  
#include 
using namespace std;
  
// Utility function to compute
// n'th palindrome number
string solve(int n, char x, char y)
{
    // Calculate the length from above
    // formula as discussed above
    int length = ceil(log2(n + 2)) - 1;
  
    // Calculate rank for length L
    int rank = n - (1 << length) + 1;
  
    string left = "", right = "";
  
    for (int i = length - 1; i >= 0; i--) {
  
        // Mask to check if i't bit
        // is set or not
        int mask = 1 << i;
  
        // If bit is set append '5' else append '4'
        bool bit = mask & rank;
  
        if (bit) {
            left += y;
            right += y;
        }
        else {
            left += x;
            right += x;
        }
    }
  
    reverse(right.begin(), right.end());
  
    return left + right;
}
  
// Driver Code
int main()
{
    int n = 23;
    char x = '4', y = '5';
    string ans = solve(n, x, y);
    cout << ans << '\n';
  
    return 0;
}


Java
// Java program to find nth even 
// palindromic number of only even 
// length composing of 4's and 5's. 
import java.util.*;
  
class GFG
{
      
    // Utility function to compute 
    // n'th palindrome number 
    static String solve(int n, char x, char y) 
    { 
        // Calculate the length from above 
        // formula as discussed above 
        int length = (int)Math.ceil(Math.log(n + 2) / 
                                    Math.log(2)) - 1; 
      
        // Calculate rank for length L 
        int rank = n - (1 << length) + 1; 
      
        String left = "", right = ""; 
      
        for (int i = length -1 ; i >= 0; i--)
        { 
      
            // Mask to check if i't bit 
            // is set or not 
            int mask = (1 << i); 
      
            // If bit is set append '5' else append '4' 
            int bit = mask & rank; 
              
            if (bit > 0)
            { 
                left += y; 
                right += y; 
            } 
            else 
            { 
                left += x; 
                right += x; 
            } 
        } 
          
        StringBuilder sb = new StringBuilder(right); 
        sb.reverse(); 
          
        right = sb.toString(); 
          
        String res = left + right;
        return res; 
    } 
      
    // Driver Code 
    public static void main (String[] args)
    { 
        int n = 23; 
        char x = '4', y = '5'; 
        String ans = solve(n, x, y); 
        System.out.println(ans); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 program to find nth even 
# palindromic number of only even 
# length composing of 4's and 5's. 
from math import ceil, log2
  
# Utility function to compute 
# n'th palindrome number 
def solve(n, x, y) : 
  
    # Calculate the length from above 
    # formula as discussed above 
    length = ceil(log2(n + 2)) - 1; 
  
    # Calculate rank for length L 
    rank = n - (1 << length) + 1; 
  
    left = ""; right = ""; 
  
    for i in range(length - 1 , -1, -1):
  
        # Mask to check if i't bit 
        # is set or not 
        mask = (1 << i); 
  
        # If bit is set append '5' 
        # else append '4' 
        bit = (mask & rank); 
  
        if (bit) :
            left += y; 
            right += y; 
              
        else :
            left += x; 
            right += x; 
  
    right = right[::-1];
      
    res = left + right;
    return res;
  
# Driver Code 
if __name__ == "__main__" : 
  
    n = 23; 
    x = '4';
    y = '5'; 
    ans = solve(n, x, y); 
    print(ans); 
      
# This code is contributed by kanugargng


C#
// C# program to find nth even 
// palindromic number of only even 
// length composing of 4's and 5's. 
using System;
  
class GFG
{
      
    // Utility function to compute 
    // n'th palindrome number 
    static String solve(int n, char x, char y) 
    { 
        // Calculate the length from above 
        // formula as discussed above 
        int length = (int)Math.Ceiling(Math.Log(n + 2) / 
                                       Math.Log(2)) - 1; 
      
        // Calculate rank for length L 
        int rank = n - (1 << length) + 1; 
      
        String left = "", right = ""; 
      
        for (int i = length -1; i >= 0; i--)
        { 
      
            // Mask to check if i't bit 
            // is set or not 
            int mask = (1 << i); 
      
            // If bit is set append '5'
            // else append '4' 
            int bit = mask & rank; 
              
            if (bit > 0)
            { 
                left += y; 
                right += y; 
            } 
            else
            { 
                left += x; 
                right += x; 
            } 
        } 
          
        right = reverse(right);
        String res = left + right;
        return res; 
    } 
      
    static String reverse(String input) 
    {
        char[] a = input.ToCharArray();
        int l, r = 0;
        r = a.Length - 1;
  
        for (l = 0; l < r; l++, r--) 
        {
            // Swap values of l and r 
            char temp = a[l];
            a[l] = a[r];
            a[r] = temp;
        }
        return String.Join("", a);
    } 
      
    // Driver Code 
    public static void Main (String[] args)
    { 
        int n = 23; 
        char x = '4', y = '5'; 
        String ans = solve(n, x, y); 
        Console.WriteLine(ans); 
    } 
}
  
// This code is contributed by Rajput-Ji


时间复杂度:  O(n) 其中n是字符串的长度