当前位置:网站首页>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;
}
边栏推荐
- Mathematical Principles of Matrix
- The master teaches you the 3D real-time character production process, the game modeling process sharing
- TinyMCE禁用转义
- 【LeetCode】Summary of Two Pointer Problems
- 【LeetCode】矩阵模拟相关题目汇总
- what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......
- STC89C52RC的P4口的应用问题
- Detailed explanation of common DNS resource record types
- Mysql_13 事务
- MongoDB permission verification is turned on and mongoose database configuration
猜你喜欢
![[CVA Valuation Training Camp] Financial Modeling Guide - Lecture 1](/img/8b/360df9a9094037dc358cb21c60cdc8.png)
[CVA Valuation Training Camp] Financial Modeling Guide - Lecture 1

What is next-generation modeling (with learning materials)

Privacy Computing Overview

Modelers experience sharing: model study method

【LeetCode】滑动窗口题解汇总

leetcode:266. 回文全排列

翁恺C语言程序设计网课笔记合集

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

The master teaches you the 3D real-time character production process, the game modeling process sharing
手写分布式配置中心(1)
随机推荐
Getting started with 3D modeling for games, what modeling software can I choose?
【LeetCode】Summary of Two Pointer Problems
Mysql_12 多表查询
Implementation principle of golang coroutine
刘润直播预告 | 顶级高手,如何创造财富
10 种常见的BUG分类
KT6368A蓝牙的认证问题_FCC和BQB_CE_KC认证或者其它说明
Senior game modelers tell newbies, what are the necessary software for game scene modelers?
电子行业MES管理系统的主要功能与用途
2 用D435i运行VINS-fusion
从单体架构迁移到 CQRS 后,我觉得 DDD 并不可怕
10 个关于 Promise 和 setTimeout 知识的面试题,通过图解一次说透彻
【CVA估值训练营】财务建模指南——第一讲
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
日志(logging模块)
仿网易云音乐小程序-uniapp
.net (C#) get year month day between two dates
《MySQL入门很轻松》第2章:MySQL管理工具介绍
SQL关联表更新
tiup uninstall