Example 1:
Input: 600
Output: 4
说明:'600', '0', '0', '00' are multiples of 300. (Note that '0' are counted twice because it appeared two times)
Example 2:
Input: 123000321013200987000789
Output: 55
Question is intended: to a full string of numeric characters, the number of sub-strings is determined multiple of 300. 0,00,000 and so calculate and consider the preamble is zero.
Solution: Let seeking is a multiple of 300, but then we can split into both a multiple of 3 is a multiple of 100
then so that you can find some rules:
1. For a multiple of three, it is the combined value of the individual bits on and on the answer is 0 3 modulo
two at least 0 2. for each multiple of 100, it is to ask (source: https: //blog.csdn.net/c___c18/article/details/97561689)
So we need to loop on each digital digits were added together, and if it is 0 is 0 and the current one is 0 for special treatment.
AC Code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int inf=0x3f3f3f3f,maxn=1e5+5; 4 char str[maxn]; 5 long long summ,now,a[3],len; 6 int main() 7 { 8 scanf("%s",str); 9 len=strlen(str); 10 for(int i=len-1;i>=0;i--){ 11 now+=str[i]-'0'; 12 now%=3; 13 if(str[i]=='0')summ++; 14 if(str[i]=='0'&&str[i+1]=='0'&&i!=len-1){ 15 a[now]++; 16 } 17 summ+=a[now]; 18 } 19 printf("%lld\n",summ); 20 return 0; 21 }