ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C

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 22 2 2 2

output

8

input

4 -33 -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^x==(数组区间和),问一共有多少区间符合(看样列)

解法:

1 单个问题,已知一个数,问区间和等于这个数的组合有多少,多个数字就加个循环就好了

2 http://oj.jxust.edu.cn/problem.php?cid=1163&pid=2(一个类似问题)

3 然后下面的代码要跑1s,如果时间掐得紧。。。则不能清空每次循环的结果(第二个代码)

 1 #include<bits/stdc++.h>
 2 typedef long long LL;
 3 typedef unsigned long long ULL;
 4 using namespace std;
 5 map<LL,LL>Mp,mp;
 6 vector<LL>Ve;
 7 LL num[300000];
 8 int n,k;
 9 int main(){
10     scanf("%d%d",&n,&k);
11     for(int i=1;i<=n;i++){
12         cin>>num[i];
13     }
14     Ve.push_back(1);
15     if(k==-1){
16         Ve.push_back(-1);
17     }else if(k!=1){
18         for(LL i=k;i<=(2e15);i*=k){
19             Ve.push_back(i);
20         }
21     }
22     LL ans=0;
23     for(LL i=0;i<Ve.size();i++){
24         Mp.clear();
25         Mp[0]=1;
26         LL sum=0;
27         for(int j=1;j<=n;j++){
28             sum+=num[j],Mp[sum]++;
29             LL pos=sum-(Ve[i]);
30             if(Mp.find(pos)!=Mp.end()){
31                 ans+=Mp[pos];
32             }
33         }
34     }
35     printf("%lld\n",ans);
36     return 0;
37 }
 1 int n;
 2     cin >> n;
 3     int k;
 4     cin >> k;
 5     FI(n) {
 6         cin >> a[i];
 7         pref[i + 1] = pref[i] + a[i];
 8     }
 9     vector<ll> v;
10     if (k == 1) {
11         v = {1};
12     } else if (k == -1) {
13         v = {1, -1};
14     } else {
15         ll t = 1;
16         while (abs(t) < 2e14) {
17             v.push_back(t);
18             t *= k;
19         }
20     }
21 //    DBN(v);
22     ll ans = 0;
23     cnt[0]++;
24     for (int i = 0; i < n; ++i) {
25         ll t = pref[i + 1];
26         for (ll need : v) {
27             ll x = t - need;
28             auto it = cnt.find(x);
29             if (it == cnt.end()) continue;
30             ans += it->second;
31 //            DBN(i, need, it->second);
32         }
33         cnt[t]++;
34     }
35     cout << ans << endl;
时间: 2024-08-04 04:35:26

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C的相关文章

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A

Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim. The killer starts with two potentia

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D

Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are m switches. E

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)

前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几天 A.A Serial Killer 题目大意:一开始给你两个字符串,每次给你当前两个串中的一个和一个新的串,用新的串换掉旧的,每次输出当前的串.(次数<=1000) 思路:二逼题 #include<iostream> using namespace std; int main() { s

【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的. 即最后会形成若干个环. g[i]显然等于那个环的大小. 即让你形成若干个环. 每个环的大小只能为A或B 则相当于问Ax+By=n是否有解. 可以枚举x然后看看n-A*x能否被B整除. 构造x个长度为A的环,y个长度为B的环就好了 [代码] #include <bits/stdc++.h> using namespace std; const in

【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) B】Recursive Queries

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个记忆化搜索. 接近O(n)的复杂度吧 [代码] #include <bits/stdc++.h> using namespace std; const int N = 1e6; int g[N+10]; int pre[N+10][20]; int f(int x){ int temp = 1; while (x){ if (x%10!=0) temp*=(x%10); x/=10; } return temp; } in

【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A】 Palindromic Supersequence

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 字符串倒着加到原串右边就好 [代码] #include <bits/stdc++.h> using namespace std; int main(){ #ifdef LOCAL_DEFINE freopen("rush_in.txt", "r", stdin); #endif ios::sync_with_stdio(0),cin.tie(0); string s; cin >&

【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) D】Tree

[链接] 我是链接,点我呀:) [题意] 让你在树上找一个序列. 这个序列中a[1]=R 然后a[2],a[3]..a[d]它们满足a[2]是a[1]的祖先,a[3]是a[2]的祖先... 且w[a[1]]<=w[a[2]]<=w[a[3]].... 且要求这个序列的长度最长 (且a[1]和a[2]的简单路径之间不能有大于等于a[1]的点 (也就是能取就取 [题解] 考虑一个naive的思路. 定义一个next[i]数组,表示i往上最近的权值大于i的节点所在的位置. 则我们每次输入2 R X的

ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined)

占坑,明天写,想把D补出来一起写. 原文地址:https://www.cnblogs.com/ZERO-/p/8455990.html

ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) ---d

D. Tree 最暴力的想法自然是,对于每一次询问,沿着父亲一点一点往上跳模拟.这样似乎并不能优化... 稍微好一点的想法是对于每一个点开一个nxt数组表示父亲中权值大于他的里面离他最近的在哪里,同样对于每一次询问模拟往上跳,每次加点模拟往上找,复杂度依旧没有变化,但为我们提供了一个思路,我们可以倍增啊! 首先我们对于2操作,我们记录sum[x][i]表示他的2^i个nxt的值的和是多少,这样我们就可以快速求出在总权值小于x的前提下最多能跳到哪里. 考虑如何求出这个数组. 定义nxt[x][i]