[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 ...
随机推荐
- Visdom可视化
Visdom是基于Pytorch的可视化工具 安装方式: pip install visdom 因为Visdom本质上是一个Web服务器,把数据渲染到网页上,因此首先需要运行这个 服务器,如下: py ...
- ubuntu server安装的一些坑
[没有root用户] ubuntu server安装的时候要你新建一个用户,安装完成后,你需要手动开启root. $ sudo passwd root 输入你当前用户的密码 输入你希望的root用户的 ...
- 选择排序--python
def findSmallest(arr): smallest = arr[0] smallest_index = 0 for i in range(1, len(arr)): if arr[i] & ...
- Vue 实现手动刷新组件
Vue 实现手动刷新组件:https://www.jianshu.com/p/742142dc95f3
- 03-Django-models
# Models 模型 - ORM - ObjectRelationMap : 把面向对象思想转换成关系数据库思想.操作上把类等价于表格 - 类对应表格 - 类中的属性对应表中的字段 - 在应用中的m ...
- sublime text 3基本参数设置及插件使用
sublime text 3常用基本设置,包括插件,字体等一些配置.写个随笔,备忘. soda主题:特别喜欢: 插件:Color Highlighter输入颜色时,可以看到颜色. 在Color Hig ...
- console 对象
JavaScript 原生中默认是没有 Console 对象,这是宿主对象(也就是游览器)提供的内置对象. 用于访问调试控制台,在不同的浏览器里效果可能不同.Console 对象方法:
- 划水日记之大哥带我走渗透I
5/24/19 周五 access注入 先测试是否有漏洞 or nor and 1=1 and 1=2 2. Order by 猜字表端,发现一共有22个 3..然后使用 union select 1 ...
- linux Nginx 的安装
确保安装了 gcc,openssl-devel,pcre-devel,zilb-devel 下载官网:http://nginx.org/ [root@localhost tools]# wget ht ...
- FPGA引脚锁定 注意err和高阻状态
1.fpga没有用的的管脚一定要设置成高阻状态设置路径如下: Assignmen->Device->Device&Pin Option->Unused pins->As ...