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. 盒子的display属性

    <body> <div style="display:inline">Box-1</div> <div style="displ ...

  2. WebDriver API——浏览器控制暨如何学习webdriver API

    在测试过程中我们可能需要对浏览器进行控制,大小控制啊,刷新页面啊,前进后退等等,最常用的两个接口是window和Navigation. 我们最常用的就是这4个,那么你是否感兴趣它们后面是什么,它们是怎 ...

  3. UVA-11892(组合游戏)

    题意: 给n堆石子,每堆有ai个,每次可以取每堆中任意数目的石子;但是上一次操作的人没有将一堆全部取走,那么下一个人还要在那一堆取; 思路: 每次取到这堆就剩一个的策略; AC代码: #include ...

  4. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  5. [APIO 2015] 雅加达的摩天楼

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4070 [算法] 考虑将每个"Doge"向其所能到达的楼连边 直接 ...

  6. bzoj 1798 Seq 维护序列seq —— 线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1798 这题还4A... 注意:cnt 从1开始:各种模 p:乘法标记初始值是 1:可能乘 0 ...

  7. poj3565Ants——KM算法

    题目:http://poj.org/problem?id=3565 首先,我们神奇地发现,没有相交边的匹配可以转化为距离和最小的匹配,所以可以使用KM算法求带权匹配: 要求的是距离和最小,所以把边权转 ...

  8. 在IIS6.0以上版本发布Ajax中,解决添加.v路径找不到的问题?

    问题描述:配置Aiax方式如下: 1.在AppCode中加入文件夹Ajax,加入两个类文件: Ajax.cs: using System; using System.Collections.Gener ...

  9. 禁用ubuntu 客人会话

    sudo vi /usr/share/lightdm/lightdm.conf.d/50-guest-wrapper.conf 添加: allow-guest=false 重启.

  10. Storm 01之 Storm基本概念及第一个demo

    2.1 Storm基本概念 在运行一个Storm任务之前,需要了解一些概念: Topologies :[tə'pɑ:lədʒɪ]拓扑结构 Streams Spouts:[spaʊt]喷出; 喷射; 滔 ...