Molly's Chemicals 776C
来源:互联网 发布:达内培训 php机构 编辑:程序博客网 时间:2024/05/16 15:43
传送门:http://codeforces.com/problemset/problem/776/C
Describe:
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.
Input
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 ≤ 10^5, 1 ≤ |k| ≤ 10).
Next line contains n integers a1, a2, …, an ( - 10^9 ≤ ai ≤ 10^9) — affection values of chemicals.
Output
Output a single integer — the number of valid segments.
Examples
input
4 2
2 2 2 2
output
8
input
4 -3
3 -6 -3 12
output
3
Tip:
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].
题目大意:给一个长度为n的数组,问有多少个区间D[L,R]内的元素和为k^x(其中x >= 0,并且k^x <= 10^9 * 10^5 = 10^14)
思路:这是一道枚举与前缀和结合的题…复杂度为n*logn*logk 10^14 ,如果存在区间a[1,L-1] = dex1 ,并且又有区间b[1,R+1] = dex2 (dex1,dex2均为前缀和) ,若区间b[1,R+1]-a[1,L-1] = k^x,则说明存在区间d[L,R] = k^x. 问区间数量时,map
#include <iostream>#include <map>#include <cstdio>#include <cmath>#include <cstring>#define N 100010using namespace std;map<long long ,int > mp;int a[N],b[N];long long dex , x ,k ,n,p,ans;int main(){ cin >> n >> k; for(int i = 0 ; i < n ;i ++){ scanf("%d",&a[i]); } p = 1; while(abs(p) < 1e15){ mp.clear(); dex = 0 ; mp[0] = 1; for(int i = 0 ; i < n ;i++){ dex += a[i]; ans += mp[dex - p]; mp[dex]++; } p *= k; if(p == 1) break; } cout << ans << endl;}
- Molly's Chemicals 776C
- 【codeforces 776C】Molly's Chemicals
- 【codeforces 776C】Molly's Chemicals 题解
- codeforces 776c Molly's Chemicals 【思维】
- Codeforces 776C-Molly's Chemicals
- CodeForces 776C Molly's Chemicals
- Codeforces 776C Molly's Chemicals 【数论】
- codeforces 776C Molly's Chemicals(前缀和 二分搜索)
- Codeforces 776C Molly's Chemicals 前缀和
- codeforces 776C Molly's Chemicals (数学+二分法)
- 【Codeforces 776 C Molly's Chemicals】+ 前缀和 + map
- Codeforces-776C-Molly's Chemicals(前缀和)
- CF 776C Molly's Chemicals 前缀和,STL
- CodeForces 776C Molly's Chemicals (思维+前缀)
- 【Codeforces 776 C. Molly's Chemicals】+ map + 前缀和
- Codeforces Round #400 C. Molly's Chemicals
- Molly's Chemicals CodeForces
- Molly's Chemicals CodeForces
- angular 创建组件报错
- 一.ARM裸机学习之ARM汇编
- URL编码转UTF-8
- 模板整理——图论·最小生成树·Kruskal
- 数学问题
- Molly's Chemicals 776C
- Java中抽象类和接口的用法和区别
- Git 查看和修改用户名和邮箱地址
- 领取积分博文
- build_all_zonelists
- JZOJ 5426. 【NOIP2017提高A组集训10.25】摘Galo
- C++ 在继承中虚函数、纯虚函数、普通函数,三者的区别
- 实习记录4 上线下线流程
- redis 分布式多线程细粒度锁