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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你生成这份Excel。以下是一个示例代码: ```python import pandas as pd import random # 定义数据集文件名 filename = 'object_dataset.xlsx' # 定义物体信息 categories = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] objects = [] for i in range(100): category = random.choice(categories) depth = random.randint(0, 255) area = random.randint(100, 10000) dangerousness = random.uniform(0, 1) objects.append({'category': category, 'depth': depth, 'area': area, 'dangerousness': dangerousness}) # 将物体信息存储到DataFrame中 df = pd.DataFrame(objects) # 将DataFrame写入Excel文件 df.to_excel(filename, index=False) ``` 你可以根据需要修改物体信息的范围和生成数据的数量来创建一个满足要求的数据集。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值