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;
}