Codeforces Round #548 (Div. 2) A. Even Substrings

You are given a string ?=?1?2…?? of length ?, which only contains digits 1, 2, …, 9.

A substring ?[?…?] of ? is a string ????+1??+2…??. A substring ?[?…?] of ? is called even if the number represented by it is even.

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

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

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

Output
Print the number of even substrings of ?.

Examples
inputCopy
4
1234
outputCopy
6
inputCopy
4
2244
outputCopy
10
Note
In the first example, the [?,?] pairs corresponding to even substrings are:

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

题意:计数以偶数结尾的连续的子串的个数

思路:遍历一遍,如果当前数字是偶数,则表示可以以它结尾,贡献是它所在的位置大小,加到答案里就好。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define PI acos(-1)
#define ms(x,y) memset(x, y, sizeof(x))
using namespace std;

const int maxn = 65005;
char s[maxn];

int main()
{
   int n;
   cin >> n;
   scanf("%s", s+1);
    
   ll ans = 0;
   for(int i=1;i<=n;i++)
       if((s[i] - '0') % 2 == 0)
           ans += i;
    
   cout << ans << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值