当前位置:网站首页>2022杭电多校第一场 1004 Ball
2022杭电多校第一场 1004 Ball
2022-08-05 00:18:00 【Rain Sure】
题目链接
题目大意
题目给定我们二维平面上的 n n n个点,横纵坐标均不超过 100000 100000 100000,问我们从中选出三个点,三个点构成的三条边中距离的中位数是素数的方案数是多少?
题解
直接枚举三个点,势必会TLE;所以我们考虑把所有边都构建出来,时间复杂度 O ( n 2 ) O(n^2) O(n2),然后按照边的大小从小到大排序,然后依次枚举每条边,如果当前边是质数,假设当前边的两个端点为 a a a 和 b b b,我们需要知道连接 a a a的边比当前边权值小的个数,连接 b b b的边比当前边权值大的个数;同理反过来也要求一下。直接枚举不太行,我们考虑使用 b i t s e t bitset bitset进行优化, p [ i ] p[i] p[i]表示有哪些点到 i i i点的距离小于等于当前边,枚举边的过程中维护即可。最终时间复杂度 O ( n 2 l o g ( n 2 ) ) O(n^2 log(n^2)) O(n2log(n2))。
代码
#include<iostream>
#include<cstring>
#include<bitset>
#include<algorithm>
using namespace std;
#define int long long
const int maxn = 2010, M = 200010;
#define x first
#define y second
typedef pair<int,int> PII;
PII a[maxn];
bool st[M];
int primes[M], cnt;
typedef struct node
{
int a, b, c;
bool operator < (const struct node &w) const {
return c < w.c;
}
}Node;
Node edges[maxn * maxn];
bitset<maxn> p[maxn];
int n, m;
void init(int n)
{
for(int i = 2; i <= n; i ++){
if(!st[i]) primes[cnt ++] = i;
for(int j = 0; primes[j] <= n / i; j ++){
st[primes[j] * i] = true;
if(i % primes[j] == 0) break;
}
}
}
int get_dist(PII a, PII b)
{
return abs(a.x - b.x) + abs(a.y - b.y);
}
signed main()
{
init(200000);
st[1] = true;
int t; cin >> t;
while(t --){
for(int i = 0; i < maxn; i ++) p[i].reset();
cin >> n >> m;
for(int i = 1; i <= n; i ++) cin >> a[i].x >> a[i].y;
int k = 0;
for(int i = 1; i <= n; i ++){
for(int j = i + 1; j <= n; j ++){
edges[k ++] = {
i, j, get_dist(a[i], a[j])};
}
}
int res = 0;
sort(edges, edges + k);
for(int i = 0; i < k; i ++){
int a = edges[i].a, b = edges[i].b, c = edges[i].c;
if(!st[c]) res += (p[a] ^ p[b]).count();
p[a][b] = 1, p[b][a] = 1;
}
cout << res << endl;
}
return 0;
}
边栏推荐
- 【Valentine's Day special effects】--Canvas realizes full screen love
- Getting started with 3D modeling for games, what modeling software can I choose?
- KT148A语音芯片ic工作原理以及芯片的内部架构描述
- Senior game modelers tell newbies, what are the necessary software for game scene modelers?
- mysql基础
- leetcode:267. 回文排列 II
- 翁恺C语言程序设计网课笔记合集
- 《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术
- Brainstorm: Complete Backpack
- 在线中文姓名生成工具推荐
猜你喜欢

机器学习(公式推导与代码实现)--sklearn机器学习库

统计单词(DAY 101)华中科技大学考研机试题

First, the basic concept of reptiles
Handwritten Distributed Configuration Center (1)

矩阵数学原理

KT6368A Bluetooth certification problem_FCC and BQB_CE_KC certification or other instructions

测试经理要不要做测试执行?

论文解读( AF-GCL)《Augmentation-Free Graph Contrastive Learning with Performance Guarantee》

Three tips for you to successfully get started with 3D modeling

2022牛客暑期多校训练营5(BCDFGHK)
随机推荐
oracle创建用户以后的权限问题
oracle创建用户
Mathematical Principles of Matrix
Chinese and Japanese color style
统计单词(DAY 101)华中科技大学考研机试题
Implementation principle of golang coroutine
RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
oracle创建表空间
.net (C#) get year month day between two dates
怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
Couple Holding Hands [Greedy & Abstract]
一、爬虫基本概念
性能测试如何准备测试数据
如何写好测试用例
E - Many Operations (按位考虑 + dp思想记录操作后的结果
tiup uninstall
Handwritten Distributed Configuration Center (1)
IDEA 文件编码修改
SQL关联表更新
ansible学习笔记分享-含剧本示例