The great dog detective Sherlock Bones is on the verge of a new discovery. But for this problem, he needs the help of his most trusted advisor -you- to help him fetch the answer to this case.

He is given a string of zeros and ones and length N.

Let F(x, y) equal to the number of ones in the string between indices x and yinclusively.

Your task is to help Sherlock Bones find the number of ways to choose indices (i, j, k) such that i < j < ksj is equal to 1, and F(i, j) is equal to F(j, k).

Input

The first line of input is T – the number of test cases.

The first line of each test case is an integer N (3 ≤ N ≤ 2 × 105).

The second line is a string of zeros and ones of length N.

Output

For each test case, output a line containing a single integer- the number of ways to choose indices (i, j, k).

Example

Input
3
5
01010
6
101001
7
1101011
Outpu2
3
7
题意:
给定01字符串,求有多少个三元组i,j,k满足i<j<k且F(i, j)=F(j, k).其中F(x,y)是字符串下标x到y的一的个数。
下标为j的一定要是1。
思路:
先处理一下0的数量分布。比如00110就是2,0,1
假设字符串以0为开头和结尾,那么字符串便可以表示为:
a,1,b,1,c,1,d,1,e,1,f
其中字母表示0的数量。
枚举j在哪一个1上,便可以得出以下式子、
ans=a*b+ b*c+a*d+ c*d+b*e+a*f+ d*e+c*f+ e*f;
可以看出,每一个字母其实就是要与之后奇数或偶数的后缀和相乘,乘积相加就是结果。
但是这样仍然不对,因为i可k还可以指向1.
于是我们把每个字母的值加上一个1,再进行计算即可。
然而这样还是有问题。
相邻的两个字母相乘时不需要+1,于是我们再用一个for循环处理一下即可。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
char s[maxn];
vector<ll>v;
ll sum[maxn];
int main()
{
int T;
scanf("%d",&T);
while(T--){int len;
scanf("%d%s",&len,s);
memset(sum,,sizeof(sum));
v.clear();
ll ans=;
for(int i=;i<len;i++){
if(s[i]==''){
ans++;
}
else{
v.push_back(ans);
ans=;
}
}
v.push_back(ans);
int sz=v.size();
for(int i=sz-;i>=;i--){
sum[i]=v[i]+sum[i+];
}
ans=;
for(int i=;i<sz;i++){
ans+=v[i]*sum[i+];
}
for(int i=;i<sz-;i++){
ans-=v[i]*v[i+]-(v[i]-)*(v[i+]-);
}
printf("%lld\n",ans);
}
return ;
}
 

Gym - 101350A Sherlock Bones(思维)的更多相关文章

  1. 组队赛Day1第一场 GYM 101350A - Sherlock Bones (DP)

    [题意] 给你一个01串.f(i,j)定义为区间[i,j]内1的个数,求区间 [i,j,k] 中 f(i,j) =f(j,k) 的情况的子串总数,要求str[j]=='1'. (题意描述引自Ilook ...

  2. Gym 102028C - Supreme Command - [思维题][2018-2019 ACM-ICPC Asia Jiaozuo Regional Contest Problem C]

    题目链接:https://codeforces.com/gym/102028/problem/C Lewis likes playing chess. Now he has n rooks on th ...

  3. Gym 101775C - Traffic Light - [思维题]

    题目链接:http://codeforces.com/gym/101775/problem/C 题意: 给出 $N$ 个红绿灯,又给出 $N+1$ 个距离 $S_i = S_0,S_1, \cdots ...

  4. Gym 100801E Easy Arithmetic (思维题)

    题目:传送门.(需要下载PDF) 题意:给定一个长度不超过1000的字符串表达式,向该表达式中加入'+'或'-',使得表达式的值最大,输出该表达式. 题解:比如300-456就改成300-4+56,遇 ...

  5. A - Arcade Game Gym - 100814A (概率思维题)

    题目链接:https://cn.vjudge.net/contest/285964#problem/A 题目大意:每一次给你你一个数,然后对于每一次操作,可以将当前的数的每一位互换,如果互换后的数小于 ...

  6. G - WiFi Password Gym - 101608G (异或思维题+曲尺)

    题目链接:https://cn.vjudge.net/contest/285962#problem/G 题目大意:给你n和m,n代表有n个数,然后让你找出一个最长的区间,使得这个区间内的所有的数的‘’ ...

  7. L - Looking for Taste Gym - 101991L 二进制枚举/思维

    方法一:因为最多是10的六次方,所以可以直接枚举二进制上的每一位来得到最优结果. AC代码: #include<iostream> #include<stack> #inclu ...

  8. Gym 100851A Adjustment Office (思维)

    题意:给定一个 n*n 的矩阵,然后有 m 个询问,问你每一行或者每一列总是多少,并把这一行清空. 析:这个题不仔细想想,还真不好想,我们可以根据这个题意,知道每一行或者每一列都可以求和公式来求,然后 ...

  9. Gym 101128A Promotions(思维 + dfs)题解

    题意:给一有向图,如果A指向B,则A是B的上级.一直i要升职那么他的上级必须都升职.现在给你一个升职人数的区间[a, b],问你升职a人时几个人必被升职,b时几个人必升职,b时几个人没有可能被升职. ...

随机推荐

  1. Linux安装配置vsftp搭建FTP的详细配置

    这里主要是说vsftp的配置:基础的可以参考Linux中VSFTP的配置 转自:https://www.jb51.net/article/103904.htm 修改配置文件 配置文件/etc/vsft ...

  2. MySQL 执行计划中Extra(Using where,Using index,Using index condition,Using index,Using where)的浅析

      关于如何理解MySQL执行计划中Extra列的Using where.Using Index.Using index condition,Using index,Using where这四者的区别 ...

  3. Session, Token, OAuth 鉴权那些事儿

    鉴权那些事 整体思路 无论什么样的服务, Web 服务总是不能绕开鉴权这个话题的, 通过有效的鉴权手段来保护网站数据, 来为特定用户提供服务. 整体来说, 有三种方式: Session-Cookie ...

  4. git执行cherry-pick时修改提交信息

    git执行cherry-pick时修改提交信息 在本地分支执行cherry-pick命令时有时需要修改commit message信息,可以加参数-e实现: git cherry-pick -e co ...

  5. 把exe注册为windows服务

    1.需要工具 Instsrv.exe(可以给系统安装和删除服务) Srvany.exe(可以让程序以服务的方式运行) 2.运行cmd,输入注册服务命令 "instsrv.exe完整路径&qu ...

  6. Java 集合系列(二)—— ArrayList

    ArrayList ArrayList 是通过一个数组来实现的,因此它是在连续的存储位置存放对象的引用,只不过它比 Array 更智能,能够根据集合长度进行自动扩容. 假设让我们来实现一个简单的能够自 ...

  7. Golang 入门系列(六)理解Go中的协程(Goroutine)

    前面讲的都是一些Go 语言的基础知识,感兴趣的朋友可以先看看之前的文章.https://www.cnblogs.com/zhangweizhong/category/1275863.html. 今天就 ...

  8. Gerrit 添加用户

    使用ssh添加用户 ssh name@localhost -p 29418 gerrit create-account username --email username@email --full-n ...

  9. Entity Framework Core系列之实战(ASP.NET Core MVC应用程序)

    本示例演示在ASP.NET 应用程序中使用EF CORE创建数据库并对其做基本的增删改查操作.当然我们默认你的机器上已经安装了.NET CORE SDK以及合适的IDE.本例使用的是Visual St ...

  10. pyspider爬虫框架webui简介-爬取阿里招聘信息

    命令行输入pyspider开启pyspider 浏览器打开http://localhost:5000/ group表示组名,几个项目可以同一个组名,方便管理,当组名修改为delete时,项目会在一天后 ...