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-28 19:38:18

hdu 5400 Arithmetic Sequence的相关文章

hdu 5400 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 h

HDU 5400 Arithmetic Sequence (2015年多校比赛第9场)

1.题目描述:点击打开链接 2.解题思路:本题利用扫描法解决.根据题意描述,[L,i)和[i,R)区间都构成了等差数列,因此可以实现用L[i],R[i]来维护从i开始向左向右可以延伸的最远长度,如果d1和d2不等,那么答案就是L[i]*R[i]求和,否则就是R[i]求和. 3.代码: //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<algorit

hdu(5400)——Arithmetic Sequence(想法题)

题意: 首先,现在给出三个数字n,d1,d2.然后第二行给出n个数字. 然后题目要求的是求这个序列中有几个区间满足一下条件之一: 1)这个区间是一个等差数列,且公差为d1或d2: 2)若一个区间为[l,r],那么有l<=i<=r,使得[l,i]范围内是公差为d1的等差数列,[i,r]区间内是公差为d2的等差数列. *注意,这里单个数字一定是满足等差数列的.而且这里数字最好都使用__int64来保存,因为这个原因我们队WA了好几次. 思路: 这里我设了头和尾两个指针,分别用l和r表示. 我用de

HDOJ 5400 Arithmetic Sequence 暴力枚举

Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 382    Accepted Submission(s): 196 Problem Description A sequence b1,b2,?,bn are called (d1,d2)-arithmetic sequence if and on

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

hdu 4441 Queue Sequence(splay)

题目链接:hdu 4441 Queue Sequence 这题看了题解写的,题解传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 #define ls l,m,rt<<1 4 #define rs m+1,r,rt<<1|1 5 using namespace std; 6 typedef long long ll; 7 8 const int N=1e6+7; 9 i

hdu 5297 Y sequence(容斥)

题目链接:hdu 5297 Y sequence 考虑62以内的指数,x为奇数个质数因子,就减掉,偶数个加上.计算x为指数的不满足数直接pow(n,1/x)即可. #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <algorithm> using namespace std; type

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

hdu 5306 Gorgeous Sequence(区间最值更新+求和)

题目链接:hdu 5306 Gorgeous Sequence 题意: 给你一个序列,有三种操作. 0 x y t:将[x,y]的数取min(a[i],t) 1 x y:求[x,y]的最大值 2 x y:求[x,y]的区间和 题解: 吉老师的课件题:传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 #define ls l,m,rt<<1 4 #define rs m+1,r,rt