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+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.
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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const long long  maxn = 100005;
 6 long long  A[maxn];
 7 int main()
 8 {
 9     long long flag,n, d1, d2;
10     long long sum, cnt;
11     while(~scanf("%I64d %I64d %I64d", &n, &d1, &d2))
12     {
13         for(int i = 0; i < n; i++)
14         {
15             scanf("%I64d", &A[i]);
16         }
17
18         sum = 0, flag = 0, cnt = 0;
19
20         for(int i = 1; i < n; i++)
21         {
22             if(A[i]-A[i-1] == d1 && flag == 0)
23             {
24                 cnt ++;
25                 continue;
26             }
27             if(A[i]-A[i-1] == d2)
28             {
29                 flag = 1;
30                 cnt ++;
31                 continue;
32             }
33             //printf("cnt = %d\n", cnt);
34             sum += (1+cnt)*cnt/2;
35             if(cnt != 0) i--;
36             cnt = 0;
37             flag = 0;
38         }
39         sum += (1+cnt)*cnt/2;
40         //if (d1!=d2)
41         printf("%I64d\n", sum + n);
42         //else printf("%I64d\n",n);
43     }
44     return 0;
45 }
46
47 [ Copy to Clipboard ]    [ Save to File]

转载于:https://www.cnblogs.com/pblr/p/4739770.html

Arithmetic Sequence相关推荐

  1. 构造 HDOJ 5400 Arithmetic Sequence

    题目传送门 题意:问有多少个区间,其中存在j使得ai + d1 == ai+1(i<j) && ai + d2 == ai+1 (i>j) 构造:用c1[i], c2[i] ...

  2. HDU 5400 Arithmetic Sequence

    HDU 5400 Arithmetic Sequence /** HDU 5400 Arithmetic Sequence 直接预处理求解就好了 预处理找出以a[i]结尾最长的subArr长度(满足条 ...

  3. Arithmetic Sequence 三分,货仓选址,nth_element,__int128(济南)

    题意 : 给一序列,每次操作选择一个数加一或者减一,需要将这个序列变成等差数列,问最小操作数 思路 : 数据范围很大又没有头绪,想到二分或者三分.对于不同的公差d,肯定是只有一个最优,两边的都比这个公 ...

  4. XTU 1278 Arithmetic Sequence

    等差数列 题目描述 给一个整数数列,请判断是否为等差数列. 输入 第一行是一个整数T(1≤T≤100),表示样例的个数. 每个样例第一行是一个整数N(3≤N≤10000),表示数列元素的个数. 第二行 ...

  5. LeetCode 1027. Longest Arithmetic Sequence--笔试题--C++解法

    LeetCode 1027. Longest Arithmetic Sequence–笔试题–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专 ...

  6. c语言序列sequence,Sequence

    Sequence Time Limit:1000MS Memory Limit:65536K Total Submit:0 Accepted:0 Description The sequence 1, ...

  7. LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告

    1.题目大意 A sequence of number is called arithmetic if it consists of at least three elements and if th ...

  8. Arithmetic Slices

    这两天一直复习动态规划,就想到leetcode上刷刷题,easy难度的很少,大部分都是medium和hard.本题是第一道DP类型medium难度的题目,但是用其他的方法比如暴力法也可以求解.首先来看 ...

  9. LeetCode 413. Arithmetic Slices

    题目: A sequence of number is called arithmetic if it consists of at least three elements and if the d ...

最新文章

  1. phpMyAdmin的配置
  2. JavaScript面向对象的支持(7)
  3. 关闭主窗体而不退出主程序 以及如何获取操作系统的关闭、注销信息
  4. 2018山东省省赛 问题 H: Dominoes
  5. PyTorch机器学习从入门到实战-CH2
  6. 非线性光纤光学_1.56 m波段高能量百飞秒光纤激光器
  7. 评估一个垃圾收集(GC)
  8. Spring Cloud实战小贴士:随机端口
  9. SQL Server如何链接到 Oracle并查询其中的数据?并实现做接口
  10. “相当一部分”HPE公司OpenStack工作人员被转移至SUSE
  11. oracle 修改字段长度_Oracle修改字符集前如何找出可能出现问题的数据?
  12. 12家存在低俗内容的网络文学企业被约谈:严肃查处 深入整改
  13. 洛谷 P2241统计方形(数据加强版) 题解
  14. 如何修改SAO用户密码
  15. 黑客之门的魅力:感染与加载
  16. DirectX11--使用DirectX Tool Kit帮助开发
  17. 用 PHP 来刷leetCode 之 电话号码的字母组合
  18. Spring AOP @Aspect没反应的处理
  19. Visual Studio 卸载 Visual Assist番茄助手
  20. 在Windows下架设FTP服务器

热门文章

  1. 零基础入门语义分割——Task2 数据扩增
  2. GBDT的回归、二分类以及多分类教程
  3. linux下 卸载qt_Ubuntu14.04安装、配置与卸载QT5的步骤详解
  4. android ndk 头文件,笔记:Android源码和NDK目录及头文件
  5. delphi java jni 互通_编程应用实例:Java通过JNI与Delphi交互
  6. 战斗服务器响应超时是否尝试重连,刺激战场:教你,从开伞到落地瞬间技巧
  7. 基于Android中实现定时器的3种解决方法
  8. 《MFC游戏开发》笔记五 定时器和简单动画
  9. java 列表对话框
  10. linux 脚本做成服务,Shell脚本注册到Linux系统服务实例