Arithmetic Sequence

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

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
 
Author
xudyh
 
对每个数先预处理出左右能够达到的最远距离。然后当d1!=d2时,用乘法原理得到区间数(左边有l[i]个区间,右边有r[i]个区间,左右就是l[i]*r[i]个区间)
当d1==d2 时,左右会算重,只算左边就好了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = ;
int a[N];
LL l[N],r[N],ans; ///l[i]记录第i个数左边与其成方差为d1的数的个数,r[i]记录第i个数右边与其方差为
///d2的数的个数(包括自身)
int main()
{
int n,d1,d2;
while(scanf("%d%d%d",&n,&d1,&d2)!=EOF){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
l[] = ,r[n]=;
ans = ;
for(int i=;i<=n;i++){ ///预处理
if(a[i]==a[i-]+d1){
l[i] = l[i-]+;
}else l[i] = ;
}
for(int i=n-;i>=;i--){
if(a[i]+d2==a[i+]){
r[i] = r[i+]+;
}else r[i]=;
}
if(d1==d2){
for(int i=;i<=n;i++){
ans+=l[i];
}
}
else
for(int i=;i<=n;i++){ ///乘法原理
// printf("%lld %lld\n",l[i],r[i]);
ans+=l[i]*r[i];
}
printf("%lld\n",ans);
}
}

最新文章

  1. Linux下删除文件的原理
  2. TaintDroid剖析之DVM变量级污点跟踪(下篇)
  3. 1.0 Quartz 2D 简介
  4. hashmap 读取
  5. javascript编程: JSON, Mapping, 回调
  6. ASP.NET 学习博客
  7. NOD32强制卸载工具使用方法【转】
  8. qt model/view 架构基础介绍之QTreeWidget
  9. 使用jdk的socket通信
  10. Appium Server 源码分析之启动运行Express http服务器
  11. JAR包数字签名与验证
  12. 关于Appium android input manager for Unicode 提示信息
  13. Python 爬虫之下载图片
  14. 使用css3实现动画来开启GPU加速
  15. HTML5事件
  16. git在Linux下的安装
  17. 主线程中的Looper.loop()一直无限循环为什么不会造成ANR
  18. 大数据量下的集合过滤—Bloom Filter
  19. mycat实现简单的mysql集群负载均衡
  20. java内存模型及内存与cpu之间的关系

热门文章

  1. python爬虫: 豆瓣电影top250数据分析
  2. W3CPLUS DEMO一些有意思的效果备份
  3. HDU - 1251 统计难题(Trie树)
  4. 爬取豆瓣Top250_Ajax动态页面
  5. Linuxx学习-特殊文件与进程
  6. PHP中文网 学习阶段规划
  7. ThreeJs 基础入门
  8. 反射的妙用-类名方法名做参数进行方法调用实例demo
  9. ogre3D学习基础7---材质详解
  10. logging——日志