HDU5400 Arithmetic Sequence
解题思路:这题看懂题目是很关键的,这个区间是等差数列,且公差为d1或d2,
特别注意单个数字也为等差数列。每次求出等差数列序列长度,然后
求出对应这种长度对应有多少种组合方式,累加起来就是结果。
注意要用long long,还有注意特判数据,如 5 -1 -1 ,5 4 3 2 1;
5 1 1, 1 2 3 4 5 ; 5 1 1, 1 1 1 1 1等。
#include<cstdio>
int main()
{
int A[], n, d1, d2;
long long sum, cnt; //注意这里要用long long 否则会WA
//int sum, cnt;
while(~scanf("%d %d %d", &n, &d1, &d2))
{
sum = cnt = ;
for(int i = ; i < n; i++) scanf("%d", &A[i]);
for(int i = ; i < n; i++)
{
if(A[i]-A[i-] == d1)
{
while(A[i]-A[i-] == d1)
{
i ++;
cnt ++;
if(i == n) break;
}
if(i == n) break; //i为n时要及时跳出,其它地方同理。
while(A[i]-A[i-] == d2)
{
i ++;
cnt ++;
if(i == n) break;
}
sum += (cnt+)*cnt/; //满足条件的序列长度为cnt+1时,共有(cnt+1)*cnt/2种组合方式
cnt = ; //cnt重新初始化
i --; //一定要回退一步,画画就知道了。
continue;
}
if(i == n) break;
if(A[i]-A[i-] == d2)
{
while(A[i]-A[i-] == d2)
{
i ++;
cnt ++;
if(i == n) break;
}
sum += (cnt+)*cnt/;
cnt = ;
i --;
}
}
sum += (cnt+)*cnt/; //这步不能少
printf("%I64d\n", sum+n); //一定要加上这个n,刚开始我加的是5,WA了一发
// printf("%d\n", sum+n);
}
return ;
}
HDU5400 Arithmetic Sequence的更多相关文章
- [hdu5400 Arithmetic Sequence]预处理,容斥
题意:http://acm.hdu.edu.cn/showproblem.php?pid=5400 思路:预处理出每个点向左和向右的最远边界,从左向右枚举中间点,把区间答案加到总答案里面.由与可能与前 ...
- hdu 5400 Arithmetic Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5400 Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Ot ...
- hdu 5400 Arithmetic Sequence(模拟)
Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...
- Arithmetic Sequence(dp)
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 19[Submit][Status][We ...
- [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...
- (模拟)Arithmetic Sequence -- HDU -- 5400
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- HZAU 21——Arithmetic Sequence——————【暴力 or dp】
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1810 Solved: 311[Submit][Status] ...
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
1020: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: ->打开链接<- Descriptio ...
- LeetCode 1027. Longest Arithmetic Sequence
原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...
随机推荐
- [C++]内存字节对齐
当我们写一个class类,然后sizeof(),然后发现这个值往往比你想象的大,这是为什么呢?这里就要讲到内存对齐的问题. 先来看一下内存对齐的几条原则: 1.对于class(struct/union ...
- Razor视图引擎 语法学习(二)
下面就和大家分享下我在asp.net官网看到的资料,学习到的点语法.1.通过使用@符号,可以直接在html页面中写C#或者VB代码:运行后: 2.页面中的C#或者VB代码都放在大括号中.运行后: 3. ...
- (转)Android之ListView原理学习与优化总结
转自: http://jishu.zol.com.cn/12893.html 在整理前几篇文章的时候有朋友提出写一下ListView的性能优化方面的东西,这个问题也是小马在面试过程中被别人问到的….. ...
- 知问前端——概述及jQuery UI
知问系统,是一个问答系统.主要功能:即会员提出问题,会员回答问题.目前比较热门的此类网站有:知乎http://www.zhihu.com.百度知道http://zhidao.baidu.com等.这里 ...
- hdu 1124 Factorial(数论)
题意: 求n!的尾0的个数 分析: 0一定是由因子2和5相乘产生的: 2的个数显然大于5的个数,故只需统计因子5的个数 n/5不能完全表示n!中5的个数(egg: 25),应该n/=5后,累加上n/2 ...
- 【转】SIP 中的Dialog,call,session 和 transaction
如果你对Sip协议中Call, Dialog, Transaction和Message之间的关系感觉到迷惑,那么,那么我可以告诉你,你并不孤单,因为大多数初学者对于这些名词之间的关系都会感到疑惑. ...
- MyBatis学习总结_11_MyBatis动态Sql语句
MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...
- slot signal机制
有一个比较 经典的实现:http://sigslot.sourceforge.net/很精简的 signal slot的实现,跨平台.webrtc项目在用,我在自己项目里也用了.这个源码有2000多行 ...
- jmeter之配置文件介绍
jmeter.bat, jmeter.properties在installpath/bin目录下 jmeter.bat文件包含如下内容: set HEAP=-Xms512m(初始化堆内存大小) -Xm ...
- js中的cookie使用
在网上找到的资料,收藏一下 function getCookies(name) { var arr = document.cookie.match(new RegExp("(^| )&quo ...