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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章