CF628B

简介: CF628B

题目大意:给定一个数字(长度<=3*10^5),判断其能被4整除的连续子串有多少个


解题思路:注意一个整除4的性质: 若bc能被4整除,则a1a2a3a4…anbc也一定能被4整除;


利用这个性质,先特判第一位数字是否能被4整除,可以则++cnt,


之后从第二位数字开始,设当前位为i,先判断a[i]能否被4整除,可以则++cnt,


再判断a[i-1]*10+a[i]能否被4整除,可以则cnt = cnt + (i)


相关证明: 设一整数各个位置为a1,a2,a3,…,an,b,c;


则(a1a2a3…an b c)%4 =( (a1a2a3…an)*100 + bc ) % 4


= (a1a2a3…an)*100 % 4 + (bc)%4 = 0 + (bc)%4 = (bc)%4 (100能被4整除)


注意,此题数据很大,要用long long

#include <bits/stdc++.h>
using namespace std;
int main() {
  string str;
  cin >> str;
  long long ans = 0;
  for (int i = 0; i < str.size(); i++) {
    if (i == 0) {
      if ((str[i] - '0') % 4 == 0) ans++;
      continue;
    }
    int b = str[i - 1] - '0';
    int c = str[i] - '0';
    if (c % 4 == 0) ans++;
    if ((b * 10 + c) % 4 == 0) ans += i;
  }
  cout << ans << endl;
  return 0;
}
相关文章
|
30天前
|
算法
CF 1561
【7月更文挑战第20天】
29 2
|
人工智能
cf 220B(莫队)
cf 220B(莫队)
81 0
|
人工智能 网络架构
CF 698A
CF 698A
63 0
CF1000F One Occurrence(莫队)
CF1000F One Occurrence(莫队)
44 0
CF708C-Andryusha and Colored Balloons(dfs)
CF708C-Andryusha and Colored Balloons(dfs)
91 0