ICM Technex 2017 and 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
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard 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].

题意:

给出n和k,并给出n个数,问有多少个连续段使得它们的和是k的幂次。


题解:

扫一遍,将每个前缀和处理用map存起来,对于每个位置枚举k的幂次,查询这个位置之前的前缀和与当前的和的差值是否为这个k的幂次。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=20+10;
map<ll,int> mp;
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    ll sum=0;
    mp[0]++;
    ll res=0;
    rep(i,1,n+1)
    {
        int a;
        scanf("%d",&a);
        sum+=a;
        mp[sum]++;
        if(k==1)
        {
            if(mp.find(sum-1)!=mp.end())
            res+=mp[sum-1];
        }
        else if(k==-1)
        {
            if(mp.find(sum-1)!=mp.end()) res+=mp[sum-1];
            if(mp.find(sum+1)!=mp.end()) res+=mp[sum+1];
        }
        else
        {
            ll now=1;
            while(abs(now)<1e14+100)
            {
                if(mp.find(sum-now)!=mp.end()) res+=mp[sum-now];
                now*=k;
            }
        }
    }
    printf("%lld\n",res);
    return 0;
}


信息学奥赛一本通高手训练1680:序列 题目 代码 #include<cstdio> #include<iostream> #include<cmath> #define LL long long using namespace std; const LL N = 1400000+7; const LL mod = 1e9+7; int n, t; LL pos[N], sum[N], ans[N]; struct node{ LL ll, rr; }a[N]; int main(){ p 阅读详情

相关推荐

Zemax新手别慌!一文搞懂几何像差:从球差、慧差到色差的实战识别与优化

本文为Zemax新手提供几何像差实战指南,详细解析球差、彗差和色差的识别与优化方法。通过软件操作关键节点和实际项目决策逻辑,帮助光学设计师快速掌握像差诊断与优化技巧,提升光学设计效率。

weixin_42524708的博客 267

Molly‘s Chemicals (前缀和+map用法,直接对应答案)

题意: 找到一个区间,使得这个区间和为k的次方倍 题解: 很容易想到用前缀和 然后求满足这个条件的式子,满足一次,答案增加一次 presum[i]-presum[j]=k^x; 但是如果我们直接去枚举,怎么可能不超时? 所以直接pass掉 但我们可以把这个式子改一下,改成这样 presum[i]=k^x+presum[j]; 先把这个题拆分成几个部分,第一个,求k^x,这个可以直接打表: for(int i=1;i<=64;i++){ excel[0]=1;

weixin_60789461的博客 5406

FinalShell连接Ubuntu报Connection refused?SSH服务排查与解决指南

SSH(Secure Shell)是远程登录Linux服务器的核心协议,其工作原理基于TCP三次握手建立加密通道。当客户端发起连接时,若目标端口无服务监听,内核将返回RST包,表现为Connection refused错误。理解这一机制对运维排查至关重要,可快速区分服务缺失与网络故障。在开发测试、虚拟机管理等场景中,SSH服务的正确配置直接影响远程操作效率。本文针对FinalShell连接Ubuntu时常见的Connection refused报错,深入剖析SSH服务安装、监听地址、防火墙放行及虚拟机网络模

weixin_29611239的博客 254

前缀和 CodeForces - 776C Molly's Chemicals

C. Molly's Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes input standard input output standard output Molly Hooper has n different kinds of chemic

PiKa__Qiu的博客 622

快手did did_gt edid的注册过程

接口 https://gdfp.ksapisrv.com/rest/infra/gdfp/report/kuaishou/android did 是本地生成的16进制 或者 获取的 android_id did_gt 是did生成时间戳 1595073601970 public static final Random f16237a = new Random(System.currentTimeMillis()); public static long m19668a() {  &n

HURUWO的技术博客 1万+

手把手教你学Simulink--双足机器人的反步控制方法仿真

本文介绍了在Simulink中基于反步控制方法进行双足机器人仿真研究的步骤。主要内容包括:1)准备工作,包括MATLAB/Simulink安装和相关工具箱配置;2)构建简化双足机器人模型;3)设计反步控制器,阐述其递归设计原理及Lyapunov函数构造方法;4)通过MATLAB Function块实现控制逻辑并连接模型;5)仿真参数配置和结果分析。文章强调反步控制适用于非线性系统,通过逐步构造虚拟控制律保证系统稳定性,并指出需要多次调试和查阅文献来优化控制效果。该教程为研究双足机器人运动控制提供了实用的仿真

MHD0815的博客 159

【Codeforces 776 C. Molly's Chemicals】+ map + 前缀和

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.

楚江枫 866

基于FPGA的FIR数字滤波器设计

clear;close all fc=1/4;fs=5/16; %输入给定指标 Rp=3;As=60;Fs=2; f=[fc,fs];m=[1,0]; %计算remezord函数所需参数f,m,dev dev=[(10^(Rp/20)-1)/(10^(Rp/20)+1),10^(-As/20)]; [N,fo,mo,W]=remezord(f,m,dev,Fs); %确定remez函数所需参数 hn=remez(N,fo,mo,W);...

FPGA/MATLAB学习教程/源码/项目合作开发 9458

codeforces 776C

codeforces 776C题目: 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

Static_Ricardo的博客 964

lammps教程:官方自带eam_database程序拟合EAM势方法介绍

使用lammps模拟金属体系,最常用的势函数为eam、meam势。 对于大多数金属或者合金来说,可以从网上下载相对应的eam或meam势力场文件。 而对于一些新的合金材料,可能就找不到相对应的力场文件。 解决的方法有两种,一是采用混合势(pair_style hybrid),第二种方法是自己拟合势函数文件。 混合势的写法在前面文章中已经介绍过,本文介绍一种拟合EAM势的方法。 该方法是运行Xiaowang Zhou编写的一个拟合程序,该程序可自动生成所需要的合金势。 这个程序能够拟合的合金元素有: Cu,

lammps_jiayou的博客 4622

计算机组成原理(第三版)唐朔飞-第七章指令系统-课后习题

机器字长是指CPU一次能处理数据的位数通常与CPU的寄存器位数有关。指令字长是指机器指令中二进制代码的总位数。存储字长是指存储单元中存放二进制代码的总位数。三者可以相等也可以不等视不同机器而定。答:OP(4位)A1(6位)A_1(6位)A1​(6位)A2(6位)A_2(6位)A2​(6位)设二地址指令格式为该指令系统的基本格式,4位操作码共有16种编码,其中13种用来定义二地址指令,还剩3种可用作扩展标志。一地址指令条数=3∗26=192。

weixin_45735391的博客 7万+

Codeforces 776C - Molly's Chemicals(思维+前缀和)

题目大意:给出n个数(a1.....an),和一个数k,问有多少个区间的和等于k的幂(1 ≤ n ≤ 10^5,1 ≤ |k| ≤ 10, - 10^9 ≤ ai ≤ 10^9) 解题思路:首先,可以从题目得出k的幂最大不能超过n*ai=1e14。然后,我们先求出前缀和sum[1]...sum[n],k的i次幂为power[i]。我们要求出一个区间和等于power[i]即sum[r...

weixin_30609287的博客 219

STM32入门教程:图像处理

然后,我们使用 STM32 的 DSP 模块中的 arm_mean_u8 函数计算原始图像的平均值,并将结果存储在 meanValue 变量中。然后,我们需要编写代码配置 STM32 的外部中断输入引脚,并在中断处理函数中读取 OV7670 摄像头模块输出的图像数据。然后,我们需要编写代码配置 STM32 的 USB OTG 接口,并在回调函数中读取 USB 摄像头输出的图像数据。一、图像采集 图像采集是图像处理的第一步,我们需要将外部图像源的数据导入到 STM32 中进行处理。

2401_85625565的博客 1624

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

Molly’s Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes Description Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has

喵喵喵? 903

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

前缀和与哈希表在子数组和问题中的应用:以Codeforces经典题为例

前缀和是一种基础的数据处理技术,通过预处理数组的累积和,可以在常数时间内计算任意子数组的和。其核心原理是将子数组和问题转化为两个前缀和的差值问题,从而避免重复计算。结合哈希表(或字典)记录前缀和的出现次数,可以将时间复杂度从O(n²)优化至O(n),极大提升了算法效率。这种技术组合在解决“子数组和等于特定值”这类问题时展现出巨大价值,广泛应用于算法竞赛、大数据分析和实时统计等场景。本文以一道经典Codeforces题目为例,深入解析如何利用前缀和与哈希表高效统计子数组和等于k的幂的情况,并详细讨论k=0、±

weixin_30619101的博客 448

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【前缀和 && 思维转换】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)Molly's Chemicals

Problem Description 输入n, k代表有n个元素,问你任意取一段区间和 满足k的m次方(m是任意正整数)让你求出:有多少段不同的区间和? 思路: 第一想法能想到的就是前缀和然后 sum[i] - sum[j] = pow[k] i: 1 to n; j : 0 to (i - 1); k : 0 to m;这样复杂度肯定会超时。于是就没想到了,然后百度借鉴借鉴。将公式转化成

ACMer 梁剑锋 的博客 786

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Molly's Chemicals

http://codeforces.com/problemset/problem/776/C 题意:给出n个数,和一个数k,现在问你有多少个区间和等于k的x次方,x从0到无穷 C++版本一 题解:先求出前缀和,也就是求有多少个sum[r]-sum[l]=pow(k,x),r>l。x最大只有lg(1e14),可以将式子变为sum[r]-pow(k,x)=sum[l]。每次结束后将前缀和放...

无限迭代中...... 802

776C Molly's Chemicals --- 前缀和

今天又写了一遍,才差不多看懂这道题,不过stl的选择真的好难,靠经验么? 这题的核心就是 sum[r]-sum[l] = pow; 因为r在l的后面 所以要先由r来找l,在放入l,即转化为sum[l]=sum[r]-pow; 1 #include<bits/stdc++.h> 2 #include<algorithm> 3 #include<io...

weixin_30770495的博客 237

Codeforces 776C-Molly's Chemicals

Molly's Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes input standard input output standard output Molly Hooper has n different kinds of c

wang_128的博客 1360

CodeForces 776C Molly's Chemicals (思维+前缀)

C. Molly's Chemicals time limit per test  2.5 seconds memory limit per test  512 megabytes input  standard input output  standard output Molly Hooper has n different kin

K键盘里的青春K 774

codeforces 776C Molly's Chemicals(连续子序列和为k的次方的个数)

C. Molly's Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes input standard input output standard output Molly Hooper has n different kinds o

Chunzhen的博客 1671

Codeforces Round #400 C. Molly's Chemicals

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 wit

Marcus-Bao的个人主页 1324

Codeforces 776C Molly's Chemicals (思维)

原题地址:http://codeforces.com/contest/776/problem/C 题意::给定nnn个数,问和等于kkk的次方的连续区间有多少个。 思路:首先最容易想到的就是枚举区间范围,但是这题数据范围1e5,那么O(n2)O(n2)O(n^2)的复杂度显然不可行.那么可以转化一下式子,设现在区间的值是由前缀和得到的.所以满足题木要求就是满足下面的式子sum[j]=kx+su...

yiqzq的博客 677

Molly's Chemicals 776C

传送门: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 aff

PH_Dream的博客 504

Molly's Chemicals CodeForces - 776C ( 思维 + 前缀和)

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.

Hanylch 1319
上一篇: Codeforces Round #257 (Div. 1) C. Jzzhu and Apples(构造题,想法题,好题)
下一篇: CROC 2016 - Final Round [Private, For Onsite Finalists Only] B(二分染色? 好题)
___fouzhe
博客等级 码龄11年 19粉丝 290原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值