zoukankan      html  css  js  c++  java
  • ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

        感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了。。。=_=

      题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数。

      题解:先处理出前缀和sum[i]。然后扫一遍这个前缀和数组:对于每个sum[i],从k的0次方开始枚举,检查map[ sum[i]-k^m ]是否大于0,,即之前是否出现过和值为sum[i]-k^m的前缀和;如果出现过和值为sum[i]-k^m的前缀和,则说明在前缀i和 前缀和值为sum[i]-k^m的这两个前缀之间所包含的那个区间,和值就是为k^m的。     另外,记得特判1和-1,,(终测就wa这了...=_=)

     1 /**
     2 *@author Wixson
     3 */
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <cstdio>
     7 #include <cstring>
     8 #include <cmath>
     9 #include <set>
    10 #include <utility>
    11 #include <vector>
    12 #include <map>
    13 #include <queue>
    14 #include <stack>
    15 const int inf=0x3f3f3f3f;
    16 const double PI=acos(-1.0);
    17 const double EPS=1e-8;
    18 using namespace std;
    19 typedef long long ll;
    20 typedef pair<int,int> P;
    21 
    22 const ll Max=1e14;
    23 int n,k;
    24 ll a[100050];
    25 ll sum[100050];
    26 map<ll,int> book;
    27 int main()
    28 {
    29     //freopen("input.txt","r",stdin);
    30     scanf("%d%d",&n,&k);
    31     for(int i=1; i<=n; i++) scanf("%I64d",&a[i]),sum[i]=sum[i-1]+a[i];
    32     //
    33     if(k==1)
    34     {
    35         book[0]=1;
    36         ll ans=0;
    37         for(int i=1; i<=n; i++)
    38         {
    39             ans+=book[sum[i]-1];
    40             book[sum[i]]++;
    41         }
    42         printf("%I64d
    ",ans);
    43     }
    44     else if(k==-1)
    45     {
    46         book[0]=1;
    47         ll ans=0;
    48         for(int i=1; i<=n; i++)
    49         {
    50             ans+=book[sum[i]+1];
    51             ans+=book[sum[i]-1];
    52             book[sum[i]]++;
    53         }
    54         printf("%I64d
    ",ans);
    55     }
    56     else
    57     {
    58         book[0]=1;
    59         ll ans=0;
    60         for(int i=1; i<=n; i++)
    61         {
    62             ll temp=1;
    63             while(temp<=Max)
    64             {
    65                 ans+=book[sum[i]-temp];
    66                 temp*=k;
    67             }
    68             book[sum[i]]++;
    69         }
    70         printf("%I64d
    ",ans);
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    temp table && check temp table
    AGP Aperture Size && UMA Frame Buffer Size
    ASP中應用BeginTrans的例子
    .NET中加密和解密的实现方法
    c#中Split等分割字符串的几种方法(转)
    AJAX网络开发技术
    MS SQL操作類
    Webconfig中使用appSettings设置连接字符串(转)
    男性10大死因与饮食有关 12食物预防猝死
    C#(IsNumeric) 字符串转换为数字的4种方法(转)
  • 原文地址:https://www.cnblogs.com/geek1116/p/6480862.html
Copyright © 2011-2022 走看看