📜  三元组的数量,以使每个值小于N并且每对和为K的倍数

📅  最后修改于: 2021-04-29 10:08:18             🧑  作者: Mango

给定两个整数NK。找出三元组(a,b,c)的数量,使0≤a ,b,c≤N并且(a + b)(b + c)(c + a)K的倍数。

例子:

方法:假设(a + b)(b + c)(c + a)K的倍数。因此,我们可以说(a + b)%K = 0(b + c)%K = 0(c + a)%K = 0
如果a属于Kx模类,则使用第一个条件, b应该在第(K – x)模类中。
从第二个条件可以看出, c属于Kx模类。现在,由于ac属于同一模类,因此它们必须满足第三种关系,即(a + c)%K = 0 。仅当x = 0x = K / 2时才有可能。
K为奇数整数时, x = K / 2无效。

因此,为了解决该问题,从0数元素到N在模类和(K / 2)模类K0的数目。

  • 如果K奇数,则结果为cnt [0] 3
  • 如果K偶数,则结果为cnt [0] 3 + cnt [K / 2] 3

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to return the number of triplets
int NoofTriplets(int N, int K)
{
    int cnt[K];
 
    // Initializing the count array
    memset(cnt, 0, sizeof(cnt));
 
    // Storing the frequency of each modulo class
    for (int i = 1; i <= N; i += 1) {
        cnt[i % K] += 1;
    }
 
    // If K is odd
    if (K & 1)
        return cnt[0] * cnt[0] * cnt[0];
 
    // If K is even
    else {
        return (cnt[0] * cnt[0] * cnt[0]
                + cnt[K / 2] * cnt[K / 2] * cnt[K / 2]);
    }
}
 
// Driver Code
int main()
{
    int N = 3, K = 2;
 
    // Function Call
    cout << NoofTriplets(N, K);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.Arrays;
 
class GFG
{
 
    // Function to return the number of triplets
    static int NoofTriplets(int N, int K)
    {
        int[] cnt = new int[K];
 
        // Initializing the count array
        Arrays.fill(cnt, 0, cnt.length, 0);
 
        // Storing the frequency of each modulo class
        for (int i = 1; i <= N; i += 1)
        {
            cnt[i % K] += 1;
        }
 
        // If K is odd
        if ((K & 1) != 0)
        {
            return cnt[0] * cnt[0] * cnt[0];
        }
        // If K is even
        else
        {
            return (cnt[0] * cnt[0] * cnt[0]
                    + cnt[K / 2] * cnt[K / 2] * cnt[K / 2]);
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int N = 3, K = 2;
 
        // Function Call
        System.out.println(NoofTriplets(N, K));
    }
}
 
// This code is contributed by Princi Singh


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the number of triplets
    static int NoofTriplets(int N, int K)
    {
        int[] cnt = new int[K];
 
        // Initializing the count array
        Array.Fill(cnt, 0, cnt.Length, 0);
 
        // Storing the frequency of each modulo class
        for (int i = 1; i <= N; i += 1)
        {
            cnt[i % K] += 1;
        }
 
        // If K is odd
        if ((K & 1) != 0)
        {
            return cnt[0] * cnt[0] * cnt[0];
        }
        // If K is even
        else
        {
            return (cnt[0] * cnt[0] * cnt[0]
                    + cnt[K / 2] * cnt[K / 2] * cnt[K / 2]);
        }
    }
 
    // Driver Code
    static public void Main ()
    {
            int N = 3, K = 2;
 
        // Function Call
        Console.Write(NoofTriplets(N, K));
    }
}
 
// This code is contributed by ajit


Python3
# Python3 implementation of the above approach
 
# Function to return the number of triplets
def NoofTriplets(N, K) :
     
    # Initializing the count array
    cnt = [0]*K;
 
    # Storing the frequency of each modulo class
    for i in range(1, N + 1) :
        cnt[i % K] += 1;
 
    # If K is odd
    if (K & 1) :
        rslt = cnt[0] * cnt[0] * cnt[0];
        return rslt
 
    # If K is even
    else :
        rslt = (cnt[0] * cnt[0] * cnt[0] +
                cnt[K // 2] * cnt[K // 2] * cnt[K // 2]);
        return rslt
 
# Driver Code
if __name__ == "__main__" :
 
    N = 3; K = 2;
 
    # Function Call
    print(NoofTriplets(N, K));
 
# This code is contributed by AnkitRai01


Javascript


输出:
9

时间复杂度: O(N)