[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 ...
随机推荐
- 操作系统汇编语言之AT&T指令
转载时格式有问题,大家看原版吧! 作者:EwenWanW 来源:CSDN 原文:https://blog.csdn.net/xiaoxiaowenqiang/article/details/805 ...
- jmeter常用性能监听器分析
jmeter中提供了很多性能数据的监听器,我们通过监听器可以来分析性能瓶颈 本文以500线程的阶梯加压测试结果来描述图表. 常用监听器 1:Transactions per Second 监听动态TP ...
- 【Linux开发】直接渲染管理
原文地址:https://dri.freedesktop.org/wiki/DRM/ DRM - Direct Rendering Manager DRM是一个内核级的设备驱动,既可以编译到内核中也可 ...
- linux/linux学习笔记-Shell基础(mooc)
一.shell概述 shell根据ascII表,将命令翻译为0101...传给内核执行. 内核->shell翻译为命令->用户(操作的界面就是shell,shell=翻译官) linux标 ...
- notepad++通过调用cmd运行java程序
notepad++运行java程序方法主要有下面两个: 通过插件NppExec运行(自行百度“notepad++运行java”) 通过运行 调用cmd编译执行java程序(下面详细讲解) 点击上面工具 ...
- 【转】centos7安装
转自:https://blog.csdn.net/qq_42570879/article/details/82853708 1.CentOS下载CentOS是免费版,推荐在官网上直接下载,网址:htt ...
- 06: django+celery+redis
目录: 1.1 Celery介绍 1.2 celery 组件 1.3 安装相关包 与 管理命令 1.4 celery与Django执行异步任务 1.5 在django中使用计划任务功能 1.1 Cel ...
- Eclipse删除已安装插件
环境:(Windows) Eclipse 1.点击菜单"Help",选择"Install New Software",在弹出的对话框中选择"alrea ...
- babel的初步了解
前段时间开始研究ast,然后慢慢的顺便把babel都研究了,至于ast稍后的时间会写一篇介绍性博客专门介绍ast,本博客先介绍一下babel的基本知识点. 背景: 由于现在前端出现了很多非es5的语法 ...
- php配置伪静态如何将.htaccess文件转换 nginx伪静态文件
php通常设置伪静态三种情况,.htaccess文件,nginx伪静态文件,Web.Config文件得形式,如何将三种伪静态应用到项目中呢, 1,.htaccess文件 实例 <IfModule ...