Arithmetic SequenceTime 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(i≤j<n),bj+1=bj+d2 .
Input There are multiple test cases.
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;
}