Codeforces 11B Jumping Jack(数学)
1 second
64 megabytes
standard input
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.
The input data consists of only one integer x ( - 109 ≤ x ≤ 109).
Output the minimal number of jumps that Jack requires to reach x.
- 2
- 3
- 6
- 3
- 0
- 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(数学)的更多相关文章
- codeforces 11B Jumping Jack
Jack is working on his jumping skills recently. Currently he's located at point zero of the number l ...
- Codeforces Beta Round #11 B. Jumping Jack 数学
B. Jumping Jack 题目连接: http://www.codeforces.com/contest/11/problem/B Description Jack is working on ...
- cf 11B Jumping Jack(贪心,数学证明一下,,)
题意: 给一个数X. 起始点为坐标0.第1步跳1格,第2步跳2格,第3步跳3格,.....以此类推. 每次可以向左跳或向右跳. 问最少跳几步可以到坐标X. 思路: 假设X是正数. 最快逼近X的方法是不 ...
- Jumping Jack CodeForces - 11B
Jumping Jack CodeForces - 11B 就是一个贪心. 基本思路: 正负没有关系,先取绝对值. 首先跳过头,然后考虑怎么回来. 设超过头的步数为kk.如果kk为偶数,那么直接在前面 ...
- codeforces 11 B.Jumping Jack 想法题
B. Jumping Jack Jack is working on his jumping skills recently. Currently he's located at point zero ...
- [BFS,大水题] Codeforces 198B Jumping on Walls
题目:http://codeforces.com/problemset/problem/198/B Jumping on Walls time limit per test 2 seconds mem ...
- CodeForces 534C Polycarpus' Dice (数学)
题意:第一行给两个数,n 和 A,n 表示有n 个骰子,A表示 n 个骰子掷出的数的和.第二行给出n个数,表示第n个骰子所能掷出的最大的数,这些骰子都有问题, 可能或多或少的掷不出几个数,输出n个骰子 ...
- codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)
题意:给你x%ci=bi(x未知),是否能确定x%k的值(k已知) ——数学相关知识: 首先:我们知道一些事情,对于k,假设有ci%k==0,那么一定能确定x%k的值,比如k=5和ci=20,知道x% ...
- Codeforces Gym 100269G Garage 数学
Garage 题目连接: http://codeforces.com/gym/100269/attachments Description Wow! What a lucky day! Your co ...
随机推荐
- Hadoop不同模式下关键配置属性
Hadoop分为三种模式: 独立(或本地)模式. 伪分布模式. 全分布模式 不同模式下关键配置项及属性内容如下面表格所示 组件名称 配置的文件名 属性名称 独立模式 伪分布模式 全分布模式 Commo ...
- tinymce原装插件源码分析(四)-fullscreen
fullscreen 作为一款文本编辑器,全屏功能是非常有必要的.在插件中主要是修改一些css style和触发resize事件. style问题(反例): 见github源码:https://git ...
- 【C++】函数和指针
最近在看C++ primer plus,感觉函数与指针这一章难点比较多,记写笔记,加强理解. From C++ Primer Plus: Chapter 7 Function:C++ Programm ...
- 第一个JavaWeb工程
这个工程主要用来研究log4j,所以就只有一个页面,希望以后慢慢进步. java动态生成网页主要使用servlet.把请求拦截下来,处理后返回结果. 这里创建的是一个maven工程. 结构如下:
- 【codeforces 589G】Hiring
[题目链接]:http://codeforces.com/problemset/problem/589/G [题意] 有n个人; 每个人每天在开始工作之前,都需要di单位的准备时间,然后才能开始工作; ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- OpenCASCADE点向平面投影
OpenCASCADE点向平面投影 OpenCASCADE的ProjLib类提供了解析曲线(直线.圆.椭圆.抛物线.双曲线)向解析曲面(平面.圆柱面.圆锥面.球面.圆环面)投影的功能,主要用来计算三维 ...
- springboot shiro配置
导入相关包(这里配合使用Ehcache缓存) <dependency> <groupId>org.apache.shiro</groupId> <artifa ...
- Tuples are immutable
A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in ...
- 5.不用拷贝的对象可以用ref
#include <iostream> #include <string> #include <boost/bind.hpp> #include <boost ...