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
  1. 3
    5
    01010
    6
    101001
    7
    1101011
Outpu2
  1. 3
    7
  2. 题意:
    给定01字符串,求有多少个三元组i,j,k满足i<j<kF(i, j)=F(j, k).其中Fx,y)是字符串下标xy的一的个数。
    下标为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;
    可以看出,每一个字母其实就是要与之后奇数或偶数的后缀和相乘,乘积相加就是结果。
    但是这样仍然不对,因为ik还可以指向1.
    于是我们把每个字母的值加上一个1,再进行计算即可。
    然而这样还是有问题。
    相邻的两个字母相乘时不需要+1,于是我们再用一个for循环处理一下即可。
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<stack>
  5. #include<queue>
  6. #include<map>
  7. #include<set>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<cmath>
  11. #include<ctime>
  12. #define fuck(x) cout<<#x<<" = "<<x<<endl;
  13. #define ls (t<<1)
  14. #define rs ((t<<1)+1)
  15. using namespace std;
  16. typedef long long ll;
  17. typedef unsigned long long ull;
  18. const int maxn = ;
  19. const int inf = 2.1e9;
  20. const ll Inf = ;
  21. const int mod = ;
  22. const double eps = 1e-;
  23. const double pi = acos(-);
  24. char s[maxn];
  25. vector<ll>v;
  26. ll sum[maxn];
  27. int main()
  28. {
  29. int T;
  30. scanf("%d",&T);
  31. while(T--){int len;
  32. scanf("%d%s",&len,s);
  33. memset(sum,,sizeof(sum));
  34. v.clear();
  35. ll ans=;
  36. for(int i=;i<len;i++){
  37. if(s[i]==''){
  38. ans++;
  39. }
  40. else{
  41. v.push_back(ans);
  42. ans=;
  43. }
  44. }
  45. v.push_back(ans);
  46. int sz=v.size();
  47. for(int i=sz-;i>=;i--){
  48. sum[i]=v[i]+sum[i+];
  49. }
  50. ans=;
  51. for(int i=;i<sz;i++){
  52. ans+=v[i]*sum[i+];
  53. }
  54. for(int i=;i<sz-;i++){
  55. ans-=v[i]*v[i+]-(v[i]-)*(v[i+]-);
  56. }
  57. printf("%lld\n",ans);
  58. }
  59. return ;
  60. }
  1.  

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. SQL Server -- 回忆笔记(四):case函数,索引,子查询,分页查询,视图,存储过程

    SQL Server知识点回忆篇(四):case函数,索引,子查询,分页查询,视图,存储过程 1. CASE函数(相当于C#中的Switch) then '未成年人' else '成年人' end f ...

  2. mysql 将一个表中的数据复制到另一个表中,sql语句

    1.表结构相同的表,且在同一数据库(如,table1,table2) Sql :insert into table1 select * from table2 (完全复制) insert into t ...

  3. java中的String整理

    基础知识模块: 参考以下博客,写得很好,深入分析了String,需要重点掌握 http://www.tiantianbianma.com/java/java-basic/page/3/ http:// ...

  4. 生成文件的MD5值

    import hashlib #########测试################# m = hashlib.md5() m.update(b"hello") m.update( ...

  5. 2018/05/14 03:56:10 [error] 12959#0: *42285845507 client intended to send too large body: 1664288 bytes

    Syntax: client_max_body_size size; Default: client_max_body_size 1m; Context: http, server, location ...

  6. linux安装成功后怎么调出终端

    一.Ubuntu 桌面如下,点击搜索 二.输入terminal 终端 三.锁定到菜单栏 四.接下来就可以练习linux下的常用命令,如:ls  mkdir  cat    touch 等等 这些命令后 ...

  7. 需求规格说明书(SRS)特点

    需求说明书的7大特征: 完整性 正确性 可行性 必要性 划分优先级 无二义性 可验证性 每条需求规格说明书的4大特点: 完整性 一致性 可修改性 可跟踪性 需求管理就是一种获取.组织并记录系统需求的系 ...

  8. odoo中各视图写法

    透视图: 还需要将一个pivot表添加到要待办任务(To-Do Tasks)中,请使用以下代码: <record id="view_pivot_todo_task" mode ...

  9. CF908G New Year and Original Order 数位DP

    传送门 看到数据范围到\(10^{700}\)毫无疑问数位DP.那么我们最重要的问题是如何有效地维护所有数位排序之后的数的值. 对于某一个数\(x\),设\(f_{x,i} (i \in [1,9]) ...

  10. 故障公告:docker swarm集群“群龙无首”造成部分站点无法访问

    今天傍晚 17:38-18:18 左右,由于 docker swarm 集群出现 "The swarm does not have a leader" 问题,造成博问.闪存.园子. ...