hdu 5400 Arithmetic Sequence

简介:

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;
}
目录
相关文章
codeforces 327 B. Hungry Sequence
题目就是让你输出n个数的序列,要保证该序列是递增的,并且第i个数的前面不能保护它的约数,我直接先对前100000的素数打表,然后输出前n个,so easy。
44 0
LeetCode 263. Ugly Number
编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。
93 0
LeetCode 263. Ugly Number
LeetCode 264. Ugly Number II
编写一个程序,找出第 n 个丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。
73 0
LeetCode 264. Ugly Number II
|
索引
LeetCode 413. Arithmetic Slices
如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列。
101 0
LeetCode 413. Arithmetic Slices
uva 10706 - Number Sequence
点击打开链接uva 10706 题目意思:    有一个数组 s[1] = 1 , s[2] = 1 2 , .......s[k] = 1....k,要求给定一个n表示数组的第几位,要求这个第几位是什么数。
951 1
LeetCode---Problem6 ZigZag Conversion
ZigZag问题思路。代码整洁并不一定执行速度就好~
791 0
|
C++
[LeetCode] Ugly Number
No matter what language you use on the OJ, you may refer to Stefan's post for a solution. The C++ is rewritten below, just really concise.
571 0
|
C++
[LeetCode] Ugly Number II
Stefan is a hero! Personally, I love his first C++ solution. 1 class Solution { 2 public: 3 int nthUglyNumber(int n) { 4 vector ...
830 0