Molly's Chemicals 776C

来源:互联网 发布:达内培训 php机构 编辑:程序博客网 时间:2024/05/16 15:43

传送门:http://codeforces.com/problemset/problem/776/C
Describe:
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 ≤ 10^5, 1 ≤ |k| ≤ 10).

Next line contains n integers a1, a2, …, an ( - 10^9 ≤ ai ≤ 10^9) — 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

Tip:
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].

题目大意:给一个长度为n的数组,问有多少个区间D[L,R]内的元素和为k^x(其中x >= 0,并且k^x <= 10^9 * 10^5 = 10^14)

思路:这是一道枚举与前缀和结合的题…复杂度为n*logn*logk 10^14 ,如果存在区间a[1,L-1] = dex1 ,并且又有区间b[1,R+1] = dex2 (dex1,dex2均为前缀和) ,若区间b[1,R+1]-a[1,L-1] = k^x,则说明存在区间d[L,R] = k^x. 问区间数量时,map

#include <iostream>#include <map>#include <cstdio>#include <cmath>#include <cstring>#define N 100010using namespace std;map<long long ,int > mp;int a[N],b[N];long long dex , x ,k ,n,p,ans;int main(){    cin >> n >> k;    for(int i = 0 ; i < n ;i ++){        scanf("%d",&a[i]);    }    p = 1;    while(abs(p) < 1e15){        mp.clear();        dex = 0 ;        mp[0] = 1;        for(int i = 0 ; i < n ;i++){            dex += a[i];            ans += mp[dex - p];            mp[dex]++;        }        p *= k;        if(p == 1)            break;    }    cout << ans << endl;}
原创粉丝点击