📌  相关文章
📜  具有被给定数字整除的偶数和奇数位数字之和的N位数字的计数

📅  最后修改于: 2021-04-29 11:42:52             🧑  作者: Mango

给定整数A,BN ,任务是找到N位数字的总数,其N位在偶数位置奇数位分别被AB整除。

例子:

天真的方法:解决此问题的最简单方法是遍历所有可能的N位数字,对于每个数字,检查偶数位置的数字总和是否可被A整除,奇数位置的数字总和是否可被B整除。或不。如果发现是真的,请增加count

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate and
// return the reverse of the number
long reverse(long num)
{
    long rev = 0;
    while (num > 0)
    {
        int r = (int)(num % 10);
        rev = rev * 10 + r;
        num /= 10;
    }
    return rev;
}
 
// Function to calculate the total
// count of N-digit numbers satisfying
// the necessary conditions
long count(int N, int A, int B)
{
     
    // Initialize two variables
    long l = (long)pow(10, N - 1),
         r = (long)pow(10, N) - 1;
 
    if (l == 1)
        l = 0;
 
    long ans = 0;
 
    for(long i = l; i <= r; i++)
    {
        int even_sum = 0, odd_sum = 0;
        long itr = 0, num = reverse(i);
 
        // Calculate the sum of odd
        // and even positions
        while (num > 0)
        {
            if (itr % 2 == 0)
                odd_sum += num % 10;
            else
                even_sum += num % 10;
                 
            num /= 10;
            itr++;
        }
         
        // Check for divisibility
        if (even_sum % A == 0 &&
             odd_sum % B == 0)
            ans++;
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    int N = 2, A = 5, B = 3;
    cout << (count(N, A, B));
 
}
 
// This code is contributed by 29AjayKumar


Java
// Java Program to implement
// the above approach
class GFG {
 
    // Function to calculate and
    // return the reverse of the number
    public static long reverse(long num)
    {
        long rev = 0;
        while (num > 0) {
            int r = (int)(num % 10);
            rev = rev * 10 + r;
            num /= 10;
        }
        return rev;
    }
 
    // Function to calculate the total
    // count of N-digit numbers satisfying
    // the necessary conditions
    public static long count(int N, int A, int B)
    {
        // Initialize two variables
        long l = (long)Math.pow(10, N - 1),
             r = (long)Math.pow(10, N) - 1;
 
        if (l == 1)
            l = 0;
 
        long ans = 0;
 
        for (long i = l; i <= r; i++) {
            int even_sum = 0, odd_sum = 0;
            long itr = 0, num = reverse(i);
 
            // Calculate the sum of odd
            // and even positions
            while (num > 0) {
 
                if (itr % 2 == 0)
                    odd_sum += num % 10;
                else
                    even_sum += num % 10;
                num /= 10;
                itr++;
            }
            // Check for divisibility
            if (even_sum % A == 0
                && odd_sum % B == 0)
                ans++;
        }
 
        // Return the answer
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 2, A = 5, B = 3;
        System.out.println(count(N, A, B));
    }
}


Python3
# Python3 Program to implement
# the above approach
 
# Function to calculate and
# return the reverse of the number
def reverse(num):
     
    rev = 0;
    while (num > 0):
        r = int(num % 10);
        rev = rev * 10 + r;
        num = num // 10;
     
    return rev;
 
# Function to calculate the total
# count of N-digit numbers satisfying
# the necessary conditions
def count(N, A, B):
     
    # Initialize two variables
    l = int(pow(10, N - 1));
    r = int(pow(10, N) - 1);
    if (l == 1):
        l = 0;
 
    ans = 0;
 
    for i in range(l, r + 1):
        even_sum = 0;
        odd_sum = 0;
        itr = 0;
        num = reverse(i);
         
        # Calculate the sum of odd
        # and even positions
        while (num > 0):
 
            if (itr % 2 == 0):
                odd_sum += num % 10;
            else:
                even_sum += num % 10;
            num = num // 10;
            itr += 1;
         
        # Check for divisibility
        if (even_sum % A == 0 and
            odd_sum % B == 0):
            ans += 1;
 
    # Return the answer
    return ans;
 
# Driver Code
if __name__ == '__main__':
     
    N = 2;
    A = 5;
    B = 3;
    print(count(N, A, B));
 
# This code is contributed by gauravrajput1


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to calculate and
// return the reverse of the number
public static long reverse(long num)
{
    long rev = 0;
    while (num > 0)
    {
        int r = (int)(num % 10);
        rev = rev * 10 + r;
        num /= 10;
    }
    return rev;
}
 
// Function to calculate the total
// count of N-digit numbers satisfying
// the necessary conditions
public static long count(int N, int A, int B)
{
     
    // Initialize two variables
    long l = (long)Math.Pow(10, N - 1),
         r = (long)Math.Pow(10, N) - 1;
 
    if (l == 1)
        l = 0;
 
    long ans = 0;
 
    for(long i = l; i <= r; i++)
    {
        int even_sum = 0, odd_sum = 0;
        long itr = 0, num = reverse(i);
 
        // Calculate the sum of odd
        // and even positions
        while (num > 0)
        {
            if (itr % 2 == 0)
                odd_sum += (int)num % 10;
            else
                even_sum += (int)num % 10;
                 
            num /= 10;
            itr++;
        }
         
        // Check for divisibility
        if (even_sum % A == 0 &&
             odd_sum % B == 0)
            ans++;
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 2, A = 5, B = 3;
    Console.WriteLine(count(N, A, B));
}
}
 
// This code is contributed by 29AjayKumar


C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate the total
// count of N-digit numbers such
// that the sum of digits at even
// positions and odd positions are
// divisible by A and B respectively
long count(int N, int A, int B)
{
    // For single digit numbers
    if (N == 1)
    {
        return 9 / B + 1;
    }
 
    // Largest possible number
    int max_sum = 9 * N;
 
    // Count of possible odd digits
    int odd_count = N / 2 + N % 2;
 
    // Count of possible even digits
    int even_count = N - odd_count;
 
    // Calculate total count of sequences of
    // length even_count with sum divisible
    // by A where first digit can be zero
    long dp[even_count][max_sum + 1] = {0};
    for (int i = 0; i <= 9; i++)
        dp[0][i % A]++;
 
    for (int i = 1; i < even_count; i++)
    {
        for (int j = 0; j <= 9; j++)
        {
            for (int k = 0; k <= max_sum; k++)
            {
                if (dp[i - 1][k] > 0)
                    dp[i][(j + k) % A] += dp[i - 1][k];
            }
        }
    }
 
    // Calculate total count of sequences of
    // length odd_count with sum divisible
    // by B where cannot be zero
    long dp1[odd_count][max_sum + 1] = {0};
    for (int i = 1; i <= 9; i++)
        dp1[0][i % B]++;
 
    for (int i = 1; i < odd_count; i++)
    {
        for (int j = 0; j <= 9; j++)
        {
            for (int k = 0; k <= max_sum; k++)
            {
                if (dp1[i - 1][k] > 0)
                    dp1[i][(j + k) % B] += dp1[i - 1][k];
            }
        }
    }
 
    // Return their product as answer
    return dp[even_count - 1][0] * dp1[odd_count - 1][0];
}
 
// Driver Code
int main()
{
    int N = 2, A = 2, B = 5;
    cout << count(N, A, B);
}
 
// This code is contributed by Rajput-Ji


Java
// Java Program to implement
// the above approach
class GFG {
 
    // Function to calculate the total
    // count of N-digit numbers such
    // that the sum of digits at even
    // positions and odd positions are
    // divisible by A and B respectively
    public static long count(int N, int A, int B)
    {
        // For single digit numbers
        if (N == 1) {
            return 9 / B + 1;
        }
 
        // Largest possible number
        int max_sum = 9 * N;
 
        // Count of possible odd digits
        int odd_count = N / 2 + N % 2;
 
        // Count of possible even digits
        int even_count = N - odd_count;
 
        // Calculate total count of sequences of
        // length even_count with sum divisible
        // by A where first digit can be zero
        long dp[][]
            = new long[even_count][max_sum + 1];
        for (int i = 0; i <= 9; i++)
            dp[0][i % A]++;
 
        for (int i = 1; i < even_count; i++) {
            for (int j = 0; j <= 9; j++) {
                for (int k = 0; k <= max_sum; k++) {
                    if (dp[i - 1][k] > 0)
                        dp[i][(j + k) % A]
                            += dp[i - 1][k];
                }
            }
        }
 
        // Calculate total count of sequences of
        // length odd_count with sum divisible
        // by B where cannot be zero
        long dp1[][]
            = new long[odd_count][max_sum + 1];
        for (int i = 1; i <= 9; i++)
            dp1[0][i % B]++;
 
        for (int i = 1; i < odd_count; i++) {
            for (int j = 0; j <= 9; j++) {
                for (int k = 0; k <= max_sum; k++) {
                    if (dp1[i - 1][k] > 0)
                        dp1[i][(j + k) % B]
                            += dp1[i - 1][k];
                }
            }
        }
 
        // Return their product as answer
        return dp[even_count - 1][0]
            * dp1[odd_count - 1][0];
    }
    // Driver Code
    public static void main(String[] args)
    {
        int N = 2, A = 2, B = 5;
        System.out.println(count(N, A, B));
    }
}


Python3
# Python 3 Program to implement
# the above approach
 
# Function to calculate the total
# count of N-digit numbers such
# that the sum of digits at even
# positions and odd positions are
# divisible by A and B respectively
def count(N, A, B):
 
    # For single digit numbers
    if (N == 1):
        return 9 // B + 1
   
    # Largest possible number
    max_sum = 9 * N
 
    # Count of possible odd digits
    odd_count = N // 2 + N % 2
 
    # Count of possible even digits
    even_count = N - odd_count
 
    # Calculate total count of
    # sequences of length even_count
    # with sum divisible by A where
    # first digit can be zero
    dp = [[0 for x in range (max_sum + 1)]
             for y in range (even_count)]
     
    for i in range(10):
        dp[0][i % A] += 1
 
    for i in range (1, even_count):
        for j in range (10):
            for k in range (max_sum + 1):
                if (dp[i - 1][k] > 0):
                    dp[i][(j + k) % A] += dp[i - 1][k]
 
    # Calculate total count of sequences of
    # length odd_count with sum divisible
    # by B where cannot be zero
    dp1 = [[0 for x in range (max_sum)]
              for y in range (odd_count)]
    for i in range (1, 10):
        dp1[0][i % B] += 1
 
    for i in range (1, odd_count): 
        for j in range (10):      
            for k in range (max_sum + 1):
                if (dp1[i - 1][k] > 0):
                    dp1[i][(j + k) % B] += dp1[i - 1][k]
            
    # Return their product as answer
    return dp[even_count - 1][0] * dp1[odd_count - 1][0]
 
# Driver Code
if __name__ == "__main__":
   
    N = 2
    A = 2
    B = 5
    print (count(N, A, B))
 
# This code is contributed by Chitranayal


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to calculate the total
// count of N-digit numbers such
// that the sum of digits at even
// positions and odd positions are
// divisible by A and B respectively
public static long count(int N, int A, int B)
{
     
    // For single digit numbers
    if (N == 1)
    {
        return 9 / B + 1;
    }
 
    // Largest possible number
    int max_sum = 9 * N;
 
    // Count of possible odd digits
    int odd_count = N / 2 + N % 2;
 
    // Count of possible even digits
    int even_count = N - odd_count;
 
    // Calculate total count of sequences of
    // length even_count with sum divisible
    // by A where first digit can be zero
    long [,]dp = new long[even_count, max_sum + 1];
    for(int i = 0; i <= 9; i++)
        dp[0, i % A]++;
 
    for(int i = 1; i < even_count; i++)
    {
        for(int j = 0; j <= 9; j++)
        {
            for(int k = 0; k <= max_sum; k++)
            {
                if (dp[i - 1, k] > 0)
                    dp[i, (j + k) % A] += dp[i - 1, k];
            }
        }
    }
 
    // Calculate total count of sequences of
    // length odd_count with sum divisible
    // by B where cannot be zero
    long [,]dp1 = new long[odd_count, max_sum + 1];
    for(int i = 1; i <= 9; i++)
        dp1[0, i % B]++;
 
    for(int i = 1; i < odd_count; i++)
    {
        for(int j = 0; j <= 9; j++)
        {
            for(int k = 0; k <= max_sum; k++)
            {
                if (dp1[i - 1, k] > 0)
                    dp1[i, (j + k) % B] += dp1[i - 1, k];
            }
        }
    }
     
    // Return their product as answer
    return dp[even_count - 1, 0] *
           dp1[odd_count - 1, 0];
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 2, A = 2, B = 5;
     
    Console.WriteLine(count(N, A, B));
}
}
 
// This code is contributed by 29AjayKumar


输出:
6





时间复杂度: O((10 N – 10 N – 1 – 1)* N)
辅助空间: O(N)

高效方法:要优化上述方法,请使用动态编程的概念。

请按照以下步骤解决问题:

  • 由于偶数位和奇数位不相互依赖,因此,给定的问题可以分解为两个子问题:
    • 对于偶数位数字: N / 2个数字序列的总数,从0到9(允许重复),以使数字之和可被A整除,其中第一个数字可以为零。
    • 对于奇数位数字: Ceil(N / 2)个数字序列的总数,从0到9(允许重复),这样数字的总和可以被B整除,其中第一个数字不能为零。
  • 所需结果是以上两项的乘积。

下面是上述方法的实现:

C++

// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate the total
// count of N-digit numbers such
// that the sum of digits at even
// positions and odd positions are
// divisible by A and B respectively
long count(int N, int A, int B)
{
    // For single digit numbers
    if (N == 1)
    {
        return 9 / B + 1;
    }
 
    // Largest possible number
    int max_sum = 9 * N;
 
    // Count of possible odd digits
    int odd_count = N / 2 + N % 2;
 
    // Count of possible even digits
    int even_count = N - odd_count;
 
    // Calculate total count of sequences of
    // length even_count with sum divisible
    // by A where first digit can be zero
    long dp[even_count][max_sum + 1] = {0};
    for (int i = 0; i <= 9; i++)
        dp[0][i % A]++;
 
    for (int i = 1; i < even_count; i++)
    {
        for (int j = 0; j <= 9; j++)
        {
            for (int k = 0; k <= max_sum; k++)
            {
                if (dp[i - 1][k] > 0)
                    dp[i][(j + k) % A] += dp[i - 1][k];
            }
        }
    }
 
    // Calculate total count of sequences of
    // length odd_count with sum divisible
    // by B where cannot be zero
    long dp1[odd_count][max_sum + 1] = {0};
    for (int i = 1; i <= 9; i++)
        dp1[0][i % B]++;
 
    for (int i = 1; i < odd_count; i++)
    {
        for (int j = 0; j <= 9; j++)
        {
            for (int k = 0; k <= max_sum; k++)
            {
                if (dp1[i - 1][k] > 0)
                    dp1[i][(j + k) % B] += dp1[i - 1][k];
            }
        }
    }
 
    // Return their product as answer
    return dp[even_count - 1][0] * dp1[odd_count - 1][0];
}
 
// Driver Code
int main()
{
    int N = 2, A = 2, B = 5;
    cout << count(N, A, B);
}
 
// This code is contributed by Rajput-Ji

Java

// Java Program to implement
// the above approach
class GFG {
 
    // Function to calculate the total
    // count of N-digit numbers such
    // that the sum of digits at even
    // positions and odd positions are
    // divisible by A and B respectively
    public static long count(int N, int A, int B)
    {
        // For single digit numbers
        if (N == 1) {
            return 9 / B + 1;
        }
 
        // Largest possible number
        int max_sum = 9 * N;
 
        // Count of possible odd digits
        int odd_count = N / 2 + N % 2;
 
        // Count of possible even digits
        int even_count = N - odd_count;
 
        // Calculate total count of sequences of
        // length even_count with sum divisible
        // by A where first digit can be zero
        long dp[][]
            = new long[even_count][max_sum + 1];
        for (int i = 0; i <= 9; i++)
            dp[0][i % A]++;
 
        for (int i = 1; i < even_count; i++) {
            for (int j = 0; j <= 9; j++) {
                for (int k = 0; k <= max_sum; k++) {
                    if (dp[i - 1][k] > 0)
                        dp[i][(j + k) % A]
                            += dp[i - 1][k];
                }
            }
        }
 
        // Calculate total count of sequences of
        // length odd_count with sum divisible
        // by B where cannot be zero
        long dp1[][]
            = new long[odd_count][max_sum + 1];
        for (int i = 1; i <= 9; i++)
            dp1[0][i % B]++;
 
        for (int i = 1; i < odd_count; i++) {
            for (int j = 0; j <= 9; j++) {
                for (int k = 0; k <= max_sum; k++) {
                    if (dp1[i - 1][k] > 0)
                        dp1[i][(j + k) % B]
                            += dp1[i - 1][k];
                }
            }
        }
 
        // Return their product as answer
        return dp[even_count - 1][0]
            * dp1[odd_count - 1][0];
    }
    // Driver Code
    public static void main(String[] args)
    {
        int N = 2, A = 2, B = 5;
        System.out.println(count(N, A, B));
    }
}

Python3

# Python 3 Program to implement
# the above approach
 
# Function to calculate the total
# count of N-digit numbers such
# that the sum of digits at even
# positions and odd positions are
# divisible by A and B respectively
def count(N, A, B):
 
    # For single digit numbers
    if (N == 1):
        return 9 // B + 1
   
    # Largest possible number
    max_sum = 9 * N
 
    # Count of possible odd digits
    odd_count = N // 2 + N % 2
 
    # Count of possible even digits
    even_count = N - odd_count
 
    # Calculate total count of
    # sequences of length even_count
    # with sum divisible by A where
    # first digit can be zero
    dp = [[0 for x in range (max_sum + 1)]
             for y in range (even_count)]
     
    for i in range(10):
        dp[0][i % A] += 1
 
    for i in range (1, even_count):
        for j in range (10):
            for k in range (max_sum + 1):
                if (dp[i - 1][k] > 0):
                    dp[i][(j + k) % A] += dp[i - 1][k]
 
    # Calculate total count of sequences of
    # length odd_count with sum divisible
    # by B where cannot be zero
    dp1 = [[0 for x in range (max_sum)]
              for y in range (odd_count)]
    for i in range (1, 10):
        dp1[0][i % B] += 1
 
    for i in range (1, odd_count): 
        for j in range (10):      
            for k in range (max_sum + 1):
                if (dp1[i - 1][k] > 0):
                    dp1[i][(j + k) % B] += dp1[i - 1][k]
            
    # Return their product as answer
    return dp[even_count - 1][0] * dp1[odd_count - 1][0]
 
# Driver Code
if __name__ == "__main__":
   
    N = 2
    A = 2
    B = 5
    print (count(N, A, B))
 
# This code is contributed by Chitranayal

C#

// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to calculate the total
// count of N-digit numbers such
// that the sum of digits at even
// positions and odd positions are
// divisible by A and B respectively
public static long count(int N, int A, int B)
{
     
    // For single digit numbers
    if (N == 1)
    {
        return 9 / B + 1;
    }
 
    // Largest possible number
    int max_sum = 9 * N;
 
    // Count of possible odd digits
    int odd_count = N / 2 + N % 2;
 
    // Count of possible even digits
    int even_count = N - odd_count;
 
    // Calculate total count of sequences of
    // length even_count with sum divisible
    // by A where first digit can be zero
    long [,]dp = new long[even_count, max_sum + 1];
    for(int i = 0; i <= 9; i++)
        dp[0, i % A]++;
 
    for(int i = 1; i < even_count; i++)
    {
        for(int j = 0; j <= 9; j++)
        {
            for(int k = 0; k <= max_sum; k++)
            {
                if (dp[i - 1, k] > 0)
                    dp[i, (j + k) % A] += dp[i - 1, k];
            }
        }
    }
 
    // Calculate total count of sequences of
    // length odd_count with sum divisible
    // by B where cannot be zero
    long [,]dp1 = new long[odd_count, max_sum + 1];
    for(int i = 1; i <= 9; i++)
        dp1[0, i % B]++;
 
    for(int i = 1; i < odd_count; i++)
    {
        for(int j = 0; j <= 9; j++)
        {
            for(int k = 0; k <= max_sum; k++)
            {
                if (dp1[i - 1, k] > 0)
                    dp1[i, (j + k) % B] += dp1[i - 1, k];
            }
        }
    }
     
    // Return their product as answer
    return dp[even_count - 1, 0] *
           dp1[odd_count - 1, 0];
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 2, A = 2, B = 5;
     
    Console.WriteLine(count(N, A, B));
}
}
 
// This code is contributed by 29AjayKumar
输出:
5





时间复杂度: O(N 2 )
辅助空间: O(N 2 )