CodeForces - 1189E Count Pairs(平方差)
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
3 3 0
0 1 2
1
6 7 2
1 2 3 4 5 6
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(平方差)的更多相关文章
- Codeforces 1189E. Count Pairs
传送门 可以算是纯数学题了吧... 看到这个 $(x+y)(x^2+y^2)$ 就可以想到化简三角函数时经常用到的操作,左右同乘 那么 $(a_i+a_j)(a_i^2+a_j^2) \equiv ...
- Codeforces 1188B - Count Pairs(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...
- 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 ...
- [MeetCoder] Count Pairs
Count Pairs Description You are given n circles centered on Y-aixs. The ith circle’s center is at po ...
- 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 ...
- codeforces D. Count Good Substrings
http://codeforces.com/contest/451/problem/D 题意:给你一个字符串,然后找出奇数和偶数长度的子串的个数,这些字串符合,通过一些连续相同的字符合并后是回文串. ...
- Codeforces 159D Palindrome pairs
http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...
- codeforces#572Div2 E---Count Pairs【数学】【同余】
题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...
- [CF1188B]Count Pairs 题解
前言 这道题目是道好题. 第一次div-2进前100,我太弱了. 题解 公式推导 我们观察这个式子. \[(a_i+a_j)(a_i^2+a_j^2)\equiv k \mod p\] 感觉少了点什么 ...
随机推荐
- Linux之python3编译安装
一,前言 centos7默认是装有python的,咱们先看一下 [root@glh ~ 20:18:03]#python Python 2.7.5 (default, Jul 13 2018, 13: ...
- MongoDB简介,安装,增删改查
MongoDB到底是什么鬼? 最近有太多的同学向我提起MongoDB,想要学习MongoDB,还不知道MongoDB到底是什么鬼,或者说,知道是数据库,知道是文件型数据库,但是不知道怎么来用 那么好, ...
- git 添加码云远程仓库和上传到码云的命令
添加远程仓库 git remote add Zk 仓库地址.git 查看远程仓库 git remote -v 上传远程仓库 git push Zk master 删除远程仓库Zkgit remot ...
- Go 逃逸分析
Go 逃逸分析 堆和栈 要理解什么是逃逸分析会涉及堆和栈的一些基本知识,如果忘记的同学我们可以简单的回顾一下: 堆(Heap):一般来讲是人为手动进行管理,手动申请.分配.释放.堆适合不可预知大小的内 ...
- diff算法实现
大致流程 var vnode = { tag: 'ul', attrs: { id: 'list' }, children: [{ tag: 'li', attrs: { className: 'it ...
- THINKPHP SQL注入处理方式
//注入的产生一般都是对用户输入的参数未做任何处理直接对条件和语句进行拼装. //不安全的写法举例1 $_GET['id']=8;//希望得到的是正整数 $data=M('Member')->w ...
- py停止工作
# 参考https://blog.csdn.net/w952470866/article/details/79132955 电脑搬动换了网络后打开pycharm显示停止工作解决办法: 将python. ...
- 网络编程 --- subprocess模块,struct模块,粘包,UDP协议,socket_server模块
目录 subprocess模块 struct模块 粘包 UDP协议 socket_server模块 subprocess模块 作用: 1.可以帮你通过代码执行操作系统的终端命令 2.并返回终端执行命令 ...
- datafram 操作集锦
Spark Python API 官方文档中文版> 之 pyspark.sql (二) 2017-11-04 22:13 by 牛仔裤的夏天, 365 阅读, 0 评论, 收藏, 编辑 摘要:在 ...
- PHP怎么实现字符串转义和还原?
首先大家可以简单了解下什么是转义字符?有什么用? 转义字符是一种特殊的字符常量.转义字符以反斜线"\"开头,后跟一个或几个字符.转义字符具有特定的含义,不同于字符原有的意义,故称“ ...