CF 628B New Skateboard

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 412424 and 124. For the string 04 the answer is three: 0404.

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
Copy
124
output
Copy
4
input
Copy
04
output
Copy
3
input
Copy
5810438174
output
Copy
9


       题目大意:给定一个数字(长度<=3*10^5),判断其能被4整除的连续子串有多少个

  解题思路:注意一个整除4的性质: 若bc能被4整除,则a1a2a3a4...anbc也一定能被4整除;

       利用这个性质,先特判第一位数字是否能被4整除,可以则++cnt,

       之后从第二位数字开始,设当前位为i,先判断a[i]能否被4整除,可以则++cnt,

       再判断a[i-1]*10+a[i]能否被4整除,可以则cnt = cnt + (i)

  相关证明: 设一整数各个位置为a1,a2,a3,...,an,b,c;

        则(a1a2a3...an b c)%4 =( (a1a2a3...an)*100 + bc ) % 4

        = (a1a2a3...an)*100 % 4 + (bc)%4 = 0 + (bc)%4 = (bc)%4 (100能被4整除)

  注意,此题数据很大,要用long long

#include<iostream>
#include<cstring>
using nsmespace std;
int main(){
	chsr s[300005];
	cin>>s;
	int len=strlen(s);
	long long sum=0;
	//处理前第一个数
	int num=s[0]-'0';
	if(!(num%4))sum++;
	for(int i=1;i<len;i++){
		//判断当前位以及往前的位
		num=s[i]-'0';
		if(!(num%4))sum++;
		num=(s[i-1]-'0')*10+s[i]-'0';
		if(!(num%4))sum+=i;
	}
	cout<<sum;
	return 0;
}

总结:其实这题的关键就是100%4=0,就是找能整除4的最小10的倍数,本题中就是100

举个栗子:给定一个数字(长度<=3*10^5),判断其能被2整除的连续子串有多少个?

#include<iostream>
#include<cstring>
//能整除2的最小10的倍数是10
using namespace std;
int main(){
	char s[300005];
	cin>>s;
	int len=strlen(s);
	long long sum=0;
	int num;
	for(int i=0;i<len;i++){
		num=s[i]-'0';
		if(!(num%2))sum+=i+1;
	}
	cout<<sum;
	return 0;
}

再举个栗子:给定一个数字(长度<=3*10^5),判断其能被40整除的连续子串有多少个?

#include<iostream>
#include<cstring>
//能整除40的最小10的倍数是1000
using namespace std;
int main(){
	char s[300005];
	cin>>s;
	int len=strlen(s);
	long long sum=0;
	//处理前2位 
	int num=s[0]-'0';
	if(!(num%40))sum++;
	num=s[1]-'0';
	if(!(num%40))sum++;
	num=(s[0]-'0')*10+s[1]-'0';
	if(!(num%40))sum++;
	for(int i=2;i<len;i++){
		num=s[i]-'0';
		if(!(num%40))sum++;
		num=(s[i-1]-'0')*10+s[i]-'0';
		if(!(num%40))sum++;
		num=(s[i-2]-'0')*100+(s[i-1]-'0')*10+s[i]-'0';
		if(!(num%40))sum+=i-1;
	}
	cout<<sum;
	return 0;
}



猜你喜欢

转载自blog.csdn.net/weixin_37609825/article/details/80111816