UVa10025

? 1 ? 2 ? ... ? n = k problem

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

数学思维:
看似复杂,情况很多种,乍一看摸不着头脑的一道题,让人心生畏惧,Don't panic
数学思维,找规律求解
对于0=1+2-3  3
  1=-1+2   2
  2=1-2+3  3
依次查看,每个数的结果好像没什么规律
再看,会发现,若1变为-1,实际上将原数-2
       2变为-2,实际上将原数-4    均为偶数,我们进步了一大步,我感觉离成功不远了,代码铁定不复杂,只需要我们想清楚
  我们持续+,直到数s>k,此时若(s-k)%2==0,说明,s可将组成s的数中的一部分变为负数来的到k
  问题转化为求满足s>k&&(s-k)%2==0的数s时,所经历的最大数 注意输出格式!减少不必要的麻烦。 PS:像这种基本的找规律题,数学思维,看得出就看,看不出就试
Version2.0
#include "cstdio"
#include "cstring"
#include "cstdlib"
#include "iostream"
#include "vector"
#include "queue"
using namespace std;
int main()
{
int t,k,s,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&k);
if(k==)
printf("3\n");
else
{
if(k<)k=-k;
s=n=;
while(s<k)s+=++n;
while((s-k)&)s+=++n;
printf("%d\n",n);
}
if(t)
printf("\n");
}
}

Version 1.0
#include "cstdio"
#include "cstring"
#include "cstdlib"
#include "iostream"
#include "vector"
#include "queue"
using namespace std;
#define LL long long
int main()
{
LL k;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&k);
int sum=;
int flag=;
if(k==)
{
printf("3\n");
}
else
{
if(k<)k=-k;
int t=;
while()
{
if(sum==k)///全为+ 15
break;
else if(sum>k)
{
if((sum-k)%==)///-在前面 13
break;
if(sum-t==k)///-在最后
{
flag=;
break;
}
else if(sum-t>k)
{
if((sum-t-k)%==)///-前后均有 12
{
flag=;
break;
}
}
}
sum+=t;
t++;
}
if(flag)t++;
printf("%d\n",--t);
}
if(T)printf("\n");
}
return ;
}

UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律的更多相关文章

  1. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  2. B. Tell Your World(几何数学 + 思维)

    B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor

    E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...

  4. 程序设计中的数学思维函数总结(代码以C#为例)

    最近以C#为例,学习了程序设计基础,其中涉及到一些数学思维,我们可以巧妙的将这些逻辑问题转换为代码,交给计算机运算. 现将经常会使用到的基础函数做一总结,供大家分享.自己备用. 1.判断一个数是否为奇 ...

  5. UVa 10025: The ? 1 ? 2 ? ... ? n = k problem

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

  6. UVALive 6909 Kevin's Problem 数学排列组合

    Kevin's Problem 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid ...

  7. hdu-5858 Hard problem(数学)

    题目链接: Hard problem Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Othe ...

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

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

  9. POJ 3100 &amp; ZOJ 2818 &amp; HDU 2740 Root of the Problem(数学)

    题目链接: POJ:id=3100" style="font-size:18px">http://poj.org/problem? id=3100 ZOJ:http ...

随机推荐

  1. scrapy--json(360美图)

    之前开始学习scrapy,接触了AJax异步加载.一直没放到自己博客,趁现在不忙,也准备为下一个爬虫做知识储存,就分享给大家. 还是从爬取图片开始,先上图给大家看看成果,QAQ. 一.图片加载的方法 ...

  2. python-10多进程

    1-多进程(multiprocessing), 1个父进程可以有多少子进程 1.1下面的例子演示了启动一个子进程并等待其结束 from multiprocessing import Process i ...

  3. 内置函数--sorted,filter,map

    sorted() 排序函数. 语法: sorted(Iterable, key=None, reverse=False) Iterable: 可迭代对象;  key: 排序规则(排序函数); reve ...

  4. Hibernate---实体类注释简介

    1.类级别注解 @Entity:映射实体类 @Entity(name="tableName") - 必须,注解将一个类声明为一个实体bean. 属性: name - 可选,对应数据 ...

  5. sql里的多行多列转一行多列小技巧

    ---恢复内容开始--- [ 今天下午接受了一个紧急小任务,是将一组比赛记录统计出来,将象棋游戏玩家的两条记录在一行里面显示,进数据库看之后是首先想到的是行转列,但是一开始就觉得不对,后来写到一半确实 ...

  6. bootstrap设计横线上加字

    1.给横线上加字 . 2.思路:通过z-index实现,可以将父元素的z-index设置成2,而横线的z-index设置成-1,这样有字的地方就可以覆盖横线,再设置字的padding达到合理的宽度 ( ...

  7. selenium定位弹出菜单

    写selenium脚本,在浏览器定位各种弹出菜单时,有时用工具很难去取菜单的属性,下面说下如何去取: 点开firebug ,切换到“脚本”界面,首先在输入框输入单字母s,待弹出下拉列表后,单击左侧的插 ...

  8. [译]11-spring bean定义的继承

    spring中bean的定义包含很多信息,如,构造器参数.property指定的依赖项.初始化方法.工厂类和工厂方法等. 如果spring容器的中每个bean都重复声明这些属性,是非常烦人也是十分低效 ...

  9. 3dMax,Maya与FBX

    3DMax下载地址(包含安装教程与注册方法):http://www.3d66.com/popsoft_1.html 3DMax已经自带导出为fbx格式的功能,所以无需安装fbx插件 Maya下载地址( ...

  10. linux备忘录-bash

    知识点 环境变量 HOME PATH MAIL SHELL RANDOM // 0~32767的随机数 declare -i number=$RANDOM*10/32768 //0-9的随机数 HIS ...