AtCoder Beginner Contest 164 D Multiple of 2019 统计余数个数+注意余数是0的统计

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 记录连续余数和,并统计相同余数和的个数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值