Arithmetic Sequence
2015-08-18 16:52
501 查看
[align=left]Problem Description[/align]
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+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 are that al,al+1,⋯,ar are (d1,d2)-arithmetic sequence.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each test case, print the answer.
[align=left]Sample Input[/align]
5 2 -2
0 2 0 -2 0
5 2
3
2 3 3 3 3
[align=left]Sample Output[/align]
12
5
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+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 are that al,al+1,⋯,ar are (d1,d2)-arithmetic sequence.
[align=left]Input[/align]
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).
[align=left]Output[/align]
For each test case, print the answer.
[align=left]Sample Input[/align]
5 2 -2
0 2 0 -2 0
5 2
3
2 3 3 3 3
[align=left]Sample Output[/align]
12
5
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const long long maxn = 100005; long long A[maxn]; int main() { long long flag,n, d1, d2; long long sum, cnt; while(~scanf("%I64d %I64d %I64d", &n, &d1, &d2)) { for(int i = 0; i < n; i++) { scanf("%I64d", &A[i]); } sum = 0, flag = 0, cnt = 0; for(int i = 1; i < n; i++) { if(A[i]-A[i-1] == d1 && flag == 0) { cnt ++; continue; } if(A[i]-A[i-1] == d2) { flag = 1; cnt ++; continue; } //printf("cnt = %d\n", cnt); sum += (1+cnt)*cnt/2; if(cnt != 0) i--; cnt = 0; flag = 0; } sum += (1+cnt)*cnt/2; //if (d1!=d2) printf("%I64d\n", sum + n); //else printf("%I64d\n",n); } return 0; } [ Copy to Clipboard ] [ Save to File]
相关文章推荐
- 取消延迟执行函数 cancelPreviousPerformRequestsWithTarget
- Junit入门学习(2)---------------TestSuite使用
- UIViewController 推出另外一个半透明的UIViewController
- UIScrollView循环滚动图片
- 黑马程序员_GUI
- GPUImage 滤镜头文件
- UIWindow介绍
- OC UIAlertView简化使用
- iOS-UIDynamic-UIKit
- 去哪网实习总结:用到的easyui组件总结(JavaWeb)
- 常识:UI行业常用名词及缩写定义
- JAVA 生成 UUID
- freemaker html页面获取map的key和value
- UITableViewCell drawRect画线在iOS7下不显示的问题
- 让UITableView的headerView或footerView跟随cell一起滚动
- java中StringBuilder、StringBuffer、String类之间的关系
- 排序检索B - List of Conquests
- iOS开发系列--UITableView全面解析
- hdoj 1509 Windows Message Queue
- iOS开发 -- UIViewContentMode各类型效果