1139A A. Even Substrings(找规律)

You are given a string s=s1s2…sn of length n, which only contains digits 1, 2, …, 9.

A substring s[l…r] of s is a string slsl+1sl+2…sr. A substring s[l…r] of s is called even if the number represented by it is even.

Find the number of even substrings of s. Note, that even if some substrings are equal as strings, but have different l and r, they are counted as different substrings.

Input
The first line contains an integer n (1≤n≤65000) — the length of the string s.

The second line contains a string s of length n. The string s consists only of digits 1, 2, …, 9.

Output
Print the number of even substrings of s.

Examples
input

4
1234
output
6
input
4
2244
output
10
Note
In the first example, the [l,r] pairs corresponding to even substrings are:

s[1…2]
s[2…2]
s[1…4]
s[2…4]
s[3…4]
s[4…4]
In the second example, all 10 substrings of s are even substrings. Note, that while substrings s[1…1] and s[2…2] both define the substring “2”, they are still counted as different substrings.

题目大意: 找出以偶数字符结尾的子序列个数,其中l<=r。
一开始用尺取法判断每段区间的个数,结果超时。。。通过提交后给的测试用例才发现是个规律题,只要找出偶数字符的下标相加就行了(下标从1开始)。

#include <iostream>
#include <cstring> 
using namespace std;
int main() {
	int n;
	long long ans = 0;
	scanf("%d", &n);
	getchar(); // 获取换行符
	char s[65005];
	scanf("%s", s);
	for(int i = 0; i < n; i++) {
		if((s[i] - '0') % 2 == 0) {
			ans += i + 1;
		}
	}
	printf("%I64d\n", ans);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/dream_it_/article/details/88737105
今日推荐