给定两个正整数N和M ,任务是使用给定的N个不同字符来计算长度为M的非回文字符串的数量。
注意:每个不同的字符可以多次使用。
例子:
Input: N = 3, M = 2
Output: 6
Explanation:
Since only 3 characters are given, those 3 characters can be used to form 32 different strings. Out of these, only 3 strings are palindromic. Hence, the remaining 6 strings are palindromic.
Input: N = 26, M = 5
Output: 11863800
方法:
请按照以下步骤解决问题:
- 使用给定的N个字符,长度为M的字符串总数为N M。
- 要使字符串成为回文,前半部分和后半部分应相等。对于M的偶数值,我们需要选择从给定的N个字符仅M / 2个字符。为奇数值,我们需要选择M /从给定的N个字符2 + 1字符。由于允许重复,因此长度为M的回文字符串总数为N (M / 2 + M%2) 。
- 非回文字符串的所需计数由以下公式给出:
NM - N(M/2 + M%2)
下面是上述方法的实现:
C++
// C++ Program to count
// non-palindromic strings
// of length M using N
// distinct characters
#include
using namespace std;
// Iterative Function to calculate
// base^pow in O(log y)
unsigned long long power(
unsigned long long base,
unsigned long long pow)
{
unsigned long long res = 1;
while (pow > 0) {
if (pow & 1)
res = (res * base);
base = (base * base);
pow >>= 1;
}
return res;
}
// Function to return the
// count of non palindromic strings
unsigned long long countNonPalindromicString(
unsigned long long n,
unsigned long long m)
{
// Count of strings using n
// characters with
// repetitions allowed
unsigned long long total
= power(n, m);
// Count of palindromic strings
unsigned long long palindrome
= power(n, m / 2 + m % 2);
// Count of non-palindromic strings
unsigned long long count
= total - palindrome;
return count;
}
int main()
{
int n = 3, m = 5;
cout<< countNonPalindromicString(n, m);
return 0;
}
Java
// Java program to count non-palindromic
// strings of length M using N distinct
// characters
import java.util.*;
class GFG{
// Iterative Function to calculate
// base^pow in O(log y)
static long power(long base, long pow)
{
long res = 1;
while (pow > 0)
{
if ((pow & 1) == 1)
res = (res * base);
base = (base * base);
pow >>= 1;
}
return res;
}
// Function to return the
// count of non palindromic strings
static long countNonPalindromicString(long n,
long m)
{
// Count of strings using n
// characters with
// repetitions allowed
long total = power(n, m);
// Count of palindromic strings
long palindrome = power(n, m / 2 + m % 2);
// Count of non-palindromic strings
long count = total - palindrome;
return count;
}
// Driver code
public static void main(String[] args)
{
int n = 3, m = 5;
System.out.println(
countNonPalindromicString(n, m));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to count non-palindromic strings
# of length M using N distinct characters
# Iterative Function to calculate
# base^pow in O(log y)
def power(base, pwr):
res = 1
while(pwr > 0):
if(pwr & 1):
res = res * base
base = base * base
pwr >>= 1
return res
# Function to return the count
# of non palindromic strings
def countNonPalindromicString(n, m):
# Count of strings using n
# characters with
# repetitions allowed
total = power(n, m)
# Count of palindromic strings
palindrome = power(n, m // 2 + m % 2)
# Count of non-palindromic strings
count = total - palindrome
return count
# Driver code
if __name__ == '__main__':
n = 3
m = 5
print(countNonPalindromicString(n, m))
# This code is contributed by Shivam Singh
C#
// C# program to count non-palindromic
// strings of length M using N distinct
// characters
using System;
class GFG{
// Iterative Function to calculate
// base^pow in O(log y)
static long power(long Base, long pow)
{
long res = 1;
while (pow > 0)
{
if ((pow & 1) == 1)
res = (res * Base);
Base = (Base * Base);
pow >>= 1;
}
return res;
}
// Function to return the
// count of non palindromic strings
static long countNonPalindromicString(long n,
long m)
{
// Count of strings using n
// characters with
// repetitions allowed
long total = power(n, m);
// Count of palindromic strings
long palindrome = power(n, m / 2 + m % 2);
// Count of non-palindromic strings
long count = total - palindrome;
return count;
}
// Driver code
public static void Main(String[] args)
{
int n = 3, m = 5;
Console.WriteLine(
countNonPalindromicString(n, m));
}
}
// This code is contributed by PrinciRaj1992
输出:
216
时间复杂度: O(log(N))