题目链接:http://codeforces.com/problemset/problem/776/C

C. Molly's Chemicals
time limit per test

2.5 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai.

Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of k. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment.

Help her to do so in finding the total number of such segments.

Input

The first line of input contains two integers, n and k, the number of chemicals and the number, such that the total affection value is a non-negative power of this number k. (1 ≤ n ≤ 105, 1 ≤ |k| ≤ 10).

Next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — affection values of chemicals.

Output

Output a single integer — the number of valid segments.

Examples
input

Copy
4 2
2 2 2 2
output

Copy
8
input

Copy
4 -3
3 -6 -3 12
output

Copy
3
Note

Do keep in mind that k0 = 1.

In the first sample, Molly can get following different affection values:

  • 2: segments [1, 1], [2, 2], [3, 3], [4, 4];
  • 4: segments [1, 2], [2, 3], [3, 4];
  • 6: segments [1, 3], [2, 4];
  • 8: segments [1, 4].

Out of these, 2, 4 and 8 are powers of k = 2. Therefore, the answer is 8.

In the second sample, Molly can choose segments [1, 2], [3, 3], [3, 4].

题目大意:给出n个数,和一个数k,现在问你有多少个区间和等于k的x次方,x从0到无穷

思路:首先想到暴力算,遍历每个区间,但是看一下N的范围,显然行不通,那么仔细想一下题目要我们求什么:

求某个区间和是否是某个数的K倍  也就是说sum[r]-sum[l]=pow(K,X)   分析一下这个公式:也许你第一眼看到觉得这样的话 你还不是要N*N的复杂度,一样行不通啊

是的 这样看起来的确行不通,l 和 r 的取值范围是N  那么X呢?   数据最大只有10^14   那么就算K=2    2^X=1e14   不用我说  也知道X很小了吧  这就是这道题入手的关键了

总共就三个  我们遍历其中两个  另外一个是不是就出来了呢?    显然 是的

当然  r>l  这是必须的     所以我们不能把公式变为sum[r]=sum[l]+pow(K,X)   为什么呢?  因为我们要快速找到sum[r]这个值出现了几次  这样的话我们就判断不了这个值是在l之前出现的

还是在 l 之后出现的 。  所以我们把公式变为  sum[l]=sum[r]-pow(K,X)   这样我们刚开始不用存下所有的sum值   存sum[r]的同时存下sum[l]   这样会就保证了sum[l]  在sum[r]前面了

不知读者是否理解了  不理解的话  自己不妨试一下两种公式 自然会明白

本题还有一个坑点  就是K=-1或者1 的时候  所以我们要用set 来存储 排除pow相等的情况  防止多算了次数

看代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const int maxn=1e5+;
const LL INF=1e14+;
LL a[maxn];
LL sum[maxn];
map<LL,LL> m;
set<LL>p;
set<LL>::iterator it;
int main()
{
int N,K; scanf("%d%d",&N,&K);
for(int i=;i<=N;i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-];
}
int D=;
LL f=;
for(int i=;i<=D;i++)
{
p.insert(f);
f*=K;
if(f>INF) break;
}
m[]=;
LL ans=;
for(int i=;i<=N;i++)
{
for(it=p.begin();it!=p.end();it++)
{
LL tmp=sum[i]-(*it);
ans+=m[tmp]; }
m[sum[i]]++;
}
cout<<ans<<endl;
return ;
}

C. Molly's Chemicals的更多相关文章

  1. Codeforces 776C - Molly's Chemicals(思维+前缀和)

    题目大意:给出n个数(a1.....an),和一个数k,问有多少个区间的和等于k的幂 (1 ≤ n ≤ 10^5, 1 ≤ |k| ≤ 10, - 10^9 ≤ ai ≤ 10^9) 解题思路:首先, ...

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

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

  3. codeforces 776C Molly's Chemicals(连续子序列和为k的次方的个数)

    题目链接 题意:给出一个有n个数的序列,还有一个k,问在这个序列中有多少个子序列使得sum[l, r] = k^0,1,2,3…… 思路:sum[l, r] = k ^ t, 前缀和sum[r] = ...

  4. C. Molly's Chemicals 暴力 + 统计技巧

    http://codeforces.com/contest/776/problem/C 一开始做的时候,就发现是预处理前缀和,然后对于每一个前缀和,如果他能成为一个贡献,就是能和前面的某些段 组合成和 ...

  5. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了...=_= 题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数. 题解 ...

  6. 【codeforces 776C】Molly's Chemicals

    [题目链接]:http://codeforces.com/contest/776/problem/C [题意] 让你找区间[i,j] 使得sum[i..j]=k^t,这里t=0,1,2,3.. -10 ...

  7. Codeforces 776C:Molly's Chemicals(思维)

    http://codeforces.com/problemset/problem/776/C 题意:给出一个有n个数的序列,还有一个k,问在这个序列中有多少个子序列使得sum[l, r] = k^0, ...

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

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

  9. 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 ...

随机推荐

  1. Java IO流的回顾与梳理(必记必会必写)

  2. MATLAB读取写入文本数据最佳方法 | Best Method for Loading & Saving Text Data Using MATLAB

    MATLAB读取文件有很多方法.然而笔者在过去进行数据处理中,由于函数太多,相互混杂,与C#,Python等语言相比,反而认为读取文本数据比较麻烦.C#和Python等高级语言中,对于大部分的文本数据 ...

  3. Maven类包冲突终极三大解决技巧 mvn dependency:tree

    Maven对于新手来说是<步步惊心>,因为它包罗万象,博大精深,因为当你初来乍到时,你就像一个进入森林的陌生访客一样迷茫. Maven对于老手来说是<真爱配方>,因为它无所不能 ...

  4. vi常用快捷键汇总

      第一部分:光标移动.复制粘贴.查找替换 [Ctrl] + [f]  下一页 [Ctrl] + [b]  上一页  0或[home]  到行首 $或[end]  到行尾  G  到最后一行  gg ...

  5. Java java.lang.Thread#join()方法分析

    结论:A 线程调用 B 线程对象的 join 方法,则 A 线程会被阻塞,直到 B 线程 挂掉 (Java Doc 原话: Watis for this thread to die). 一.分析 查看 ...

  6. C#常见编译报错

    mCaster.PlayAnim(ANIMID.ASTD); No overload for method 'PlayAnim' takes '1' arguments PlayAnim()内有两个参 ...

  7. HBase优化实战

    本文来自网易云社区. 背景 Datastream一直以来在使用HBase分流日志,每天的数据量很大,日均大概在80亿条,10TB的数据.对于像Datastream这种数据量巨大.对写入要求非常高,并且 ...

  8. 解决eclipse中启动Tomcat成功但是访问不了Tomcat问题

    自己搭建了一个springMVC项目,中间出了一些问题,在排查问题的过程中发现eclipse成功启动了Tomcat,但是在浏览器中输入localhost:8080却给我一个冷冷的404,我以为是Tom ...

  9. conda install 安装太慢怎么办?

    小编我在安装tensorflow和keras的过程中,安装进程太慢,小木棍一直在转圈...抓狂... 如何解决??? 使用清华提供的anaconda镜像,使用以后真的很快! Anaconda 镜像使用 ...

  10. windows server2008虚拟机系统盘扩容

    windows server2008虚拟机的系统盘空间过小,对系统盘进行扩容,操作如下: 1.将虚拟机关机: 2.VMware对该虚拟机进行设置,选中磁盘,点击扩容,输入扩容大小,等待扩容完成: 3. ...