zoukankan      html  css  js  c++  java
  • Arithmetic Sequence

    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(1in) such that for every j(1j<i),bj+1=bj+d1 and for every j(ij<n),bj+1=bj+d2.

    Teacher Mai has a sequence a1,a2,,an. He wants to know how many intervals [l,r](1lrn) 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(1n105,|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
    题意:给定一序列和d1,d2.问有多少间隔可以保证存在i,对于每一个j,有j(1j<i),bj+1=bj+d1, j(ij<n),bj+1=bj+d2.成立。
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 const int N = 211100;
     8 const int oo = 0x3f3f3f3f;
     9 long long al[N], ar[N], as[N], n;  // al数组存从左边到这个点一直满足的+d1的数有几个,ar是往右边数满足+d2的有几个。
    10 int main()
    11 {
    12     long long d1, d2, i, ans;
    13     while(~scanf("%lld %lld %lld", &n, &d1, &d2))
    14     {
    15         for(i = 1; i <= n; i++)
    16             scanf("%lld", &as[i]);
    17         al[1] = ar[n] = 1;
    18         for(i = 2; i <= n; i++)
    19             if(as[i] == as[i-1]+d1)
    20                 al[i] = al[i-1]+1;
    21             else al[i] = 1;  // 只要间隔,不满足了那么连续的就是1个,它自身
    22         for(i = n-1; i >= 1; i--)
    23         {
    24             if(as[i]+d2 == as[i+1])
    25                 ar[i] = ar[i+1]+1;
    26             else ar[i] = 1;
    27         }
    28         ans = 0;
    29         for(i = 1; i <= n; i++)
    30         {
    31             if(d1 == d2) ans += al[i];  // 如果d1,d2相等,al直接相加
    32             else ans += al[i]*ar[i];  // if不等,就等于两者相乘,即间隔种类数
    33         }
    34         printf("%lld
    ", ans);
    35     }
    36     return 0;
    37 }
    让未来到来 让过去过去
  • 相关阅读:
    你不知道的JS系列上( 40 ) - 什么是类
    CF356E
    [HDU4135]CO Prime(容斥)
    [POJ 2888]Magic Bracelet[Polya Burnside 置换 矩阵]
    Polya定理与Burnside引理
    选举
    David与Vincent的博弈游戏[树型DP]
    Vincent的城堡
    三元组
    vue打包体积优化之旅
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4740409.html
Copyright © 2011-2022 走看看