[CF1188B]Count Pairs 题解
前言
这道题目是道好题。
第一次div-2进前100,我太弱了。
题解
公式推导
我们观察这个式子。
\]
感觉少了点什么,我们想到两边同时乘一个\((a_i-a_j)\)。
于是它变成了:
\]
也就是:
\]
把\(k\)乘进去变成:
\]
变换一下就是
\]
公式到这里就推完了
代码实现
实现很简单,根据上面的的公式,由于k是确定的,我们对于所有的\(a_i\)把\((a_i^4-ka_i)\)取模之后放入一个STL map中,然后我们就可以计算有多少数跟它相同了。
复杂度
鉴于STL map的复杂度,时间复杂度为\(\Theta(nlog_2n)\)。
代码
#include <cstdio>
#include <map>
using namespace std;
long long read(){
long long x = 0; int zf = 1; char ch = ' ';
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
}
map<long long, long long> mp;
int main() {
long long n = read(), p = read(), k = read();
long long res = 0;
for (int i = 1; i <= n; ++i){
long long x = read();
long long tmp = ((((((x * x) % p * x) % p * x) % p - k * x) % p) % p + p) % p ;
if (mp.count(tmp) == true)
res += mp[tmp];
++mp[tmp];
}
printf("%I64d\n", res);
return 0;
}
[CF1188B]Count Pairs 题解的更多相关文章
- CF1188B Count Pairs
[题目描述] 给定一个质数 \(p\) , 一个长度为 \(n\)n 的序列 \(a = \{ a_1,a_2,\cdots,a_n\}\)一个整数 \(k\). 求所有数对 \((i, j)\) ( ...
- [MeetCoder] Count Pairs
Count Pairs Description You are given n circles centered on Y-aixs. The ith circle’s center is at po ...
- 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 ...
- 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 ...
- [LeetCode]Swap Nodes in Pairs题解
Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 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 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 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 ...
随机推荐
- kafka之config/server.properties配置参数说明
broker.id--服务器编号 host.name--推荐写本机ip advertised.host.name--外网访问ip advertised.port--外网访问端口 num.partiti ...
- P1074 靶形数独 dfs+预处理
https://www.luogu.org/problemnew/show/P1074 显然是dfs 而且没有什么剪枝记忆化之类的 但是预处理比较麻烦 我用三个二维数组存状态:visx[x][i]代表 ...
- [转帖]Oracle 使用sqlnet.ora/trigger限制/允许某IP或IP段访问指定用户
Oracle 使用sqlnet.ora/trigger限制/允许某IP或IP段访问指定用户 原创 Oracle 作者:maohaiqing0304 时间:2016-05-03 17:05:46 17 ...
- 【转】centos7安装
转自:https://blog.csdn.net/qq_42570879/article/details/82853708 1.CentOS下载CentOS是免费版,推荐在官网上直接下载,网址:htt ...
- 最全mysql笔记整理
mysql笔记整理 作者:python技术人 博客:https://www.cnblogs.com/lpdeboke Windows服务 -- 启动MySQL net start mysql -- 创 ...
- Linux下查看日志用到的常用命令
杀僵尸进程 部分程序员,肯定喜欢下面命令: ps -ef | grep java (先查java进程ID) kill -9 PID(生产环境谨慎使用) kill.killall.pkill命令的区别 ...
- 【转】Hadoop 1.x中fsimage和edits合并实现
在NameNode运行期间,HDFS的所有更新操作都是直接写到edits中,久而久之edits文件将会变得很大:虽然这对NameNode运行时候是没有什么影响的,但是我们知道当NameNode重启的时 ...
- 剑指offer-二叉树的下一结点-树-python
题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 思路:中序遍历,pNode节点的下一个节点根据中序 ...
- Qt是什么?Qt简介(非常全面)
http://c.biancheng.net/view/1792.html Qt(官方发音 [kju:t],音同 cute)是一个跨平台的 C++ 开发库,主要用来开发图形用户界面(Graphical ...
- Ubuntu 16.04安装N卡驱动、cuda、cudnn和tensorflow GPU版
安装驱动 最开始在英伟达官网下载了官方驱动,安装之后无法登录系统,在登录界面反复循环,用cuda里的驱动也出现了同样的问题.最后解决办法是把驱动卸载之后,通过命令行在线安装驱动. 卸载驱动: sudo ...