【Codeforces 776 C Molly's Chemicals】+ 前缀和 + map
来源:互联网 发布:sql删除语句 编辑:程序博客网 时间:2024/05/16 15:01
C. Molly’s Chemicals
time limit per test2.5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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 ≤ 105, 1 ≤ |k| ≤ 10).
Next line contains n integers a1, a2, …, an ( - 109 ≤ ai ≤ 109) — 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
Note
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 的幂 进行统计?
AC代码:
#include<cstdio>#include<map>#include<algorithm>using namespace std;typedef long long LL;const int M = 1e5 + 10;LL a[M];map <LL,LL> m;int main(){ LL N,K; scanf("%lld %lld",&N,&K); LL ans = 0,pl = 0; for(int i = 1; i <= N; i++) scanf("%lld",&a[i]),pl += abs(a[i]); LL cut = 1,num = 0; while(abs(cut) <= pl){ ans = 0,m.clear(),m[0] = 1; for(int i = 1; i <= N; i++) ans += a[i],num += m[ans - cut],m[ans]++; cut *= K; if(cut == 1) break; } printf("%lld\n",num); return 0;}
- 【Codeforces 776 C Molly's Chemicals】+ 前缀和 + map
- 【Codeforces 776 C. Molly's Chemicals】+ map + 前缀和
- codeforces 776C Molly's Chemicals(前缀和 二分搜索)
- Codeforces 776C Molly's Chemicals 前缀和
- Codeforces-776C-Molly's Chemicals(前缀和)
- Codeforces Round #400 C. Molly's Chemicals(前缀和)
- CodeForces 776C Molly's Chemicals (思维+前缀)
- CF 776C Molly's Chemicals 前缀和,STL
- 【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 Round #400 Molly's Chemicals 前缀和
- Molly's Chemicals 776C
- Molly's Chemicals CodeForces
- Molly's Chemicals CodeForces
- sql条件查询
- python virtualenv flask
- 如何写好 Git commit messages
- js建造者模式
- java安全架构____java DSA数字签名原理
- 【Codeforces 776 C Molly's Chemicals】+ 前缀和 + map
- html标签详细讲解表
- 算法课第一周
- 什么是猴子补丁(monkey patch)
- 'utf8' codec can't decode byte --python
- 大数据量高并发访问SQL优化方法
- AndroidStudio配置NDK/JNI开发环境细节
- VerifyError: Error #1014: 无法找到类 mx.containers::Panel
- Java JUC并发 AbstractQueuedSynchronizer学习