给定两个整数N和K。找出三元组(a,b,c)的数量,使0≤a ,b,c≤N并且(a + b) , (b + c)和(c + a)是K的倍数。
例子:
Input: N = 3, K = 2
Output: 9
Triplets possible are:
{(1, 1, 1), (1, 1, 3), (1, 3, 1)
(1, 3, 3), (2, 2, 2), (3, 1, 1)
(3, 1, 1), (3, 1, 3), (3, 3, 3)}
Input: N = 5, K = 3
Output: 1
Only possible triplet is (3, 3, 3)
方法:假设(a + b) , (b + c)和(c + a)是K的倍数。因此,我们可以说(a + b)%K = 0 , (b + c)%K = 0和(c + a)%K = 0 。
如果a属于K的x模类,则使用第一个条件, b应该在第(K – x)模类中。
从第二个条件可以看出, c属于K的x模类。现在,由于a和c属于同一模类,因此它们必须满足第三种关系,即(a + c)%K = 0 。仅当x = 0或x = K / 2时才有可能。
当K为奇数整数时, x = K / 2无效。
因此,为了解决该问题,从0数元素到N的第在模类和(K / 2)个模类K的0的数目。
- 如果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)