Arithmetic Sequence

Arithmetic Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

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

题意:给定一序列和d1,d2.问有多少间隔可以保证存在i,对于每一个j,有j(1≤j<i),bj+1=bj+d1, j(i≤j<n),bj+1=bj+d2.成立。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 const int N = 211100;
 8 const int oo = 0x3f3f3f3f;
 9 long long al[N], ar[N], as[N], n;  // al数组存从左边到这个点一直满足的+d1的数有几个,ar是往右边数满足+d2的有几个。
10 int main()
11 {
12     long long d1, d2, i, ans;
13     while(~scanf("%lld %lld %lld", &n, &d1, &d2))
14     {
15         for(i = 1; i <= n; i++)
16             scanf("%lld", &as[i]);
17         al[1] = ar[n] = 1;
18         for(i = 2; i <= n; i++)
19             if(as[i] == as[i-1]+d1)
20                 al[i] = al[i-1]+1;
21             else al[i] = 1;  // 只要间隔,不满足了那么连续的就是1个,它自身
22         for(i = n-1; i >= 1; i--)
23         {
24             if(as[i]+d2 == as[i+1])
25                 ar[i] = ar[i+1]+1;
26             else ar[i] = 1;
27         }
28         ans = 0;
29         for(i = 1; i <= n; i++)
30         {
31             if(d1 == d2) ans += al[i];  // 如果d1,d2相等,al直接相加
32             else ans += al[i]*ar[i];  // if不等,就等于两者相乘,即间隔种类数
33         }
34         printf("%lld\n", ans);
35     }
36     return 0;
37 }
时间: 2024-11-06 19:18:34

Arithmetic Sequence的相关文章

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

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

Arithmetic Sequence(dp)

Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 19[Submit][Status][Web Board] Description Giving a number sequence A with length n, you should choosingm numbers from A(ignore the order) which can form an arithmetic sequ

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

HZAU 21——Arithmetic Sequence——————【暴力 or dp】

Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1810  Solved: 311[Submit][Status][Web Board] Description Giving a number sequence A with length n, you should choosing m numbers from A(ignore the order) which can form an arithmetic

【leetcode】1027. Longest Arithmetic Sequence

题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is

LeetCode 1027. Longest Arithmetic Sequence

dp问题,一维是没法dp的,因为我们还要记录一个diff才能判断是否是Arithmetic Sequence. dp[i][diff] 表示以 A[i] 结尾,等差为 diff 的最大长度.从这种角度来看本题和 LeetCode 300. Longest Increasing Subsequence / 354. Russian Doll Envelopes 极为相似. class Solution { public: int longestArithSeqLength(vector<int>

Arithmetic Sequence(多校第九场)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5400 这题其实就是求有多少公差为d1和公差为d2的等差序列和前半段是公差为d1后半段的公差为d2的序列..... 我们可以用个b数组保存序列中相邻两项的和,然后直接查找b数组的值是否等于d1或d2.再来进行处理.. #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm>

[hdu5400 Arithmetic Sequence]预处理,容斥

题意:http://acm.hdu.edu.cn/showproblem.php?pid=5400 思路:预处理出每个点向左和向右的最远边界,从左向右枚举中间点,把区间答案加到总答案里面.由与可能与前面的区间重叠,需要减去重复的答案,由于左边界非降,所以重叠的区间长度很容易得到. #pragma comment(linker, "/STACK:10240000") #include <map> #include <set> #include <cmath&