这道题仔细思考后就可以得到比较快捷的解法,只要求出满足n*(n+1)/2 >= |k| ,且n*(n+1)/2-k为偶数的n就可以了。注意n==0时需要特殊判断。

我的解题代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm> using namespace std; int main()
{
long long T,K,k,n;
cin >> T;
while(T--)
{
cin >> K; if(K<0) k=-K; else k=K;
if(K==0) cout << 3 << endl;
else
{
double ans = (sqrt(1.0+8*k)-1)/2;
n=ceil(ans);
while((n*(n+1)/2-K)%2) n++;
cout << n << endl;
}
if(T) cout << endl;
}
return 0;
}

附上题目如下:

The problem

Given the following formula, one can set operators '+' or '-' instead of each '?', in order to obtain a given k
? 1 ? 2 ? ... ? n = k

For example: to obtain k = 12 , the expression to be used will be:
- 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 
with n = 7

The Input

The first line is the number of test cases, followed by a blank line.

Each test case of the input contains integer k (0<=|k|<=1000000000).

Each test case will be separated by a single line.

The Output

For each test case, your program should print the minimal possible n (1<=n) to obtain k with the above formula.

Print a blank line between the outputs for two consecutive test cases.

Sample Input

2

12

-3646397

Sample Output

7

2701
 

UVa 10025: The ? 1 ? 2 ? ... ? n = k problem的更多相关文章

  1. UVA 10025 (13.08.06)

     The ? 1 ? 2 ? ... ? n = k problem  Theproblem Given the following formula, one can set operators '+ ...

  2. UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律

    UVa10025 ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operator ...

  3. UVA 10025(数学)

     The ? 1 ? 2 ? ... ? n = k problem  The problem Given the following formula, one can set operators ' ...

  4. uva 1567 - A simple stone game(K倍动态减法游戏)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=4342">题目链接:uva 1567 - ...

  5. UVa 1363 (数论 数列求和) Joseph's Problem

    题意: 给出n, k,求 分析: 假设,则k mod (i+1) = k - (i+1)*p = k - i*p - p = k mod i - p 则对于某个区间,i∈[l, r],k/i的整数部分 ...

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

    分析:因为数字之间只有加减变换,所以-k和k是一样的,都可以当成整数来考虑,只要找到最小的n满足sum=n*(n+1)/2>=k:且sum和k同奇同偶即可,做法是用二分查找,然后在就近查找 因为 ...

  7. UVA 12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

    题目链接:https://vjudge.net/problem/UVA-12412 题目大意 略. 分析 比较大规模的模拟,注意输入输出,浮点数精度,还有排名相同的输出顺序,还有一些边界情况处理. 代 ...

  8. K - problem 问题

    Leetcode 有几个题目, 分别是 2sum, 3sum(closest), 4sum 的求和问题和 single Number I II, 这些题目难点在于用最低的时间复杂度找到结果 2-sum ...

  9. 【例题 6-4 UVA - 11988】Broken Keyboard (a.k.a. Beiju Text)

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会链表的插入操作的话.这个就不难了. 放置两个哨兵节点. 然后模拟插入一个节点的过程就好. 实时修改光标就好->即下一个插入的 ...

随机推荐

  1. iOS图片拉伸

    常用的图片拉伸场景有:聊天页面的气泡,需要根据内容拉伸,但圆角拉伸后会变形,为避免圆角拉伸,可以指定拉伸区域.UIImage实体调用以下方法即可指定拉伸区域. - (UIImage *)stretch ...

  2. Thinkphp 3.2及以上版本实现支付宝担保交易、即时到账接口类、函数和使用方法

    给客户开发网站时需要用到支付宝在线付款功能,小云到thinkphp网站溜了一圈,代码是有,可是都不怎么全,因此这篇文章诞生了! 本篇文章讲解了三个类的实现,担保交易.即时到账.双功能收款(该功能支付宝 ...

  3. Mediator 中介者 协调者模式

    简介 定义:用一个[中介者对象]封装一系列的[对象交互],中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 中介者模式的结构 抽象中介者Mediator:定义好[ ...

  4. Android省电开发 浅析

    相信对于Android App省电的开发,一切性能优化都可以达到App的省电开发,所以一个省电的Android应用,性能优化占据很重要的位置.除此之外整理了几点关于Android应用省电的开发技巧. ...

  5. Java 数据类型转换(转换成字节型)

    package com.mystudypro.byteutil; import java.io.UnsupportedEncodingException; public class ConToByte ...

  6. 国内优秀npm镜像

    淘宝npm镜像 淘宝npm镜像:http://npm.taobao.org/ 1.临时使用 npm --registry https://registry.npm.taobao.org install ...

  7. RSA PKCS1padding 填充模式

    在BouncyCastle实现RSA的PKCS1V1.5模式中,如果是公钥加密信息(forEncryption=true),密钥长度为1024位,那么输出的密文块长度为128个字节,输入的明文块长度为 ...

  8. [转]Delphi中进行延时的4种方法

    1.挂起,不占CPU sleep 2.不挂起,占cpu procedure Delay(msecs:integer); var FirstTickCount:longint; begin FirstT ...

  9. js 去掉浏览器右击默认事件

    1.整个页面所有的右击事件 document.oncontextmenu = function(){ return false; } 2.特定的区域 document.getElementById(& ...

  10. 利用iframe实现提交表单是页面部分刷新

    直接上代码: <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...