P1102 A-B 数对(hash)

题目

题目传送门
在这里插入图片描述

题解

  • hash的入门题
  • hash函数:讲原本的数字 m o d mod mod一个数的得到的值
  • 对于本题来说要求 A − B = C A-B=C AB=C,移项后发现 A − C = B A-C=B AC=B我们只需要求 A − C A-C AC后的值有多少个即可
  • 开结构体来实现 v a l val val表示当前位置的值, n u m num num表示当前数字出现几次

code

#include <bits/stdc++.h> 
using namespace std; 
const int maxn = 1000003; 
typedef long long LL; 

template <typename T> 
inline void read(T &s) {
	s = 0; 
	T w = 1, ch = getchar(); 
	while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
	while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); } 
	s *= w; 
}

LL ans; 
int n, c; 
int a[maxn]; 
struct node { LL val; int num; } h[maxn]; 
const int mod = 1000003; 

inline int ha(LL x) { return x % mod; } 

inline int find(LL x) {
	int y = ha(abs(x)); 
	while (h[y].val && h[y].val != x) ha(++y); 
	return y; 
} 

inline void insert(LL x) {
	int y = find(x); 
	h[y].num++; 
	h[y].val = x;  
}

int main() {
	read(n), read(c); 
	for (int i = 1; i <= n; ++i) 
		read(a[i]), insert(a[i]); 
	for (int i = 1; i <= n; ++i) 
		ans += h[find(a[i] - c)].num; 
	printf("%lld\n", ans); 
	return 0; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值