2019牛客多校训练营(第四场)——K

原题题址

题目描述

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.

输入描述:

A single line consisting a string consisted of characters ‘0’ to ‘9’.

输出描述:

The number of substrings that are multiples of 300 when considered as decimal integers.

示例1

输入

600

输出

4

说明
‘600’, ‘0’, ‘0’, ‘00’ are multiples of 300. (Note that ‘0’ are counted twice because it appeared two times)

示例2

输入

123000321013200987000789

输出

55
备注:

let the string in the input be s, 1 ≤ ∣ s ∣ ≤ 1 0 5 1 \leq |s| \leq 10^5 1s105

这题我其实也就想了个思路,码代码队友比我行,我就偷懒了。
这题思路不难啊。
首先你得发现个问题,300的倍数最少俩0,不过值得一提的是题目把0也可以当做300的倍数

那么发发现个之后呢,先处理非0的,那么要找到一串0(最少俩),找0前面的系数,问题就转换为找3的倍数问题,那就把这一串0的前面的前缀和求出来,找3的倍数的区间即可,要注意的是,这个区间的右端点必须与0串相连。前缀和找3的倍数,不如让前缀和对3取模,那么只要端点的差值为0,就是满足的(设左端点,右端点的取模前缀和都为x,那么左,右端点的前缀和设为3k+x,3m+x,显然两者作差是3的倍数,0也可以的话)

前缀和的处理一定记住,要包括0串的前一个数字,换句话说就是,找这个数字之前的和他前缀和取模相等的数字,但是需要注意的是,如果这个数字原数字就是3的倍数,那还得加一个它自身。这一套操作下来就能解决非0系数的问题,但是请注意,0的个数是会影响答案的。设满足要求的系数个数(即0串前面能被3整除的子串)为 P P P个,每多一个0,这个串对应的满足要求的子串个数就会多 P P P个,就是最终这个串的满足要求的子串(0的个数 - 1)*( P P P

前缀在前面出现了几次就可以用数字存,边更新边使用。

然后处理全为0的,也就是排列组合可得到n(n+1)/2的公式,这种方法需要注意的是,处理的0必须是原来字符串的0。

到此为止,就全部讲完了,看不懂可以多看两遍,画一画,应该就能理解。

不想码代码?

以下代码由 zy dalao在不知情的情况下支撑,建议自己交,这个代码能理解理解,不能理解交着玩玩也行。

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#define int long long
#define Mod 1000000007
#define pi (acos(-1))
#define inf 0x3f3f3f3f
#define M 100005
using namespace std;
 
int aa[M];
int a[M];
int m[M];
int ans2[M];
int ans1[M];
int ans0[M];
 
signed main(){
    char s[M];
    gets(s);
    int n=strlen(s);
    int sum=0;
    a[0]=0;
    a[0]=0;
    int k=0;
    for(int i = 0 ; s[i] != '\0' ; i ++ )
    {
        a[i+1]=a[i]+s[i]-'0';
        aa[i+1]=s[i]-'0';
        m[i+1]=a[i+1]%3;
        if(s[i]=='0')
        {
            k++;
            sum+=k;
        }
        else
        {
            k=0;
        }
    }
    aa[n+1]=1;
    aa[n+2]=1;
    for(int i = 1 ; i <= n ; i ++ )
    {
        if(m[i]==0)
        {
            ans0[i]=ans0[i-1]+1;
            ans1[i]=ans1[i-1];
            ans2[i]=ans2[i-1];
        }
        else if(m[i]==1)
        {
            ans0[i]=ans0[i-1];
            ans1[i]=ans1[i-1]+1;
            ans2[i]=ans2[i-1];
        }
        else if(m[i]==2)
        {
            ans0[i]=ans0[i-1];
            ans1[i]=ans1[i-1];
            ans2[i]=ans2[i-1]+1;
        }
    }
     
    int last=0;
    int cnt=0;
    int lp=0;
    for(int i = 1 ; i <= n+2 ; i ++ )
    {
         
        if(aa[i]!=0&&aa[i-1]==0&&aa[i-2]==0&&(i-2)>0&&lp>0)
        {
            if(last==1)
            sum+=(ans1[lp-1])*(cnt-1);
            else if(last==2)
            sum+=ans2[lp-1]*(cnt-1);
            else sum+=(ans0[lp-1]+1)*(cnt-1);
        }
        if(aa[i]==0&&aa[i-1]==0&&aa[i-2]!=0&&(i-2)>0)
        {
            last=m[i-2];
            lp=i-2;
        }
        if(aa[i]==0)
        cnt++;
        else
        cnt=0;
    }
    printf("%lld",sum);
    return 0;
}

.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值