给定数字N ,任务是找到所有N位数回文数的总和,这些数字可以被9整除,并且其中的数字不包含0。
注意:总和可能非常大,取10 9 +7为模。
例子:
Input: N = 2
Output: 99
Explanation:
There is only one 2-digit palindromic number divisible by 9 is 99.
Input: N = 3
Output: 4995
天真的方法:想法是遍历所有N位数字,并检查数字是否是回文数且可被9整除,并且其中是否不包含零。如果是,则所有数字的总和给出所需的结果。
高效方法:以下是对该问题陈述的一些观察:
- 如果N为奇数,则数字可以采用“ ab..x..ba”的形式;如果N为偶数,则数字可以为“ ab..xx..ba”的形式,其中’a’,’b’ x可以用1到9之间的数字代替。
- 如果数字“ ab..x..ba”可以被9整除,则(2 *(a + b)+ x)必须被9整除。
- 对于(a,b)的每个可能的对,总是存在一个这样的“ x”,因此该数字可以被9整除。
- 然后可以从下式计算可被9整除的N位回文数:
Since we have 9 digits to fill at the position a and b.
Therefore, total possible combinations will be:
if N is Odd then, count = pow(9, N/2)
if N is Even then, count = pow(9, N/2 - 1)
as N/2 digits are repeated at the end to get the
number palindromic.
‘x’的唯一性证明:
- a和b的最小值为1,因此最小值之和= 2。
- a和b的最大值为9,因此最小值之和= 18。
- 由于数字是回文数,因此总和由下式给出:
sum = 2*(a+b) + x;
- 2 *(a + b)的格式为2、4、6、8,…,36。为使和可被9整除,x的值可以是:
sum one of the possible value of x sum + x
2 7 9
4 5 9
6 3 9
8 1 9
. . .
. . .
. . .
36 9 45
步骤如下:
- 经过上述观察,得出的所有N位回文数的位数在第k个位置可被9整除的总和为:
sum at kth position = 45*(number of combinations)/9
- 循环遍历[1,N]并找到可能组合的数量,以在当前索引处放置一个数字。
- 使用上述公式找到当前位数的所有位数的总和。
- 在给定所需结果的情况下,每次迭代计算出的所有总和之和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
long long int MOD = 1000000007;
// Function to find a^b efficiently
long long int power(long long int a,
long long int b)
{
// Base Case
if (b == 0)
return 1;
// To store the value of a^b
long long int temp = power(a, b / 2);
temp = (temp * temp) % MOD;
// Multiply temp by a until b is not 0
if (b % 2 != 0) {
temp = (temp * a) % MOD;
}
// Return the final ans a^b
return temp;
}
// Function to find sum of all N-digit
// palindromic number divisible by 9
void palindromicSum(int N)
{
long long int sum = 0, res, ways;
// Base Case
if (N == 1) {
cout << "9" << endl;
return;
}
if (N == 2) {
cout << "99" << endl;
return;
}
ways = N / 2;
// If N is even, decrease ways by 1
if (N % 2 == 0)
ways--;
// Find the total number of ways
res = power(9, ways - 1);
// Iterate over [1, N] and find the
// sum at each index
for (int i = 0; i < N; i++) {
sum = sum * 10 + 45 * res;
sum %= MOD;
}
// Print the final Sum
cout << sum << endl;
}
// Driver Code
int main()
{
int N = 3;
palindromicSum(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
static int MOD = 1000000007;
// Function to find a^b efficiently
static int power(int a, int b)
{
// Base Case
if (b == 0)
return 1;
// To store the value of a^b
int temp = power(a, b / 2);
temp = (temp * temp) % MOD;
// Multiply temp by a until b is not 0
if (b % 2 != 0)
{
temp = (temp * a) % MOD;
}
// Return the final ans a^b
return temp;
}
// Function to find sum of all N-digit
// palindromic number divisible by 9
static void palindromicSum(int N)
{
int sum = 0, res, ways;
// Base Case
if (N == 1)
{
System.out.print("9" + "\n");
return;
}
if (N == 2)
{
System.out.print("99" + "\n");
return;
}
ways = N / 2;
// If N is even, decrease ways by 1
if (N % 2 == 0)
ways--;
// Find the total number of ways
res = power(9, ways - 1);
// Iterate over [1, N] and find the
// sum at each index
for (int i = 0; i < N; i++)
{
sum = sum * 10 + 45 * res;
sum %= MOD;
}
// Print the final Sum
System.out.print(sum + "\n");
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
palindromicSum(N);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python 3 program for the above approach
MOD = 1000000007
# Function to find a^b efficiently
def power(a, b):
# Base Case
if (b == 0):
return 1
# To store the value of a^b
temp = power(a, b // 2)
temp = (temp * temp) % MOD
# Multiply temp by a until b is not 0
if (b % 2 != 0):
temp = (temp * a) % MOD
# Return the final ans a^b
return temp
# Function to find sum of all N-digit
# palindromic number divisible by 9
def palindromicSum(N):
sum = 0
# Base Case
if (N == 1):
print("9")
return
if (N == 2):
print("99")
return
ways = N // 2
# If N is even, decrease ways by 1
if (N % 2 == 0):
ways -= 1
# Find the total number of ways
res = power(9, ways - 1)
# Iterate over [1, N] and find the
# sum at each index
for i in range(N):
sum = sum * 10 + 45 * res
sum %= MOD
# Print the final Sum
print(sum)
# Driver Code
if __name__ == "__main__":
N = 3
palindromicSum(N)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG{
static int MOD = 1000000007;
// Function to find a^b efficiently
static int power(int a, int b)
{
// Base Case
if (b == 0)
return 1;
// To store the value of a^b
int temp = power(a, b / 2);
temp = (temp * temp) % MOD;
// Multiply temp by a until b is not 0
if (b % 2 != 0)
{
temp = (temp * a) % MOD;
}
// Return the readonly ans a^b
return temp;
}
// Function to find sum of all N-digit
// palindromic number divisible by 9
static void palindromicSum(int N)
{
int sum = 0, res, ways;
// Base Case
if (N == 1)
{
Console.Write("9" + "\n");
return;
}
if (N == 2)
{
Console.Write("99" + "\n");
return;
}
ways = N / 2;
// If N is even, decrease ways by 1
if (N % 2 == 0)
ways--;
// Find the total number of ways
res = power(9, ways - 1);
// Iterate over [1, N] and find the
// sum at each index
for(int i = 0; i < N; i++)
{
sum = sum * 10 + 45 * res;
sum %= MOD;
}
// Print the readonly sum
Console.Write(sum + "\n");
}
// Driver Code
public static void Main(String[] args)
{
int N = 3;
palindromicSum(N);
}
}
// This code is contributed by AbhiThakur
Javascript
输出:
4995
时间复杂度: O(N),其中N是给定的数字。
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。