Count Pairs
You are given a prime number p, n integers a1,a2,…,an, and an integer k.
Find the number of pairs of indexes (i,j) (1≤i<j≤n) for which (ai+aj)(ai^ 2 + aj^ 2)≡k mod p.
Input
The first line contains integers n,p,k (2≤n≤3?10^ 5, 2≤p≤10^9, 0≤k≤p?1). pp is guaranteed to be prime.
The second line contains nn integers a1,a2,…,an (0≤ai≤p?10≤ai≤p?1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
input
3 3 0
0 1 2
output
1
input
6 7 2
1 2 3 4 5 6
output
3
Note
In the first example:
(0+1)(0^2 + 1 ^2)=3≡1mod3.
(0+2)(0^2 + 2 ^2)=8≡2mod3.
(1+2)(1^2 + 2 ^2)=15≡0mod3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1,5), (2,3), (4,6).
思路:原式:(x+y)(x^ 2 + y^2)%p=k%p,將等式兩邊同時(shí)乘以(x-y)就行,得到:x ^ 4%p-kx%p=y ^ 4%p-ky%p;
因?yàn)閤^4%p-k*x%p可能小于0;所以要在加個(gè)p,具體看代碼:
總結(jié)
以上是生活随笔為你收集整理的Count Pairs的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微信撤回消息服务器还有存底么,真得有“后
- 下一篇: 安全加密 - 秘钥和算法关系