UVa10025-The ? 1 ? 2 ? ... ? n = k problem

分析:因为数字之间只有加减变换,所以-k和k是一样的,都可以当成整数来考虑,只要找到最小的n满足sum=n*(n+1)/2>=k;且sum和k同奇同偶即可,做法是用二分查找,然后在就近查找
因为1,2,3,4,5,6……的sum变化是奇奇偶偶奇奇偶偶奇奇偶偶……
程序:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static long l,r,mid;
public static long Binary(long k){
l=0;r=100000;
long ans=0;
while(l<=r){
mid=(l+r)/2;
long sum=mid*(mid+1)/2;
if(sum>=k){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
return ans;
}
public static long fun(long k){
if(k==0)
return 3;
long ans=Binary(k);
long sum=ans*(ans+1)/2;
if(k%2!=0){
for(long i=ans;i>=0;i--){
long s=i*(i+1)/2;
if(s<k)break;
if(s%2!=0)
return i;
}
for(long i=ans;;i++){
long s=i*(i+1)/2;
if(s%2!=0)
return i;
}
}
else{
for(long i=ans;i>=0;i--){
long s=i*(i+1)/2;
if(s<k)break;
if(s%2==0)
return i;
}
for(long i=ans;;i++){
long s=i*(i+1)/2;
if(s%2==0)
return i;
}
}
}
public static void main(String args[]){
Scanner cin=new Scanner(System.in);
int n;
n=cin.nextInt();
for(int i=0;i<n;i++){
long k;
k=cin.nextInt();
if(k<0)
k=-k;
long ans=fun(k);
if(i!=0)
System.out.println();
System.out.println(ans);
}
}
}
UVa10025-The ? 1 ? 2 ? ... ? n = k problem的更多相关文章
- UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律
UVa10025 ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operator ...
- UVa 10025: The ? 1 ? 2 ? ... ? n = k problem
这道题仔细思考后就可以得到比较快捷的解法,只要求出满足n*(n+1)/2 >= |k| ,且n*(n+1)/2-k为偶数的n就可以了.注意n==0时需要特殊判断. 我的解题代码如下: #incl ...
- K - problem 问题
Leetcode 有几个题目, 分别是 2sum, 3sum(closest), 4sum 的求和问题和 single Number I II, 这些题目难点在于用最低的时间复杂度找到结果 2-sum ...
- YOURPHP的分页完整版
html代码 <?php print_r($ser['searchtype']);?> <select name="searchtype"> <opt ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- UVA 10025 (13.08.06)
The ? 1 ? 2 ? ... ? n = k problem Theproblem Given the following formula, one can set operators '+ ...
- 剑指OFFER之二叉树中和为某一值的路径(九度OJ1368)
题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 输入: 每个测试案例包括n+1行: 第一行为2 ...
- 剑指OFFER之把数组排成最小的数(九度OJ1504)
题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 输入: 输 ...
- 【原创】UVAOJ水题10025解题报告
首先是原题,转自UVAOJ The ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can s ...
随机推荐
- jbpm node signal
task-node (任务节点) 其性质和node节点一样,在没有task的时候,也都是自动执行,不等待.task-node被归类为一个等待节点,是指在task-node中的task列表中的task没 ...
- java ReentrantReadWriteLock
// read and write lock is mutual exclusion lock //Listing 7-3. Using ReadWriteLock to Satisfy a Dict ...
- 分布式集群中,设定时间同步服务器,以及ntpd与ntpdate的区别
什么时候配置时间同步? 当分布式集群配置好了以后,马上配置的是SSH无密钥配置,然后就是配置时间同步. 时间同步在集群中特别重要. 一:时间同步 1.时间同步 集群中必须有一个统一的时间 如果是内网, ...
- MYSQL 中常用日期时间函数使用
MySQL Date 函数 下面的表格列出了 MySQL 中最重要的内建日期函数: 函数 描述 NOW() 返回当前的日期和时间 CURDATE() 返回当前的日期 CURTIME() 返回当前的时间 ...
- Cygwin + CMake 测试
https://www.cygwin.com/ apt-get for cygwin? wget rawgit.com/transcode-open/apt-cyg/master/apt-cyg in ...
- String.format(转)
转自:http://blog.csdn.net/lonely_fireworks/article/details/7962171 方便自己查阅. 常规类型的格式化 String类的format()方法 ...
- eclipse有时候会报错:Cannot change version of project facet Dynamic Web Module to 2.5。这个错误不会影响程序的运行,不过看着总是不舒服。这个问题现在可以解决啦。
把项目WEB-INF底下的web.xml文件头部的: <?xml version="1.0" encoding="UTF-8"?> < ...
- [LeetCode] Search for a Range(二分法)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Marshal.SecureStringToBSTR
Marshal.StringToBSTR 方法 命名空间:System.Runtime.InteropServices程序集:mscorlib(在 mscorlib.dll 中) // 使用一个Int ...
- Java: Difference between ArrayList and LinkedList
Basically, they are just two different implementations of List interface. LinkedList is implemented ...