zoukankan      html  css  js  c++  java
  • F : yeleng与新型电瓶车(1315)

    题目链接:http://acm.xidian.edu.cn/problem.php?id=1315

      前缀和 + 枚举 + map维护,map中key只有一个,不可重复,value可以重复;

    区间和为sum[i] - sum[j],题目条件是 sum[i] - sum[j] = k,即查找是否存在sum[j] = sum[i] - k,在value中存放sum[i]之前的sum[j]有多少个,若find != end(),则做sum[j]++,否则将新的<sum[i],1>压map;

    ***注意数字范围

    ***1、-1、0要特殊处理

     1 #include<stdio.h>
     2 #include<math.h> 
     3 #include<map>
     4 
     5 using namespace std;
     6 
     7 map<long long,int> S;
     8 int main()
     9 {
    10     int n;
    11     int m;
    12     int A[100005];
    13     long long sum[100005];
    14     while(scanf("%d %d",&n,&m) != EOF)
    15     {
    16         S.clear();
    17         int ans = 0;
    18         sum[0] = 0;
    19         S.insert(pair<long long,int> (0,1));
    20         for(int i = 1;i <= n;i++)
    21         {
    22             scanf("%d",&A[i]);
    23             sum[i] = sum[i - 1] + A[i];
    24             if(m == 1)
    25                 {
    26                 if(S.find(sum[i] - 1) != S.end())
    27                     ans += S[sum[i] - 1];
    28                 }
    29             else if(m == 0)
    30             {
    31                 if(S.find(sum[i]) != S.end())
    32                     ans += S[sum[i]];
    33             }
    34             else if(m == -1)
    35             {
    36                 if(S.find(sum[i] - 1) != S.end() )
    37                     ans += S[sum[i] - 1];
    38                 if(S.find(sum[i] + 1) != S.end() )
    39                     ans += S[sum[i] + 1];
    40             }
    41             else{
    42                 long long temp = 0;
    43                 for(int k = 0;temp < 1e14 && temp > -1e14;k++)
    44                 {
    45                     temp = pow(m,k);
    46                     if(S.find(sum[i] - temp ) != S.end())
    47                         ans += S[sum[i] - temp];
    48                 }
    49             }
    50             if(S.find(sum[i]) != S.end())
    51             S[sum[i]]++; 
    52             else
    53             S.insert(pair<long long,int> (sum[i],1));    
    54         }
    55         printf("%d
    ",ans);
    56     }
    57     return 0;
    58 } 
  • 相关阅读:
    Arctic Network POJ
    Journey CodeForces
    Free Goodies UVA
    MU Puzzle HDU
    Balance POJ
    1sting 大数 递推
    最大报销额 暴力。。
    洛谷P2826 LJJ的数学课
    2018年12月29日
    2018年12月28日
  • 原文地址:https://www.cnblogs.com/Dicer/p/9108740.html
Copyright © 2011-2022 走看看