Codeforces_776_C_(思维)(前缀和)
2.5 seconds
512 megabytes
standard input
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.
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 a single integer — the number of valid segments.
4 2
2 2 2 2
8
4 -3
3 -6 -3 12
3
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,问将任意相邻的一段数字加起来是k的幂的方法数。
一直没有思路,只能想到枚举起点终点。。。
看了题解,用的方法感觉挺巧妙的。
思路:对n个数求前缀和,记录下前缀和的种数(即前缀中能得到值s的种数),然后对每个k的幂(power)求sum-power的和。
在其前缀中有多少种方式组成sum-power,那么当前就能多少种power。
#include <cstdio>
#include <cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define N 100005
#define LL long long int num[N];
map<LL,int> ma;
int main()
{
int n,k;
LL tmp=;
scanf("%d%d",&n,&k);
for(int i=; i<n; i++)
scanf("%d",&num[i]);
LL power=,sum=,res=;
while(abs(power)<1e15)
{
ma.clear();
ma[]=;
sum=;
for(int i=; i<n; i++)
{
sum+=num[i];
res+=ma[sum-power];
ma[sum]++;
}
power*=k;
if(power==)
break;
//cout<<res<<endl;
}
printf("%I64d\n",res);
return ;
}
Codeforces_776_C_(思维)(前缀和)的更多相关文章
- [JZOJ5280]膜法师题解--思维+前缀和
[JZOJ5280]膜法师题解--思维+前缀和 题目链接 暴 力 过 于
- C. Multi-Subject Competition 思维+前缀和+填表加减复杂度(复杂度计算错误)
题意: 给出n个学生 m类题目 每个人会做s[i]类的题 并且做这个题的能力为r[i] 组成一个竞赛队 要求可以选择一些题目 在竞赛队中 擅长每一个题目的 人数要均等 求max(sigma(r[ ...
- Codeforces 776C - Molly's Chemicals(思维+前缀和)
题目大意:给出n个数(a1.....an),和一个数k,问有多少个区间的和等于k的幂 (1 ≤ n ≤ 10^5, 1 ≤ |k| ≤ 10, - 10^9 ≤ ai ≤ 10^9) 解题思路:首先, ...
- 2019牛客多校训练第三场B.Crazy Binary String(思维+前缀和)
题目传送门 大致题意: 输入整数n(1<=n<=100000),再输入由n个0或1组成的字符串,求该字符串中满足1和0个数相等的最长子串.子序列. sample input: 801001 ...
- atcode E - guruguru(思维+前缀)
题目链接:http://arc077.contest.atcoder.jp/tasks/arc077_c 题解:一道思维题.不容易想到类似区间求和具体看一下代码. #include <iostr ...
- CodeForces - 519D(思维+前缀和)
题意 https://vjudge.net/problem/CodeForces-519D 给定每个小写字母一个数值,给定一个只包含小写字母的字符串 s,求 s 的子串 t 个数,使 t满足: 首位字 ...
- Spotlights【思维+前缀和优化】
https://blog.csdn.net/mengxiang000000/article/details/53291883 原博客地址 http://codeforces.com/group/1 ...
- Educational Codeforces Round 102 (Rated for Div. 2) D. Program (思维,前缀和)
题意:给你一个只含\(+\)和\(-\)的字符串,给你一个数\(x\),\(x\)初始为\(0\),随着字符串的遍历会加一减一,现在有\(m\)个询问,每个询问给出一个区间\([l,r]\)表示将这个 ...
- Codeforces Round #696 (Div. 2) D. Cleaning (思维,前缀和)
题意:有一堆石子,你每次可以选择相邻(就算两堆石子中间有很多空堆也不算)的两堆石子,使得两堆石子的个数同时\(-1\),你在刚开始的时候有一次交换相邻石子的机会,问你最后能否拿走所有石子. 题解:对于 ...
随机推荐
- yarn-cli 添加
添加依赖包 当你想使用另一个包时,你要先把它添加到依赖列表中.也就是执行 yarn add [package-name] 命令将它安装到你的项目中. 这也将同时更新你的 package.json 和 ...
- 用Java开发50个棋类游戏
眼下已经开发完了两个 1A2B 24点 打算开发以下的.直接在QQ上玩. QQ机器人已经有了.我们直接写业务即可.有兴趣的參与.机器人婷婷体验群 Java技术交流 207224939 四棋 小枪大炮 ...
- Servlet学习笔记(八)—— 文件下载
一.文件下载概述 比如图片或者HTML这类静态资源,仅仅要在浏览器中打开正确的网址就行下载.仅仅要资源放在应用程序文件夹或者其下的子文件夹中,但不在WEB-INF下.Servlet/JSP容器就会将资 ...
- docker (1) ---简介,使用
一.docker简介: 容器( container-based )虚拟化方案,充分利用了操作系统本身已有的机 制和特性,以实现轻量级的虚拟化(每个虚拟机安装的不是完整的虚拟机), 甚至有人把他称为新一 ...
- HDU1236 排名 题解
Problem Description 今天的上机考试尽管有实时的Ranklist,但上面的排名仅仅是依据完毕的题数排序,没有考虑 每题的分值,所以并非最后的排名.给定录取分数线.请你敲代码找出最后 ...
- easyUI 动态添加窗体
有一张页面A,在页面开头引用了jquery.easyUI.min.js. 现在想达到这么一种效果,点击页面A的一个按钮,弹出一个easyUI窗体.因为想分模块的原因,这个窗体对应的是另一张页面B.在点 ...
- java Bean及其使用
1 getter/setter方法 java例子: public class student { private String name; public String getName() { retu ...
- iOS 证书详解
引言 关于开发证书配置(Certificates & Identifiers & Provisioning Profiles),相信做iOS开发的同学没少被折腾.对于一个iOS开发小白 ...
- 【PA 2014】Kuglarz
[题目链接] 点击打开链接 [算法] sum[i]表示前i个杯子中,杯子底下藏有球的杯子总数 那么,知道[i,j]这段区间中,藏有球的 ...
- JeePlus:目录
ylbtech-JeePlus:目录 1.返回顶部 0. http://www.jeeplus.org/ 0.2.文档 http://wiki.jeeplus.org/docs/show/75 0.3 ...