hdu 5400 Arithmetic Sequence
2015-08-21 11:20
561 查看
点击打开题目链接
[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+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.
[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]
[align=left]Sample Output[/align]
题意:
第一行n,d1,d2,表示n个数,d1和d2代表等差
第二行n个数。
叫你任选一个区间[l,r],要求这个区间中能找到一个点i,(l<=i<=r)使得前半部分[l,i]是以d1为等差的等差数列,后半部分[i,r]是以d2为等差的等差数列,问你有多少不同的区间。
这个题要注意当d1==d2时,前半部分和后半部分构成等差,所以计算时可能会算重,还有就是中间过程计算时会超int,全程用long long就行。
[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+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.
[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
题意:
第一行n,d1,d2,表示n个数,d1和d2代表等差
第二行n个数。
叫你任选一个区间[l,r],要求这个区间中能找到一个点i,(l<=i<=r)使得前半部分[l,i]是以d1为等差的等差数列,后半部分[i,r]是以d2为等差的等差数列,问你有多少不同的区间。
这个题要注意当d1==d2时,前半部分和后半部分构成等差,所以计算时可能会算重,还有就是中间过程计算时会超int,全程用long long就行。
#include<stdio.h> #include<string.h> #include<vector> #include<algorithm> #define ll long long #define maxn 100005 using namespace std; ll a[maxn],l[maxn],r[maxn]; int main() { ll n,d1,d2; while(scanf("%lld%lld%lld",&n,&d1,&d2)!=EOF) { for(ll i=1;i<=n;i++) scanf("%lld",&a[i]); l[1]=1; for(ll i=2;i<=n;i++) { if(a[i-1]+d1==a[i]) l[i]=l[i-1]+1; else l[i]=1; } r =1; for(ll i=n-1;i>=1;i--) { if(a[i]+d2==a[i+1]) r[i]=r[i+1]+1; else r[i]=1; } ll ans=0; for(ll i=1;i<=n;i++) { if(d1==d2) ans+=r[i]-1; else ans+=(l[i]-1)*(r[i]-1)+l[i]+r[i]-2; } printf("%lld\n",ans+n); } return 0; }
相关文章推荐
- HDU 4746 Mophues (莫比乌斯反演应用)
- 代码实现UIPickerView
- 第十四篇 continue VS. break
- CoreBluetooth使用 (服务端中心设备)
- Xcode强大的多视图立体分层显示View UI Herarchy
- Hibernate SQL优化技巧dynamic-insert="true" dynamic-update="true"
- Jsp重定向(response.sendRedirect())和转发(request.getRequestDispatcher().forward(request,r)的区别
- Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project '项目名'
- 在Android Studio中进行单元测试和UI测试
- Uiautomator Viewer中文显示乱码问题
- The requested URL /get_data.xml was not found on this server.
- spoj 375 QTREE - Query on a tree 树链剖分 LCT 动态树
- UIDeviceOrientation和UIInterfaceOrientation中left、right的含义
- Pop Sequence (25)--栈模拟经典
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class
- [LeetCode#52]N-Queens II
- Android开源资料大集合_架构&UI
- apue源码运行配置
- iOS UIViewController 和 nib 相关的3个方法
- Java之String,StringBuffer,StringBuilder类