0
点赞
收藏
分享

微信扫一扫

hdoj Arithmetic Sequence 5400 (构造等差数列+技巧) 好题

浮游图灵 2023-04-20 阅读 111


Arithmetic Sequence


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



Problem Description


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)。


#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 100010
using namespace std;
long long l[N],r[N];
int a[N];
int main()
{
	int n,d1,d2;
	int i,j;
	while(scanf("%d%d%d",&n,&d1,&d2)!=EOF)
	{
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);
		l[1]=1;
		for(i=2;i<=n;i++)
		{
			if(a[i]==a[i-1]+d1)
				l[i]=l[i-1]+1;
			else
				l[i]=1;
		}
		r[n]=1;
		for(i=n-1;i>=1;i--)
		{
			if(a[i]==a[i+1]-d2)
				r[i]=r[i+1]+1;
			else
				r[i]=1;
		}
		long long cnt=0;
		if(d1!=d2)
		{
			for(i=1;i<=n;i++)
				cnt+=l[i]*r[i];
		}
		else
		{
			for(i=1;i<=n;i++)
				cnt+=r[i];
		}
		printf("%lld\n",cnt);
	} 
	return 0;
}


举报

相关推荐

等差数列划分

算法-等差数列

等差数列求和

24 求等差数列

蓝桥杯——等差数列

1246. 等差数列

0 条评论