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\] 感觉少了点什么 ...
随机推荐
- iOS 如何判断一个点在某个指定区域中
在iOS 开发中会遇到 判断位置的情况 iOS 自己都有函数实现的这些功能. 判断一个点是否在这个rect区域中 bool CGRectContainsPoint(CGRect rect,CGPoin ...
- 多核cpu关闭、开启核心
列表 # ls /sys/devices/system/cpu/ 关闭 # echo '0' > /sys/devices/system/cpu/cpu1/online 开启 # echo '1 ...
- java的输入流中的两个坑
1.输入流的所有实例中的read()方法皆是阻塞的. 输入流只会在发生错误或者异常关闭的时候回返回-1,如果流中没有数据,不会返回-1而是一直阻塞. 2.BufferedReader的readLine ...
- unity图片保留周边,中间延伸
1.先把图片切割,类似下面这样的 2.然后在使用的时候(选择图片类型的时候选择sliced)
- Winserver-Exception from HRESULT: 0x800A03EC
Q: 程序在VS中手动执行没问题,但是排了JOB报异常:Exception from HRESULT: 0x800A03EC at Excel.WorkbookClass.SaveAs() A: ...
- 静态链接 VS 动态链接
什么是链接? 链接其实就是连接的意思,将所有相关的东西连接起来. 简单理解静态连接和动态链接: 静态链接:编译时完成链接 动态链接:程序运行起来后,根据需求再去链接,这就是动态链接 静态链接 什么是静 ...
- apache Directory Studio 简易使用
apache Directory Studio 简易使用 本文首发:https://www.somata.work/2019/apacheDirectoryStudioSimpleUse.html 以 ...
- python中redis
一.简介 二.redis的安装和使用 三.python操作readis之安装和支持存储类型 四.python操作redis值普通链接 五.python操作redis值连接池 六.操作之String操作 ...
- 日期时间格式化 SimpleDateFormat与DateTimeFormatter
原文:https://www.jianshu.com/p/b212afa16f1f 1.SimpleDateFormat为什么不是线程安全的? 如果我们把SimpleDateFormat定义成stat ...
- JAVA设计模式之单例模式(单件模式)—Singleton Pattern
1.什么是单例模式? <Head First 设计模式>中给出如下定义:确保一个类只有一个实例,并提供一个全局访问点. 关键词:唯一实例对象. 2.单例模式的实现方式: 2.1 懒汉式 对 ...