[ccJULY14] Count Substrings

描述 Description

Given a string S consisting of only 1s and 0s, find the number of substrings which start and end both in 1.

In this problem, a substring is defined as a sequence of continuous characters Si, Si+1, …, Sj where 1 ≤ i ≤ j ≤ N.

输入格式 InputFormat

First line contains T, the number of testcases. Each testcase consists of N(the length of string) in one line and string in second line.

输出格式 OutputFormat

For each testcase, print the required answer in one line.

样例输入 SampleInput

2
4
1111
5
10001

样例输出 SampleOutput

10
3


CodeChef CSUB


水。

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int i,j,t,n,m,l,r,k,z,y,x;
int T;
char ch;
int main()
{
    scanf("%d",&T); 
    while (T--)
    {
        scanf("%d\n",&n);
        m=0;
        for (i=1;i<=n;i++) 
        {
            ch=getchar();
            if (ch=='1') m++;
        }
        printf("%lld\n",((long long)m+1)*m/2);
    }
    return 0;
}