CodeForces 628 B New Skateboard
来源:互联网 发布:网络的图标不见了 编辑:程序博客网 时间:2024/06/05 01:11
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 by4.
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 usegets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead ofScanner/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整除
题解:直接找一个子串的最后两位看他能否整除4,如果能,那么这个子串就能整除4,所以枚举每相邻两位看是否能整除4即可
#include<cstdio>#include<cstring>const int maxn = 3e5 + 10;char s[maxn];int main(){ while(~scanf("%s",s)) { int len = strlen(s); __int64 ans = 0; int k = s[0] - '0'; if(k % 4 == 0) ans++; for(int i = 1 ; i < len ; i++) { int m = s[i] - '0'; int p = s[i-1] - '0'; if(m % 4 == 0) ans++; if((p * 10 + m) % 4 == 0) { ans += i; } } printf("%I64d\n",ans); } return 0;}
- codeforces 628 B. New Skateboard
- codeforces 628B New Skateboard
- CodeForces 628B New Skateboard
- CodeForces 628B New Skateboard
- CodeForces 628B New Skateboard
- CodeForces 628B New Skateboard
- CodeForces 628 B New Skateboard
- codeforces 628B New Skateboard
- Codeforces 628B New Skateboard (数学)
- Codeforces 628B New Skateboard【数学】
- CodeForces 628 B. New Skateboard(水~)
- CodeForces 628B New Skateboard (数学)
- Codeforces 628B New Skateboard【数学】
- (CodeForces 628B)New Skateboard
- 【CodeForces 628B】 New Skateboard (数学水)
- codeforces 628 B. New Skateboard (数学-被4整除)
- CF 628B. New Skateboard
- Educational Codeforces Round 8-B. New Skateboard
- Effective C++_Item16笔记
- linux内核综述
- 在Linux下搭建SVN服务器
- Laravel 在views中加载公共页面怎么实现
- HDOJ 1272 小希的迷宫(并查集)
- CodeForces 628 B New Skateboard
- java设计模式_开放封闭原则
- 多校联合训练8&&HDU 5828
- Java--------迭代器Iterator
- 使用egypt+graphviz生成函数调用关系图示例
- POJ -- 1458 Common Subsequence
- poj3126 Prime Path
- eg:输入1 6 5 其中6是偶数,二进制表示为110,反转后为011,代表3,所以最终输出1 3 5.
- float与double的MAX-MIN