hdu 5400 Arithmetic Sequence
2015-08-19 23:05
549 查看
click here~~
题目大意:就是给你n个数,然后一个d1和d2,求:
1:这个区间是一个等差数列,且公差为d1或d2;
2:若区间的下标范围为[l,r],应有l<=i<=r,使得[l,i]范围是公差为d1的等差数列,[i,r]范围是公差为d2的等差数列,就是找一共有几种排列方法
吧
解题思路:首先由至少 n 个,然后根据数据推出公式就行了,
直接给出代码吧。。。。
[code] ***Arithmetic Sequence*** 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
题目大意:就是给你n个数,然后一个d1和d2,求:
1:这个区间是一个等差数列,且公差为d1或d2;
2:若区间的下标范围为[l,r],应有l<=i<=r,使得[l,i]范围是公差为d1的等差数列,[i,r]范围是公差为d2的等差数列,就是找一共有几种排列方法
吧
解题思路:首先由至少 n 个,然后根据数据推出公式就行了,
直接给出代码吧。。。。
[code]#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 1e5+5; int data[maxn]; int main() { int d1,d2,n,sum,j,i,t; __int64 ans, k; while(~scanf("%d%d%d",&n,&d1,&d2)) { t = 1; sum = j = 0; ans = 0; k = 1; for(i=1; i<=n; i++) scanf("%d",&data[i]); for(i=1; i<n; i++) { if(t) { if(data[i]+d1 == data[i+1]) k++; else t = 0; j = 0; } if(!t) { if(data[i]+d2 == data[i+1]) { k++; j++; } else { ans += (k+1)*k/2; if(sum+k > i) ans--; k = 1; t = 1; sum = i; if(j) i--; } } } ans += (k+1)*k/2; if(sum+k > i) ans--; printf("%I64d\n",ans); } return 0; }
相关文章推荐
- hdu 2817 A sequence of numbers(快速幂)
- 动态改变UITabBarController的菜单文字
- easyui------显示隐藏列功能
- UIView.animateWithDuration 没有动画过渡效果直接跳到最后
- UIScrollView滚动时隐藏底部导航栏问题
- LightOJ 题目1427 - Substring Frequency (II)(AC自动机)
- 实现UITableView循环利用
- STL使用:deque双端队列
- 8.18 [LeetCode 52] N-Queens II
- UI_04 事件处理
- uva 11248 Frequency Hopping (最大流)
- HDU 3836--Equivalent Sets【求有向图最少增加多少边使图强连通 && Scc缩点新建图】
- priority_queue优先队列容器
- UVA 694-The Collatz Sequence
- GUID全球唯一标识
- GUID全球唯一标识
- ios UIButton的图片和文字位置
- iOS UI基础-2.0按钮操作与形变
- iOS ui 学习 1-2天的笔记
- UITableViewController