Educational Codeforces Round 8 B 找规律
2016-07-15 23:36
387 查看
B. New Skateboard
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
The only line contains string s (1 ≤ |s| ≤ 3·105). The string s contains only digits from 0 to 9.
Output
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.
Examples
Input
Output
Input
Output
Input
Output
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
The only line contains string s (1 ≤ |s| ≤ 3·105). The string s contains only digits from 0 to 9.
Output
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.
Examples
Input
124
Output
4
Input
04
Output
3
Input
5810438174
Output
9 题意:给你一个串 询问子串中可以除尽4的个数 子串允许有前导‘0’ 题解:1.首先对于单个字符 ‘0’ ‘4’ ‘8’可以被除进 2.对于两个连续字符 组成的数exm=(s[i]-'0')+(s[i-1]-'0')*10必须除尽4 3.我们知道100能够整除4 所以只需要统计 以 (满足条件的两个连续字符)为后缀的子串的个数 求和
#include<bits/stdc++.h> #define ll __int64 #define mod 1e9+7 #define PI acos(-1.0) #define bug(x) printf("%%%%%%%%%%%%%",x); #define inf 1e8 using namespace std; #define ll long long char s[300005]; map<int,int> mp; ll ans; int main() { scanf("%s",s); ans=0; for(int i=0;i<=96;i+=4) mp[i]=1; int len=strlen(s); for(int i=0;i<len;i++) { if((s[i]-'0')%4==0) ans++; int exm=(s[i]-'0')+(s[i-1]-'0')*10; if(mp[exm]) { ans=ans+i; } } printf("%I64d\n",ans); return 0; }
相关文章推荐
- HDU 1253 胜利大逃亡
- STM32学习笔记之独立看门狗(IWDG)的使用
- The Review For PostGraduate No.16
- CVPR2016论文+code
- JavaScript事件知识点总结
- nil,null,NsNull
- Linux 普通进程 后台进程 守护进程
- 聊聊高并发系统限流特技-2
- js 学习基础 随机生成表格 颜色随机
- iOS 25个性能优化/内存优化常用方法
- HDU——1045Fire Net(最大匹配)
- 比较ascill码表后排序
- jQuery之动画介绍
- svn for mac 的注意事项
- (OK) Android 添加 开机启动 脚本
- cocos2dx学习之路----第七篇(坐标系统中本地坐标与世界坐标的转换详解)
- Num73 投资管理_简单分页查询&多条件分页
- Merge Sorted Array
- 工作中常用的Linux命令(不断更新中)
- RxAndroid 与OkHttpClient打造下载实例