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;
}
时间: 2024-08-24 08:45:53

hdu 5400 Arithmetic Sequence的相关文章

hdu 2454 Degree Sequence of Graph G

点击打开链接 Degree Sequence of Graph G Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1997    Accepted Submission(s): 850 Problem Description Wang Haiyang is a strong and optimistic Chinese youngst

hdu 2062 Subset sequence【有点康拓展开的意思】

Subset sequence Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3441    Accepted Submission(s): 1740 Problem Description Consider the aggregate An= { 1, 2, -, n }. For example, A1={1}, A3={1,2,

hdu 1711 Number Sequence

点击打开链接hdu1711 思路:KMP kmp算法: 1 kmp是用来匹配字符串,只能够匹配单一的字符串 2 kmp的算法的过程:   1:假设文本串的长度为n,模式串的长度为m:   2:先例用O(m)的时间去预处理next数组,next数组的意思指的是当前的字符串匹配失败后要转到的下一个状态:   3:利用o(n)的时间去完成匹配: 3 时间复杂度为o(n+m)即o(n): 代码: #include<algorithm> #include<iostream> #include

KMP专题【完结】

第一题 hdu 1711 Number Sequence 点击打开hdu 1711 思路: 1 kmp是用来匹配字符串,只能够匹配单一的字符串 2 kmp的算法的过程:   1:假设文本串的长度为n,模式串的长度为m:   2:先例用O(m)的时间去预处理next数组,next数组的意思指的是当前的字符串匹配失败后要转到的下一个状态:   3:利用o(n)的时间去完成匹配: 3 时间复杂度为o(n+m)即o(n): 点击查看代码 第二题 hdu 1686 oulipo 点击打开hdu 1686

[再寄小读者之数学篇](2014-11-19 等差数列的部分和)

设 $\sed{a_k}_{k=1}^n$ 为等差数列, 则 $$\bex a_1+\cdots+a_n=\frac{n(a_1+a_n)}{2}. \eex$$ Ref. [Proof Without Words: Partial Sums of an Arithmetic Sequence, The College Mathematics Journal]. A visual proof that a partial sum of an arithmetic sequence equals

再寄小读者之数学篇[2014.07.01-2014.12.31]

[再寄小读者之数学篇](2014-12-24 乘积型不等式)   [再寄小读者之数学篇](2014-12-04 $\left(1+\frac{1}{x}\right)^x>\frac{2ex}{2x+1},\forall\ x>0.$)  试证: $$\bex \left(1+\frac{1}{x}\right)^x>\frac{2ex}{2x+1},\forall\ x>0. \eex$$    [再寄小读者之数学篇](2014-11-27 华中科技大学2014年高等代数考研试题

hdu 5504 GT and sequence【BestCoder Round #60 】

GT and sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1441    Accepted Submission(s): 336 Problem Description You are given a sequence of N integers. You should choose some numbers(at

HDU 3038 How Many Answers Are Wrong? :带权并查集

链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 题目: Problem Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. T

HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

链接: HDU : http://acm.hdu.edu.cn/showproblem.php?pid=2489 POJ  : http://poj.org/problem?id=3925 题目: Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a comp