codeforces#572Div2 E---Count Pairs【数学】【同余】
题目:http://codeforces.com/contest/1189/problem/E
题意:给定$n$个互不相同数,一个$k$和一个质数$p$。问这$n$个数中有多少对数$(a_i+a_j)(a_i^2+a_j^2)\equiv k\,mod\,p$
思路:这一场的题目好像都很思维啊,代码量不多,想得出来就能写。
同余式左右两边同乘一个非零的数同余式不变,所以原式可以变为
$(a_i-a_j)(a_i+a_j)(a_i^2+a_j^2)\equiv (a_i-a_j)k = (a_i^2-a_j^2)(a_i^2+a_j^2)\equiv (a_i-a_j)k = (a_i^4-a_j^4)\equiv a_ik-a_jk = a_i^4 - a_ik \equiv a_j^4 - a_jk$
所以对于每一个数我们只需要计算$a_i^4-a_ik\,mod\,p$,然后根据这个能取到这个值的集合大小来计算答案就行了。
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; int n, p, k;
const int maxn = 3e5 + ;
int num[maxn];
map<int, int>mp; int main()
{
scanf("%d%d%d", &n, &p, &k);
for(int i = ; i < n; i++){
scanf("%d", &num[i]);
int res = ((LL)num[i] * num[i] % p * num[i] % p - k) % p * num[i] % p;
res = (res + p) % p;
if(mp.find(res) != mp.end()){
mp[res] = mp[res] + ;
}
else{
mp[res] = ;
}
}
map<int, int>::iterator iter;
int ans = ;
for(iter = mp.begin(); iter != mp.end(); iter++){
int x = iter->second;
//cout<<iter->first<<":"<<iter->second<<endl;
ans += x * (x - ) / ;
}
printf("%d\n", ans);
return ;
}
codeforces#572Div2 E---Count Pairs【数学】【同余】的更多相关文章
- 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 ...
- CF1188B/E Count Pairs(数学)
数同余的个数显然是要把\(i,j\)分别放到\(\equiv\)的两边 $ (a_i + a_j)(a_i^2 + a_j^2) \equiv k \bmod p $ 左右两边乘上\((a_i-a_j ...
- CodeForces - 1189E Count Pairs(平方差)
Count Pairs You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Fi ...
- [MeetCoder] Count Pairs
Count Pairs Description You are given n circles centered on Y-aixs. The ith circle’s center is at po ...
- 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 ...
- Codeforces 1188B - Count Pairs(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...
- Codeforces 1189E. Count Pairs
传送门 可以算是纯数学题了吧... 看到这个 $(x+y)(x^2+y^2)$ 就可以想到化简三角函数时经常用到的操作,左右同乘 那么 $(a_i+a_j)(a_i^2+a_j^2) \equiv ...
- Codeforces 839C Journey - 树形动态规划 - 数学期望
There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- [Visual Studio] - 使用 Fiddler 时,禁止监控 VSHub 请求的方法
背景 VS + Fiddler 调试 WebAPI,监控请求包含大量 VSHub Request.http://localhost:49161/vshub/bb195f2e0d5c4765b9411f ...
- JUC之AbstractQueuedSynchronizer原理分析 - 独占/共享模式
1. 简介 AbstractQueuedSynchronizer (抽象队列同步器,以下简称 AQS)出现在 JDK 1.5 中,由大师 Doug Lea 所创作.AQS 是很多同步器的基础框架. R ...
- 题解 Luogu P1110 【[ZJOI2007]报表统计】
感谢 @cmy962085349 提供的hack数据,已经改对了. 先声明,我好像是题解里写双$fhq$ $treap$里唯一能过的...(最后两个点啊) 思路:首先看题目,$MIN_GAP_SORT ...
- python 之 并发编程(线程Event、协程)
9.14 线程Event connect线程执行到event.wait()时开始等待,直到check线程执行event.set()后立即继续线程connect from threading impor ...
- sass快速使用
sass的使用 建议使用一种语法格式(scss) scss sass转换 sass-convert main.scss main.sass sass变量声明 example: $headline-ff ...
- zookeeper启动占用8080端口,跟HDFS默认使用的8080端口冲突
zookeeper最近的版本中有个内嵌的管理控制台是通过jetty启动,也会占用8080 端口. 通过查看zookeeper的官方文档,发现有3种解决途径: (1).删除jetty. (2)修改端口. ...
- Spring全框架讲解
Day 01: https://blog.csdn.net/sinat_29211659/article/details/81335229
- VBA Exit For语句
当想要根据特定标准退出For循环时,就可以使用Exit For语句.当执行Exit For时,控件会立即跳转到For循环之后的下一个语句. 语法 以下是在VBA中Exit For语句的语法. Exit ...
- css 层叠 比较特殊性
css 层叠: 多个相同的css声明(属性),应用到同一个元素上.当一个标签声明冲突时,浏览器会自动出发层叠机制 1:比较优先级 2:比较特殊性 3:比较源次序 依次经过上面的1,2,3的比较后,最终 ...
- 两通道实信号使用一个FFT同时计算算法
前言 在工程的实际应用场景中,往往是需要最省资源量.而DSP资源和BRAM资源对FPGA来说弥足珍贵. 对于同时存在多个通道的实信号需要做FFT而言,常规做法是每个通道用一个FFT IP,FFT IP ...