ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了。。。=_=
题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数。
题解:先处理出前缀和sum[i]。然后扫一遍这个前缀和数组:对于每个sum[i],从k的0次方开始枚举,检查map[ sum[i]-k^m ]是否大于0,,即之前是否出现过和值为sum[i]-k^m的前缀和;如果出现过和值为sum[i]-k^m的前缀和,则说明在前缀i和 前缀和值为sum[i]-k^m的这两个前缀之间所包含的那个区间,和值就是为k^m的。 另外,记得特判1和-1,,(终测就wa这了...=_=)
/**
*@author Wixson
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <utility>
#include <vector>
#include <map>
#include <queue>
#include <stack>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; const ll Max=1e14;
int n,k;
ll a[];
ll sum[];
map<ll,int> book;
int main()
{
//freopen("input.txt","r",stdin);
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++) scanf("%I64d",&a[i]),sum[i]=sum[i-]+a[i];
//
if(k==)
{
book[]=;
ll ans=;
for(int i=; i<=n; i++)
{
ans+=book[sum[i]-];
book[sum[i]]++;
}
printf("%I64d\n",ans);
}
else if(k==-)
{
book[]=;
ll ans=;
for(int i=; i<=n; i++)
{
ans+=book[sum[i]+];
ans+=book[sum[i]-];
book[sum[i]]++;
}
printf("%I64d\n",ans);
}
else
{
book[]=;
ll ans=;
for(int i=; i<=n; i++)
{
ll temp=;
while(temp<=Max)
{
ans+=book[sum[i]-temp];
temp*=k;
}
book[sum[i]]++;
}
printf("%I64d\n",ans);
}
return ;
}
ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals的更多相关文章
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀
A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem 2-SAT
题目链接:http://codeforces.com/contest/776/problem/D D. The Door Problem time limit per test 2 seconds m ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几 ...
- 【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem
再来回顾一下2-SAT,把每个点拆点为是和非两个点,如果a能一定推出非b,则a->非b,其他情况同理. 然后跑强连通分量分解,保证a和非a不在同一个分量里面. 这题由于你建完图发现都是双向边,所 ...
- 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D
Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlo ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C
Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an aff ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) B
Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...
随机推荐
- Android -----listView的重要属性
android:transcriptMode="alwaysScroll" android:cacheColorHint="#00000000" android ...
- Coursera公开课-Machine_learing:编程作业4
编程作业: Neural Network Learning 源码上传到gitlab. 对于神经网络的理解也都在源码注释里面了,感兴趣可以看看.
- ios数据的基本类型和流程控制
swift的声明变量方式和js是类似的.基本类型基本都和java的差不多,多了字符类型. let:用于声明常量: var:用于声明变量: 基本类型有:double,float,Int(数字类型):bo ...
- [Windows Server 2003] 安装IIS6.0及FTP
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装IIS6. ...
- [源码阅读]RocketMQ-策略篇
一:为什么要阅读rocketmq的源码? 1 可以了解mq的底层实现逻辑. 二:打算怎么读,行动路径是哪儿些? 1: 本地启动 2 分步调试 3 fork项目,添加中文注释,提交到自己的代码库.并改 ...
- 常见Android安装启动失败问题
1.INSTALL_FAILED_VERSION_DOWNGRADE版本过低2.Failed to install Funm_AND.apk on device 'QWOJLVR8KNHYA6NR': ...
- (转)Arcgis for javascript实现百度地图ABCD marker的效果
概述: 在我的博客中,有一篇相关的文章,这段时间,有很多人问我求源码,只是时间过去已长,源代码已找不到,乘着这个9.3放假,又重新实现了下,并相关代码做了优化,在此贴出来,方便大家使用. 相关文章地址 ...
- java_servlet执行流程和生命周期
- js 判断 微信浏览器
<script type="text/javascript"> window.onload = function() { isWeixinBrowser(); } // ...
- mysql高可用架构mha之master_ip_failover脚本
脚本如下: #!/usr/bin/env perl use strict; use warnings FATAL => 'all'; use Getopt::Long; my ...