arithmetic Sequence Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 624 Accepted Submission (s): 280
Problem Description A sequence b1,b2,⋯,bn is called (D1,D2)-arithmetic sequence if and only if there exist I (1≤i≤n) su CH, every J (1≤j<i), Bj+1=bj+d1and 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 is that Al,al+1,⋯,ar is (D1,D2)-arithmetic sequence.
Input There is multiple test cases.
For each test case, the first line contains three numbers N,D1,D2 (1≤n≤105,|d1|,|d2|≤1000), and the next line contains n inte Gers 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
Author Xudyh
Source multi-university Training Contest 9
This problem has a brain hole, the length can be 0, do not necessarily have to exist J.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Vector> using namespace std;
#define LL Long long #define MAXN 100007 int NUM[MAXN];
int LEF[MAXN];
int RIG[MAXN];
int main () {//freopen ("In.txt", "R", stdin);
Freopen ("Outx.txt", "w", stdout);
int n,d1,d2; while (scanf ("%d%d%d", &n,&d1,&d2)!=eof) {for (int i = 1;i <= n; i++) scanf ("%d", &num[
I]);
memset (lef,0,sizeof (lef));
memset (rig,0,sizeof (rig));
for (int i = 2;i <= n; i++) if (Num[i]-num[i-1] = = D1) Lef[i] = lef[i-1]+1;
for (int i = n-1; i>=1; i--) if (num[i+1]-num[i] = = D2) rig[i] = rig[i+1]+1;
ll ans = 0;
if (D1 = = D2) {for (int i = 1;i <= n; i++) ans + = lef[i]+1;
} else {for (int i = 1;i <= n; i++) ans + = 1ll* (lef[i]+1) * (rig[i]+1); } cout<<ans<<endl;
} return 0;
}