当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
[Cloud Native--Kubernetes] Pod Controller
【unity编译器扩展之模型动画拷贝】
Mysql_12 多表查询
【无标题】
Handwritten Distributed Configuration Center (1)
How to automatically push my new articles to my fans (very simple, can't learn to hit me)
00、数组及字符串常用的 API(详细剖析)
数据类型及输入输出初探(C语言)
Some thoughts on writing
3. Actual combat---crawl the result page corresponding to Baidu's specified entry (a simple page collector)
what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......
工业物联网 —— 新型数据库的召唤
导入JankStats检测卡帧库遇到问题记录
【无标题】
线程三连鞭之“线程的状态”
node使用redis
关于使用read table 语句
大师教你3D实时角色制作流程,游戏建模流程分享
测试经理要不要做测试执行?
LeetCode Hot 100
![Couple Holding Hands [Greedy & Abstract]](/img/7d/1cafc000dc58f1c5e2e92150be7953.png)








