链接:https://ac.nowcoder.com/acm/contest/884/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.

题解:首先我们分析满足条件的情况:能被300整除的子串
   1.串的最后两位肯定为00
   2.前面的各位和肯定能整除以3
   
   所以我们可以 开一个变量 sum, 记录下 ”前i位的和 mod 3 的情况“。 情况有三种:sum==0,sum==1,sum==2
   再开一个数组 cnt[],记录下 ”前i为的和 mod 3 的情况 出现的次数“。也就是只记录 cnt[0],cnt[1],cnt[2]
   
   为什么要记录 1和2的情况呢?举个例子:
   在[L,R]的区间中,如果 在L处的sum 和 在R处的sum相等的话,那么就意味着 L~R的数位和 mod 3 == 0.也就是[L,R]为一个能整除3的子串。
   
   
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#pragma GCC optimize(2)
//#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include<fstream>
#include<sstream>
#include<iterator>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<set> using namespace std;
typedef double dou;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii; #define pai acos(-1.0)
#define M 100005
#define inf 0x3f3f3f3f
#define mod 1000000007
#define left k<<1
#define right k<<1|1
#define lson L, mid, left
#define rson mid + 1, R, right
#define W(a) while(a)
#define ms(a,b) memset(a,b,sizeof(a))
#define Abs(a) (a ^ (a >> 31)) - (a >> 31) char str[M];
int cnt[M]; int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
W (cin>>str) {
ll ans = ;
int len = strlen(str);
for (int i = ; i < len; i++) {
if (str[i] == '') ans++;//先统计单个0的情况
}
cnt[] = ;
int sum = ;//前i位mod 3的值
for (int i = ; i < len; i++) {
sum += str[i] - '';
sum %= ;
if (i + < len && str[i] == '' && str[i + ] == '') {//满足尾数为00 且在长度范围内
ans += cnt[sum];
}
cnt[sum]++;//cnt[0] 、cnt[1]、cnt[2]
}
cout << ans << endl;
}
return ;
}

2019牛客网暑假多校训练第四场 K —number的更多相关文章

  1. 2019牛客多校训练第四场K.number(思维)

    题目传送门 题意: 输入一个只包含数字的字符串,求出是300的倍数的子串的个数(不同位置的0.00.000等都算,并考虑前导零的情况). sample input: 600 1230003210132 ...

  2. nowcoder(牛客网)提高组模拟赛第四场 解题报告

    T1 动态点分治 就是模拟..... 但是没有过!! 看了题解之后发现.... 坑点:有可能 \(x<=r\),但是

  3. HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)

    HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...

  4. 牛客网 湖南大学2018年第十四届程序设计竞赛重现赛 A game

    链接:https://www.nowcoder.com/acm/contest/125/A来源:牛客网 Tony and Macle are good friends. One day they jo ...

  5. 2018牛客网暑假ACM多校训练赛(第三场)I Expected Size of Random Convex Hull 计算几何,凸包,其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-I.html 题目传送门 - 2018牛客多校赛第三场 I ...

  6. 2018牛客网暑假ACM多校训练赛(第三场)G Coloring Tree 计数,bfs

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-G.html 题目传送门 - 2018牛客多校赛第三场 G ...

  7. 2018牛客网暑假ACM多校训练赛(第三场)D Encrypted String Matching 多项式 FFT

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-D.html 题目传送门 - 2018牛客多校赛第三场 D ...

  8. 2018牛客网暑假ACM多校训练赛(第二场)E tree 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round2-E.html 题目传送门 - 2018牛客多校赛第二场 E ...

  9. 2018牛客网暑假ACM多校训练赛(第十场)H Rikka with Ants 类欧几里德算法

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round10-H.html 题目传送门 - https://www.n ...

随机推荐

  1. Asp.net MVC中ReturnUrl的使用

    1.控制器(Controller)[HttpPost][ValidateInput(false)]public ActionResult Add(Article article,string retu ...

  2. AS-PATH(路径属性)路由路径欺骗术

    AS-PATH(路径属性)路由路径欺骗术: ①:抓取感兴趣流量——前缀与访问 ②:创建路由地图 ③:路由地图第一法则——permit 10 ④:在第一法则中,匹配(感兴趣流量) ⑤:设置 路径欺骗术— ...

  3. Vulkan SDK 之 DrawCube

    Waiting for a Swapchain Buffer Beginning the Render Pass Bind the Pipeline Bind the Descriptor Sets ...

  4. jQuery中:first,:first-child,first()的使用区别

    ul li:first  先获取页面中所有li节点对象数组,然后返回数组中的第一个li节点对象 . :first-child  选择器选取属于其父元素的第一个子元素的所有元素. first() 返回被 ...

  5. 第九篇 AJAX

    AJAX 阅读目录(Content) 概述 AJAX常见应用情景 AJAX的优缺点 jQuery实现的AJAX $.ajax参数 AJAX请求如何设置csrf_token 序列化 Django内置的s ...

  6. 实验3- 熟悉常用的 HBase 操作

        石家庄铁道大学信息科学与技术学院               实验报告 2018年----2019年  第一学期                       题目:  熟悉常用的 HBase ...

  7. CodeForces - 402B Trees in a Row (暴力)

    题意:给定n个数,要求修改其中最少的数,使得这n个数满足ai + 1 - ai = k. 分析: 暴力,1000*1000. 1.这n个数,就是一个首项为a1,公差为k的等差数列.k已知,如果确定了a ...

  8. windows操作

    5.windows激活 数字权利许可工具激活 https://jingyan.baidu.com/article/9113f81b4d49232b3314c75e.html 4.网络连接不上 原因,v ...

  9. 【pwnable.kr】 flag

    pwnable从入门到放弃 第四题 Download : http://pwnable.kr/bin/flag 下载下来的二进制文件,对着它一脸懵逼,题目中说是逆向题,这我哪会啊... 用ida打开看 ...

  10. spring+springMVC+mybatis , 项目启动遇坑

    github上找的框架组合例子 结合自己的数据库作为新项目开发. 但是项目启动时,tomcat启动失败: 检查不出错误. 于是改换maven引入jetty插件来启动项目, 结果在未改动的任何代码的情况 ...