Codeforces 628 B.New Skateboard
1 second
256 megabytes
standard input
standard output
Max wants to buy a new skateboard. He has calculated the amount of money that is needed to buy a new skateboard. He left a calculator on the floor and went to ask some money from his parents. Meanwhile his little brother Yusuf came and started to press the keys randomly. Unfortunately Max has forgotten the number which he had calculated. The only thing he knows is that the number is divisible by 4.
You are given a string s consisting of digits (the number on the display of the calculator after Yusuf randomly pressed the keys). Your task is to find the number of substrings which are divisible by 4. A substring can start with a zero.
A substring of a string is a nonempty sequence of consecutive characters.
For example if string s is 124 then we have four substrings that are divisible by 4: 12, 4, 24 and 124. For the string 04 the answer is three: 0, 4, 04.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
The only line contains string s (1 ≤ |s| ≤ 3·105). The string s contains only digits from 0 to 9.
Print integer a — the number of substrings of the string s that are divisible by 4.
Note that the answer can be huge, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
124
4
04
3
5810438174
9
题意就是找有多少个子串可以被4整除。因为100可以被4整除,所以100位以上的都可以,只要看个位和十位的数是否可以被4整除。
写这个题的时候傻了,只是算一下个位和十位就好了,然后把前面的位数加上就可以了,。
因为想一下,举个例子,xyzab,如果ab%4==0,那么xyzab可以组成多少个子串呢?那就是ab,zab,yzab,xyzab,就是a所在的位置啊,因为是从0开始的,所以位数+1,我可能是个傻子。。。
这样这道题就可以a了。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=*1e5+;
char a[N];
int main(){
while(~scanf("%s",a)){
ll len,cnt,ans;
len=strlen(a);
ans=;
for(int i=;i<len;i++){
if((a[i]-'')%==)ans++;
}
for(int i=;i<len-;i++){
cnt=(a[i]-'')*+(a[i+]-'');
if(cnt%==)ans+=i+;
}
cout<<ans<<endl;
}
return ;
}
最新文章
- Redis系列一之数据结构
- iOS开发--应用设置及用户默认设置【2、读取应用中的设置】
- OpenGL-选择与拾取
- javascrpt随笔
- JAXB--学习2
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block
- S3C2440之IIC裸机驱动
- [Linux] PHP程序员玩转Linux系列-lnmp环境的搭建
- Android 结合实际项目学会ListView局部刷新和相关知识《一》
- 洛谷P2120 [ZJOI2007]仓库建设 斜率优化DP
- [swarthmore cs75] Lab 0 Warmup &; Basic OCaml
- Codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
- hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询
- 记一次排查局网内的ARP包 “不存在的” MAC 地址及 “不存在的”IP 所发的ARP包
- grafana,graphite,influxdb with docker
- 在VS中让一个JS文件智能提示另一个JS文件中的成员2--具体引用
- Oracle win32_11gR2_client.zip
- MQTT的学习研究(十)【转】mosquitto——一个开源的mqtt代理
- la3523 白书例题 圆桌骑士 双联通分量+二分图
- 移动端base.css