HLG 1005 Counting Subsequences [序列定和]

Description

 "47 is the quintessential random number," states the 47 society. And there might be a grain of truth in that.

For example, the first ten digits of the Euler's constant are:

2 7 1 8 2 8 1 8 2 8

And what's their sum? Of course, it is 47.

You are given a sequence S of integers we saw somewhere in the nature. Your task will be to compute how strongly does this sequence support the above claims.

We will call a continuous subsequence of S interesting if the sum of its terms is equal to 47.

E.g., consider the sequence S = (24, 17, 23, 24, 5, 47). Here we have two interesting continuous subsequences: the sequence (23, 24) and the sequence (47).

Given a sequence S, find the count of its interesting subsequences.

Input

The first line of the input file contains an integer T(T <= 10) specifying the number of test cases. Each test case is preceded by a blank line.

The first line of each test case contains the length of a sequence N(N <= 500000). The second line contains N space-separated integers – the elements of the sequence. Sum of any continuous subsequences will fit in 32 bit signed integers.

Output

For each test case output a single line containing a single integer – the count of interesting subsequences of the given sentence.

Sample Input
2
 
13
2 7 1 8 2 8 1 8 2 8 4 5 9
 
7
2 47 10047 47 1047 47 47
Sample Output
3
4

code:

View Code
#include<stdio.h>
#include<stdlib.h>
#define INFTY 2000000000
struct node
{
long z,x;
}q[500010];
int cmp(const void*p1,const void*p2)
{
struct node*c=(struct node*)p1;
struct node*d=(struct node*)p2;
return (c->z)-(d->z);
}
int main()
{
long n,i,t,p,j,c;
scanf("%ld",&t);
while(t--)
{


c=0;q[0].x=0;q[0].z=0;
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
scanf("%ld",&p);
q[i].z=q[i-1].z+p;
q[i].x=i;
}
q[n+1].z=INFTY;
qsort(q,n+1,sizeof(struct node),cmp);
i=0;j=0;
while(j<=n)
{
if(q[j].z-q[i].z<47)
j++;
else if(q[j].z-q[i].z>47)
i++;
else
{
int ie,je;
for(ie=i;q[ie+1].z==q[ie].z;ie++);
ie++;
for(je=j;q[je+1].z==q[je].z;je++);
je++;
while(i<ie&&j<je)
if(q[i].x>q[j].x)
j++;
else{
c+=(je-j);
i++;
}
i=ie;j=je;
}
}
printf("%ld\n",c);
}
return 0;
}

 

转载于:https://www.cnblogs.com/dream-wind/archive/2012/03/14/2396247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值