📜  生成相等的商和余数的整数对的计数,直到 X 和 Y

📅  最后修改于: 2021-10-26 06:02:50             🧑  作者: Mango

给定两个整数XY ,任务是计算对(m, n) 的数量,使得m / n = m % n1 ≤ m ≤ x1 ≤ n ≤ y

例子:

方法:根据以下观察可以解决给定的问题:

  • 要满足条件,分子必须采用(kn + k)形式。因此, (kn + k) / n = (kn + k) % n = k。
  • 这也意味着k < n 。因此, k * k < k * n + k <= x。因此, k < sqrt(x)
  • 因此,对分子从1迭代到sqrt(x)就足够了。
  • 重写k * n + k ≤ x给我们n <= (x / k – 1) 。此外,来自约束的n > kn <= y
  • 对于每个可能的分子值,计算可能的分母值并更新总计数。

下面是上述方法的实现。

C++
// C++ Program for teh above approach
#include 
using namespace std;
 
// Function to calculate the number
// of pairs satisfying (m / n = m % n)
void countOfPairs(int x, int y)
{
    int count = 0;
 
    // Iterate from 1 to sqrt(x)
    for (int k = 1; k * k <= x; ++k) {
 
        // Combining the conditions -
        // 1) n > k
        // 2) n <= y
        // 3) n <= (x/ k -1)
        count += max(0, min(y, x / k - 1) - k);
    }
    cout << count << "\n";
}
 
// Driver code
int main()
{
    int x = 4;
    int y = 5;
    countOfPairs(x, y);
    return 0;
}


Java
// Java Program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to calculate the number
    // of pairs satisfying (m / n = m % n)
    static void countOfPairs(int x, int y)
    {
        int count = 0;
 
        // Iterate from 1 to sqrt(x)
        for (int k = 1; k * k <= x; ++k) {
 
            // Combining the conditions -
            // 1) n > k
            // 2) n <= y
            // 3) n <= (x/ k -1)
            count
                += Math.max(
                    0, Math.min(y, x / k - 1) - k);
        }
        System.out.print(count);
    }
    // Driver code
    public static void main(String[] args)
    {
        int x = 4;
        int y = 5;
        countOfPairs(x, y);
    }
}


Python3
# python 3 Program for teh above approach
from math import sqrt
 
# Function to calculate the number
# of pairs satisfying (m / n = m % n)
def countOfPairs(x, y):
    count = 0
 
    # Iterate from 1 to sqrt(x)
    for k in range(1,int(sqrt(x)) + 1, 1):
       
        # Combining the conditions -
        # 1) n > k
        # 2) n <= y
        # 3) n <= (x/ k -1)
        count += max(0, min(y, x / k - 1) - k)
    print(int(count))
 
# Driver code
if __name__ == '__main__':
    x = 4
    y = 5
    countOfPairs(x, y)
     
    # This code is contributed by bgangwar59.


C#
// C# Program for the above approach
using System;
 
public class GFG {
 
    // Function to calculate the number
    // of pairs satisfying (m / n = m % n)
    static void countOfPairs(int x, int y)
    {
        int count = 0;
 
        // Iterate from 1 to sqrt(x)
        for (int k = 1; k * k <= x; ++k) {
 
            // Combining the conditions -
            // 1) n > k
            // 2) n <= y
            // 3) n <= (x/ k -1)
            count
                += Math.Max(
                    0, Math.Min(y, x / k - 1) - k);
        }
        Console.Write(count);
    }
    // Driver Code
    static public void Main()
    {
        int x = 4;
        int y = 5;
        countOfPairs(x, y);
    }
}


Javascript


输出:
2

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