You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
792 B
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
char s[N];
int presum[N];
int precnt[3][N];
int main()
{
scanf("%s", s + 1);
int n = strlen(s + 1);
for (int i = 1; i <= n; i++) presum[i] = presum[i - 1] + s[i] - '0';
for (int i = 1; i <= n; i++)
precnt[presum[i] % 3][i] = 1;
for (int k = 0; k < 3; k++)
for (int i = 1; i <= n; i++)
precnt[k][i] += precnt[k][i - 1];
long long ans = count(s + 1, s + n + 1, '0');
for (int i = 1; i <= n; i++)
if (s[i] == '0' && s[i + 1] == '0')
ans += precnt[presum[i - 1] % 3][i - 1] + (presum[i - 1] % 3 == 0);
printf("%lld", ans);
return 0;
}