📌  相关文章
📜  计算三元组 (a, b, c) 使得 a + b, b + c 和 a + c 都可以被 K 整除2套

📅  最后修改于: 2021-09-07 02:06:17             🧑  作者: Mango

给定两个正整数NK ,任务是计算三元组(a, b, c) 的数量,使得0 < a, b, c < N and (a + b) , (b + c) and (c) + a)都是K 的倍数。

例子:

朴素的方法:请参阅上一篇文章,了解解决此问题的最简单方法。
时间复杂度: O(N 3 )
辅助空间: O(1)

高效的方法:上述方法也可以基于以下观察进行优化:

  • 给定条件由同余公式表示:
  • 不使用同余公式也可以观察到上述关系:
    • 因为(a + b)K的倍数,而(c + b)K的倍数。因此, (a + b) − (c + b) = a – c也是K的倍数,即a ≡ b ≡ c (mod K)
    • 因此,表达式可以进一步评估为:

根据以上观察,可以计算出以下两种情况的结果:

  • 如果K 是奇数,那么a ≡ b ≡ c ≡ 0(mod K)因为所有三个都是全等的,并且三元组的总数可以计算为(N / K) 3
  • 如果K 是偶数,则K可被2a ≡ 0 (mod K)b ≡ 0 (mod K)c ≡ 0 (mod K)整除。因此,三胞胎的总数可以计算为(N / K) 3 ((N + (K/2)) / K) 3

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to count the number of
// triplets from the range [1, N - 1]
// having sum of all pairs divisible by K
int countTriplets(int N, int K)
{
    // If K is even
    if (K % 2 == 0) {
        long long int x = N / K;
        long long int y = (N + (K / 2)) / K;
 
        return x * x * x + y * y * y;
    }
 
    // Otherwise
    else {
        long long int x = N / K;
        return x * x * x;
    }
}
 
// Driver Code
int main()
{
    int N = 2, K = 2;
    cout << countTriplets(N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to count the number of
// triplets from the range [1, N - 1]
// having sum of all pairs divisible by K
static int countTriplets(int N, int K)
{
     
    // If K is even
    if (K % 2 == 0)
    {
        int x = N / K;
        int y = (N + (K / 2)) / K;
 
        return x * x * x + y * y * y;
    }
 
    // Otherwise
    else
    {
        int x = N / K;
        return x * x * x;
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 2, K = 2;
     
    System.out.print(countTriplets(N, K));
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to count the number of
# triplets from the range [1, N - 1]
# having sum of all pairs divisible by K
def countTriplets(N, K):
 
    # If K is even
    if (K % 2 == 0):
        x = N // K
        y = (N + (K // 2)) // K
 
        return x * x * x + y * y * y
 
    # Otherwise
    else:
        x = N // K
        return x * x * x
 
# Driver Code
if __name__ == "__main__":
 
    N = 2
    K = 2
     
    print(countTriplets(N, K))
 
# This code is contributed by ukasp


Javascript


C#
// C# program for the above approach
 
using System;
 
class GFG{
 
// Function to count the number of
// triplets from the range [1, N - 1]
// having sum of all pairs divisible by K
static int countTriplets(int N, int K)
{
     
    // If K is even
    if (K % 2 == 0)
    {
        int x = N / K;
        int y = (N + (K / 2)) / K;
 
        return x * x * x + y * y * y;
    }
 
    // Otherwise
    else
    {
        int x = N / K;
        return x * x * x;
    }
}
 
// Driver Code
    static void Main() {
       int N = 2, K = 2;
     
       Console.Write(countTriplets(N, K));
    }
}
 
// This code is contributed by SoumikMondal


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live