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. Laravel操作上传文件的方法

    1.获取上传的文件 $file=$request->file('file');2.获取上传文件的文件名(带后缀,如abc.png) $filename=$file->getClientOr ...

  2. Elasticsearch和Head插件安装

    环境: CentOS7  Elasticsearch-6.3.2 JDK8 准备: JDK8 下载地址:http://www.oracle.com/technetwork/java/javase/do ...

  3. windows系统下用VScode配置远程编辑服务器文件的环境!通过Rmate方法

    虽然公司电脑win可以通过Xshell通过SSH远程连接家中内网linux服务器了,但是只能用vim编辑文件有点不爽. 于是上网查询,windows下使用vscode远程编辑服务器文件的办法.参照博文 ...

  4. 477. Total Hamming Distance

    class Solution { public: int totalHammingDistance(vector<int>& nums) { ; ; i < ; i++) { ...

  5. 把SmartForm转换成PDF

    摘要:将SmartForm转换为PDF的过程包括3个简单步骤. 调用智能窗体,然后返回OTF数据. 使用“转换”功能模块将OTF数据转换为所需格式. 下载文件 呈现宏“code”时出错:为参数“lan ...

  6. 笔记-redis深入学习-1

    笔记-redis深入学习-1 redis的基本使用已经会了,但存储和读取只是数据库系统最基础的功能: 数据库系统还得为可靠实现这两者提供一系列保证: 数据.操作备份和恢复,主要是持久化: 高可用:主要 ...

  7. HDFS HA(High Availability)高可用性

    HDFS HA(High Availability)高可用性 参考文献: 官方文档 全文翻译 Hadoop组件之-HDFS(HA实现细节) 这张图片的个人理解 由于NameNode在Hadoop1只有 ...

  8. 22、(转载)jQueryMobile 知识点总结

    本文转自:http://www.cnblogs.com/jxyedu HTML5技术生态介绍 H5的现状与未来 HTML5是用于取代1999年所制定的 HTML 4.01 和 XHTML 1.0 标准 ...

  9. Python 绘制棋盘

    import turtle pen = turtle.Pen() pen.speed(10) width = 30 # 格子宽度 count = 18 # 横向纵向格子数 o = width * co ...

  10. Python 爬取网页中JavaScript动态添加的内容(二)

    使用 selenium + phantomjs 实现 1.准备环境 selenium(一个用于web应用程测试的工具)安装:pip install seleniumphantomjs(是一种无界面的浏 ...