【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) = ...
随机推荐
- Quick Sort Algorithm
快速排序算法实现代码: //============================================================================ // Name : ...
- mysql 语句优化心得
排序导致性能较慢 优化策略:1.尽量不使用排序 2.只查有索引的结果然后 内连接查询 select bizchance0_.* from biz_chance bizchance0_, biz_b ...
- Tomcat redis 配置
http://www.cnblogs.com/interdrp/p/4868740.html http://blog.csdn.net/qq584852076/article/details/4650 ...
- CodeVs——T 3304 水果姐逛水果街Ⅰ
http://codevs.cn/problem/3304/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Des ...
- Cocos2d 游戏状态机
加cocos2d 是标题党. 事实上跟cocos2d无关. 1.游戏背景介绍 比方有这么一个"记忆"类的比赛游戏.你和电脑对战.轮到谁的回合,谁翻两张牌.假设两张牌一样,就消掉这两 ...
- Android 监听电量的状态
监控手机电量的变化和充电状态 在BatteryManager中有一个粘性广播,不需要BroadcastReceiver作为接收器,在注册时将传入null IntentFilter filter = n ...
- amazeui页面分析之登录页面
amazeui页面分析之登录页面 一.总结 1.tpl命名空间:tpl命名空间的样式都是从app.css里面来的,app.css用用来移动网站开发的样式 2.表单样式:am-form到am-form- ...
- 作为一个新人,怎样学习嵌入式Linux?(韦东山)
这篇文章是引用韦老师的部分关于新人怎么学习嵌入式Linux的经验,引用如下: 1.电脑一开机,那些界面是谁显示的?是BIOS,它做什么?一些自检,然后从硬盘上读入windows,并启动它. 类似的, ...
- (转)RMAN-06054: media recovery requesting unknown archived log for thread...
转自:http://blog.itpub.net/29800581/viewspace-1307267/ 使用rman执行recover database 的时候出现RMAN-06054的错误提示: ...
- MySQL 概述和基础
# MySQL 概述 什么是数据库 存储数据的仓库 都有哪些公司在用数据库 金融机构.游戏网站.购物网站.论坛网站-- 提供数据库服务的软件 MySQL.Oracle.SQL Server.DB2.M ...