codeforces#572Div2 E---Count Pairs【数学】【同余】
2024-08-27 06:32:58
题目: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 ;
}
最新文章
- WinForm中实现播放mp3 、mp4文件
- hashmap先按照value从大到小排序,value相等时按照key从小到大排序
- DedeCMS Xss+Csrf Getshell \dede\file_manage_control.php
- 。。。在学习新框架Spring MVC的感受。。。
- atitit.表格的绑定client side 最佳实践
- Remote Displayer for Android
- QTextCodec::makeDecoder函数,plugins需要是动态链接库
- linux mount (挂载命令)详解
- JSP页面上用横线代替文本框
- python 操作word文档
- codeforces 620F. Xors on Segments
- 利用jsoup爬取百度网盘资源分享连接(多线程)
- IONIC2新建项目并添加导航
- List集合数据太多进行分批,List的subList方法应用
- 初识 go 语言:方法,接口及并发
- python -- while循环,格式化输出,运算符,初识编码
- call与apply简单介绍
- Delphi Qjson
- [转]jquery设置select选中,赋值等操作
- 获取指定ip段的所有存活主机的主机名和操作系统