Count Pairs

You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk.

Find the number of pairs of indexes (i,j)(i,j) (1≤i<j≤n1≤i<j≤n) for which (ai+aj)(a2i+a2j)≡kmodp(ai+aj)(ai2+aj2)≡kmodp.

Input

The first line contains integers n,p,kn,p,k (2≤n≤3⋅1052≤n≤3⋅105, 2≤p≤1092≤p≤109, 0≤k≤p−10≤k≤p−1). ppis guaranteed to be prime.

The second line contains nn integers a1,a2,…,ana1,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)(02+12)=1≡1mod3(0+1)(02+12)=1≡1mod3.

(0+2)(02+22)=8≡2mod3(0+2)(02+22)=8≡2mod3.

(1+2)(12+22)=15≡0mod3(1+2)(12+22)=15≡0mod3.

So only 11 pair satisfies the condition.

In the second example, there are 33 such pairs: (1,5)(1,5), (2,3)(2,3), (4,6)(4,6).

题意:

找出有几组数对满足i<j,且(ai+aj)(ai^2+aj^2)≡k mod p

思路:

如果两两找O(n^2)肯定超时,所以想到需要让i与j分离,即i与j分列等式两边,这样只需扫一遍。

两边同乘(ai-aj),制造平方差,整理后得(ai^4-aj^4)==k(ai-aj),即ai^4-k*ai==aj^4-k*aj

排序后找相同对即可。

#include<bits/stdc++.h>
#define MAX 300005
using namespace std;
typedef long long ll; ll a[MAX]; int main()
{
int t,m,i,j;
ll n,p,k;
scanf("%I64d%I64d%I64d",&n,&p,&k);
for(i=;i<=n;i++){
scanf("%I64d",&a[i]);
a[i]=(a[i]*a[i]%p*a[i]%p*a[i]%p-k*a[i]%p+p)%p;
}
sort(a+,a+n+);
ll c=,ans=;
for(i=;i<=n;i++){
if(a[i-]==a[i]){
c++;
ans+=c;
}
else c=;
}
printf("%I64d\n",ans);
return ;
}

CodeForces - 1189E Count Pairs(平方差)的更多相关文章

  1. Codeforces 1189E. Count Pairs

    传送门 可以算是纯数学题了吧... 看到这个 $(x+y)(x^2+y^2)$ 就可以想到化简三角函数时经常用到的操作,左右同乘 那么 $(a_i+a_j)(a_i^2+a_j^2) \equiv  ...

  2. Codeforces 1188B - Count Pairs(思维题)

    Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...

  3. Codeforces 1188B Count Pairs (同余+分离变量)

    题意: 给一个3e5的数组,求(i,j)对数,使得$(a_i+a_j)(a_i^2+a_j^2)\equiv k\ mod\ p$ 思路: 化简$(a_i^4-a_j^4)\equiv k(a_i-a ...

  4. [MeetCoder] Count Pairs

    Count Pairs Description You are given n circles centered on Y-aixs. The ith circle’s center is at po ...

  5. CodeForces - 1189 E.Count Pairs (数学)

    You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Find the numbe ...

  6. codeforces D. Count Good Substrings

    http://codeforces.com/contest/451/problem/D 题意:给你一个字符串,然后找出奇数和偶数长度的子串的个数,这些字串符合,通过一些连续相同的字符合并后是回文串. ...

  7. Codeforces 159D Palindrome pairs

    http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...

  8. codeforces#572Div2 E---Count Pairs【数学】【同余】

    题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...

  9. [CF1188B]Count Pairs 题解

    前言 这道题目是道好题. 第一次div-2进前100,我太弱了. 题解 公式推导 我们观察这个式子. \[(a_i+a_j)(a_i^2+a_j^2)\equiv k \mod p\] 感觉少了点什么 ...

随机推荐

  1. vue 分组左右选择

    <el-col :span="12"> <div style="text-align: left" class="transferd ...

  2. Ipython 和 python 的区别

    IPython是一个python交互shell,它比默认的python shell更易于使用.它支持自动变量完成.自动缩进.bash shell命令,并且内置了许多有用的函数和函数. IPython是 ...

  3. Java调用Python相关问题:指定python环境、传入参数、返回结果

    本篇文章涉及到的操作均在Windows系统下进行,Java调用python在原理上不难,但是可能在实际应用中会有各种各样的需求,网上其他的资料很不全,所以又总结了这篇文章,以供参考. 一.指定pyth ...

  4. django_rest framework 接口开发(二)

    1 a. 认证 - 仅使用: from django.views import View from rest_framework.views import APIView from rest_fram ...

  5. 并发编程.md

    操作系统基础 人机矛盾: CPU利用率低 磁带存储+批处理:降低数据的读取时间,提高CPU的利用率 多道操作系统------在一个任务遇到IO的时候主动让出CPU,给其他任务使用 由操作系统完成 切换 ...

  6. Sqlserver2008[索引]

    SQL索引有两种:聚集索引.非聚集索引 目的:提高sqlserver 系统的性能,加快数据的查询速度与减少系统的响应时间 注意点:一个表只能有一个聚集索引,但可以有多个非聚集索引 索引的存储机制: 聚 ...

  7. Mongodb创建用户Error: couldn’t add user: Use of SCRAM-SHA-256 requires undigested passwords

    解决方案:修改mechanisms加密方式为SCRAM-SHA-1 db.createUser({ user: "admin", pwd: "xxx", rol ...

  8. <<构建之法>>--第二次作业

    GIT地址 https://github.com/Panghu98/AchaoCalculator.git GIT用户名 Panghu98 学号后五位 62632 博客地址 https://www.c ...

  9. Dymola — 多学科系统仿真平台

            Dymola 是法国Dassault Systems公司的多学科系统仿真平台,广泛应用于国内外汽车.工业.交通.能源等行业的系统总体架构设计.指标分解以及系统功能验证及优化等.Dymo ...

  10. G1垃圾收集器深度理论讲解【纯理论】

    在上三次中对于G1官方解读之后,接下来还得回到G1的理论化知识的了解阶段..确实G1是概念比较复杂,而且它也是未来JDK的垃圾回收的主流,所以花再多的时间在这上面也是值得的,先来回顾一下上次过过的理论 ...