HDU 5400 Arithmetic Sequence(数学)——多校练习9
来源:互联网 发布:火狐兼容的js报表打印 编辑:程序博客网 时间:2024/06/01 09:28
Arithmetic Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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 sequencea1,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.
Teacher Mai has a sequence
Input
There are multiple test cases.
For each test case, the first line contains three numbersn,d1,d2(1≤n≤105,|d1|,|d2|≤1000) , the next line contains n integers a1,a2,⋯,an(|ai|≤109) .
For each test case, the first line contains three numbers
Output
For each test case, print the answer.
Sample Input
5 2 -20 2 0 -2 05 2 32 3 3 3 3
Sample Output
125
题意:给你一个序列b1,b2,⋯,bn,问有多少个区间[l,r]满足该区间存在一个i,使得[l,i]是以d1为公差的等差数列,[i,r]是以d2为公差的等差数列
解题思路:老办法,观察样例吧,第一个样例的取法是{0},{0,2},{0,2,0},{0,2,0,-2},{2},{2,0},{2,0,-2},{0},{0,-2},{-2},{-2,0},{0}共12种由此可见我们只需先找出满足题意的区间[l,r],知道里面序列的个数k,那么该区间的情况数为
但是有一点不能忽略,即一个元素bi,若它既属于前一个区间,又属于后一个区间的话,它本身会被多计算一次,比如样例1中的-2,它既属于区间[1,4],又属于区间[4,5],那么若单纯带公式再相加,那么取法{-2}会被计算两次,所以解题的时候判断一下,减去多算的即为结果。
放上一组样例以供参考
Input
5 2 2
0 2 0 2 4
Output
9
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 100005;const int inf = 1000000000;const int mod = 258280327;int s[N];int main(){ int n,d1,d2,i,t,j,sum; __int64 ans,k; while(~scanf("%d%d%d",&n,&d1,&d2)) { t=1;sum=j=0;k=1;ans=0; for(i=1;i<=n;i++) scanf("%d",&s[i]); for(i=1;i<n;i++) { if(t) { if(s[i]+d1==s[i+1]) k++; else t=0; j=0; } if(!t) { if(s[i]+d2==s[i+1]) { k++; j++; } else { ans+=(k+1)*k/2; if(sum+k>i) ans--; //printf("%d\n",k); k=1; t=1; sum=i; if(j) i--; } } } ans+=(k+1)*k/2; if(sum+k>i) ans--; //printf("%d\n",k); printf("%I64d\n",ans); } return 0;}菜鸟成长记
0 0
- HDU 5400 Arithmetic Sequence(数学)——多校练习9
- 2015多校第九场 HDU 5400 Arithmetic Sequence 数学
- HDU-5400 Arithmetic Sequence(数学 || DP)
- HDU 5400 Arithmetic Sequence(数学,容斥)
- Hdu 5400 Arithmetic Sequence 数学推理
- HDU 5400 Arithmetic Sequence
- hdu 5400 Arithmetic Sequence
- HDU 5400 Arithmetic Sequence
- HDU 5400 Arithmetic Sequence
- HDU 5400 Arithmetic Sequence
- hdu 5400 Arithmetic Sequence
- hdu 5400 Arithmetic Sequence
- hdu 5400 Arithmetic Sequence
- hdu(5400)——Arithmetic Sequence(想法题)
- HDU 5400 Arithmetic Sequence(2015 Multi-University Training Contest 9 2015多校联合)
- hdu 5400 Arithmetic Sequence 2015多校联合训练赛#9 枚举
- 多校第九场 1005 hdu 5400 Arithmetic Sequence ( dp)
- hdu 5400 Arithmetic Sequence(计数)
- ppt倒计时器制作方法
- Qt中对sqlite加密
- linux 上传文件夹
- ASP.net中TextBoxLabel等控件渲染的不同状态
- 保证任务在主线程中执行(NSObject+GCD.h)
- HDU 5400 Arithmetic Sequence(数学)——多校练习9
- C++函数调用详解
- 从头到尾彻底理解KMP
- iOS项目开发实战——自定义圆形进度提示控件
- Load Asset Bundle in Editor
- UIViewController 推出另外一个半透明的UIViewController
- 软件项目管理(CMMI成熟度)实践——之系统测试
- 黑马程序员_TCPUDP
- drools6入门例子