高精度预取模+哈希:CCPC2023深圳M

https://vjudge.net/problem/CSG-1249

对于取模操作,我们可以预处理,则三数之和只能是 0 , M , 2 M 0,M,2M 0,M,2M

然后哈希一下即可

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
 #define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
 #define debug(...) void(0)
#endif
#define int long long
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||
ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//srand(time(0));
#define N 510
#define M2 20010
#define mo (int)(1e14+9)
int n, m, i, j, k, T;
int M, K, a[N], b[M2], nxt; 
char s[M2]; 
int ans; 
map<int, int>mp; 

int Mod(int a) {
	return (a%mo+mo)%mo; 
}

int check() {
	int flg=0, j, k; 
	for(int i=1; i<=K; ++i) {
		if(b[i]>=10) flg=1; 
		j=i%K+1; k=b[i]/10; b[i]%=10; b[j]+=k; 
	}
	return flg; 
}

void no9() {
	int flg=0, i; 
	for(i=1; i<=K; ++i) if(b[i]!=9) break; 
	if(i>K) {
		for(i=1; i<=K; ++i) b[i]=0; 
	}
}

signed main()
{
	#ifdef LOCAL
	  freopen("in.txt", "r", stdin);
	  freopen("out.txt", "w", stdout);
	#endif
//	T=read();
//	while(T--) {
//
//	}
	n=read(); K=read(); 
	for(j=1, M=0; j<=K; ++j) M=(M*10+9)%mo; 
	debug("M : %lld\n", M); 
	for(i=1; i<=n; ++i) {
		memset(b, 0, sizeof(b)); 
		scanf("%s", s+1); m=strlen(s+1); 
		reverse(s+1, s+m+1); 
		for(j=1; j<=m; ++j) {
			k=(j-1)%K+1; b[k]+=s[j]-'0';
		}
		while(check()); no9(); 
		for(j=K; j>=1; --j) debug("%lld|", b[j]); 
		for(j=K; j>=1; --j)	a[i]=(a[i]*10+b[j])%mo; 
		debug(" %lld : %lld\n", i, a[i]); 
	}
	for(j=1; j<=n; ++j) {
		mp.clear(); 
		for(i=1; i<=j; ++i) mp[Mod(a[i]+a[j])]++; 
		for(i=j; i<=n; ++i) {
			ans+=mp[Mod(M-a[i])];
			ans+=mp[Mod(2*M-a[i])]; 
			ans+=mp[Mod(0-a[i])]; 
		}
	}
	printf("%lld", ans); 
	return 0;
}
相关推荐
深念Y2 天前
哈希与向量:计算机理解现实的两座桥梁
人工智能·数学·机器学习·向量·hash·哈希·空间
进击的荆棘5 天前
C++起始之路——哈希表的实现
数据结构·c++·散列表·哈希
罗湖老棍子7 天前
Beads(信息学奥赛一本通- P1461) [POI 2010] KOR-Beads(洛谷-P3498)
算法·字符串·哈希
罗湖老棍子8 天前
A Horrible Poem(信息学奥赛一本通- P1460) [POI 2012] OKR-A Horrible Poem(洛谷-P3538)
算法·哈希·欧拉筛·错位重叠
qeen8711 天前
【算法笔记】模拟与高精度加减乘除
c++·笔记·算法·高精度·模拟
nap-joker12 天前
通过知识整合重新审视医学图像检索
哈希·医学影像·图像检索·知识整合·基于内容的相似度排名·分布外检测
北京阿尔泰科技厂家14 天前
24位高精度热电阻采集模块DAM-3149A:工业测温领域的全能新标杆
人工智能·高精度·仪器仪表·工业自动化数据采集·分布式监测数据采集·工业测温·热电阻采集模块
罗湖老棍子15 天前
Power Strings(信息学奥赛一本通- P1457)
算法·字符串·哈希
Q741_1471 个月前
力扣高频面试题详解 数组 链表 力扣 56.合并区间 力扣 160.相交链表 C++ 每日练习
c++·算法·leetcode·链表·数组·哈希
小江的记录本1 个月前
【HashMap】HashMap 系统性知识体系全解(附《HashMap 面试八股文精简版》)
java·前端·后端·容器·面试·hash·哈希