📜  来自前 N 个自然数且余数至少为 K 的对数

📅  最后修改于: 2022-05-13 01:56:05.297000             🧑  作者: Mango

来自前 N 个自然数且余数至少为 K 的对数

给定两个正整数NK ,任务是在[1, N]范围内找到对(a, b)的数量,使得a%b至少为 K

例子:

朴素方法:解决给定问题的最简单方法是在[1, N]范围内生成所有可能的对(a, b) ,如果a%b的值至少为 K ,则计算这对对。检查所有对后,打印获得的总对。

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

高效方法:上述方法也可以通过迭代范围[1, N]并固定对中的第二个数字,即b来优化。对于每个固定的b ,将有一个N/b周期,并且每个周期都可以与(b – K)个元素组合。因此,总共有(N/b)*(b – K)个元素。现在对于剩余的N%b元素,将有max(0, n%b – k + 1)对。请按照以下步骤解决问题:

  • 如果K的值为0 ,则打印N 2作为有效对的结果数。
  • 初始化变量,例如ans0 ,它存储对的结果计数。
  • 使用变量b遍历范围[K + 1, N]并执行以下步骤:
    • (N/b)*(b – K)的值添加到变量ans中。
    • (N % b – K + 1)的最大值或0添加到变量ans中。
  • 执行上述步骤后,打印ans的值作为结果对数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of pairs
// (a, b) such that a%b is at least K
int countTotalPairs(int N, int K)
{
    // Base Case
    if (K == 0) {
        return N * N;
    }
 
    // Stores resultant count of pairs
    int ans = 0;
 
    // Iterate over the range [K + 1, N]
    for (int b = K + 1; b <= N; b++) {
 
        // Find the cycled elements
        ans += (N / b) * (b - K);
 
        // Find the remaining elements
        ans += max(N % b - K + 1, 0);
    }
 
    // Return the resultant possible
    // count of pairs
    return ans;
}
 
// Driver Code
int main()
{
    int N = 5, K = 2;
    cout << countTotalPairs(N, K);
 
    return 0;
}


Java
/*package whatever //do not write package name here */
import java.io.*;
 
class GFG {
 
  // Function to count the number of pairs
  // (a, b) such that a%b is at least K
  public static int countTotalPairs(int N, int K)
  {
 
    // Base case
    if (K == 0) {
      return N * N;
    }
 
    // Stores resultant count of pairs
    int ans = 0;
 
    // Iterate over the range [K + 1, N]
    for (int i = K + 1; i <= N; i++)
    {
 
      // Find the cycled element
      ans += (N / i) * (i - K);
      if ((N % i) - K + 1 > 0)
      {
 
        // Find the remaining element
        ans += (N % i) - K + 1;
      }
    }
 
    // Return the resultant possible
    // count of pairs
    return ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 5, K = 2;
    System.out.println(countTotalPairs(N, K));
  }
}
 
// This code is contributed by maddler.


Python3
# Python program for the above approach;
 
# Function to count the number of pairs
# (a, b) such that a%b is at least K
def countTotalPairs(N, K):
    # Base Case
    if (K == 0) :
        return N * N
     
 
    # Stores resultant count of pairs
    ans = 0
 
    # Iterate over the range [K + 1, N]
    for b in range(K + 1, N + 1) :
 
        # Find the cycled elements
        ans += (N // b) * (b - K)
 
        # Find the remaining elements
        ans += max(N % b - K + 1, 0)
     
 
    # Return the resultant possible
    # count of pairs
    return ans
 
 
# Driver Code
 
N = 5
K = 2
print(countTotalPairs(N, K))
 
# This code is contributed by _saurabh_jaiswal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count the number of pairs
// (a, b) such that a%b is at least K
static int countTotalPairs(int N, int K)
{
    // Base Case
    if (K == 0) {
        return N * N;
    }
 
    // Stores resultant count of pairs
    int ans = 0;
 
    // Iterate over the range [K + 1, N]
    for (int b = K + 1; b <= N; b++) {
 
        // Find the cycled elements
        ans += (N / b) * (b - K);
 
        // Find the remaining elements
        ans += Math.Max(N % b - K + 1, 0);
    }
 
    // Return the resultant possible
    // count of pairs
    return ans;
}
 
// Driver Code
public static void Main()
{
    int N = 5, K = 2;
    Console.Write(countTotalPairs(N, K));
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
7

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