Dynamic Programming - number of cattle and more passengers school

Dynamic Programming - number of cattle and more passengers school

The meaning of problems: counting the number of substrings in the string 300 can be divisible

Ideas: the number divisible by 3, you and divisible by 3. Operators comprising '00' prefix before a number of 3% by number, other than 0 is divisible by the number 300 can be

LL a[5];
int main()
{
    string s;cin >> s;
    int len = s.size();
    LL sum = 0, ans = 0;
    a[0] = 1;
    for(int i = 0;i < len;++i)
    {
        if(s[i] == '0' && s[i+1] == '0')
            ans += a[sum];
        sum = (sum+s[i]+'0')%3;
        a[sum]++;
    }
    for(int i = 0;i < len;++i)
        if(s[i] == '0') ans++;
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/shuizhidao/p/11258565.html