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

题意:给一个含有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这了...=_=)

 /**
*@author Wixson
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <utility>
#include <vector>
#include <map>
#include <queue>
#include <stack>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; const ll Max=1e14;
int n,k;
ll a[];
ll sum[];
map<ll,int> book;
int main()
{
//freopen("input.txt","r",stdin);
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++) scanf("%I64d",&a[i]),sum[i]=sum[i-]+a[i];
//
if(k==)
{
book[]=;
ll ans=;
for(int i=; i<=n; i++)
{
ans+=book[sum[i]-];
book[sum[i]]++;
}
printf("%I64d\n",ans);
}
else if(k==-)
{
book[]=;
ll ans=;
for(int i=; i<=n; i++)
{
ans+=book[sum[i]+];
ans+=book[sum[i]-];
book[sum[i]]++;
}
printf("%I64d\n",ans);
}
else
{
book[]=;
ll ans=;
for(int i=; i<=n; i++)
{
ll temp=;
while(temp<=Max)
{
ans+=book[sum[i]-temp];
temp*=k;
}
book[sum[i]]++;
}
printf("%I64d\n",ans);
}
return ;
}

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals的更多相关文章

  1. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀

    A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A

    Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each ...

  3. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 2-SAT

    题目链接:http://codeforces.com/contest/776/problem/D D. The Door Problem time limit per test 2 seconds m ...

  4. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)

    前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几 ...

  5. 【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem

    再来回顾一下2-SAT,把每个点拆点为是和非两个点,如果a能一定推出非b,则a->非b,其他情况同理. 然后跑强连通分量分解,保证a和非a不在同一个分量里面. 这题由于你建完图发现都是双向边,所 ...

  6. 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...

  7. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D

    Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlo ...

  8. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C

    Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an aff ...

  9. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) B

    Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...

随机推荐

  1. Codeforces 771C

    我的树形dp果然是渣... 题意:给一棵树,共n(0<n<=15e4)个节点,可在树上进行跳跃,每次跳的最大距离为k(0<k<=5),定义f(s,t)为(dis(s,t)+k) ...

  2. Android Market google play store帐号注册方法流程 及发布应用注意事项【转载】

    [转载]http://www.cnblogs.com/zdz8207/archive/2012/07/09/google-play-store-registered.html Android Mark ...

  3. 无法定位程序输入点_except_handler4_common于动态链接库msvcrt.dll

    这是由于sp3加载的驱动造成的:只需要将C:\WINDOWS\system32\dwmapi.dll重新命名一下即可以解决. 可以调试程序当系统加载到“c:\Program Files\China M ...

  4. CSS——盒子

    CSS中的盒子具有以下几个种重要的属性: 1.border(边框) :盒子的厚度 2.padding(内边距):盒子内容距离盒子边框的距离 3.margin(外边距):盒子边框与其他的盒子的距离

  5. Java中面向对象三大特性之——多态

    多态的概述:  多态是继封装.继承之后,面向对象的第三大特性. 生活中,比如跑的动作,小猫.小狗和大象,跑起来是不一样的.再比如飞的动作,昆虫.鸟类和飞机,飞起来也是不一样的.可见,同一行为,通过不同 ...

  6. 配置Android的NDK开发环境(eclipse)

    ndk下载地址: http://blog.csdn.net/zhanghuoding/article/details/51345256 在eclipse设置ndk位置 右键你的工程,android t ...

  7. Linux下/var/log/btmp文件

    今天查看了一下服务器,发现/var/log/btmp日志文件比较大占用磁盘空间,搜索一下,此文件是记录错误登录的日志,就是说有很多人试图使用密码字典登录ssh服务,此日志需要使用lastb程序打开. ...

  8. spine骨骼动画组件使用详解

    1. spine骨骼动画工具 骨骼动画: 把动画打散, 通过工具,调骨骼的运动等来形成动画spine是一个非常流行的2D骨骼动画制作工具spine 动画美术人员导出3个文件:    (1) .png文 ...

  9. 数组题汇总(python3)

    题目主要来自<剑指offer>和LeetCode,用python3来写的代码. 1.二维数组的查找: 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列 ...

  10. Python 模块的导入 day5

    一.模块 1.标准模块 python自带的 2.第三方模块 需要自己安装的模块 3.自己写的python文件 一个python文件,就是一个模块 (1)导入模块的实质 就是把你导入的模块运行了一遍 ( ...