HDU 5400(Arithmetic Sequence-暴力找区间)

Arithmetic Sequence

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


Problem Description
A sequence b1,b2,,bn are called (d1,d2) -arithmetic sequence if and only if there exist i(1in) such that for every j(1j<i),bj+1=bj+d1 and for every j(ij<n),bj+1=bj+d2 .

Teacher Mai has a sequence a1,a2,,an . He wants to know how many intervals [l,r](1lrn) 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(1n105,|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
 

Author
xudyh
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5421  5420  5419  5418  5417 
 

暴力找区间





#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int n;
ll d1,d2,a[MAXN],b[MAXN];
 
int main()
{
//	freopen("E.in","r",stdin);
	
	while(cin>>n>>d1>>d2)
	{
		For(i,n) scanf("%lld",&a[i]);
		
		For(i,n-1) b[i]=a[i+1]-a[i];
	//	For(i,n-1) cout<<b[i]<<' ';cout<<endl;
	
		if (d1==d2)
		{
			ll sz=1,ans=0;
			Fork(i,2,n) if (a[i]-a[i-1]==d1) sz++; else ans+=sz*(sz+1)/2,sz=1;
			if (sz) ans+=sz*(sz+1)/2;
			cout<<ans<<endl; 
		} else {
			ll sz=1,ans=0; bool flag=0;
			Fork(i,2,n)
			{
				if (!flag)
				{
					if (a[i]-a[i-1]==d1) sz++;
					else if (a[i]-a[i-1]==d2) sz++,flag=1;
					else ans+=sz*(sz+1)/2,sz=1,flag=0;
				}
				else
				{
					if (a[i]-a[i-1]==d2) sz++;
					else if (a[i]-a[i-1]==d1) ans+=sz*(sz+1)/2-1,sz=2,flag=0;
					else ans+=sz*(sz+1)/2,sz=1,flag=0;
				}
			}if (sz) ans+=sz*(sz+1)/2;
			cout<<ans<<endl; 
			
		} 
		
	
	}
	
		
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值