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 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].
题意:问k^x==(数组区间和),问一共有多少区间符合(看样列)
解法:
1 单个问题,已知一个数,问区间和等于这个数的组合有多少,多个数字就加个循环就好了
2 http://oj.jxust.edu.cn/problem.php?cid=1163&pid=2(一个类似问题)
3 然后下面的代码要跑1s,如果时间掐得紧。。。则不能清空每次循环的结果(第二个代码)
#include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
map<LL,LL>Mp,mp;
vector<LL>Ve;
LL num[];
int n,k;
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
cin>>num[i];
}
Ve.push_back();
if(k==-){
Ve.push_back(-);
}else if(k!=){
for(LL i=k;i<=(2e15);i*=k){
Ve.push_back(i);
}
}
LL ans=;
for(LL i=;i<Ve.size();i++){
Mp.clear();
Mp[]=;
LL sum=;
for(int j=;j<=n;j++){
sum+=num[j],Mp[sum]++;
LL pos=sum-(Ve[i]);
if(Mp.find(pos)!=Mp.end()){
ans+=Mp[pos];
}
}
}
printf("%lld\n",ans);
return ;
}
int n;
cin >> n;
int k;
cin >> k;
FI(n) {
cin >> a[i];
pref[i + ] = pref[i] + a[i];
}
vector<ll> v;
if (k == ) {
v = {};
} else if (k == -) {
v = {, -};
} else {
ll t = ;
while (abs(t) < 2e14) {
v.push_back(t);
t *= k;
}
}
// DBN(v);
ll ans = ;
cnt[]++;
for (int i = ; i < n; ++i) {
ll t = pref[i + ];
for (ll need : v) {
ll x = t - need;
auto it = cnt.find(x);
if (it == cnt.end()) continue;
ans += it->second;
// DBN(i, need, it->second);
}
cnt[t]++;
}
cout << ans << endl;
ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C的更多相关文章
- 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) B
Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了...=_= 题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数. 题解 ...
随机推荐
- java中的几种内部类
Java中的几种内部类 内部类,听名字就可以知道是什么意思,就是类里面的类.有成员内部类,静态内部类,局部内部类和匿名内部类. 下面说一个每种内部类的的使用. 一. 成员内部类
- 谈谈嵌套for循环的理解
谈谈嵌套for循环的理解 说for的嵌套,先说一下一个for循环的是怎么用的. 这次的目的是为了用for循环输出一个乘法口诀表,一下就是我的一步步理解. 一. 语法: ...
- 机器学习: Linear Discriminant Analysis 线性判别分析
Linear discriminant analysis (LDA) 线性判别分析也是机器学习中常用的一种降维算法,与 PCA 相比, LDA 是属于supervised 的一种降维算法.PCA考虑的 ...
- CodeForces - 840D:(主席树求出现区间出现次数大于某值的最小数)
Once, Leha found in the left pocket an array consisting of n integers, and in the right pocket q que ...
- codevs 4768跳石头
传送门 4768 跳石头 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在 ...
- xen添加网卡
brctl addbr xenbr0 ifconfig xenbr0 up ifconfig xenbr0 192.168.0.1 /etc/xen/scripts/network-bridge st ...
- saltstack api安装使用
Salt自然也是提供api的,使用api对自动化有极大的帮助,我们使用rest风格的api,当然大家都知道salt是python写的,那么自然也就提供了对应的api,但是并不建议使用,因为调用pyth ...
- bzoj4589
fwt 原理并不知道 nim游戏石子异或和=0后手赢 那么也就是求a[1]^a[2]^...^a[n]=0的方案数 这个和bzoj3992一样可以dp dp[i][j]表示前i个数异或和为j的方案数 ...
- 五、怎样修改oracle某个用户的密码
1.键入命令:sqlplus / as sysdba 2.在sqlplus窗口执行命令: alter user you_username identified by you_password;
- CS231n 2016 通关 第三章-SVM 作业分析
作业内容,完成作业便可熟悉如下内容: cell 1 设置绘图默认参数 # Run some setup code for this notebook. import random import nu ...