2019牛客多校四 K number(dp 或 前缀和)

 

number

题目描述 

 

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 \leq |s| \leq 10^51s105.

给一个字符串,问有多少个子串能够整除 300 , 包含前导 0 。

dp[i][j] 表示在 i 位置及之前所有位置形成的数 % 300 == j 的数量

那么当前状态可推向 [i + 1][(j * 10 + s[i + 1] - '0') % 300]

故有状态转移方程  dp[i][(j * 10 + s[i] - '0') % 300] += dp[i - 1][j] .. 每一次注意初始值 dp[i][s[i] - '0'] = 1.

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;

const int maxn = 1e5 + 10;
LL dp[maxn][310];
char s[maxn];

int main() {
    while(~scanf("%s",s + 1)){
        int len = strlen(s + 1);
        clr(dp,0);
        for(int i = 1;i <= len;++ i){
            int tmp = s[i] - '0';
            dp[i][tmp] = 1;
            for(int j = 0;j < 300;++ j)
                dp[i][(j * 10 + tmp) % 300] += dp[i - 1][j];
        }
        LL ans = 0;
        for(int i = 1;i <= len;++ i)
            ans += dp[i][0];
        cout << ans << endl;
    }
    return 0;
}
View Code(dp)

 

当然这个题也可以前缀和做,显然 % 300 == 0 需要两个条件,末尾两个0,前面的 % 3 == 0,故可以记录当前位置之前  前缀和 % 3 == 0,  1 , 2 的数量,然后加上对应的数量即可

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)
 
using namespace std;
 
const int maxn = 1e5 + 10;
char s[maxn];
int sum[maxn];
int mp[maxn * 10];
 
int main() {
    while(~scanf("%s",s + 1)){
        clr(mp,0);
        int sz = strlen(s + 1);
        LL ans = 0;
        for(int i = 1;i <= sz;++ i){
            sum[i] = sum[i - 1] + s[i] - '0';
            sum[i] %= 3;
            if(s[i] == '0') ++ ans;
        }
        for(int i = 1;i + 1 <= sz;++ i){
            if(s[i] == '0' && s[i + 1] == '0'){
                ++ ans;
//                printf("===>%d , ",mp[sum[i - 1]]); debug(sum[i - 1]);
                ans += mp[sum[i - 1]];
                if(sum[i - 1] % 3 != 0) -- ans;
            }
//            debug(ans);
            ++ mp[sum[i]];
        }
//        debug(ans);
        cout << ans << endl;
    }
    return 0;
}
View Code(前缀和)

 

转载于:https://www.cnblogs.com/rookie-acmer/p/11258415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值