Codeforces 776C - Molly's Chemicals(思维+前缀和)
题目大意:给出n个数(a1.....an),和一个数k,问有多少个区间的和等于k的幂 (1 ≤ n ≤ 10^5, 1 ≤ |k| ≤ 10, - 10^9 ≤ ai ≤ 10^9)
解题思路:首先,可以从题目得出k的幂最大不能超过n*ai=1e14。然后,我们先求出前缀和sum[1]...sum[n],k的i次幂为power[i]。我们要求出一个区间和等于power[i]即sum[r]-sum[l]=power[i],转换一下sum[r]=sum[l]+power[i]。
所以我们只需要从1到n遍历一遍,记录下sum[r]的出现次数,用map存起来,即map[sum[i]+power[i]]++。最后如果sum[r]可以从我们的sum[1]到sum[n]这些前缀和里找到,那就把ans+=map[sum[r]]。
代码:
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
const int N = 1e5 + ; ll sum[N];//前缀和
ll power[]; int main() {
ll n, k;
cin >> n >> k;
sum[] = ;
map<ll, ll>mp;
for (int i = ; i <= n; i++) {
cin >> sum[i];
sum[i] += sum[i - ];
} ll t = ;
ll cnt = ;
power[] = ;
//1和-1需要特判
if (k == || k == -) {
if (k == -) {
power[] = -;
cnt++;
}
}
else {
for (int i = ; i <= ; i++) {
t *= k;
if (t>1e14)
break;
power[++cnt] = t;
}
}
//sum[l]=0时的情况
for (int i = ; i <= cnt; i++) {
mp[power[i] + ]++;
} ll ans = ;
for (int i = ; i <= n; i++) {
//计算ans一定要写在这里
ans += mp[sum[i]];
//记录sum[r]=sum[l]+power[j]的出现次数
for (int j = ; j <= cnt; j++) {
mp[sum[i] + power[j]]++;
}
}
cout << ans << endl;
/*这种写法是错误的,因为sum[r]一定在sum[l]之后,可能出现sum[l]+power[i]得到的值不在sum[l]后面,
而在sum[l]前面,那么也就不满足是sum[r]的条件了,下面这样写就会把这种情况也记录在内了。
for (int i = 1; i <= n; i++) {
ans += mp[sum[i]];
}
cout << ans << endl;
*/
return ;
}
Codeforces 776C - Molly's Chemicals(思维+前缀和)的更多相关文章
- codeforces 776C Molly's Chemicals(连续子序列和为k的次方的个数)
题目链接 题意:给出一个有n个数的序列,还有一个k,问在这个序列中有多少个子序列使得sum[l, r] = k^0,1,2,3…… 思路:sum[l, r] = k ^ t, 前缀和sum[r] = ...
- CodeForces - 776C(前缀和+思维)
链接:CodeForces - 776C 题意:给出数组 a[n] ,问有多少个区间和等于 k^x(x >= 0). 题解:求前缀和,标记每个和的个数.对每一个数都遍历到1e5,记录到答案. # ...
- C. Molly's Chemicals
题目链接:http://codeforces.com/problemset/problem/776/C C. Molly's Chemicals time limit per test 2.5 sec ...
- Codeforces_776_C_(思维)(前缀和)
C. Molly's Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes input stand ...
- CodeForces 816B Karen and Coffee(前缀和,大量查询)
CodeForces 816B Karen and Coffee(前缀和,大量查询) Description Karen, a coffee aficionado, wants to know the ...
- [JZOJ5280]膜法师题解--思维+前缀和
[JZOJ5280]膜法师题解--思维+前缀和 题目链接 暴 力 过 于
- Codeforces 776C:Molly's Chemicals(思维)
http://codeforces.com/problemset/problem/776/C 题意:给出一个有n个数的序列,还有一个k,问在这个序列中有多少个子序列使得sum[l, r] = k^0, ...
- 【codeforces 776C】Molly's Chemicals
[题目链接]:http://codeforces.com/contest/776/problem/C [题意] 让你找区间[i,j] 使得sum[i..j]=k^t,这里t=0,1,2,3.. -10 ...
- 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...
随机推荐
- syntax error: non-declaration statement outside function body
在函数外部使用形如:name:="mark"这样语句会出现 syntax error: non-declaration statement outside function bod ...
- Codeforces 895.E Eyes Closed
E. Eyes Closed time limit per test 2.5 seconds memory limit per test 256 megabytes input standard in ...
- [LeetCode] 25. Reverse Nodes in k-Group ☆☆☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- JQ笔记-加强版
Query初级 一.介绍.基本写法 什么是JQ: 一个优秀的JS库,大型开发必备 JQ的好处: 简化JS的复杂操作 不再需要关心兼容性 提供大量实用方法 如何学习JQ: www.jquery. ...
- TabLayout 使用方法 (基础)
此为布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:a ...
- Material Design 之 TabLayout 使用
记录 (非常详细的 TabLayout用法), 学习 http://www.jianshu.com/p/13f334eb16ce
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(三)
属性配置 1.配置application.properties文件 配置web访问端口和context path server.port = 8081 server.servlet.context-p ...
- JAVA多线程提高八:线程锁技术
前面我们讲到了synchronized:那么这节就来将lock的功效. 一.locks相关类 锁相关的类都在包java.util.concurrent.locks下,有以下类和接口: |---Abst ...
- GridControl详解(七)事件
private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventA ...
- docker-It's possible that too few managers are online. Make sure more than half of the managers are online.
问题:docker ---- It's possible that too few managers are online. Make sure more than half of the manag ...