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的次方数. 题解 ...
随机推荐
- ffmpeg: error while loading shared libraries: libavdevice.so.52
今天在编译安装ffmpeg的时候出现了题目中的问题,最终解决方案如下: errors: ffmpeg正常安装后执行ffmpeg时出现如下错误:ffmpeg: error while loading s ...
- SQLite多线程使用总结
SQLite支持3种线程模式: 单线程:这种模式下,没有进行互斥,多线程使用不安全.禁用所有的mutex锁,并发使用时会出错.当SQLite编译时加了SQLITE_THREADSAFE=0参数,或者在 ...
- Object.prototype.constructor
Returns a reference to the Object function that created the instance's prototype. 注意这个属性的值是函数本省的引用,而 ...
- ubuntu中使用gensim+word2vec[备忘]
python版本: 2.7.12 0. 安装python和pip 1. 用pip依次安装: numpy, cython,scipy,pattern,word2vec 五个工具包 2. 用pip安装ge ...
- AJAX获取数据,需要添加事件
如果是通过异步请求从后端获取的数据来渲染页面,要添加事件,必须要在页面已有的元素上,添加时间代理.因为页面渲染需要时间,如果直接绑定在响应时间元素上面,很有可能触发不了事件.
- vue.js created函数注意事项
因为created钩子函数是页面一加载完就会调用的函数,所以如果你想在这个组件拿值或者是赋值,很可能this里面能拿到数据,但是如果你用this.赋值的话,控制台或者debugger都会发现this里 ...
- JAVA 内部类 (二)
一.为什么要使用内部类 为什么要使用内部类?在<Think in java>中有这样一句话:使用内部类最吸引人的原因是:每个内部类都能独立地继承一个(接口的)实现,所以无论外围类是否已经继 ...
- ununtu 下安装 Nvidia 显卡驱动
本人电脑硬件配置:CPU : AMD Athlon(tm) II X2 215 Processor × 2 显示卡 : GeForce 6150SE nForce 430/integrated/SSE ...
- shell脚本自动部署nignx反向代理及web服务器,共享存储
#!/bin/bash systemctl status nginx var=$? ] then yum install epel-release -y ] then echo "epel库 ...
- TypeScript完全解读(26课时)_15.模块和命名空间
新建文件夹ts-modules 并新建index.ts 在根index.ts内引入 新建a.ts文件 ts在1.5之前有两个概念一个是内部模块,一个是外部模块,因为在1.5之前es6的标准还没有提出 ...