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\),你在刚开始的时候有一次交换相邻石子的机会,问你最后能否拿走所有石子. 题解:对于 ...
随机推荐
- 百度编辑器拦截上传图片提交上传腾讯server并返回腾讯url
微信图文编辑时图片必须为上传到腾讯server的url,外连接所有屏蔽,则须要改动百度编辑器的源代码,源代码详细处理地方例如以下图.类为BinaryUploader.java InputStream ...
- Mysql-SQL优化-统计某种类型的个数
有时我们想统计某种类型有多少个,会用这个SQL. 全表扫描之余,还要filesort.耗时1.34秒. mysql> select country,count(*) from t1 group ...
- oracle 11g GRID 中 关于 OLR 须要知道的一些内容
oracle 11g GRID 中 关于 OLR 须要知道的一些内容 1.检查olr 的状态: [root@vmrac1 ~]# ocrcheck -local Status of Oracle ...
- [Java][Android] 多线程同步-主线程等待全部子线程完毕案例
有时候我们会遇到这种问题:做一个大的事情能够被分解为做一系列相似的小的事情,而小的事情无非就是參数上有可能不同样而已! 此时,假设不使用线程,我们势必会浪费许多的时间来完毕整个大的事情.而使用线程的话 ...
- iOS动画案例(1) 仿qq账号信息里的一个动画
受人所托,模仿qq账号信息里的一个动画,感觉挺有意思,也没感觉有多难,就开始做了,结果才发现学的数学知识都还给体育老师了,研究了大半天才做出来. 先看一下动画效果: 用到的知识点: ( ...
- web 开发之js---理解并解决IE的内存泄漏方式
程序当中任何编程内存操作不当都会导致内存泄漏 http://wenku.baidu.com/link?url=8ba4UIn1aaevxTagH-F4vID79-bAfxdcLdeujGFn7PBnv ...
- Android 获取手机SIM卡运营商
直接上代码: /** * 获取SIM卡运营商 * * @param context * @return */ public static String getOperators(Context con ...
- 【bzoj2282】[Sdoi2011]消防
两次bfs可得直径,答案一定不会小于所有点到直径的距离最大值,只要把直径上的边权设为0,任选直径上一点bfs可得将最大值作为二分下界,二分直径左右端点的舍弃部分 #include<algorit ...
- 【bzoj1507】[NOI2003]Editor
第二次写rope了 rope大法好!!! #include<algorithm> #include<iostream> #include<ext/rope> #in ...
- RDA PQ工具使用 (屏参调整)
使用客户规格书,制作屏参文件. 注意:,必填区和计算区,必填区根据屏参的SPEC填写,具体的屏参文件参数通过点击“Calc Sync”生成. 如:HV320WHB-N81的SPEC: 打开“Color ...