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

📅  最后修改于: 2021-05-08 16:43:26             🧑  作者: 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