高精度预取模+哈希: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;
}
相关推荐
NuyoahC2 天前
算法笔记(七)——哈希表
c++·笔记·算法·哈希
mikey棒棒棒17 天前
算法练习题25——合并多项式
java·算法·hashmap·哈希·多项式
___Dream19 天前
codetop+acwing加减乘除法与位运算的总结,别再饶头啦!!!
数据结构·c++·算法·高精度
EthanXone19624 天前
【学习笔记】SSL密码套件的选择
密码学·ssl·哈希·密钥交换·密码套件
叫我龙翔1 个月前
【项目日记】高并发内存池---实现线程缓存
c++·缓存·哈希
Zfox_2 个月前
位图与布隆过滤器 —— 海量数据处理
数据结构·c++·算法·容器·哈希
Tisfy2 个月前
LeetCode 3137.K 周期字符串需要的最少操作次数:哈希表计数
leetcode·字符串·散列表·题解·哈希表·哈希
Roylele7322 个月前
leetCode - - - 哈希表
java·数据结构·算法·leetcode·散列表·哈希·hash table
Davieyang.D.Y2 个月前
互联网应用主流框架整合【Redis数据结构及常用命令】
数据结构·redis·链表·有序集合·哈希·无需集合
wanghonghui1232 个月前
洛谷 P1080 [NOIP2012 提高组] 国王游戏
贪心算法·高精度·noip提高组