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,问将任意相邻的一段数字加起来是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_(思维)(前缀和)的更多相关文章

  1. [JZOJ5280]膜法师题解--思维+前缀和

    [JZOJ5280]膜法师题解--思维+前缀和 题目链接 暴 力 过 于

  2. C. Multi-Subject Competition 思维+前缀和+填表加减复杂度(复杂度计算错误)

    题意: 给出n个学生 m类题目 每个人会做s[i]类的题 并且做这个题的能力为r[i]  组成一个竞赛队 要求可以选择一些题目  在竞赛队中 擅长每一个题目的 人数要均等  求max(sigma(r[ ...

  3. Codeforces 776C - Molly's Chemicals(思维+前缀和)

    题目大意:给出n个数(a1.....an),和一个数k,问有多少个区间的和等于k的幂 (1 ≤ n ≤ 10^5, 1 ≤ |k| ≤ 10, - 10^9 ≤ ai ≤ 10^9) 解题思路:首先, ...

  4. 2019牛客多校训练第三场B.Crazy Binary String(思维+前缀和)

    题目传送门 大致题意: 输入整数n(1<=n<=100000),再输入由n个0或1组成的字符串,求该字符串中满足1和0个数相等的最长子串.子序列. sample input: 801001 ...

  5. atcode E - guruguru(思维+前缀)

    题目链接:http://arc077.contest.atcoder.jp/tasks/arc077_c 题解:一道思维题.不容易想到类似区间求和具体看一下代码. #include <iostr ...

  6. CodeForces - 519D(思维+前缀和)

    题意 https://vjudge.net/problem/CodeForces-519D 给定每个小写字母一个数值,给定一个只包含小写字母的字符串 s,求 s 的子串 t 个数,使 t满足: 首位字 ...

  7. Spotlights【思维+前缀和优化】

    https://blog.csdn.net/mengxiang000000/article/details/53291883   原博客地址 http://codeforces.com/group/1 ...

  8. Educational Codeforces Round 102 (Rated for Div. 2) D. Program (思维,前缀和)

    题意:给你一个只含\(+\)和\(-\)的字符串,给你一个数\(x\),\(x\)初始为\(0\),随着字符串的遍历会加一减一,现在有\(m\)个询问,每个询问给出一个区间\([l,r]\)表示将这个 ...

  9. Codeforces Round #696 (Div. 2) D. Cleaning (思维,前缀和)

    题意:有一堆石子,你每次可以选择相邻(就算两堆石子中间有很多空堆也不算)的两堆石子,使得两堆石子的个数同时\(-1\),你在刚开始的时候有一次交换相邻石子的机会,问你最后能否拿走所有石子. 题解:对于 ...

随机推荐

  1. visio中怎样画线条或箭头

    1.在"画图"工具栏上,单击"铅笔"工具  或"线条"工具  . (凝视   假设看不到"画图"工具栏,请单击" ...

  2. 神马都是浮云,unity中自己写Coroutine协程源代码

    孙广东   2014.7.19 无意之间看到了,Unity维基上的一篇文章,  是关于自己写协程的介绍. 认为非常好,这样能更好的了解到协程的执行机制等特性.还是不错的. 原文链接地址例如以下: ht ...

  3. js中的封装、继承、多态

    Javascript是一门解释型的语言,是基于对象的,并不是真正的面向对象的语言,对变量类型的应用也是宽松的,其实它同样可以模拟面向对象的功能:  1 function myfun1(){  2    ...

  4. c# GDI+绘制不同字体的字符串

    一段字符串中可能既有汉字又有字母,对于汉字和字母分别采用不同的字体进行绘制直接po代码了 Bitmap bmp = new Bitmap(iWidth, iHeight); Graphics g = ...

  5. (七)Java 变量类型

    Java 变量类型 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ...

  6. 【bzoj1029】[JSOI2007]建筑抢修

    按照t2从小到大排列之后贪心. 若当前任务可以插入,则插入. 若当前任务不可以插入,分两种情况: ①当前任务的耗时大于等于之前插入的任务的最大耗时:跳过当前任务 ②当前任务的耗时小于之前插入的任务的耗 ...

  7. [DevExpress]DevExpress的安装与使用

    一.下载安装文件 依据自己的须要选择不同的版本号.下面为15.1 安装时选择自己须要的模块进行安装,之后进行激活,购买授权或者"其它方式". 二.安装完 在VSIDE工具栏会添加下 ...

  8. 修改spring boot 启动logo

    修改spring boot 启动logo 在项目添加文件banner.txt,将需要的logo写在里面 效果:

  9. linux 查看php扩展

    php -i |less 查看配置文件在哪里,编译参数 php -m |less 查看php加载的模块 less可以自由的上下访问,比more要灵活一点. 如果不使用less,信息一次性给予,不太好查 ...

  10. 红米note怎么打开USB调试模式

    红米note到货后,打开USB调试模式是一些新手很棘手的问题,所以要手机助手成功识别红米note,你必须打开红米note的USB调试模式.在安卓4.2版本中,系统的USB调试模式不是非常简单地被打开的 ...