Codeforces Round #400 C. Molly's Chemicals
来源:互联网 发布:连衣裙 知乎 编辑:程序博客网 时间:2024/05/29 15:11
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.
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 a single integer — the number of valid segments.
4 22 2 2 2
8
4 -33 -6 -3 12
3
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].
题意:给定n个数和k 让你找出有多少个区间满足k^t?
orz 一群人在宿舍开车想了一个小时这个题都没想出什么好的做法,真是菜到抠脚了。
思维总是局限于要去求前缀和然后枚举所有的区间和去验证是否满足k^t,这样铁定超时啊。。。
其实应该换个思路的 我们枚举的区间和有 sum[i]-sum[j]=k^t ,那么我们枚举区间和太费时
我们可以将等式转变为 sum[i]=k^t+sum[j], j的范围我们知道 0---n-1 那么对于k^t的值我们可以先打个表存储.
sum[i]就是前缀和数组.....,然后记录mp[sum[i]]的结果就行....orz... 注意处理一下k==-1和k==1的情况即可.
#include<bits/stdc++.h>#define ll long long using namespace std;const int maxn=1e5+10;const ll inf=1e16+10;int a,n,k;ll sum[maxn],b[70];map<ll,ll>mp;void print(){b[0]=1;for(int i=1;i<=64;i++)if(b[i-1]<inf)b[i]=b[i-1]*k;return ;}int main(){ll ans=0;cin>>n>>k;mp.clear();memset(sum,0,sizeof(sum));print();for(int i=1;i<=n;i++){scanf("%d",&a);for(int j=0;j<=64&&b[j]<inf;j++){if(k==-1||k==1){if(k==-1)mp[sum[i-1]-1]++;mp[sum[i-1]+1]++;break;}else{mp[sum[i-1]+b[j]]++;}}sum[i]=sum[i-1]+a;ans+=mp[sum[i]];}printf("%lld\n",ans);return 0;}
总体来说这种逆向思维的题目一向是自己的弱点.....以后正向思维等式写出来之后 就应该想办法转化等式往逆向思维想一想、、、、
- Codeforces Round #400 C. Molly's Chemicals
- Codeforces Round #400 C. Molly's Chemicals (二分)
- Codeforces Round #400 C. Molly's Chemicals [二分]
- Codeforces Round #400 C. Molly's Chemicals(前缀和)
- Codeforces Round #400 Molly's Chemicals 前缀和
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
- Codeforces Round #400 (Div. 1 + Div. 2, combined)C. Molly's Chemicals【思维】
- Codeforces Round #400 (Div. 1 + Div. 2, combined) C:Molly's Chemicals(前缀+思维)
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals
- Codeforces Round #400 (Div. 1 + Div. 2, combined)C. Molly's Chemicals
- Codeforces Round #400 (Div. 1 + Div. 2, combined) 776C Molly's Chemicals
- Molly's Chemicals CodeForces
- Molly's Chemicals CodeForces
- 【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
- 末尾0的个数
- 使用Android Studio的小技巧
- fopen()函数
- 使用IntelliJ IDEA和Maven创建Java Web项目
- 使用Executor接口(1)
- Codeforces Round #400 C. Molly's Chemicals
- Java 加解密类的功能用法介绍及实际项目中数据加密解密的解决办法
- UVA 11988 Broken Keyboard 【链表】
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)
- 关于存储过程的那些事
- Problem C. gNumbers Google APAC 2016 University Test Round B
- Android 热修复Tinker接入实战
- Java多线程断点续传代码详解
- C/S和B/S模式