当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Handwritten Distributed Configuration Center (1)

怎样进行在不改变主线程执行的时候,进行日志的记录

KT6368A蓝牙的认证问题_FCC和BQB_CE_KC认证或者其它说明

电子行业MES管理系统的主要功能与用途

oracle创建表空间

Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions

标识符、关键字、常量 和变量(C语言)

redis可视化管理软件Redis Desktop Manager2022

Xiaohei's leetcode journey: 95. Longest substring with at least K repeating characters

入门3D游戏建模师知识必备
随机推荐
jenkins发送邮件系统配置
LeetCode Hot 100
Getting started with 3D modeling for games, what modeling software can I choose?
GO中sync包自由控制并发的方法
First, the basic concept of reptiles
刘润直播预告 | 顶级高手,如何创造财富
[Happy Qixi Festival] How does Nacos realize the service registration function?
Senior game modelers tell newbies, what are the necessary software for game scene modelers?
LeetCode Hot 100
leetcode经典例题——单词拆分
golang 协程的实现原理
隐私计算综述
QSunSync 七牛云文件同步工具,批量上传
Flask框架 根据源码分析可扩展点
KT6368A Bluetooth certification problem_FCC and BQB_CE_KC certification or other instructions
Modelers experience sharing: model study method
Brainstorm: Complete Backpack
mysql基础
MongoDB permission verification is turned on and mongoose database configuration
oracle创建用户