【题目链接】:http://codeforces.com/contest/776/problem/C
【题意】
让你找区间[i,j]
使得sum[i..j]=k^t,这里t=0,1,2,3..
-10<=k<=10
求出区间个数;
【题解】
/*
k^0==1
要求选[l,r]
sum[l..r]==k^t,t>=0,t=1,2,3...
前缀和
pre[i]-pre[j] =k^t; j∈[1..i-1]
pre[i]=k^t+pre[j-1];
把dic[k^t+pre[j-1]]++;这里t∈....
累加前缀和之后直接ans+=dic[pre[i]];
注意处理一下k=1和k=-1的情况(不一样的!);
*/
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5+100;
LL pre[N],a[N],temp[200],ans = 0;
int n, k,ma = 0;
map <LL, LL> dic;
int main()
{
//freopen("F:\rush.txt", "r", stdin);
rei(n), rei(k);
LL t = 1;
temp[0] = 1;
if (k == 1||k==-1)
{
if (k == 1)
{
ma = 0;
temp[0] = 1;
}
else
{
ma = 1;
temp[0] = 1;
temp[1] = -1;
}
}
else
{
while (abs(t*k) < 1e15)
{
ma++;
t = t*k;
temp[ma] = t;
}
}
rep1(i, 1, n)
rel(a[i]);
rep1(i, 0, ma)
dic[temp[i] + 0]++;
rep1(i, 1, n)
{
pre[i] = pre[i - 1] + a[i];
ans += dic[pre[i]];
rep1(j, 0, ma)
dic[pre[i] + temp[j]]++;
}
printf("%lld
", ans);
return 0;
}