zoukankan      html  css  js  c++  java
  • CodeChef Count Substrings

    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 = 100010;
    int n , m , e[N] , ans ;
    inline int gcd( int a , int b ) { return b == 0 ? a : gcd(b,a%b); }
    int Run() {
        scanf("%d",&n);
        for( int i = 0 ; i < n ; ++i ) scanf("%d",&e[i]);
        sort( e , e + n );
        for( int i = 1 ; i < n ; ++i ) if( gcd(e[0],e[i]) == 1 ) {
            return n;
        }
        return -1;
    }
    int main()
    {
        #ifdef LOCAL
            freopen("in","r",stdin);
        #endif
        ios::sync_with_stdio(0);
        int _ , cas = 1 ;
        scanf("%d",&_);
        while(_--) printf("%d
    ",Run());
    }
    View Code
  • 相关阅读:
    用户代理列表--爬虫伪装浏览器访问用
    python爬虫解析页面数据的三种方式
    requests模块的使用
    Python pip源更改
    个人博客项目开发
    Django之中间件
    Django之发送邮件
    Django组件之用户认证组件
    Django之Cookie与session
    Django组件之分页器
  • 原文地址:https://www.cnblogs.com/hlmark/p/4296777.html
Copyright © 2011-2022 走看看