当前位置: 首页 > news >正文

C. Good Subarrays

time limit per test

2 seconds

memory limit per test

256 megabytes

You are given an array a1,a2,…,ana1,a2,…,an consisting of integers from 00 to 99. A subarray al,al+1,al+2,…,ar−1,aral,al+1,al+2,…,ar−1,ar is good if the sum of elements of this subarray is equal to the length of this subarray (∑i=lrai=r−l+1∑i=lrai=r−l+1).

For example, if a=[1,2,0]a=[1,2,0], then there are 33 good subarrays: a1…1=[1],a2…3=[2,0]a1…1=[1],a2…3=[2,0] and a1…3=[1,2,0]a1…3=[1,2,0].

Calculate the number of good subarrays of the array aa.

Input

The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains one integer nn (1≤n≤1051≤n≤105) — the length of the array aa.

The second line of each test case contains a string consisting of nn decimal digits, where the ii-th digit is equal to the value of aiai.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case print one integer — the number of good subarrays of the array aa.

Example

Input

Copy

3
3
120
5
11011
6
600005

Output

Copy

3
6
1

Note

The first test case is considered in the statement.

In the second test case, there are 66 good subarrays: a1…1a1…1, a2…2a2…2, a1…2a1…2, a4…4a4…4, a5…5a5…5 and a4…5a4…5.

In the third test case there is only one good subarray: a2…6a2…6.

解题说明:此题是一道数列题,确保区间内每个元素的和为区间长度。因为区间和等于前缀和相减即sum[i]−sum[j−1]。所以由题意可以得到:sum[i]−sum[j−1]=i−j+1。移项后得:sum[i]−i=sum[j−1]−j+1。统计 sum[i]−i的个数即可。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<map>
#include<queue>
#include<vector>
#include<set>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
const ll mm = 1e5 + 60;
ll s[mm], n, ans;
map<ll, ll>mp;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		memset(s, 0, sizeof(s));
		ans = 0;
		mp.clear();
		mp[0] = 1;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			char c;
			cin >> c;
			s[i] = s[i - 1] + c - '0';
			ans += mp[s[i] - i]++;
		}
		cout << ans << endl;
	}
	return 0;
}

相关文章:

  • 操作系统基础:07 我们的任务
  • MySQL-锁
  • leetcode 3508 设计路由器 模拟 深拷贝 二分 bound
  • (即插即用模块-特征处理部分) 三十二、(TGRS 2024) MDAF 多尺度双表示对齐过滤器
  • 深入解析TCP拥塞控制机制:从原理到现代算法优化
  • 如何在Agent中设置Memory
  • 【数学建模】佳点集(Good Point Set)在智能优化算法中的应用与实现
  • ChatGPT-如何让AI写作不那么生硬!
  • 学习笔记九——Rust所有权机制
  • 探索 MCP 和 A2A 协议: 本质上新协议都基于 HTTP的
  • 【Spring底层分析】Spring IoC
  • RocketMQ 安装时启动一闪而过 start mqnamesrv.cmd
  • 插值算法 - 最近邻插值实现
  • 用python写一个简单的射击游戏
  • skynet.socket 完整通信流程
  • Mysql8配置文件
  • 【贪心之摆动序列】
  • 三、Virtual Device Manager
  • CST1019.基于Spring Boot+Vue智能洗车管理系统
  • 从零手写RPC-version0
  • 鸿蒙电脑正式发布,余承东:国产软件起步晚,基础弱,探索面向未来的电脑体验
  • 巴基斯坦副总理兼外长达尔将访华
  • 解放日报“解码上海AI产业链”:在开源浪潮中,集聚要素抢先机
  • 旅马大熊猫“福娃”“凤仪”平安回国
  • 特朗普指控FBI前局长“暗示刺杀总统”,“8647”藏着什么玄机?
  • 戛纳打破“疑罪从无”惯例,一法国男演员被拒之门外