题目链接: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 ;
}

最新文章

  1. 浅谈Static关键字
  2. Stack的三种含义
  3. JavaBean学习总结(上)
  4. MMDrawerController在表视图和导航栏中的使用
  5. python Tornado(招聘的一个比较经常问到的知识)
  6. Linux/UNIX 定时任务 cron 详解
  7. 在项目中那个少用if else 语句,精简代码,便于维护的方法(1)
  8. SQL Server 2012将数据库备份到网络中的共享文件夹
  9. Codeforces Gym 100418J Lucky tickets 数位DP
  10. Python中使用Flask、MongoDB搭建简易图片服务器
  11. Jersey框架二:Jersey对JSON的支持
  12. leetcod Pow(x, n)
  13. 解读Batch Normalization
  14. SAS-决策树模型
  15. 【转载】sprintf()函数 和 printf()函数
  16. IDA Pro 在CSAPP lab2中的使用
  17. 手机APP支付--整合支付宝支付控件
  18. linux上安装jdk1.8
  19. POJ 1287
  20. 【持续更新】NOIP注意事项

热门文章

  1. 深数据 - Deep Data
  2. XE下 SVG格式的图标使用方法
  3. tomcat的内存配置,关于-Xms -Xmx -XX:PermSize -XX:MaxPermSize的理解和区别
  4. HTML &lt;area&gt; 标签区域map标签
  5. 【转】Android系统概览
  6. 家用wifi信号覆盖增强扩展实用指南
  7. EF进阶篇(三)——上下文
  8. 【TJOI2017】异或和
  9. luoguP4172 [WC2006]水管局长
  10. 微信开发——测试号申请,接口配置,JS接口安全域名,自定义菜单