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

You are given a string 𝑠=𝑠1𝑠2𝑠𝑛s=s1s2…sn of length 𝑛n, which only contains digits 11, 22, ..., 99.

A substring 𝑠[𝑙𝑟]s[l…r] of 𝑠s is a string 𝑠𝑙𝑠𝑙+1𝑠𝑙+2𝑠𝑟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𝑛650001≤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 11, 22, ..., 99.

Output

Print the number of even substrings of 𝑠s.

Examples
input
Copy
4
1234
output
Copy
6
input
Copy
4
2244
output
Copy
10
Note

In the first example, the [𝑙,𝑟][l,r] pairs corresponding to even substrings are:

  • 𝑠[12]s[1…2]
  • 𝑠[22]s[2…2]
  • 𝑠[14]s[1…4]
  • 𝑠[24]s[2…4]
  • 𝑠[34]s[3…4]
  • 𝑠[44]s[4…4]

In the second example, all 1010 substrings of 𝑠s are even substrings. Note, that while substrings 𝑠[11]s[1…1] and 𝑠[22]s[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;
}

  

posted @ 2019-03-23 12:57  HazelNuto  阅读(233)  评论(0编辑  收藏  举报