给定正整数N ,任务是找到长度为N的字母数字回文字符串的数量。由于此类字符串的数量可能很大,因此请以10 9 + 7为模输出答案。
例子:
Input: N = 2
Output: 62
Explanation: There are 26 palindromes of the form {“AA”, “BB”, …, “ZZ”}, 26 palindromes of the form {“aa”, “bb”, …, “cc”} and 10 palindromes of the form {“00”, “11”, …, “99”}. Therefore, the total number of palindromic strings = 26 + 26 + 10 = 62.
Input: N = 3
Output: 3844
天真的方法:最简单的方法是生成所有可能的长度为N的字母数字字符串,并针对每个字符串检查它是否是回文式。因为在每个位置上总共可以放置62个字符。因此,有62 N个可能的字符串。
时间复杂度: O(N * 62 N )
辅助空间: O(N)
高效的方法:为了优化上述方法,其思想是利用回文的性质。可以看出,如果字符串的长度是偶数,则对于从0到N / 2的每个索引i ,索引i和(N – 1 – i)处的字符都是相同的。因此,对于从0到N / 2的每个位置,都有62 N / 2个选项。同样,如果长度为奇数,则存在62 (N + 1)/ 2个选项。因此,可以说,对于一些N ,有62个ceil(N / 2)个可能的回文字符串。
请按照以下步骤解决问题:
- 对于给定的N值,使用模幂计算62 ceil(N / 2) mod 10 9 + 7 。
- 将62 ceil(N / 2) mod 10 9 + 7打印为所需答案。
下面是上述方法的实现:
C++14
// C++ program for the
// above approach
#include
using namespace std;
// Function to calculate
// (x ^ y) mod p
int power(int x, int y,
int p)
{
// Initialize result
int res = 1;
// Update x if it is more
// than or equal to p
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1;
x = (x * x) % p;
}
// Return the final
// result
return res;
}
// Driver Code
int main()
{
// Given N
int N = 3;
int flag, k, m;
// Base Case
if((N == 1) ||
(N == 2))
cout << 62;
else
m = 1000000000 + 7;
// Check whether n
// is even or odd
if(N % 2 == 0)
{
k = N / 2;
flag = true;
}
else
{
k = (N - 1) / 2;
flag = false;
}
if(flag != 0)
{
// Function Call
int a = power(62,
k, m);
cout << a;
}
else
{
// Function Call
int a = power(62,
(k + 1), m);
cout << a;
}
}
// This code is contributed by sanjoy_62
Java
// Java program for the
// above approach
import java.util.*;
class GFG{
// Function to calculate
// (x ^ y) mod p
static int power(int x,
int y, int p)
{
// Initialize result
int res = 1;
// Update x if it is more
// than or equal to p
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1;
x = (x * x) % p;
}
// Return the final
// result
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given N
int N = 3;
int flag, k, m =0;
// Base Case
if ((N == 1) || (N == 2))
System.out.print(62);
else
m = 1000000000 + 7;
// Check whether n
// is even or odd
if (N % 2 == 0)
{
k = N / 2;
flag = 1;
}
else
{
k = (N - 1) / 2;
flag = 0;
}
if (flag != 0)
{
// Function Call
int a = power(62, k, m);
System.out.print(a);
}
else
{
// Function Call
int a = power(62, (k + 1), m);
System.out.print(a);
}
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to calculate (x ^ y) mod p
def power(x, y, p):
# Initialize result
res = 1
# Update x if it is more
# than or equal to p
x = x % p
if (x == 0):
return 0
while (y > 0):
# If y is odd, multiply
# x with result
if ((y & 1) == 1):
res = (res * x) % p
# y must be even now
y = y >> 1
x = (x * x) % p
# Return the final result
return res
# Driver Code
# Given N
N = 3
# Base Case
if((N == 1) or (N == 2)):
print(62)
else:
m = (10**9)+7
# Check whether n
# is even or odd
if(N % 2 == 0):
k = N//2
flag = True
else:
k = (N - 1)//2
flag = False
if(flag):
# Function Call
a = power(62, k, m)
print(a)
else:
# Function Call
a = power(62, (k + 1), m)
print(a)
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to calculate
// (x ^ y) mod p
static int power(int x, int y,
int p)
{
// Initialize result
int res = 1;
// Update x if it is more
// than or equal to p
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1;
x = (x * x) % p;
}
// Return the final
// result
return res;
}
// Driver Code
public static void Main()
{
// Given N
int N = 3;
int flag, k, m = 0;
// Base Case
if ((N == 1) || (N == 2))
Console.Write(62);
else
m = 1000000000 + 7;
// Check whether n
// is even or odd
if (N % 2 == 0)
{
k = N / 2;
flag = 1;
}
else
{
k = (N - 1) / 2;
flag = 0;
}
if (flag != 0)
{
// Function Call
int a = power(62, k, m);
Console.Write(a);
}
else
{
// Function Call
int a = power(62, (k + 1), m);
Console.Write(a);
}
}
}
// This code is contributed by code_hunt
Javascript
3844
时间复杂度: O(log N)
辅助空间: O(N)