AtCoder Beginner Contest 164 比赛人数11302 比赛开始后15分钟看到所有题
AtCoder Beginner Contest 164 D Multiple of 2019 统计余数个数+注意余数是0的统计
总目录详见https://blog.csdn.net/mrcrack/article/details/104454762
在线测评地址https://atcoder.jp/contests/abc164/tasks/abc164_d
样例模拟如下
1817181712114
3
x%2019对应的余数
4 4
14 14
114 114
2114 95
12114 0
712114 1426
1712114 2
81712114 1165
181712114 95
7181712114 1917
17181712114 1924
817181712114 465
1817181712114 1165
(18171(2114)) 余数是95 故18171能被2019整除
(12114) 余数是0 故12114能被2019整除
(18171(81712114)) 余数是1165 故18171能被2019整除
A B C D 余数都相同,可以组合成被2019整除的数有6个
AB,AC,AD,
BC,BD,
CD
(4-1+1)*(4-1)/2=6个
特别注意,余数是0的统计数量cnt[0]*(cnt[0]+1)/2
AC代码如下
#include <stdio.h>
#include <string.h>
#define maxn 200010
#define mod 2019
#define LL long long
char s[maxn];
int cnt[mod];
LL ans;
int main(){
int n,i,x,y;
scanf("%s",s+1);
n=strlen(s+1),y=1,x=0;
for(i=n;i>=1;i--){
x=(x+(s[i]-'0')*y)%mod;
y=y*10%mod;
cnt[x]++;
}
for(i=1;i<2019;i++)
if(cnt[i]>1)ans+=(LL)(cnt[i]-1+1)*(cnt[i]-1)/2;
ans+=(LL)cnt[0]*(cnt[0]+1)/2;//特殊余数处理
printf("%lld\n",ans);
return 0;
}
类似的问题有
AtCoder Beginner Contest 158 E Divisible Substring 记录连续余数和,并统计相同余数和的个数