Count Substrings

 
Problem code: CSUB
 
 

All submissions for this problem are available.

Read problems statements in Mandarin Chinese and Russian.

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.

Input

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.

Output

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

Constraints

  • 1T105
  • 1N105
  • Sum of N over all testcases ≤ 105

Example

Input:
2
4
1111
5
10001 Output:
10
3

存在任意两个数 gcd = 1 必然为n , 否则为 -1

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue> using namespace std;
const int N = ;
int n , m , e[N] , ans ;
inline int gcd( int a , int b ) { return b == ? a : gcd(b,a%b); }
int Run() {
scanf("%d",&n);
for( int i = ; i < n ; ++i ) scanf("%d",&e[i]);
sort( e , e + n );
for( int i = ; i < n ; ++i ) if( gcd(e[],e[i]) == ) {
return n;
}
return -;
}
int main()
{
#ifdef LOCAL
freopen("in","r",stdin);
#endif
ios::sync_with_stdio();
int _ , cas = ;
scanf("%d",&_);
while(_--) printf("%d\n",Run());
}

CodeChef Count Substrings的更多相关文章

  1. 【LeetCode】1180. Count Substrings with Only One Distinct Letter 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 组合数 日期 题目地址:https://leetcod ...

  2. scau 2015寒假训练

    并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...

  3. nodejs api 中文文档

    文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...

  4. CF451D Count Good Substrings (DP)

    Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...

  5. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  6. [Swift]LeetCode696. 计数二进制子串 | Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  7. LeetCode 696 Count Binary Substrings 解题报告

    题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...

  8. 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  9. [LeetCode&Python] Problem 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

随机推荐

  1. C++基础之static(静态)变量

    static 表示静态   作用: 1.在函数体内,静态变量的值维持不变(记忆功能) 2.是一个本地的全局函数,即只能被本模块的函数访问(隐藏功能)   static变量: static全局变量和普通 ...

  2. 新特性2-lambda表达式

    最近几天学习了一下lambda表达式,看了不少博客,感觉有一篇博客总结的一句话总结的很好:lambda表达式是一段可以传递的代码,它的核心思想是将面向对象中的传递数据变成传递行为.其实以前也有传递行为 ...

  3. ps:新建Photoshop图像

    从现在起我们开始正式地接触Photoshop,为了保证大家的快捷键设置与教程内容一致.请确认Photoshop的快捷键设置是默认值.可从菜单[编辑 键盘快捷键]打开快捷键设置,在组选项里面选择“Pho ...

  4. css3-css3属性选择器

    在HTML中,通过各种各样的属性可以给元素增加很多附加的信息.例如,通过id属性可以将不同div元素进行区分. 在CSS2中引入了一些属性选择器,而CSS3在CSS2的基础上对属性选择器进行了扩展,新 ...

  5. HTML表单(来自MDN的总结)

    表单介绍 HTML表单是用户和web站点或应用程序之间交互的主要内容之一.它们允许用户将数据发送到web站点.大多数情况下,数据被发送到web服务器,但是web页面也可以拦截它自己并使用它. HTML ...

  6. 20 简述BASE64编码的作用和c#对其的支持?

  7. ht-7 treeSet特性

    TreeSetTreeSet可以对set集合中的元素进行排序,默认按照asic码表的自然顺序排序,之所以treeset能排序是因为底层是二叉树,数据越多越慢,TreeSet是依靠TreeMap来实现的 ...

  8. php单点登录实现原理实例详解

    单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任. 单点登录在大型网站里使用 ...

  9. web选择文件夹上传

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  10. 前端JS编码规范

    对初学者来说应该学习的JavaScript编码规范: 传送门: http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=29292475 ...