博客
关于我
Lougu P1102 A-B 数对【hash】
阅读量:354 次
发布时间:2019-03-04

本文共 795 字,大约阅读时间需要 2 分钟。

这道题的暴力破解其实非常简单,但我们选择使用哈希表来实现,这样可以更高效地解决问题。

首先,我们需要包含必要的头文件:

#include 
#include
#include
#include
using namespace std;

接下来,我们定义了一些常量和变量:

#define P 4000037long long a[P], b[200010], c[P];long long n, C, x, qm, ans;

然后,我们定义了两个关键函数:

long long hash(long long x) {    return x % P;}long long dw(long long x) {    long long j = 0;    qm = hash(abs(x));    while (a[hash(j + qm)] != x && c[hash(j + qm)] != 0) {        j++;    }    return hash(j + qm);}

最后是主函数的逻辑:

int main() {    scanf("%lld%lld", &n, &C);    for (int i = 1; i <= n; i++) {        scanf("%lld", &b[i]);        a[dw(b[i])] = b[i];        c[dw(b[i])]++;    }    for (int i = 1; i <= n; i++) {        ans += c[dw(b[i] - C) % P];    }    cout << ans << endl;}

通过这种方式,我们可以高效地完成题目的要求。

转载地址:http://gsle.baihongyu.com/

你可能感兴趣的文章
Oracle spatial 周边查询SQL
查看>>
Oracle Spatial空间数据库建立
查看>>
UML— 活动图
查看>>
oracle sqlplus已停止工作,安装完成客户端后sqlplus报“段错误”
查看>>
oracle SQLserver 函数
查看>>
oracle sql分组(group,根据多个内容分组)在select之后from之前 再进行select查询,复杂子查询的使用
查看>>
Oracle Statspack分析报告详解(一)
查看>>
oracle tirger_在Oracle中,临时表和全局临时表有什么区别?
查看>>
Oracle Validated Configurations 安装使用 说明
查看>>
oracle where 条件的执行顺序分析1
查看>>
oracle 中的 CONCAT,substring ,MINUS 用法
查看>>
Oracle 中的 decode
查看>>
oracle 中表一对多取多方的最新的一条数据
查看>>
oracle 使用 PL/SQL Developer创建表并插入单条、多条数据
查看>>
oracle 使用leading, use_nl, rownum调优
查看>>
oracle 修改字段类型方法
查看>>
Oracle 修改数据库表数据提交之后进行回滚
查看>>
UML-总结
查看>>
oracle 内存参数示意图
查看>>
Oracle 写存储过程的一个模板还有一些基本的知识点
查看>>