L - New Skateboard

来源:互联网 发布:茶叶出口数据统计图 编辑:程序博客网 时间:2024/06/10 15:29

L - New Skateboard

Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
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.

Sample Input
Input
124
Output
4
Input
04
Output
3
Input
5810438174
Output
9


1.题意:求一个数字串可以分解成多少个能被4整除的子串(子串可相**同)。
2. 思路:用数论:最后两位能被4整除的数能被4整除,对数字串扫描。
3. 失误:应注意扫描的过程一定要清楚,提取相同的部分,不同的再做判断,如代码中的0位。
4. 代码如下:


#include<cstdio>#include<cstring>char str[400000+10];int main(){    __int64 i,l,cnt,sh,g;    while(~scanf("%s",str))    {        l=strlen(str);        cnt=0;        for(i=l-1;i>0;--i)//逐位判断         {            g=str[i]-'0';            sh=(str[i-1]-'0')*10+g;            if(sh%4==0)            {                cnt+=i;             }             if(g%4==0)             {                ++cnt;              }           }        if((str[0]-'0')%4==0)//判断0位的是否能整除         ++cnt;        printf("%I64d\n",cnt);    }    return 0; } 
0 0
原创粉丝点击