📜  前N个自然数的对数,其和可被K整除

📅  最后修改于: 2021-04-26 10:25:37             🧑  作者: Mango

给定NK的整数值。任务是从自然数的集合中找到对数,该对数直到N {1、2、3……N-1,N},其和可被K整除。
注意: 1 <= K <= N <= 10 ^ 6。
例子:

简单方法:一个简单的方法是使用嵌套循环并检查所有可能的对及其除以K。这种方法的时间复杂度为O(N ^ 2),效率不高。
高效的方法:一种有效的方法是使用基本的哈希技术。
首先,创建数组rem [K],其中rem [i]包含从1到N的整数,当除以K时将给出余数i。rem [i]可以通过公式rem [i] =(N – i)/ K + 1。
其次,如果满足以下条件,则两个整数的和可被K整除:

  • 两个整数都可以被K整除。其计数由rem [0] *(rem [0] -1)/ 2计算。
  • 第一个整数的余数为R,其他数的余数为KR。其计数由rem [R] * rem [KR]计算,其中R在1到K / 2之间变化。
  • K是偶数,其余均为K / 2。其计数由rem [K / 2] *(rem [K / 2] -1)/ 2计算。

所有这些情况的计数之和给出了所需的对数,以使它们的和可被K整除。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to  find  the number of pairs
// from the set of natural numbers up to
// N whose sum is divisible by K
int findPairCount(int N, int K)
{
    int count = 0;
 
    // Declaring a Hash to store count
    int rem[K];
 
    rem[0] = N / K;
 
    // Storing the count of integers with
    // a specific remainder in Hash array
    for (int i = 1; i < K; i++)
        rem[i] = (N - i) / K + 1;
 
    // Check if K is even
    if (K % 2 == 0) {
        // Count of pairs when both
        // integers are divisible by K
        count += (rem[0] * (rem[0] - 1)) / 2;
 
        // Count of pairs when one remainder
        // is R and other remainder is K - R
        for (int i = 1; i < K / 2; i++)
            count += rem[i] * rem[K - i];
 
        // Count of pairs when both the
        // remainders are K / 2
        count += (rem[K / 2] * (rem[K / 2] - 1)) / 2;
    }
    else {
        // Count of pairs when both
        // integers are divisible by K
        count += (rem[0] * (rem[0] - 1)) / 2;
 
        // Count of pairs when one remainder is R
        // and other remainder is K - R
        for (int i = 1; i <= K / 2; i++)
            count += rem[i] * rem[K - i];
    }
 
    return count;
}
 
// Driver code
int main()
{
    int N = 10, K = 4;
 
    // Print the count of pairs
    cout << findPairCount(N, K);
 
    return 0;
}


Java
// Java implementation of the approach
class GfG
{
 
// Function to find the number of pairs
// from the set of natural numbers up to
// N whose sum is divisible by K
static int findPairCount(int N, int K)
{
    int count = 0;
 
    // Declaring a Hash to store count
    int rem[] = new int[K];
 
    rem[0] = N / K;
 
    // Storing the count of integers with
    // a specific remainder in Hash array
    for (int i = 1; i < K; i++)
        rem[i] = (N - i) / K + 1;
 
    // Check if K is even
    if (K % 2 == 0)
    {
        // Count of pairs when both
        // integers are divisible by K
        count += (rem[0] * (rem[0] - 1)) / 2;
 
        // Count of pairs when one remainder
        // is R and other remainder is K - R
        for (int i = 1; i < K / 2; i++)
            count += rem[i] * rem[K - i];
 
        // Count of pairs when both the
        // remainders are K / 2
        count += (rem[K / 2] * (rem[K / 2] - 1)) / 2;
    }
    else
    {
        // Count of pairs when both
        // integers are divisible by K
        count += (rem[0] * (rem[0] - 1)) / 2;
 
        // Count of pairs when one remainder is R
        // and other remainder is K - R
        for (int i = 1; i <= K / 2; i++)
            count += rem[i] * rem[K - i];
    }
 
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 10, K = 4;
 
    // Print the count of pairs
    System.out.println(findPairCount(N, K));
}
}
 
// This code is contributed by Prerna Saini


Python3
# Python3 implementation of the approach
 
# Function to find the number of pairs
# from the set of natural numbers up to
# N whose sum is divisible by K
def findPairCount(N, K) :
    count = 0;
     
    # Declaring a Hash to store count
    rem = [0] * K;
     
    rem[0] = N // K;
     
    # Storing the count of integers with
    # a specific remainder in Hash array
    for i in range(1, K) :
        rem[i] = (N - i) // K + 1;
         
    # Check if K is even
    if (K % 2 == 0) :
         
        # Count of pairs when both
        # integers are divisible by K
        count += (rem[0] * (rem[0] - 1)) // 2;
         
        # Count of pairs when one remainder
        # is R and other remainder is K - R
        for i in range(1, K // 2) :
            count += rem[i] * rem[K - i];
         
        # Count of pairs when both the
        # remainders are K / 2
        count += (rem[K // 2] * (rem[K // 2] - 1)) // 2;
         
    else :
         
        # Count of pairs when both
        # integers are divisible by K
        count += (rem[0] * (rem[0] - 1)) // 2;
         
        # Count of pairs when one remainder is R
        # and other remainder is K - R
        for i in rage(1, K//2 + 1) :
            count += rem[i] * rem[K - i];
     
    return count;
 
# Driver code
if __name__ == "__main__" :
 
    N = 10 ; K = 4;
 
    # Print the count of pairs
    print(findPairCount(N, K));
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
class GfG
{
 
// Function to find the number of pairs
// from the set of natural numbers up to
// N whose sum is divisible by K
static int findPairCount(int N, int K)
{
    int count = 0;
 
    // Declaring a Hash to store count
    int[] rem = new int[K];
 
    rem[0] = N / K;
 
    // Storing the count of integers with
    // a specific remainder in Hash array
    for (int i = 1; i < K; i++)
        rem[i] = (N - i) / K + 1;
 
    // Check if K is even
    if (K % 2 == 0)
    {
        // Count of pairs when both
        // integers are divisible by K
        count += (rem[0] * (rem[0] - 1)) / 2;
 
        // Count of pairs when one remainder
        // is R and other remainder is K - R
        for (int i = 1; i < K / 2; i++)
            count += rem[i] * rem[K - i];
 
        // Count of pairs when both the
        // remainders are K / 2
        count += (rem[K / 2] * (rem[K / 2] - 1)) / 2;
    }
    else
    {
        // Count of pairs when both
        // integers are divisible by K
        count += (rem[0] * (rem[0] - 1)) / 2;
 
        // Count of pairs when one remainder is R
        // and other remainder is K - R
        for (int i = 1; i <= K / 2; i++)
            count += rem[i] * rem[K - i];
    }
 
    return count;
}
 
// Driver code
static void Main()
{
    int N = 10, K = 4;
 
    // Print the count of pairs
    System.Console.WriteLine(findPairCount(N, K));
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
10

时间复杂度:O(K)。