【codeforces 776C】Molly's Chemicals 题解

博客介绍了如何利用前缀和解决codeforces上的776C问题,即求解在给定化学物质序列中,能够通过连续子序列和得到非负整数幂次k的段的数量。博主分享了解题思路,包括问题转化、避免效率低下的查找方法,以及处理特殊情况的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【codeforces 776C】Molly’s Chemicals 题解

题目

题目链接:http://codeforces.com/problemset/problem/776/C

C. Molly’s Chemicals
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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

题目大意

n k ( 1n105 , 1|k|10 ),输入一个序列 a1,a2,...,an(|ai|109) ,输出符合 i,j,x(1ijn,x0) 使 ai+...+aj=kx 条件 i,j,x 的个数。

题解

看题目,就是求符合条件的区间和问题。穷举明显会爆炸,求区间和的典型做法线段树也不行,查询太多也会爆炸。当时做的时候上网找到一个办法,就是求前缀和。

使 sum[i]=a1+...+ai ,求 ai+...+aj 便转化为求 sum[j]sum[i1] 的问题, sum[i] 可以很方便地用 sum[i]=sum[i1]+an 求得。至此,问题转化为:求符合 i,j(0i<jn),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);
    }
}

这段代码参考了网上某个前缀和的代码,把数值相同的sum[i]丢进了map容器里,并把 i 的值丢进对应的vector容器。之后用kx+sum[i]去找 sum[j] 的值,注意 i<j ,找到符合条件的 j 结果加一就好了。

看似很美好,结果在过这个数据时候就爆炸了。

100000 -1
1 -1 1 -1 1 -1 1 -1 1 -1 …

分析发现,在这个数据中,map里面就存了0,1,-1几个数值,后面映射的vector带了大量的数据,在找j的时候效率非常低下。

能不能避免这个查找呢?反查一下,用 sum[j] 去找 kx+sum[i] 。因为 i<j ,可以在求 sum[j] 之后在map里找到之前的数据,明显里面数据全部小于 j ,即是里面的数据都符合i的条件,也就不用映射vector了直接映射个long long数值表示当前符合条件的个数,答案直接加上这个值就好了。最后再塞进去 kx+sum[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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值