【codeforces 776C】Molly's Chemicals
【题目链接】:http://codeforces.com/contest/776/problem/C
【题意】
让你找区间[i,j]
使得sum[i..j]=k^t,这里t=0,1,2,3..
-10<=k<=10
求出区间个数;
【题解】
/*
k^0==1
要求选[l,r]
sum[l..r]==k^t,t>=0,t=1,2,3...
前缀和
pre[i]-pre[j] =k^t; j∈[1..i-1]
pre[i]=k^t+pre[j-1];
把dic[k^t+pre[j-1]]++;这里t∈....
累加前缀和之后直接ans+=dic[pre[i]];
注意处理一下k=1和k=-1的情况(不一样的!);
*/
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5+100;
LL pre[N],a[N],temp[200],ans = 0;
int n, k,ma = 0;
map <LL, LL> dic;
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n), rei(k);
LL t = 1;
temp[0] = 1;
if (k == 1||k==-1)
{
if (k == 1)
{
ma = 0;
temp[0] = 1;
}
else
{
ma = 1;
temp[0] = 1;
temp[1] = -1;
}
}
else
{
while (abs(t*k) < 1e15)
{
ma++;
t = t*k;
temp[ma] = t;
}
}
rep1(i, 1, n)
rel(a[i]);
rep1(i, 0, ma)
dic[temp[i] + 0]++;
rep1(i, 1, n)
{
pre[i] = pre[i - 1] + a[i];
ans += dic[pre[i]];
rep1(j, 0, ma)
dic[pre[i] + temp[j]]++;
}
printf("%lld\n", ans);
return 0;
}
【codeforces 776C】Molly's Chemicals的更多相关文章
- Codeforces 776C:Molly's Chemicals(思维)
http://codeforces.com/problemset/problem/776/C 题意:给出一个有n个数的序列,还有一个k,问在这个序列中有多少个子序列使得sum[l, r] = k^0, ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- 不同jquery对象触发相同的函数 “.js-story-title,.js-mt-index-cont2”
$(document).on("click",".js-story-title,.js-mt-index-cont2",function () {}
- JavaScript原型及原型链
代码一: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- Mongodb总结3-稍微封装一下
上次发表的2,纯粹是Demo,演示API的用法. 今天,稍微封装了下,看得更清楚. 考虑到不容易做得很有通用性,所以封装的一般,换种场景需要直接修改代码,但是有一部分是可以复用的. 最近项目,很可能只 ...
- C# 文件转byte数组,byte数组再转换文件
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明
http://blog.csdn.net/centre10/article/details/6050466 来自于:http://blog.csdn.net/centre10/article/deta ...
- Java基础学习总结(54)——JSON和Map转换的工具类
在pom.xml文件中引入如下jar <dependency> <groupId>commons-lang</groupId> <artifactId> ...
- WebService学习总结(5)——WebService常见开发框架比较
在SOA领域,我们认为Web Service是SOA体系的构建单元(building block).对于服务开发人员来说,AXIS和CXF一定都不会陌生.这两个产品都是Apache孵化器下面的Web ...
- the steps that may be taken to solve a feature selection problem:特征选择的步骤
參考:JMLR的paper<an introduction to variable and feature selection> we summarize the steps that m ...
- $OEM$文件夹的使用 (By无约而来)
WIN7-OEM资料包中的目录都是以$OEM$文件夹出现的.比$OEM$高一级的目录,我通常是用来表示下一级的$OEM$的属性,例如,X64_ADMIN_LOADER表示此目录下的$OEM$文件夹是用 ...
- 关于Android中设置闹钟的相对比较完善的解决方案
我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这里写的这个demo抽出来了封装了一个类库,大家直接调用其中的设置闹钟和取消闹钟的 ...