您的位置:首页 > 产品设计 > UI/UE

Arithmetic Sequence

2015-08-18 16:52 501 查看
[align=left]Problem Description[/align]
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+d1and 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.

[align=left]Input[/align]
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).

[align=left]Output[/align]
For each test case, print the answer.

[align=left]Sample Input[/align]

5 2 -2

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

[align=left]Sample Output[/align]

12
5

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long  maxn = 100005;
long long  A[maxn];
int main()
{
long long flag,n, d1, d2;
long long sum, cnt;
while(~scanf("%I64d %I64d %I64d", &n, &d1, &d2))
{
for(int i = 0; i < n; i++)
{
scanf("%I64d", &A[i]);
}

sum = 0, flag = 0, cnt = 0;

for(int i = 1; i < n; i++)
{
if(A[i]-A[i-1] == d1 && flag == 0)
{
cnt ++;
continue;
}
if(A[i]-A[i-1] == d2)
{
flag = 1;
cnt ++;
continue;
}
//printf("cnt = %d\n", cnt);
sum += (1+cnt)*cnt/2;
if(cnt != 0) i--;
cnt = 0;
flag = 0;
}
sum += (1+cnt)*cnt/2;
//if (d1!=d2)
printf("%I64d\n", sum + n);
//else printf("%I64d\n",n);
}
return 0;
}

[ Copy to Clipboard ]    [ Save to File]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: