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 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^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;
复制代码

 

posted @   樱花落舞  阅读(262)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2016-07-23 Codeforces Round #364 (Div. 2) B
2016-07-23 Codeforces Round #364 (Div. 2) A
点击右上角即可分享
微信分享提示