📜  计算产品少于N的有序对

📅  最后修改于: 2021-05-08 16:18:45             🧑  作者: Mango

给定整数N。任务是计算有序对(a,b)的数量,以使a * b < N

例子:

Input: N = 5
Output: 8
Ordered Pairs are = (1, 1), (1, 2), (1, 3),
(1, 4), (2, 1), (2, 2), (3, 1), (4, 1).

Input: N = 1000
Output: 7053

天真的方法:运行两个for循环,直到N – 1并计算乘积小于N的有序对。

高效方法:让我们考虑一个有序对(a,b) 。然后,如果两个数的乘积小于ni:e a * b ,则其中至少一个必须小于n的平方根。我们可以通过矛盾证明,如果两个数字均大于n的平方根,则其乘积将不小于n。

因此,您可以将所有整数取为最大sqrt(n – 1),而不是将所有整数取为最大n。对于每个a,计算b> = a的数量,使a * b 。然后将结果乘以2,即可为看到的每对(a,b)数对(b,a)。之后,减去sqrt(n – 1)的整数部分,以确保对(a,a)被精确计数一次。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to return count of Ordered pairs
// whose product are less than N
int countOrderedPairs(int N)
{
    // Initialize count to 0
    int count_pairs = 0;
  
    // count total pairs
    for (int i = 1; i <= sqrt(N - 1); ++i) {
        for (int j = i; j * i < N; ++j)
            ++count_pairs;
    }
  
    // multiply by 2 to get ordered_pairs
    count_pairs *= 2;
  
    // subtract redundant pairs (a, b) where a==b.
    count_pairs -= int(sqrt(N - 1));
  
    // return answer
    return count_pairs;
}
  
// Driver code
int main()
{
    int N = 5;
  
    // function call to print required answer
    cout << countOrderedPairs(N);
  
    return 0;
}


Java
// Java implementation of above approach
  
class GFG{
// Function to return count of Ordered pairs
// whose product are less than N
static int countOrderedPairs(int N)
{
    // Initialize count to 0
    int count_pairs = 0;
  
    // count total pairs
    for (int i = 1; i <= (int)Math.sqrt(N - 1); ++i) {
        for (int j = i; j * i < N; ++j)
            ++count_pairs;
    }
  
    // multiply by 2 to get ordered_pairs
    count_pairs *= 2;
  
    // subtract redundant pairs (a, b) where a==b.
    count_pairs -= (int)(Math.sqrt(N - 1));
  
    // return answer
    return count_pairs;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 5;
  
    // function call to print required answer
    System.out.println(countOrderedPairs(N));
}
}
// This code is contributed by mits


Python3
# Python3 implementation of above approach
  
from math import sqrt
# Function to return count of Ordered pairs
# whose product are less than N
def countOrderedPairs(N):
    # Initialize count to 0
    count_pairs = 0
  
    # count total pairs
    p = int(sqrt(N-1)) + 1
    q = int(sqrt(N))+2
    for i in range(1,p,1):
        for j in range(i,q,1):
            count_pairs += 1
      
    # multiply by 2 to get ordered_pairs
    count_pairs *= 2
  
    # subtract redundant pairs (a, b) where a==b.
    count_pairs -= int(sqrt(N - 1))
  
    # return answer
    return count_pairs
  
# Driver code
if __name__ == '__main__':
    N = 5
  
    # function call to print required answer
    print(countOrderedPairs(N))
  
# This code is contributed by
# Surendra_Gangwar


C#
//C# implementation of above approach 
  
using System;
  
public class GFG{
    // Function to return count of Ordered pairs 
// whose product are less than N 
static int countOrderedPairs(int N) 
{ 
    // Initialize count to 0 
    int count_pairs = 0; 
  
    // count total pairs 
    for (int i = 1; i <= (int)Math.Sqrt(N - 1); ++i) { 
        for (int j = i; j * i < N; ++j) 
            ++count_pairs; 
    } 
  
    // multiply by 2 to get ordered_pairs 
    count_pairs *= 2; 
  
    // subtract redundant pairs (a, b) where a==b. 
    count_pairs -= (int)(Math.Sqrt(N - 1)); 
  
    // return answer 
    return count_pairs; 
} 
  
// Driver code 
    static public void Main (){
      
    int N = 5; 
    // function call to print required answer 
    Console.WriteLine(countOrderedPairs(N)); 
} 
} 
// This code is contributed by Sachin.


PHP


输出:
8

时间复杂度: O(N * sqrt(N))