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

题意:给一个含有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. day02_12/12/2016_bean的实例化之静态工厂方式

  2. SQL Server 2008R2 Set IDENTITY_INSERT 表名 ON/OFF不能与insert into select 的语句一起执行?

    大家都知数据库表中的列可以自增长,但是有时候我们需要插入数据的时候会指定这一列的数据. 这时候我们可以很简单的利用sql语句来执行新增一条的数据,如下: set IDENTITY_INSER 表名 o ...

  3. 在Swift中,如何像Objective-C定义可选接口?

    Objective-C中的protocol里存在@optional关键字,被这个关键字修饰的方法并非必须要被实现.我们可以通过接口定义一系列方法,然后由实现接口的类选择性地实现其中几个方法.在Coco ...

  4. Linux查找目录下的按时间过滤的文件

    在维护项目中,有时会指定都一些条件进行过滤文件,并对该批文件进行操作:这时我们将使用shell命令进行操作:直接上代码 #!/bin/sh #BEGIN #`find ./ ! -name " ...

  5. iOS - UITableView 单选功能实现

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property(nonatomic,copy)NSStrin ...

  6. JS——模拟百度搜索

    注意事项: 1.for循环移除子节点时,其长度是变化的 2.在文档流中,input.img.p等标签与其他标签有3px的距离,利用左浮动,可以消除3px距离 3.背景图片定位时,第一个值是x轴方向的值 ...

  7. C# 定时无操作则退出登陆,回到登陆界面。

    有时候根据需求需要为程序添加在规定的时间内无操作则退出当前的登陆程序的功能,如下代码模拟描述的需求功能. using System; using System.Collections.Generic; ...

  8. 汇编:MSR/MRS/BIC指令

    1.MRS指令MRS指令的格式为:MRS{条件}   通用寄存器,程序状态寄存器(CPSR或SPSR)MRS指令用于将程序状态寄存器的内容传送到通用寄存器中.该指令一般用在以下两种情冴: Ⅰ.当需要改 ...

  9. Sprinboot优雅配置监听,并记录所有启动事件

    在阅读Springboot启动源码的时候,发现Springboot自动启动listeners是通过uopeizhi文件配置的,本文就是采用Springboot方式自动装入listeners. 项目依赖 ...

  10. cuda_device_functions.h:32:31: fatal error: cuda/include/cuda.h: 没有那个文件或目录

    问题在复现工程https://github.com/google/hdrnet时遇到. 现象: 解决办法: 修改hdrnet文件夹下的Makefile文件:在在nvcc里面添加路径:-I /usr/l ...