📜  具有整数相交点的线对数

📅  最后修改于: 2021-04-26 06:23:38             🧑  作者: Mango

给定两个整数数组P []Q [] ,其中每个0 <= i 0 <= j p iq j表示线方程x – y = -p ix + y = q j 。任务是从具有整数相交点的P []Q []中查找对数。

例子:

方法:

  • 通过求解两个方程并分析整数相交点的条件,可以轻松解决该问题。
  • 这两个方程是x – y = -px + y = q
  • 求解xy ,得到x =(qp)/ 2y =(p + q)/ 2
  • 很明显,当且仅当pq具有相同的奇偶校验时,整数交点才是可能的。
  • p 0p 1分别为偶数和奇数p i的数目。
  • 类似地, q 0q 1分别表示偶数和奇数q i的数量。
  • 因此,所需的答案是p 0 * q 0 + p 1 * q 1

下面是上述方法的实现:

CPP
// C++ program to Number of pairs of lines
// having integer intersection points
  
#include 
using namespace std;
  
// Count number of pairs of lines
// having integer intersection point
int countPairs(int* P, int* Q, int N, int M)
{
    // Initialize arrays to store counts
    int A[2] = { 0 }, B[2] = { 0 };
  
    // Count number of odd and even Pi
    for (int i = 0; i < N; i++)
        A[P[i] % 2]++;
  
    // Count number of odd and even Qi
    for (int i = 0; i < M; i++)
        B[Q[i] % 2]++;
  
    // Return the count of pairs
    return (A[0] * B[0] + A[1] * B[1]);
}
  
// Driver code
int main()
{
    int P[] = { 1, 3, 2 }, Q[] = { 3, 0 };
    int N = sizeof(P) / sizeof(P[0]);
    int M = sizeof(Q) / sizeof(Q[0]);
  
    cout << countPairs(P, Q, N, M);
  
    return 0;
}


Java
// Java program to Number of pairs of lines
// having integer intersection points
class GFG
{
  
// Count number of pairs of lines
// having integer intersection point
static int countPairs(int []P, int []Q, 
                      int N, int M)
{
    // Initialize arrays to store counts
    int []A = new int[2], B = new int[2];
  
    // Count number of odd and even Pi
    for (int i = 0; i < N; i++)
        A[P[i] % 2]++;
  
    // Count number of odd and even Qi
    for (int i = 0; i < M; i++)
        B[Q[i] % 2]++;
  
    // Return the count of pairs
    return (A[0] * B[0] + A[1] * B[1]);
}
  
// Driver code
public static void main(String[] args)
{
    int []P = { 1, 3, 2 };
    int []Q = { 3, 0 };
    int N = P.length;
    int M = Q.length;
  
    System.out.print(countPairs(P, Q, N, M));
}
}
  
// This code is contributed by Rajput-Ji


Python
# Python3 program to Number of pairs of lines
# having eger ersection pos
  
# Count number of pairs of lines
# having eger ersection po
def countPairs(P, Q, N, M):
      
    # Initialize arrays to store counts
    A = [0] * 2
    B = [0] * 2
  
    # Count number of odd and even Pi
    for i in range(N):
        A[P[i] % 2] += 1
  
    # Count number of odd and even Qi
    for i in range(M):
        B[Q[i] % 2] += 1
  
    # Return the count of pairs
    return (A[0] * B[0] + A[1] * B[1])
  
# Driver code
  
P = [1, 3, 2]
Q = [3, 0]
N = len(P)
M = len(Q)
  
print(countPairs(P, Q, N, M))
  
# This code is contributed by mohit kumar 29


C#
// C# program to Number of pairs of lines
// having integer intersection points
using System;
  
class GFG
{
      
    // Count number of pairs of lines
    // having integer intersection point
    static int countPairs(int []P, int []Q, 
                        int N, int M)
    {
        // Initialize arrays to store counts
        int []A = new int[2];
        int []B = new int[2];
      
        // Count number of odd and even Pi
        for (int i = 0; i < N; i++)
            A[P[i] % 2]++;
      
        // Count number of odd and even Qi
        for (int i = 0; i < M; i++)
            B[Q[i] % 2]++;
      
        // Return the count of pairs
        return (A[0] * B[0] + A[1] * B[1]);
    }
      
    // Driver code
    public static void Main()
    {
        int []P = { 1, 3, 2 };
        int []Q = { 3, 0 };
        int N = P.Length;
        int M = Q.Length;
      
        Console.Write(countPairs(P, Q, N, M));
    }
}
  
// This code is contributed by AnkitRai01


输出:
3

时间复杂度: O(P + Q)