📜  非常大数 N 模 M 的因子数,其中 M 是任何素数

📅  最后修改于: 2021-09-02 07:39:25             🧑  作者: Mango

给定一个大数 N,任务是找到数 N 模 M 的因子总数,其中 M 是任何素数。
例子:

推荐:在继续解决方案之前,请先在{IDE}上尝试您的方法。

数的因数的定义:
在数学中,整数 N 的因数也称为 N 的除数,是一个整数 M,它可以乘以某个整数以产生 N。
任何数都可以写成:

其中 P1、P2、P3…Pn 是不同的素数,A1、A2、A3…An 是相应素数出现的次数。
给定数的因子总数的一般公式为:

其中 A1, A2, A3, … An 是 N 的不同质因数的计数。
这里不能使用 Sieve 的实现来找到大量的质因数分解,因为它需要比例空间。
方法:

  1. 计算次数2是给定数 N 的因数。
  2. 3迭代到√(N)以找到素数除以特定数的次数,该数每次减少N / i
  3. 将数字N除以其相应的最小素因数,直到N 变为 1
  4. 使用公式求该数的因数

下面是上述方法的实现。

C++
// C++ implementation to find the
// Number of factors of very
// large number N modulo M
 
#include 
using namespace std;
 
#define ll long long
ll mod = 1000000007;
 
// Function for modular
// multiplication
ll mult(ll a, ll b)
{
    return ((a % mod) *
        (b % mod)) % mod;
}
 
// Function to find the number
// of factors of large Number N
ll calculate_factors(ll n)
{
    ll ans, cnt;
    cnt = 0;
    ans = 1;
     
    // Count the number of times
    // 2 divides the number N
    while (n % 2 == 0) {
        cnt++;
        n = n / 2;
    }
     
    // Condition to check
    // if 2 divides it
    if (cnt) {
        ans = mult(ans, (cnt + 1));
    }
     
    // Check for all the possible
    // numbers that can divide it
    for (int i = 3; i <= sqrt(n);
                         i += 2) {
        cnt = 0;
         
        // Loop to check the number
        // of times prime number
        // i divides it
        while (n % i == 0) {
            cnt++;
            n = n / i;
        }
         
        // Condition to check if
        // prime number i divides it
        if (cnt) {
            ans = mult(ans, (cnt + 1));
        }
    }
    // Condition to check if N
    // at the end is a prime number.
    if (n > 2) {
        ans = mult(ans, (2));
    }
    return ans % mod;
}
 
// Driver Code
int main()
{
    ll n = 193748576239475639;
    mod = 17;
 
    cout << calculate_factors(n) << endl;
 
    return 0;
}


Java
// Java implementation to find the
// Number of factors of very
// large number N modulo M
class GFG{
  
static long  mod = 1000000007L;
  
// Function for modular
// multiplication
static long  mult(long  a, long  b)
{
    return ((a % mod) *
        (b % mod)) % mod;
}
  
// Function to find the number
// of factors of large Number N
static long  calculate_factors(long  n)
{
    long  ans, cnt;
    cnt = 0;
    ans = 1;
      
    // Count the number of times
    // 2 divides the number N
    while (n % 2 == 0) {
        cnt++;
        n = n / 2;
    }
      
    // Condition to check
    // if 2 divides it
    if (cnt % 2 == 1) {
        ans = mult(ans, (cnt + 1));
    }
      
    // Check for all the possible
    // numbers that can divide it
    for (int i = 3; i <= Math.sqrt(n);
                         i += 2) {
        cnt = 0;
          
        // Loop to check the number
        // of times prime number
        // i divides it
        while (n % i == 0) {
            cnt++;
            n = n / i;
        }
          
        // Condition to check if
        // prime number i divides it
        if (cnt % 2 == 1) {
            ans = mult(ans, (cnt + 1));
        }
    }
    // Condition to check if N
    // at the end is a prime number.
    if (n > 2) {
        ans = mult(ans, (2));
    }
    return ans % mod;
}
  
// Driver Code
public static void main(String[] args)
{
    long  n = 193748576239475639L;
    mod = 17;
  
    System.out.print(calculate_factors(n) +"\n");
}
}
 
// This code is contributed by sapnasingh4991


Python 3
# Python 3 implementation to find the
# Number of factors of very
# large number N modulo M
from math import sqrt
 
mod = 1000000007
 
# Function for modular
# multiplication
def mult(a, b):
    return ((a % mod) * (b % mod)) % mod
 
# Function to find the number
# of factors of large Number N
def calculate_factors(n):
    cnt = 0
    ans = 1
     
    # Count the number of times
    # 2 divides the number N
    while (n % 2 == 0):
        cnt += 1
        n = n // 2
     
    # Condition to check
    # if 2 divides it
    if (cnt):
        ans = mult(ans, (cnt + 1))
     
    # Check for all the possible
    # numbers that can divide it
    for i in range(3, int(sqrt(n)), 2):
        cnt = 0
         
        # Loop to check the number
        # of times prime number
        # i divides it
        while (n % i == 0):
            cnt += 1
            n = n // i
         
        # Condition to check if
        # prime number i divides it
        if (cnt):
            ans = mult(ans, (cnt + 1))
 
    # Condition to check if N
    # at the end is a prime number.
    if (n > 2):
        ans = mult(ans, 2)
    return ans % mod
 
# Driver Code
if __name__ == '__main__':
    n = 19374857
    mod = 17
 
    print(calculate_factors(n))
 
# This code is contributed by Surendra_Gangwar


C#
// C# implementation to find the
// Number of factors of very
// large number N modulo M
using System;
 
class GFG{
   
static long  mod = 1000000007L;
   
// Function for modular
// multiplication
static long  mult(long  a, long  b)
{
    return ((a % mod) *
        (b % mod)) % mod;
}
   
// Function to find the number
// of factors of large Number N
static long  calculate_factors(long  n)
{
    long  ans, cnt;
    cnt = 0;
    ans = 1;
       
    // Count the number of times
    // 2 divides the number N
    while (n % 2 == 0) {
        cnt++;
        n = n / 2;
    }
       
    // Condition to check
    // if 2 divides it
    if (cnt % 2 == 1) {
        ans = mult(ans, (cnt + 1));
    }
       
    // Check for all the possible
    // numbers that can divide it
    for (int i = 3; i <= Math.Sqrt(n);
                         i += 2) {
        cnt = 0;
           
        // Loop to check the number
        // of times prime number
        // i divides it
        while (n % i == 0) {
            cnt++;
            n = n / i;
        }
           
        // Condition to check if
        // prime number i divides it
        if (cnt % 2 == 1) {
            ans = mult(ans, (cnt + 1));
        }
    }
 
    // Condition to check if N
    // at the end is a prime number.
    if (n > 2) {
        ans = mult(ans, (2));
    }
    return ans % mod;
}
   
// Driver Code
public static void Main(String[] args)
{
    long  n = 193748576239475639L;
    mod = 17;
   
    Console.Write(calculate_factors(n) +"\n");
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
8

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live