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
4 2
2 2 2 2
output
8
input
4 -3
3 -6 -3 12
output
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].

题意:问k^x==(数组区间和),问一共有多少区间符合(看样列)

解法:

1 单个问题,已知一个数,问区间和等于这个数的组合有多少,多个数字就加个循环就好了

2 http://oj.jxust.edu.cn/problem.php?cid=1163&pid=2(一个类似问题)

3 然后下面的代码要跑1s,如果时间掐得紧。。。则不能清空每次循环的结果(第二个代码)

 #include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
map<LL,LL>Mp,mp;
vector<LL>Ve;
LL num[];
int n,k;
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
cin>>num[i];
}
Ve.push_back();
if(k==-){
Ve.push_back(-);
}else if(k!=){
for(LL i=k;i<=(2e15);i*=k){
Ve.push_back(i);
}
}
LL ans=;
for(LL i=;i<Ve.size();i++){
Mp.clear();
Mp[]=;
LL sum=;
for(int j=;j<=n;j++){
sum+=num[j],Mp[sum]++;
LL pos=sum-(Ve[i]);
if(Mp.find(pos)!=Mp.end()){
ans+=Mp[pos];
}
}
}
printf("%lld\n",ans);
return ;
}
 int n;
cin >> n;
int k;
cin >> k;
FI(n) {
cin >> a[i];
pref[i + ] = pref[i] + a[i];
}
vector<ll> v;
if (k == ) {
v = {};
} else if (k == -) {
v = {, -};
} else {
ll t = ;
while (abs(t) < 2e14) {
v.push_back(t);
t *= k;
}
}
// DBN(v);
ll ans = ;
cnt[]++;
for (int i = ; i < n; ++i) {
ll t = pref[i + ];
for (ll need : v) {
ll x = t - need;
auto it = cnt.find(x);
if (it == cnt.end()) continue;
ans += it->second;
// DBN(i, need, it->second);
}
cnt[t]++;
}
cout << ans << endl;

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

  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) B

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

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

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

随机推荐

  1. the art of seo(chapter five)

    Keyword Research ***The Theory Behind Keyword Research***1.When users go to search engines and type ...

  2. tensorflow sigmoid_cross_entropy_with_logits 函数解释

    tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,labels=None, logits=None, name=None) sigmoid_ ...

  3. 《UML和模式应用》读书笔记(一)

    一.绪论 1. 面向对象分析和设计 1.1 什么是分析和设计 分析(analysis)强调的是对问题和需求的调查研究,而不是解决方案. 设计(design)强调的是满足需求的概念上的解决方案,而不是其 ...

  4. 谈谈javaScript

    谈谈javaScript  (杰我学习) 一. 什么是JavaScript       人们通常所说的JavaScript,其正式名称为ECMAScript.这个标准由ECMA组织发展和维护.ECMA ...

  5. Piggy-Bank(复习完全背包)

    传送门 题目大意: 有一个存钱的储存罐,给你它存满钱之前和之后的重量,和几类硬币的面值和重量. 求装满储钱罐时最小能得到多少钱. 题解:完全背包变形. 因为要求最小 一开始赋值大数. code: #i ...

  6. 如何应用AutoIt,把局域网中所有的机器名展示在一个combox中?

    有时候,我们会遇到以下情况: 你想与局域网中的某台机器建立连接,你就需要输入对方的机器名. 现在我比较懒,我不想输入对方的机器名,或者对方的机器名很难记住,那怎么办呢? 那就做一个combox在页面上 ...

  7. 条件变量pthread_cond_wait()和pthread_cond_signal()详解

    条件变量        条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起:另一个线程使"条件成立" ...

  8. poj1275收银员——差分约束

    题目:http://poj.org/problem?id=1275 做的第一道差分约束题... 首先,根据题意得出一些不等关系(f为前缀和雇佣人数): 0 <= f[i] - f[i-1] &l ...

  9. 解压缩zip,tar,tar.gz,tar.bz2文件

    .tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gunz ...

  10. 比利牛斯獒犬 flask web

    1. 使用 session.get('name') 直接从会话中读取 name 参数的值.和普通的字典一样,这里使用 get() 获取字典中键对应的值以避免未找到键的异常情况,因为对于不存在的键, g ...