【codeforces 776C】Molly’s Chemicals 题解
题目
题目链接:http://codeforces.com/problemset/problem/776/C
C. Molly’s Chemicalstime limit per test2.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputMolly 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.
InputThe 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.
OutputOutput a single integer — the number of valid segments.
ExamplesInput4 2
2 2 2 2Output8Input4 -3
3 -6 -3 12Output3NoteDo 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].
题目大意
有
n
,
题解
看题目,就是求符合条件的区间和问题。穷举明显会爆炸,求区间和的典型做法线段树也不行,查询太多也会爆炸。当时做的时候上网找到一个办法,就是求前缀和。
使 sum[i]=a1+...+ai ,求 ai+...+aj 便转化为求 sum[j]−sum[i−1] 的问题, sum[i] 可以很方便地用 sum[i]=sum[i−1]+an 求得。至此,问题转化为:求符合 i,j(0≤i<j≤n),sum[j]−sum[i]=kx 条件 i,j,x 个数。
转化完成了,接着怎么做?明显穷举是不行的,搞那么多当然有更高的姿势水平啦T_T。把式子转换一下,得 sum[j]=kx+sum[i] ,如果有 sum[j] 等于 kx+sum[i] ,把符合条件的 j 罗列下就好了。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10e5 + 10;
long long sum[maxn];
map<long long, vector<int> > m;
int main()
{
int n, k, t;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
{
scanf("%d", &t);
sum[i] = sum[i - 1] + t;
m[sum[i]].push_back(i);
}
long long re = 0;
long long tt = 1;
while (1)
{
for (int i = 1; i <= n; i++)
{
auto ptr = m.find(sum[i - 1] + tt);
if (ptr == m.end()) continue;
auto &ttt = ptr->second;
for (auto it = ttt.begin(); it != ttt.end(); it++)
{
if (*it >= i) re++;
}
}
//printf("%d\n", re);
if (tt > (10e14 / abs(k)) || tt < (-10e14 / abs(k))) break;
tt *= k;
}
printf("%lld\n", re);
}
}
这段代码参考了网上某个前缀和的代码,把数值相同的
看似很美好,结果在过这个数据时候就爆炸了。
100000 -1
1 -1 1 -1 1 -1 1 -1 1 -1 …
分析发现,在这个数据中,map里面就存了0,1,-1几个数值,后面映射的vector带了大量的数据,在找
能不能避免这个查找呢?反查一下,用
sum[j]
去找
kx+sum[i]
。因为
i<j
,可以在求
sum[j]
之后在map里找到之前的数据,明显里面数据全部小于
j
,即是里面的数据都符合
最后还要注意下 kx 的求法,注意下 k=1,k=−1 时要做下处理,以及别溢出就好了 (|kx|≤1014) 。
第一次写前缀和的题目,也算是个笔记了,希望都能看懂吧。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10e5 + 10;
long long sum[maxn];
map<long long, long long> m;
vector<long long> kn;
int main()
{
int n, k, t;
scanf("%d%d", &n, &k);
long long tt = 1;
while (1)
{
kn.push_back(tt);
if (k == 1 || (k == -1 && tt == -1) || tt > (10e14 / abs(k)) || tt < (-10e14 / abs(k))) break;
tt *= k;
}
long long re = 0;
for (auto j : kn) m[j]++;
for (int i = 1; i <= n; i++)
{
scanf("%d", &t);
sum[i] = sum[i - 1] + t;
re += m[sum[i]];
for (auto j : kn) m[sum[i] + j]++;
}
printf("%lld\n", re);
}