Count pairs
Practice
2.9 (17 votes)
Sorting
Hashmap
Implementation
Hash tables
Data structures
Basics of hash tables
Hashing
Math
Observation
Problem
88% Success 5373 Attempts 30 Points 1.5s Time Limit 256MB Memory 1024 KB Max Code

You are given an array $$A$$ consisting of $$N$$ non-negative integers. You are also given 2 integers $$p$$(a prime number) and $$k$$. You are required to count number of pairs $$(i, j)$$ where, $$1 \leq i < j \leq N$$ and satisying: 

                                                     $$ (A_i^2 + A_j^2 + A_i*A_j) \text{ mod }p = k$$ 

where \(a \text{ mod } p = b\) means that \(b\) is the remainder when \(a\) is divided by \(p\). In particular, \(0 \leq b < p\).

You are given $$T$$ test cases.

Input format

  • The first line contains a single integer $$T$$ denoting the number of test cases.
  • For each test case:
    • The first line contains three space-separated integers $$N$$, $$k$$, and $$p$$ denoting the length of the array, the required remainder/modulo, and the prime number respectively.
    • The second line contains $$N$$ space-separated integers denoting the integer array $$A$$.

Output format

For each test case, print the number of pairs satisfying the equation in a separate line.

Constraints

$$ 1 \le T \le 1000 \\
1 \le N \le 5 \times 10^5 \\
1 \le p \le 10^9 \\
\text{p is prime} \\
0 \le k < p \\
0 \le A_i \le 10^9 \\
\text{Sum of N over all test cases does not exceed } 10^6 $$

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
62 votes
Tags:
ApprovedBinary SearchHash MapsMediumOpenString Manipulation
Points:30
48 votes
Tags:
Ad-HocAlgorithmsApprovedHash MapsMediumOpen
Points:30
19 votes
Tags:
Data StructuresHash MapsHash TablesMedium