【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) = ...
随机推荐
- 00089_字节输出流OutputStream
1.字节输出流OutputStream (1)OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节,定义了输出字节流的基本共性功能方法: (2)输出流中定义都是写wri ...
- [Ramda] Simple log function for debugging Compose function / Using R.tap for logging
const log = function(x){ console.log(x); return x; } const get = R.curry(function(prop, obj){ return ...
- Android 最简洁的获取联系人头像的代码
ContentResolver cr = view.getContext().getContentResolver(); Uri uri = ContentUris.withAppendedId(Co ...
- sea.js五分钟上手
SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架.本文给大家分享sea.js知识总结,感兴趣的朋友一起学习吧http://reactjs.cn/http://reactjs. ...
- [D3] Build a Column Chart with D3 v4
Column and bar charts are staples of every visualization library. They also make a great project for ...
- akka---Getting Started Tutorial (Java): First Chapter
原文地址:http://doc.akka.io/docs/akka/2.0.2/intro/getting-started-first-java.html Introduction Welcome t ...
- C语言结构体的字节对齐原则
为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问,这就需要各种类型数据 ...
- flink DataStream API使用及原理
传统的大数据处理方式一般是批处理式的,也就是说,今天所收集的数据,我们明天再把今天收集到的数据算出来,以供大家使用,但是在很多情况下,数据的时效性对于业务的成败是非常关键的. Spark 和 Flin ...
- python的list和数组的区别
list不是数组(额外安装Pynum) 1)可修改,list数据结构内容可以被程序修改 2)可动态增减,长度不固定 3)list里面的数据项可以是不同类型数据,也可以是list 4)两个list可“链 ...
- 【hdu 6208】The Dominator of Strings
[链接]h在这里写链接 [题意] 问你n个串里面有没有一个串,使得其余n-1个串都是他的子串. [题解] 后缀数组. 答案肯定是那个最长的串. 则,把那个串求一下Sa数组(注意仅仅那个最长的串求). ...