📜  计算除数和余数的除数

📅  最后修改于: 2021-04-23 06:41:17             🧑  作者: Mango

给定一个正整数N,任务是找到数m,从而当数N除以M,商等于它的余数,即(⌊N/M⌋= N mod M)表示,其中所有的计数⌊ ⌋表示给定数字的底值。

例子:

原始的方法:最简单的方法是基于这样的事实的M不能多于N为任何大于N,商将是零。而其与N的模将始终为N。因此,迭代从1到N的所有数字,并对满足给定条件的所有此类数字进行计数。

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

高效方法:最佳想法是基于以下陈述的观察结果:

观察证明:

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

  • 查找N的所有除数,并将它们存储在数组中。这可以以O(√N)的复杂度来计算。
  • 通过遍历数组检查所有除数,如果除数减1满足给定条件⌊N/M⌋= N mod M ,则增加计数。

下面是上述方法的实现:

C++
// C++ program of the above approach
 
#include 
using namespace std;
 
// Function to calculate the
// count of numbers such that it
// gives same quotient and remainder
void countDivisors(long long int n)
{
 
    int count = 0;
 
    // Stores divisor of number N.
    vector divisor;
 
    // Iterate through numbers from 2
    // to sqrt(N) and store divisors of N
    for (int i = 2; i <= sqrt(n); i++) {
 
        if (n % i == 0) {
 
            if (n / i == i)
                divisor.push_back(i);
 
            else {
 
                divisor.push_back(i);
                divisor.push_back(n / i);
            }
        }
    }
 
    // As N is also divisor of itself
    divisor.push_back(n);
 
    // Iterate through divisors
    for (auto x : divisor) {
        x -= 1;
 
        // Checking whether x satisfies the
        // required condition
        if ((n / x) == (n % x))
            count++;
    }
 
    // Print the count
    cout << count;
}
 
// Driver Code
int main()
{
    // Given N
    long long int N = 1000000000000;
 
    // Function Call
    countDivisors(N);
 
    return 0;
}


Java
// Java program of the above approach
class GFG {
     
    // Function to calculate the
    // count of numbers such that it
    // gives same quotient and remainder
    static void countDivisors(int n)
    {   
        int count = 0;
        int j = 0;
     
        // Stores divisor of number N.
        int divisor[] =  new int[n];
     
        // Iterate through numbers from 2
        // to sqrt(N) and store divisors of N
        for (int i = 2; i <= Math.sqrt(n); i++) {   
            if (n % i == 0) {
                if (n / i == i){
                    divisor[j] = i;
                     j += 1;
                }
                else {
                    divisor[j] = i;
                    divisor[j + 1] = n / i;                   
                    j += 2;
                }
            }
        }
     
        // As N is also divisor of itself
        divisor[j] = n;
     
        // Iterate through divisors
        for (int i = 0; i <= j; i++)
        {
            int x = divisor[i];
            x -= 1;
     
            // Checking whether x satisfies the
            // required condition
            if ((n / x) == (n % x))
                count++;
        }
     
        // Print the count
        System.out.print(count);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
       
        // Given N
        int N = 10000000;
     
        // Function Call
        countDivisors(N);
    }
}
 
// This code is contributed by AnkThon


Python3
# Python3 program of the above approach
from math import floor, sqrt
 
# Function to calculate the
# count of numbers such that it
# gives same quotient and remainder
def countDivisors(n):
     
    count = 0
 
    # Stores divisor of number N.
    divisor = []
 
    # Iterate through numbers from 2
    # to sqrt(N) and store divisors of N
    for i in range(2, floor(sqrt(n))):
        if (n % i == 0):
            if (n // i == i):
                divisor.append(i)
            else:
                divisor.append(i)
                divisor.append(n // i)
 
    # As N is also divisor of itself
    divisor.append(n)
 
    # Iterate through divisors
    for x in divisor:
        x -= 1
 
        # Checking whether x satisfies the
        # required condition
        if ((n // x) == (n % x)):
            count += 1
 
    # Print the count
    print(count)
 
# Driver Code
if __name__ == '__main__':
     
    # Given N
    N = 1000000000000
 
    # Function Call
    countDivisors(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program of the above approach
using System;
class GFG {
     
    // Function to calculate the
    // count of numbers such that it
    // gives same quotient and remainder
    static void countDivisors(int n)
    {   
        int count = 0;
        int j = 0;
     
        // Stores divisor of number N.
        int []divisor =  new int[n];
     
        // Iterate through numbers from 2
        // to sqrt(N) and store divisors of N
        for (int i = 2; i <= Math.Sqrt(n); i++) {   
            if (n % i == 0) {
                if (n / i == i){
                    divisor[j] = i;
                     j += 1;
                }
                else {
                    divisor[j] = i;
                    divisor[j + 1] = n / i;                   
                    j += 2;
                }
            }
        }
     
        // As N is also divisor of itself
        divisor[j] = n;
     
        // Iterate through divisors
        for (int i = 0; i <= j; i++)
        {
            int x = divisor[i];
            x -= 1;
     
            // Checking whether x satisfies the
            // required condition
            if ((n / x) == (n % x))
                count++;
        }
     
        // Print the count
        Console.Write(count);
    }
     
    // Driver Code
    public static void Main(String[] args)
    {
       
        // Given N
        int N = 10000000;
     
        // Function Call
        countDivisors(N);
    }
}
 
// This code contributed by shikhasingrajput


输出:
84

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