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

hdu 5400 Arithmetic Sequence

2015-08-19 23:05 549 查看
click here~~

[code]                         ***Arithmetic Sequence***

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.

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


题目大意:就是给你n个数,然后一个d1和d2,求:

1:这个区间是一个等差数列,且公差为d1或d2;

2:若区间的下标范围为[l,r],应有l<=i<=r,使得[l,i]范围是公差为d1的等差数列,[i,r]范围是公差为d2的等差数列,就是找一共有几种排列方法



解题思路:首先由至少 n 个,然后根据数据推出公式就行了,

直接给出代码吧。。。。

[code]#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5+5;

int data[maxn];
int main()
{
    int d1,d2,n,sum,j,i,t;
    __int64 ans, k;
    while(~scanf("%d%d%d",&n,&d1,&d2))
    {
        t = 1;
        sum = j = 0;
        ans = 0;
        k = 1;
        for(i=1; i<=n; i++)
            scanf("%d",&data[i]);
        for(i=1; i<n; i++)
        {
            if(t)
            {
                if(data[i]+d1 == data[i+1])
                    k++;
                else
                    t = 0;
                j = 0;
            }
            if(!t)
            {
                if(data[i]+d2 == data[i+1])
                {
                    k++;
                    j++;
                }
                else
                {
                    ans += (k+1)*k/2;
                    if(sum+k > i)
                        ans--;
                    k = 1;
                    t = 1;
                    sum = i;
                    if(j)
                    i--;
                }
            }
        }
        ans += (k+1)*k/2;
        if(sum+k > i)
            ans--;
        printf("%I64d\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: