📜  商和大数的余数的程序

📅  最后修改于: 2021-05-04 22:44:55             🧑  作者: Mango

给定字符串数字并给出另一个数字(例如m)[0 <= m <= 10 ^ 18]。任务是计算给定数字的模数。
例子:

Input : num = "214"
         m = 5
Output : Remainder = 4
         Quotient = 42         

Input : num = "214755974562154868"
        m = 17
Output : Remainder = 15
         Quotient = 12632704386009109
         
Input : num = "6466868439215689498894"
        m = 277
Output : Remainder = 213  
         Quotient = 23346095448432092053         

为了找到商,我们可以在算法进行过程中打印商,也可以将这些数字存储在数组中,然后再打印。

Initialize mod = 0
First take first digit (from right) and find 
mod using formula:
mod = (mod * 10 + digit) % m
quo[i] = mod / m
where i denotes the position of quotient number

Let's take an example.
num = 12345
m = 9
Initialize mod = 0
quo[i] = (mod * 10 + num[i]) / m
mod = (mod * 10 + num[i]) % m
Where i denotes the position of the i-th digit

1) quo[1] = (0 * 10 + 1) / 9 = 0
   mod = (0 * 10 + 1) % 9 = 1
2) quo[2] = (1 * 10 + 2) / 9 = 12 / 9 = 1 
   mod = (1 * 10 + 2) % 9 = 12 % 9 = 3
3) quo[3] = (3 * 10 + 3) / 9 = 33 / 9 = 3
   mod = (3 * 10 + 3) % 9 = 33 % 9 = 6
4) quo[4] = (6 * 10 + 4) / 9 = 64 / 9 = 7
   mod = (6 * 10 + 4) % 9 = 64 % 9 = 1
5) quo[5] = (1 * 10 + 5) / 9 = 15 / 9 = 1
   mod = (1 * 10 + 5) % 9 = 15 % 9 = 6


Concatenating all values of quotient together
(from 1 to n) where n is the number of digits. 
Thus, modulus is 6 and quotient is 01371.

我们也可以使用这种技术来找到大数的商和余数。
下面是上述方法的实现:

C++
// CPP program to find quotient and remainder
// when a number is divided by large number
// represented as string.
#include 
using namespace std;
 
typedef long long ll;
 
// Function to calculate the modulus
void modBigNumber(string num, ll m)
{
    // Store the modulus of big number
    vector vec;
    ll mod = 0;
 
    // Do step by step division
    for (int i = 0; i < num.size(); i++) {
          
        int digit = num[i] - '0';
 
        // Update modulo by concatenating
        // current digit.
        mod = mod * 10 + digit;
 
        // Update quotient
        int quo = mod / m;
        vec.push_back(quo);
 
        // Update mod for next iteration.
        mod = mod % m;       
    }
 
    cout << "\nRemainder : " << mod << "\n";
 
    cout << "Quotient : ";
 
    // Flag used to remove starting zeros
    bool zeroflag = 0;
    for (int i = 0; i < vec.size(); i++) {
        if (vec[i] == 0 && zeroflag == 0)
            continue;
        zeroflag = 1;
        cout << vec[i];
    }
 
    return;
}
 
// Driver Code
int main()
{
    string num = "14598499948265358486";
    ll m = 487;
    modBigNumber(num, m);
    return 0;
}


Java
// Java program to find quotient and remainder
// when a number is divided by large number
// represented as String.
import java.util.Vector;
 
class GFG {
 
// Function to calculate the modulus
    static void modBigNumber(String num, long m) {
        // Store the modulus of big number
        Vector vec = new Vector<>();
        long mod = 0;
 
        // Do step by step division
        for (int i = 0; i < num.length(); i++) {
 
            int digit = num.charAt(i) - '0';
 
            // Update modulo by concatenating
            // current digit.
            mod = mod * 10 + digit;
 
            // Update quotient
            int quo = (int) (mod / m);
            vec.add(vec.size(), quo);
 
            // Update mod for next iteration.
            mod = mod % m;
        }
 
        System.out.print("\nRemainder : " + mod + "\n");
 
        System.out.print("Quotient : ");
 
        // Flag used to remove starting zeros
        boolean zeroflag = false;
        for (int i = 0; i < vec.size(); i++) {
            if (vec.get(i) == 0 && zeroflag == false) {
                continue;
            }
            zeroflag = true;
            System.out.print(vec.get(i));
        }
 
        return;
    }
 
// Driver Code
    public static void main(String[] args) {
 
        String num = "14598499948265358486";
        long m = 487;
        modBigNumber(num, m);
    }
}


Python3
# Python 3 program to find quotient and remainder
# when a number is divided by large number
# represented as string.
 
# Function to calculate the modulus
def modBigNumber(num, m):
    # Store the modulus of big number
    vec = []
    mod = 0
 
    # Do step by step division
    for i in range(0,len(num),1):
        digit = ord(num[i]) - ord('0')
 
        # Update modulo by concatenating
        # current digit.
        mod = mod * 10 + digit
 
        # Update quotient
        quo = int(mod / m)
        vec.append(quo)
 
        # Update mod for next iteration.
        mod = mod % m    
     
    #print("\n")
    print("Remainder :",mod)
 
    print("Quotient :",end = " ")
 
    # Flag used to remove starting zeros
    zeroflag = 0;
    for i in range(0,len(vec),1):
        if (vec[i] == 0 and zeroflag == 0):
            continue
        zeroflag = 1
        print(vec[i],end="")
 
    return
 
# Driver Code
if __name__ == '__main__':
    num = "14598499948265358486"
    m = 487
    modBigNumber(num, m)
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find quotient and remainder
// when a number is divided by large number
// represented as String.
using System;
using System.Collections.Generic;
 
class GFG {
 
// Function to calculate the modulus
    static void modBigNumber(string num, long m) {
        // Store the modulus of big number
        List vec = new List();
        long mod = 0;
 
        // Do step by step division
        for (int i = 0; i < num.Length; i++) {
 
            int digit = num[i] - '0';
 
            // Update modulo by concatenating
            // current digit.
            mod = mod * 10 + digit;
 
            // Update quotient
            int quo = (int) (mod / m);
            vec.Add(quo);
 
            // Update mod for next iteration.
            mod = mod % m;
        }
 
        Console.Write("Remainder : " + mod + "\n");
 
        Console.Write("Quotient : ");
 
        // Flag used to remove starting zeros
        bool zeroflag = false;
        for (int i = 0; i < vec.Count; i++) {
            if (vec[i] == 0 && zeroflag == false) {
                continue;
            }
            zeroflag = true;
            Console.Write(vec[i]);
        }
 
        return;
    }
 
// Driver Code
    public static void Main() {
 
        string num = "14598499948265358486";
        long m = 487;
        modBigNumber(num, m);
    }
}
// This Code is contributed by mits


PHP


Javascript


输出:

Remainder = 430
Quotient = 29976385930729688