📜  在给定的大正整数乘积中找到缺失的数字

📅  最后修改于: 2022-05-13 01:56:10.630000             🧑  作者: Mango

在给定的大正整数乘积中找到缺失的数字

给定字符串AB形式的两个大整数,以及它们的乘积也是字符串C的形式,使得乘积的一位数字被X替换,任务是在乘积C中找到被替换的数字。

例子:

朴素方法:解决给定问题的最简单方法是使用本文中讨论的方法找到两个大整数 A 和 B 的乘积,然后将其与 C 进行比较以找到结果缺失的数字X

时间复杂度: O((log 10 A)*(log 10 B))
辅助空间: O(log 10 A + log 10 B)

有效的方法:上述方法也可以通过使用以下观察进行优化:

因此,根据上述等式, A * B = C可以转换为(A % 11) * (B % 11) = (C % 11) ,其中等式左侧为常数,右侧为手边将是 1 个变量X的方程,可以求解该方程以找到X的值。在对11进行模运算后, X可能有一个负值,在这种情况下,请考虑X的正值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the replaced digit
// in the product of a*b
int findMissingDigit(string a, string b,
                     string c)
{
    // Keeps track of the sign of the
    // current digit
    int w = 1;
 
    // Stores the value of a % 11
    int a_mod_11 = 0;
 
    // Find the value of a mod 11 for
    // large value of a as per the
    // derived formula
    for (int i = a.size() - 1; i >= 0; i--) {
        a_mod_11 = (a_mod_11 + w * (a[i] - '0')) % 11;
        w = w * -1;
    }
 
    // Stores the value of b % 11
    int b_mod_11 = 0;
    w = 1;
 
    // Find the value of b mod 11 for
    // large value of a as per the
    // derived formula
    for (int i = b.size() - 1;
         i >= 0; i--) {
 
        b_mod_11 = (b_mod_11
                    + w * (b[i] - '0'))
                   % 11;
        w = w * -1;
    }
 
    // Stores the value of c % 11
    int c_mod_11 = 0;
 
    // Keeps track of the sign of x
    bool xSignIsPositive = true;
    w = 1;
 
    for (int i = c.size() - 1; i >= 0; i--) {
 
        // If the current digit is the
        // missing digit, then keep
        // the track of its sign
        if (c[i] == 'x') {
            xSignIsPositive = (w == 1);
        }
        else {
            c_mod_11 = (c_mod_11
                        + w * (c[i] - '0'))
                       % 11;
        }
        w = w * -1;
    }
 
    // Find the value of x using
    // the derived equation
    int x = ((a_mod_11 * b_mod_11)
             - c_mod_11)
            % 11;
 
    // Check if x has a negative sign
    if (!xSignIsPositive) {
        x = -x;
    }
 
    // Return positive equivaluent
    // of x mod 11
    return (x % 11 + 11) % 11;
}
 
// Driver Code
int main()
{
    string A = "123456789";
    string B = "987654321";
    string C = "12193263111x635269";
    cout << findMissingDigit(A, B, C);
 
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
    // Function to find the replaced digit
    // in the product of a*b
    public static int findMissingDigit(String a, String b, String c)
    {
       
        // Keeps track of the sign of the
        // current digit
        int w = 1;
 
        // Stores the value of a % 11
        int a_mod_11 = 0;
 
        // Find the value of a mod 11 for
        // large value of a as per the
        // derived formula
        for (int i = a.length() - 1; i >= 0; i--) {
            a_mod_11 = (a_mod_11 + w * (a.charAt(i) - '0')) % 11;
            w = w * -1;
        }
 
        // Stores the value of b % 11
        int b_mod_11 = 0;
        w = 1;
 
        // Find the value of b mod 11 for
        // large value of a as per the
        // derived formula
        for (int i = b.length() - 1; i >= 0; i--) {
 
            b_mod_11 = (b_mod_11 + w * (b.charAt(i) - '0')) % 11;
            w = w * -1;
        }
 
        // Stores the value of c % 11
        int c_mod_11 = 0;
 
        // Keeps track of the sign of x
        boolean xSignIsPositive = true;
        w = 1;
 
        for (int i = c.length() - 1; i >= 0; i--) {
 
            // If the current digit is the
            // missing digit, then keep
            // the track of its sign
            if (c.charAt(i) == 'x') {
                xSignIsPositive = (w == 1);
            } else {
                c_mod_11 = (c_mod_11 + w * (c.charAt(i) - '0')) % 11;
            }
            w = w * -1;
        }
 
        // Find the value of x using
        // the derived equation
        int x = ((a_mod_11 * b_mod_11) - c_mod_11) % 11;
 
        // Check if x has a negative sign
        if (!xSignIsPositive) {
            x = -x;
        }
 
        // Return positive equivaluent
        // of x mod 11
        return (x % 11 + 11) % 11;
    }
 
    // Driver Code
    public static void main(String args[]) {
        String A = "123456789";
        String B = "987654321";
        String C = "12193263111x635269";
        System.out.println(findMissingDigit(A, B, C));
 
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python3 Program to implement the above approach
 
# Function to find the replaced digit
# in the product of a*b
def findMissingDigit(a, b, c):
   
    # Keeps track of the sign of the
    # current digit
    w = 1
 
    # Stores the value of a % 11
    a_mod_11 = 0
 
    # Find the value of a mod 11 for
    # large value of a as per the
    # derived formula
    for i in range(len(a) - 1, -1, -1):
        a_mod_11 = (a_mod_11 + w * (ord(a[i]) - ord('0'))) % 11
        w = w * -1
 
    # Stores the value of b % 11
    b_mod_11 = 0
    w = 1
 
    # Find the value of b mod 11 for
    # large value of a as per the
    # derived formula
    for i in range(len(b) - 1, -1, -1):
        b_mod_11 = (b_mod_11 + w * (ord(b[i]) - ord('0'))) % 11
        w = w * -1
 
    # Stores the value of c % 11
    c_mod_11 = 0
 
    # Keeps track of the sign of x
    xSignIsPositive = True
    w = 1
 
    for i in range(len(c) - 1, -1, -1):
        # If the current digit is the
        # missing digit, then keep
        # the track of its sign
        if (c[i] == 'x'):
            xSignIsPositive = (w == 1)
        else:
            c_mod_11 = (c_mod_11 + w * (ord(c[i]) - ord('0'))) % 11
        w = w * -1
 
    # Find the value of x using
    # the derived equation
    x = ((a_mod_11 * b_mod_11) - c_mod_11) % 11
 
    # Check if x has a negative sign
    if (not xSignIsPositive):
        x = -x
 
    # Return positive equivaluent
    # of x mod 11
    return (x % 11 + 11) % 11
 
A = "123456789"
B = "987654321"
C = "12193263111x635269"
print(findMissingDigit(A, B, C))
 
# This code is contributed by divyeshrabadiya07.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the replaced digit
// in the product of a*b
static int findMissingDigit(string a, string b,
                     string c)
{
   
    // Keeps track of the sign of the
    // current digit
    int w = 1;
 
    // Stores the value of a % 11
    int a_mod_11 = 0;
 
    // Find the value of a mod 11 for
    // large value of a as per the
    // derived formula
    for (int i = a.Length - 1; i >= 0; i--) {
        a_mod_11 = (a_mod_11 + w * ((int)a[i] - 48)) % 11;
        w = w * -1;
    }
 
    // Stores the value of b % 11
    int b_mod_11 = 0;
    w = 1;
 
    // Find the value of b mod 11 for
    // large value of a as per the
    // derived formula
    for (int i = b.Length - 1;
         i >= 0; i--) {
 
        b_mod_11 = (b_mod_11
                    + w * ((int)b[i] - 48))
                   % 11;
        w = w * -1;
    }
 
    // Stores the value of c % 11
    int c_mod_11 = 0;
 
    // Keeps track of the sign of x
    bool xSignIsPositive = true;
    w = 1;
 
    for (int i = c.Length - 1; i >= 0; i--) {
 
        // If the current digit is the
        // missing digit, then keep
        // the track of its sign
        if (c[i] == 'x') {
            xSignIsPositive = (w == 1);
        }
        else {
            c_mod_11 = (c_mod_11
                        + w * ((int)c[i] - '0'))
                       % 11;
        }
        w = w * -1;
    }
 
    // Find the value of x using
    // the derived equation
    int x = ((a_mod_11 * b_mod_11)
             - c_mod_11)
            % 11;
 
    // Check if x has a negative sign
    if (xSignIsPositive == false) {
        x = -x;
    }
 
    // Return positive equivaluent
    // of x mod 11
    return (x % 11 + 11) % 11;
}
 
// Driver Code
public static void Main()
{
    string A = "123456789";
    string B = "987654321";
    string C = "12193263111x635269";
    Console.Write(findMissingDigit(A, B, C));
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
2

时间复杂度: O(log 10 A + log 10 B)
辅助空间: O(1)