More than 2019 cattle off summer school camp (fourth) K number (thinking)

传送门
300iq loves numbers who are multiple of 300.
One day he got a string consisted of numbers. He wants to know how many substrings in the string are multiples of 300 when considered as decimal integers.
Note that leading and trailing zeros are allowed (both in original string and substrings you chose) and the same substring appearing in different places can be counted multiple times.

Meaning of the questions:

To a string (by the numeral '0' - '9' composition), and asked how many non-empty string is a multiple of 300

Ideas:

When i bits to each of the front of the recording and the value of mod 3, where cnt into an array, which records the current value appears several times, the value of the position i, to any position in front of the section, and this value appears certainly mod 3 = 0, 0 if there are two back again, it is equal to 0 mod 300

Code:

#include <iostream>
#include<algorithm>
#include <stdio.h>
#include <string>
#include <string.h>
#include <map>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <deque>
#include <stack>
#include <iostream>
typedef long long ll;
using namespace std;
const int N=1e5+10;
int a[100];
int n;
char ch[N];
ll cnt[4];
int main()
{
    while(scanf("%s",ch)!=-1)
    {
        ll ans=0;
        int x=0;
        int len=strlen(ch);
        cnt[0]=1;
        for(int i=0;i<len;i++)
        {
            if(ch[i]=='0')ans++;
            if(ch[i]=='0'&&ch[i+1]=='0')ans+=cnt[x];
            x=(x+ch[i]-'0')%3;
            cnt[x]++;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
Published 160 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44091178/article/details/104819592
Recommended