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. java动态返回一个大对象里多个小对象map返回,el表达式用c:set拼接

    基于堆内存,先把map放到返回值里 Map _map=new HashMap(); modelAndView.addObject("pledgeInsurance",_map);/ ...

  2. 谈一谈你对js线程的理解

    js线程:javascript是单线程的,所有任务都需要排队,这些任务分为同步任务和异步任务,单线程上有一个主线程任务.同步任务必须再主线程上排队进行,而异步任务(类似于点击事件)必须在主线程上的任务 ...

  3. 准备篇(二)C语言

    因为C语言部分打算单独维护,所以 目录: 1. C语言基础篇(零)gcc编译和预处理 2. C语言基础篇(一)关键字 3. C语言基础篇(二)运算符 4. C语言指针篇(一)指针与指针变量 5. C语 ...

  4. Codeforces Round #458C DP

    C. Travelling Salesman and Special Numbers time limit per test 1 second memory limit per test 256 me ...

  5. [Noip2016]愤怒的小鸟(状压DP)

    题目描述 题意大概就是坐标系上第一象限上有N只猪,每次可以构造一条经过原点且开口向下的抛物线,抛物线可能会经过某一或某些猪,求使所有猪被至少经过一次的抛物线最少数量. 原题中还有一个特殊指令M,对于正 ...

  6. 线性表(List)

    1.什么是线性表(List)? 零个或多个数据元素的有限序列. (1)元素之间是有序的. (2)线性表强调是有限的. 2.线性表有哪些操作? (1)线性表的创建和初始化,InitList (2)判空, ...

  7. CV限制符--C++

    C/C++提供多种声明变量和函数存储持续性.作用域和链接性的关键字,有些被称为存储说明符(store class specifier)或 cv 限定符(cv-qualifier),这里就一起学习一下c ...

  8. Python 协程与事件循环

    Table of Contents 前言 协程 async & await 事件循环 asyncio 的事件循环 结语 参考链接 前言 Python 标准库 asyncio 是我目前接触过的最 ...

  9. operator、explicit与implicit

    说这个之前先说下什么叫隐式转换和显示转换 1.所谓隐式转换,就是系统默认的转换,其本质是小存储容量数据类型自动转换为大存储容量数据类型. 例如:float f = 1.0: double d=f:这样 ...

  10. poj1273 网络流入门题 dinic算法解决,可作模板使用

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62078   Accepted: 2384 ...