Codeforces Round #400 C. Molly's Chemicals

来源:互联网 发布:连衣裙 知乎 编辑:程序博客网 时间:2024/05/29 15:11

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 ≤ 1051 ≤ |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 22 2 2 2
output
8
input
4 -33 -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, 24 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^t?

orz 一群人在宿舍开车想了一个小时这个题都没想出什么好的做法,真是菜到抠脚了。

思维总是局限于要去求前缀和然后枚举所有的区间和去验证是否满足k^t,这样铁定超时啊。。。

其实应该换个思路的  我们枚举的区间和有  sum[i]-sum[j]=k^t ,那么我们枚举区间和太费时

我们可以将等式转变为  sum[i]=k^t+sum[j],  j的范围我们知道 0---n-1  那么对于k^t的值我们可以先打个表存储.

sum[i]就是前缀和数组.....,然后记录mp[sum[i]]的结果就行....orz...   注意处理一下k==-1和k==1的情况即可.

 

#include<bits/stdc++.h>#define ll long long using namespace std;const int maxn=1e5+10;const ll inf=1e16+10;int a,n,k;ll sum[maxn],b[70];map<ll,ll>mp;void print(){b[0]=1;for(int i=1;i<=64;i++)if(b[i-1]<inf)b[i]=b[i-1]*k;return ;}int main(){ll ans=0;cin>>n>>k;mp.clear();memset(sum,0,sizeof(sum));print();for(int i=1;i<=n;i++){scanf("%d",&a);for(int j=0;j<=64&&b[j]<inf;j++){if(k==-1||k==1){if(k==-1)mp[sum[i-1]-1]++;mp[sum[i-1]+1]++;break;}else{mp[sum[i-1]+b[j]]++;}}sum[i]=sum[i-1]+a;ans+=mp[sum[i]];}printf("%lld\n",ans);return 0;}

总体来说这种逆向思维的题目一向是自己的弱点.....以后正向思维等式写出来之后 就应该想办法转化等式往逆向思维想一想、、、、


1 0
原创粉丝点击