HDU 5400 Arithmetic Sequence (枚举思维+计数)水题

Arithmetic Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1865    Accepted Submission(s): 817


 

Problem Description

A sequence b1,b2,⋯,bn are called (d1,d2) -arithmetic sequence if and only if there exist i(1≤i≤n) such that for every j(1≤j<i),bj+1=bj+d1 and for every j(ij<n),bj+1=bj+d2 .

Teacher Mai has a sequence a1,a2,⋯,an . He wants to know how many intervals [l,r](1≤l≤r≤n) there are that al,al+1,⋯,ar are (d1,d2) -arithmetic sequence.

 

 

Input

There are multiple test cases.

For each test case, the first line contains three numbers n,d1,d2(1≤n≤105,|d1|,|d2|≤1000) , the next line contains n integers a1,a2,⋯,an(|ai|≤109) .

 

 

Output

For each test case, print the answer.

 

 

Sample Input

 

5 2 -2 0 2 0 -2 0 5 2 3 2 3 3 3 3

 

 

Sample Output

 

12 5

 

 

Author

xudyh

 

 

Source

2015 Multi-University Training Contest 9

 

 

Recommend

wange2014   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long

const int  maxn =1e5+5;
const int mod=1e9+7;
/*
题目大意:给定n个数和d1,d2,
然后计数区间,区间满足存在一个点,
使得左半边以d1为差,右半边以d2为差。

思维枚举,枚举的是区间里的那个点,
考虑从每个点都往左右延扩的最大距离,
(包括当前点)进行组合计数。

但要看到特殊情况,就是d1和d2相同时会有重复的,
这时候枚举的就变成以i为末尾的满足条件的计数,只要线性相加即可。



*/

ll gcd(ll x,ll y) { return y==0?x:gcd(y,x%y); }

int n,d1,d2;
int dat[maxn];
ll dpl[maxn],dpr[maxn];

int main()
{
    while(scanf("%d%d%d",&n,&d1,&d2)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",&dat[i]);

        dpl[1]=dpr[n]=1;
        for(int i=2;i<=n;i++)
            if(dat[i]-dat[i-1]==d1) dpl[i]=dpl[i-1]+1;
            else dpl[i]=1;
        for(int i=n-1;i>=1;i--)
            if(dat[i+1]-dat[i]==d2) dpr[i]=dpr[i+1]+1;
            else dpr[i]=1;

        ///注意特殊情况,当d1和d2相同时,只要枚举以位置i为末尾的计数即可。
        ll ans=0;  for(int i=1;i<=n;i++) ans+=((d1==d2)?dpl[i]:(dpl[i]*dpr[i]));

        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值