B. Jumping Jack
time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output

Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point x.
In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach x.

Input

The input data consists of only one integer x ( - 109 ≤ x ≤ 109).

Output

Output the minimal number of jumps that Jack requires to reach x.

Sample test(s)
input
2
output
3
input
6
output
3
input
0
output
0

题意:

Jack在数轴上跳。他第i次跳能够往左或往右跳i个单位。如今问你他最少花多少步跳到点x。

思路:

能够无论x正负把x变成正数。这样是等效的。然后他最快的跳法当然是往一个方向一直跳。假设这样恰好能到x。这样肯定是步数最小的解。假设不能恰好跳到。我们设按这样的方法跳第一个比x大的位置为y。

假设y-x是偶数的话。我们还是能够达到最优步数。

就是先将第(y-x)/2步往左跳。其他不变。那么恰好也能到x。假设为奇数。变成下一个偶数即可了。为什么能够这样呢。由于无论如何。x=(±1)+(±2)+(±3)+.......(±n)。所以要想跳到必须奇偶性一致。比y小一定不行、所以仅仅有在y右边找了。

具体见代码:

#include<bits/stdc++.h>
int main()
{
int tp,x,ans;
while(~scanf("%d",&x))
{
x=x<0?-x:x,ans=ceil((-1+sqrt(1+8.0*x))/2),tp=ans*(ans+1)/2-x;
while(tp&1) ans++,tp+=ans;
printf("%d\n",ans);
}
return 0;
}

Codeforces 11B Jumping Jack(数学)的更多相关文章

  1. codeforces 11B Jumping Jack

    Jack is working on his jumping skills recently. Currently he's located at point zero of the number l ...

  2. Codeforces Beta Round #11 B. Jumping Jack 数学

    B. Jumping Jack 题目连接: http://www.codeforces.com/contest/11/problem/B Description Jack is working on ...

  3. cf 11B Jumping Jack(贪心,数学证明一下,,)

    题意: 给一个数X. 起始点为坐标0.第1步跳1格,第2步跳2格,第3步跳3格,.....以此类推. 每次可以向左跳或向右跳. 问最少跳几步可以到坐标X. 思路: 假设X是正数. 最快逼近X的方法是不 ...

  4. Jumping Jack CodeForces - 11B

    Jumping Jack CodeForces - 11B 就是一个贪心. 基本思路: 正负没有关系,先取绝对值. 首先跳过头,然后考虑怎么回来. 设超过头的步数为kk.如果kk为偶数,那么直接在前面 ...

  5. codeforces 11 B.Jumping Jack 想法题

    B. Jumping Jack Jack is working on his jumping skills recently. Currently he's located at point zero ...

  6. [BFS,大水题] Codeforces 198B Jumping on Walls

    题目:http://codeforces.com/problemset/problem/198/B Jumping on Walls time limit per test 2 seconds mem ...

  7. CodeForces 534C Polycarpus' Dice (数学)

    题意:第一行给两个数,n 和 A,n 表示有n 个骰子,A表示 n 个骰子掷出的数的和.第二行给出n个数,表示第n个骰子所能掷出的最大的数,这些骰子都有问题, 可能或多或少的掷不出几个数,输出n个骰子 ...

  8. codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)

    题意:给你x%ci=bi(x未知),是否能确定x%k的值(k已知) ——数学相关知识: 首先:我们知道一些事情,对于k,假设有ci%k==0,那么一定能确定x%k的值,比如k=5和ci=20,知道x% ...

  9. Codeforces Gym 100269G Garage 数学

    Garage 题目连接: http://codeforces.com/gym/100269/attachments Description Wow! What a lucky day! Your co ...

随机推荐

  1. 比较排序算法(PHP)

    冒泡排序 两两比较相邻记录的关键字,如果反序则交换,大的数字往下沉,一直到最大的出现在数组最后 function swap(&$x, &$y) { $temp = $x; $x = $ ...

  2. 处理Oracle 11g在用EXP导出时,空表不能导出

    一.问题原因:     11G中有个新特性,当表无数据时,不分配segment,以节省空间 想要给空表也分配segmant,有以下两个办法: 1.insert一行,再rollback就产生segmen ...

  3. ASP.NET-Razor语法03

     ASP.NET MVC中使用Razor语法 @{} @{ string s ="super xiao lizi"; @s; // 里面的这个@代表着在页面上输出这个s // 我记 ...

  4. 2.Maven特点,Maven约定,建立第一个Maven项目

     1 Maven是跨平台的项目管理工具.主要服务于基于java平台的项目构建,依赖管理和项目信息管理. 项目构建 清理à编译à測试à报告à打包à部署 理想的项目构建: 高度自己主动化 跨平台 可重 ...

  5. Redit集群搭建-Sentinel模式搭建

    Redit集群搭建 学习了: Windows:http://blog.csdn.net/mrxiagc/article/details/52799081 Linux:https://www.cnblo ...

  6. [MST] Attach Behavior to mobx-state-tree Models Using Actions

    Models are not just a nifty feature for type checking. They enable you to attach behavior to your ac ...

  7. 主程的晋升攻略(3):IP、DNS和CDN

    有段时间我面试程序猿时,喜欢问这个问题:局域网IP有哪些IP段?由这个问题再追问NAT(网络地址转换). 为什么不是每一个设备一个公网IP? 先说个关于QQ的小故事,最早开发QQ时.小马哥他们也没想到 ...

  8. 工作笔记5.JAVA图片验证码

    本文主要内容为:利用JAVA图片制作验证码. 设计思路: 1.拷贝AuthImageServlet.class图片验证码 2.配置web.xml 3.JSP中,调用封装好的AuthImageServl ...

  9. storm trident function函数

    package cn.crxy.trident; import java.util.List; import backtype.storm.Config; import backtype.storm. ...

  10. rest_framework_HyperlinkedIdentityField

    #生成链接 HyperlinkedIdentityField class UserInfoSerializer(serializers.ModelSerializer): group = serial ...