码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5400 Arithmetic Sequence

时间:2015-08-19 23:44:34      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

click here~~

                         ***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 个,然后根据数据推出公式就行了,
直接给出代码吧。。。。

#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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 5400 Arithmetic Sequence

标签:

原文地址:http://blog.csdn.net/qingshui23/article/details/47790569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!