Arithmetic Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 937 Accepted Submission(s): 411
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
.
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.
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) .
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
Recommend
暴力找区间
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int n;
ll d1,d2,a[MAXN],b[MAXN];
int main()
{
// freopen("E.in","r",stdin);
while(cin>>n>>d1>>d2)
{
For(i,n) scanf("%lld",&a[i]);
For(i,n-1) b[i]=a[i+1]-a[i];
// For(i,n-1) cout<<b[i]<<' ';cout<<endl;
if (d1==d2)
{
ll sz=1,ans=0;
Fork(i,2,n) if (a[i]-a[i-1]==d1) sz++; else ans+=sz*(sz+1)/2,sz=1;
if (sz) ans+=sz*(sz+1)/2;
cout<<ans<<endl;
} else {
ll sz=1,ans=0; bool flag=0;
Fork(i,2,n)
{
if (!flag)
{
if (a[i]-a[i-1]==d1) sz++;
else if (a[i]-a[i-1]==d2) sz++,flag=1;
else ans+=sz*(sz+1)/2,sz=1,flag=0;
}
else
{
if (a[i]-a[i-1]==d2) sz++;
else if (a[i]-a[i-1]==d1) ans+=sz*(sz+1)/2-1,sz=2,flag=0;
else ans+=sz*(sz+1)/2,sz=1,flag=0;
}
}if (sz) ans+=sz*(sz+1)/2;
cout<<ans<<endl;
}
}
return 0;
}