A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n minutes. After a round ends, the next round starts immediately. This is repeated over and over again.

Each round has the same scenario. It is described by a sequence of n numbers: d1,d2,…,dn(−10^6≤di≤10^6). The i-th element means that monster's hp (hit points) changes by the value didi during the i-th minute of each round. Formally, if before the i-th minute of a round the monster's hp is h, then after the i-th minute it changes to h:=h+di

The monster's initial hp is H. It means that before the battle the monster has H hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to 0. Print -1 if the battle continues infinitely.

Input

The first line contains two integers H and n (1≤H≤10^12, 1≤n≤2⋅10^5). The second line contains the sequence of integers d1,d2,…,dn (−10^6≤di≤10^6), where di is the value to change monster's hp in the i-th minute of a round.

Output

Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer k such that k is the first minute after which the monster is dead.

Examples
input
1000 6
-100 -200 -300 125 77 -4
output
9
input
1000000000000 5
-1 0 0 0 0
output
4999999999996
input
10 4
-3 -6 5 4
output
-1
题意解释:输入,给定了怪物的hp和n轮战斗对怪物造成的伤害。输出,怪物的hp降到0及以下即被击败输出第几分钟怪物被击败或-1.
解题思路:先判断第一个周期造成的最高伤害是多少和第一个周期是否对怪物造成了伤害,来确定怪物是否能被击败。然后我们通过计算求出到打败怪物的前一个周期的时间,再判断最后一个周期何时击败怪物。
其实可以说是个数学题,这里我用了二分来求解,但是long long精度不够,中间判断的时候将long long转为double来提高精度。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[];
int main()
{
ll hp,n;
scanf("%lld%lld%lld",&hp,&n,&a[]);
ll t=a[];
ll aa=;
ll mmin=min(t,aa);
ll flag=;
for(int i=;i<=n;++i)
{
scanf("%lld",&a[i]);
t+=a[i];
if(mmin>t)
{
mmin=t;
flag=i;
}
}
ll tmp=hp;
tmp+=mmin;
if(tmp<=)
{
for(int i=;i<=n;++i)
{
hp+=a[i];
if(hp<=)
{
cout<<i;
return ;
}
}
}
else
{
if(t>=)
{
cout<<-;
return ;
}
ll l=,r=;
while(l<=r)
{
double mid=(l+r)/;
if(hp+(mid*t)+mmin<=)
{
r=mid-;
}
else
{
l=mid+;
}
}
hp+=l*t;
for(int i=;i<=n;++i)
{
hp+=a[i];
if(hp<=)
{
cout<<i+l*n;
return ;
}
}
}
}

CF1141E Superhero Battle的更多相关文章

  1. E. Superhero Battle Codeforces Round #547 (Div. 3) 思维题

    E. Superhero Battle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #547 (Div. 3) E. Superhero Battle

    E. Superhero Battle A superhero fights with a monster. The battle consists of rounds, each of which ...

  3. 【CF1141E】Superhero Battle

    \[x*p\ge y\rightarrow x=\lfloor{{y-1}\over p}\rfloor+1\]

  4. Codeforces1141E(E题)Superhero Battle

    A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly nn min ...

  5. E. Superhero Battle

    链接 [https://codeforces.com/contest/1141/problem/E] 题意 怪物开始的生命值,然后第i分钟生命值的变化 问什么时候怪物生命值为非正 分析 有一个巨大的坑 ...

  6. 【Codeforces 1141E】Superhero Battle

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 二分最后轮了几圈. 二分之后直接o(N)枚举具体要多少时间即可. 注意爆long long的情况. 可以用对数函数,算出来有多少个0 如果大于 ...

  7. Codeforces Round #547 (Div. 3) E. Superhero Battle (数学)

    题意:有一个HP为\(h\)的大怪兽,你需要轮流进行\(i\)次操作.每次可以使\(h+=d_i\)(\(d_i\)有正有负),当第\(n\)次操作完成后,再从第一次开始,问能否使得怪兽的HP变为\( ...

  8. Codeforces Round #547 (Div. 3) 题解

    Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. 标准模板库中的栈(stack)

    ////C++数据结构与算法(第4版) Adam Drozdek 著  徐丹  吴伟敏<<清华大学出版社>> STL中的通用栈类实现为容器适配器:使用以指定方式运行的容器.栈容 ...

  2. 二、js中基础知识

    该篇文章主要是强化一下自己javaScript的基础,让写代码变得更轻松些.基础好的请忽略.    JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解 ...

  3. Mac OS 下升级gcc遇到的坑

    为了升级gcc,原来版本是4.8,想升级到4.9,因为在Mac上安装cocoNLP一直出错,想试试看是不是gcc版本太低的原因,因此在此想要升级到4.9 经历了三个问题: - brew update太 ...

  4. intent 跳转

    一.不需要返回值的跳转 Intent intent=new Intent(); intent.setClass(目前的acitivy.this, 目标activity.class); startAct ...

  5. php注册与登录

    一.注册 1.注册界面 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  6. 图解IDEA中配置Maven并创建Maven的Web工程

    打开IDEA,File->Settings,如下图所示: 2.在Settings中按照如下进行配置,如下图所示:

  7. Wincc V7.3SE安装截图

    打开某个工程出错,能力所限,找不到问题,没能解决

  8. Python测试进阶——(3)编写Python程序监控计算机的服务是否正常运行

    用python写了个简单的监控进程的脚本,当发现进程消失的时候,立即调用服务,开启服务. 脚本的工作原理是这样的:脚本读取配置文件,读取预先配置好的调用系统服务的路径和所要监控的服务在进程管理器中的进 ...

  9. visio 2019 激活方法

    今日因工作需要使用visio,无奈下载2019版本需要激活,很多功能无法使用,最近在网上发现一个非常简单就是一个本地可执行脚本,本人已亲测完全激活成功,随分享给大家 复制下面代码: @echo off ...

  10. springCloud 之 Eureka服务治理

    服务治理是微服务架构中最核心和基础的模块 首先我们创建一个springCloud eureka service的springboot 工程,该工程提供一个服务中心,用来注册服务,第二个工程是clien ...