UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律
? 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 数学思维+规律的更多相关文章
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- B. Tell Your World(几何数学 + 思维)
B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor
E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...
- 程序设计中的数学思维函数总结(代码以C#为例)
最近以C#为例,学习了程序设计基础,其中涉及到一些数学思维,我们可以巧妙的将这些逻辑问题转换为代码,交给计算机运算. 现将经常会使用到的基础函数做一总结,供大家分享.自己备用. 1.判断一个数是否为奇 ...
- UVa 10025: The ? 1 ? 2 ? ... ? n = k problem
这道题仔细思考后就可以得到比较快捷的解法,只要求出满足n*(n+1)/2 >= |k| ,且n*(n+1)/2-k为偶数的n就可以了.注意n==0时需要特殊判断. 我的解题代码如下: #incl ...
- UVALive 6909 Kevin's Problem 数学排列组合
Kevin's Problem 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid ...
- hdu-5858 Hard problem(数学)
题目链接: Hard problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- UVa10025-The ? 1 ? 2 ? ... ? n = k problem
分析:因为数字之间只有加减变换,所以-k和k是一样的,都可以当成整数来考虑,只要找到最小的n满足sum=n*(n+1)/2>=k:且sum和k同奇同偶即可,做法是用二分查找,然后在就近查找 因为 ...
- POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)
题目链接: POJ:id=3100" style="font-size:18px">http://poj.org/problem? id=3100 ZOJ:http ...
随机推荐
- source insight插件
直使用sourceinsight编辑C/C++代码,sourceinsight是一个非常好用的编辑工具可以任意定位,跳转,回退,本人一直 使用该工具做C/C++开发,sourceinsight能够满足 ...
- javascript的js调用
本来从一开始接触编程开始,自己就一直写后端,但是对于前端真的不会,但是没办法呀,公司要做,所以,好吧,开始了写一写简单的javascript的内容其中,在xxx.jsp页面中出现了这个 <a c ...
- 19,django组件之contenttype(一)
django组件之contenttype(一) 方式1:适用于1张表和另一张表要关联的时候. 1.路飞学成表设计: 2.将2个价格策略表合并1张表. 3.如果再加一张表,那价格策略表的表结构会发生改变 ...
- JDBC---java与数据库中数据类型的对应关系
基础数据类型 Java 类型 SQL 类型 int 或 java.lang.Integer INTEGER long 或 java.lang.Long BIGINT short 或 java.lang ...
- 7 Django分页器文章分页
1.复习 2.这节课要解决的问题? 3.分页的原理 4.准备工作 (1)创建Django项目 C:\Users\Administrator\Desktop\root3>django-admin ...
- springmvc上传图片并显示--支持多图片上传
实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...
- vue理解$nextTick
首先要明确: Vue 实现响应式并不是数据发生变化之后 DOM 立即变化,而是按一定的策略进行 DOM 的更新. $nextTick 是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据之后使用 ...
- Java架构师必会的技能
Java架构师必会的技能 我把它分为了五大专题 工程化专题 工程化专题 git git安装使用 git日常使用:fetch/pull/push/revert/rebase git分支管理git flo ...
- 【HTML&CSS】 第一章:DTD文档声明
<!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. <!DOCTYPE> 声明不是 HTML 标签:它是指示 web 浏览器关 ...
- 如何写一套下拉刷新的控件?《MJRefresh原理浅析》(附Demo下载地址)
相信大家有很多人在做项目的时候都在使用MJRefresh 控件来实现下拉刷新的功能: MJRefresh经过不断的重构与更新迭代,现在不管是功能上还是代码结构上都是相当不错的,都是很值我们去学习的. ...